StoreProduct.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/12
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\store\StoreProductAttrValue as StoreProductAttrValueModel;
  9. use app\admin\model\system\SystemStoreProductStock;
  10. use app\models\system\SystemUserLevel;
  11. use app\models\user\UserLevel;
  12. use crmeb\basic\BaseModel;
  13. use crmeb\services\GroupDataService;
  14. use crmeb\services\workerman\ChannelService;
  15. use crmeb\traits\ModelTrait;
  16. use app\models\store\{
  17. StoreBargain, StoreCombination, StoreSeckill
  18. };
  19. /**
  20. * TODO 产品Model
  21. * Class StoreProduct
  22. * @package app\models\store
  23. */
  24. class StoreProduct extends BaseModel
  25. {
  26. /**
  27. * 数据表主键
  28. * @var string
  29. */
  30. protected $pk = 'id';
  31. /**
  32. * 模型名称
  33. * @var string
  34. */
  35. protected $name = 'store_product';
  36. use ModelTrait;
  37. protected function getSliderImageAttr($value)
  38. {
  39. $sliderImage = json_decode($value, true) ?: [];
  40. foreach ($sliderImage as &$item) {
  41. $item = str_replace('\\', '/', $item);
  42. }
  43. return $sliderImage;
  44. }
  45. protected function getImageAttr($value)
  46. {
  47. return str_replace('\\', '/', $value);
  48. }
  49. public function getDescriptionAttr($value)
  50. {
  51. return htmlspecialchars_decode($value);
  52. }
  53. public static function getValidProduct($productId, $field = 'add_time,browse,cate_id,code_path,cost,ficti,give_integral,id,image,is_sub,is_bargain,is_benefit,is_best,is_del,is_hot,is_new,is_postage,is_seckill,is_show,keyword,mer_id,mer_use,ot_price,postage,price,sales,slider_image,sort,stock,store_info,store_name,unit_name,vip_price,spec_type,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,video_link')
  54. {
  55. $Product = self::where('is_del', 0)->where('is_show', 1)->where('id', $productId)->field($field)->find();
  56. if ($Product) return $Product->toArray();
  57. else return false;
  58. }
  59. public static function getGoodList($limit = 18, $field = '*')
  60. {
  61. $list = self::validWhere()->where('is_good', 1)->order('sort desc,id desc')->limit($limit)->field($field)->select();
  62. $list = count($list) ? $list->toArray() : [];
  63. if (!empty($list)) {
  64. foreach ($list as $k => $v) {
  65. $list[$k]['activity'] = self::activity($v['id']);
  66. }
  67. }
  68. return $list;
  69. }
  70. public static function validWhere()
  71. {
  72. return self::where('is_del', 0)->where('is_show', 1)->where('mer_id', 0);
  73. }
  74. public static function getProductList($data, $uid, $store_id = 0)
  75. {
  76. $sId = $data['sid'];
  77. $cId = $data['cid'];
  78. $keyword = $data['keyword'];
  79. $priceOrder = $data['priceOrder'];
  80. $salesOrder = $data['salesOrder'];
  81. $news = $data['news'];
  82. $page = $data['page'];
  83. $limit = $data['limit'];
  84. $type = $data['type']; // 某些模板需要购物车数量 1 = 需要查询,0 = 不需要
  85. $model = self::validWhere();
  86. if ($sId) {
  87. $model->whereIn('id', function ($query) use ($sId) {
  88. $query->name('store_product_cate')->where('cate_id', $sId)->field('product_id')->select();
  89. });
  90. } elseif ($cId) {
  91. $model->whereIn('id', function ($query) use ($cId) {
  92. $query->name('store_product_cate')->whereIn('cate_id', function ($q) use ($cId) {
  93. $q->name('store_category')->where('pid', $cId)->field('id')->select();
  94. })->field('product_id')->select();
  95. });
  96. }
  97. if (!empty($keyword)) $model->where('keyword|store_name', 'LIKE', htmlspecialchars("%$keyword%"));
  98. if ($news != 0) $model->where('is_new', 1);
  99. $baseOrder = '';
  100. if ($priceOrder) $baseOrder = $priceOrder == 'desc' ? 'price DESC' : 'price ASC';
  101. // if($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';//真实销量
  102. if ($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';//虚拟销量
  103. if ($baseOrder) $baseOrder .= ', ';
  104. $model->order($baseOrder . 'sort DESC, add_time DESC');
  105. $list = $model->page((int)$page, (int)$limit)->field('id,store_name,store_info,keyword,cate_id,image,IFNULL(sales,0) + IFNULL(ficti,0) as sales,price,stock,ot_price')->select()->each(function ($item) use ($uid, $type, $store_id) {
  106. $item['percent'] = bcdiv($item['stock'], bcadd($item['stock'], $item['sales']), 4) * 100;
  107. $item['keyword'] = explode(',', $item['keyword']);
  108. if ($type) {
  109. $item['is_att'] = StoreProductAttrValueModel::where('product_id', $item['id'])->count() ? true : false;
  110. if ($uid) $item['cart_num'] = StoreCart::where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->where('type', 'product')->where('product_id', $item['id'])->where('uid', $uid)->value('cart_num');
  111. else $item['cart_num'] = 0;
  112. if (is_null($item['cart_num'])) $item['cart_num'] = 0;
  113. }
  114. if ($store_id) {
  115. $stockinfo = SystemStoreProductStock::where('product_id', $item['id'])->where('store_id', $store_id)->find();
  116. $item['price'] = $stockinfo ? SystemStoreProductStock::where('product_id', $item['id'])->where('store_id', $store_id)->min('price') : $item['price'];
  117. }
  118. });
  119. $list = count($list) ? $list->toArray() : [];
  120. if (!empty($list)) {
  121. foreach ($list as $k => $v) {
  122. $list[$k]['activity'] = self::activity($v['id']);
  123. }
  124. }
  125. return self::setLevelPrice($list, $uid);
  126. }
  127. /*
  128. * 分类搜索
  129. * @param string $value
  130. * @return array
  131. * */
  132. public static function getSearchStorePage($keyword, $page, $limit, $uid, $cutApart = [' ', ',', '-'])
  133. {
  134. $model = self::validWhere();
  135. $keyword = trim($keyword);
  136. if (strlen($keyword)) {
  137. $cut = false;
  138. foreach ($cutApart as $val) {
  139. if (strstr($keyword, $val) !== false) {
  140. $cut = $val;
  141. break;
  142. }
  143. }
  144. if ($cut !== false) {
  145. $keywordArray = explode($cut, $keyword);
  146. $sql = [];
  147. foreach ($keywordArray as $item) {
  148. $sql[] = '(`store_name` LIKE "%' . $item . '%" OR `keyword` LIKE "%' . $item . '%")';
  149. }
  150. $model = $model->where(implode(' OR ', $sql));
  151. } else {
  152. $model = $model->where('store_name|keyword', 'LIKE', "%$keyword%");
  153. }
  154. }
  155. $list = $model->field('id,store_name,cate_id,image,ficti as sales,price,stock')->page($page, $limit)->select();
  156. $list = count($list) ? $list->toArray() : [];
  157. if (!empty($list)) {
  158. foreach ($list as $k => $v) {
  159. $list[$k]['activity'] = self::activity($v['id']);
  160. }
  161. }
  162. return self::setLevelPrice($list, $uid);
  163. }
  164. /**
  165. * 新品产品
  166. * @param string $field
  167. * @param int $limit
  168. * @return false|\PDOStatement|string|\think\Collection
  169. */
  170. public static function getNewProduct($field = '*', $limit = 0, $uid = 0, bool $bool = true, $store_id = 0)
  171. {
  172. if (!$limit && !$bool) return [];
  173. $model = self::where('is_new', 1)->where('is_del', 0)->where('mer_id', 0)
  174. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  175. ->order('sort DESC, id DESC');
  176. if ($limit) $model->limit($limit);
  177. $list = $model->select();
  178. $list = count($list) ? $list->toArray() : [];
  179. if (!empty($list)) {
  180. foreach ($list as $k => $v) {
  181. if ($store_id) {
  182. $stockinfo = SystemStoreProductStock::where('product_id', $list[$k]['id'])->where('store_id', $store_id)->find();
  183. $item['price'] = $stockinfo ? SystemStoreProductStock::where('product_id', $list[$k]['id'])->where('store_id', $store_id)->min('price') : $list[$k]['price'];
  184. }
  185. $list[$k]['activity'] = self::activity($v['id']);
  186. }
  187. }
  188. return self::setLevelPrice($list, $uid);
  189. }
  190. /**
  191. * 热卖产品
  192. * @param string $field
  193. * @param int $limit
  194. * @return false|\PDOStatement|string|\think\Collection
  195. */
  196. public static function getHotProduct($field = '*', $limit = 0, $uid = 0, $store_id = 0)
  197. {
  198. $model = self::where('is_hot', 1)->where('is_del', 0)->where('mer_id', 0)
  199. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  200. ->order('sort DESC, id DESC');
  201. if ($limit) $model->limit($limit);
  202. $list = $model->select();
  203. $list = count($list) ? $list->toArray() : [];
  204. if (!empty($list)) {
  205. foreach ($list as $k => $v) {
  206. if ($store_id) {
  207. $stockinfo = SystemStoreProductStock::where('product_id', $list[$k]['id'])->where('store_id', $store_id)->find();
  208. $item['price'] = $stockinfo ? SystemStoreProductStock::where('product_id', $list[$k]['id'])->where('store_id', $store_id)->min('price') : $list[$k]['price'];
  209. }
  210. $list[$k]['activity'] = self::activity($v['id']);
  211. }
  212. }
  213. return self::setLevelPrice($list, $uid);
  214. }
  215. /**
  216. * 热卖产品
  217. * @param string $field
  218. * @param int $page
  219. * @param int $limit
  220. * @return array|\think\Collection
  221. * @throws \think\db\exception\DataNotFoundException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. * @throws \think\exception\DbException
  224. */
  225. public static function getHotProductLoading($field = '*', $page = 0, $limit = 0)
  226. {
  227. if (!$limit) return [];
  228. $model = self::where('is_hot', 1)->where('is_del', 0)->where('mer_id', 0)
  229. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  230. ->order('sort DESC, id DESC');
  231. if ($page) $model->page($page, $limit);
  232. $list = $model->select();
  233. if (is_object($list)) return $list->toArray();
  234. return $list;
  235. }
  236. /**
  237. * 精品产品
  238. * @param string $field
  239. * @param int $limit
  240. * @return false|\PDOStatement|string|\think\Collection
  241. */
  242. public static function getBestProduct($field = '*', $limit = 0, $uid = 0, bool $bool = true, $store_id = 0)
  243. {
  244. if (!$limit && !$bool) return [];
  245. $model = self::where('is_best', 1)->where('is_del', 0)->where('mer_id', 0)
  246. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  247. ->order('sort DESC, id DESC');
  248. if ($limit) $model->limit($limit);
  249. $list = $model->select();
  250. $list = count($list) ? $list->toArray() : [];
  251. if (!empty($list)) {
  252. foreach ($list as $k => $v) {
  253. $list[$k]['activity'] = self::activity($v['id']);
  254. // var_dump($store_id);
  255. if ($store_id) {
  256. $stockinfo = SystemStoreProductStock::where('product_id', $list[$k]['id'])->where('store_id', $store_id)->find();
  257. var_dump($stockinfo);
  258. $item['price'] = $stockinfo ? SystemStoreProductStock::where('product_id', $list[$k]['id'])->where('store_id', $store_id)->min('price') : $list[$k]['price'];
  259. }
  260. }
  261. }
  262. return self::setLevelPrice($list, $uid);
  263. }
  264. /**
  265. * 设置会员价格
  266. * @param object | array $list 产品列表
  267. * @param int $uid 用户uid
  268. * @return array
  269. * */
  270. public static function setLevelPrice($list, $uid, $isSingle = false)
  271. {
  272. if (is_object($list)) $list = count($list) ? $list->toArray() : [];
  273. if (!sys_config('vip_open')) {
  274. if (is_array($list)) return $list;
  275. return $isSingle ? $list : 0;
  276. }
  277. $levelId = UserLevel::getUserLevel($uid);
  278. if ($levelId) {
  279. $discount = UserLevel::getUserLevelInfo($levelId, 'discount');
  280. $discount = bcsub(1, bcdiv($discount, 100, 2), 2);
  281. } else {
  282. $discount = SystemUserLevel::getLevelDiscount();
  283. $discount = bcsub(1, bcdiv($discount, 100, 2), 2);
  284. }
  285. //如果不是数组直接执行减去会员优惠金额
  286. if (!is_array($list))
  287. //不是会员原价返回
  288. if ($levelId)
  289. //如果$isSingle==true 返回优惠后的总金额,否则返回优惠的金额
  290. return $isSingle ? bcsub($list, bcmul($discount, $list, 2), 2) : bcmul($discount, $list, 2);
  291. else
  292. return $isSingle ? $list : 0;
  293. //当$list为数组时$isSingle==true为一维数组 ,否则为二维
  294. if ($isSingle)
  295. $list['vip_price'] = isset($list['price']) ? bcsub($list['price'], bcmul($discount, $list['price'], 2), 2) : 0;
  296. else
  297. foreach ($list as &$item) {
  298. $item['vip_price'] = isset($item['price']) ? bcsub($item['price'], bcmul($discount, $item['price'], 2), 2) : 0;
  299. }
  300. return $list;
  301. }
  302. /**
  303. * 优惠产品
  304. * @param string $field
  305. * @param int $limit
  306. * @return false|\PDOStatement|string|\think\Collection
  307. */
  308. public static function getBenefitProduct($field = '*', $limit = 0, $store_id = 0)
  309. {
  310. $model = self::where('is_benefit', 1)
  311. ->where('is_del', 0)->where('mer_id', 0)->where('stock', '>', 0)
  312. ->where('is_show', 1)->field($field)
  313. ->order('sort DESC, id DESC');
  314. if ($limit) $model->limit($limit);
  315. $data = $model->select();
  316. if (count($data) > 0) {
  317. foreach ($data as $k => $v) {
  318. if ($store_id) {
  319. $stockinfo = SystemStoreProductStock::where('product_id', $data[$k]['id'])->where('store_id', $store_id)->find();
  320. $data['price'] = $stockinfo ? SystemStoreProductStock::where('product_id', $data[$k]['id'])->where('store_id', $store_id)->min('price') : $data[$k]['price'];
  321. }
  322. $data[$k]['activity'] = self::activity($v['id']);
  323. }
  324. }
  325. return $data;
  326. }
  327. public static function cateIdBySimilarityProduct($cateId, $field = '*', $limit = 0)
  328. {
  329. $pid = StoreCategory::cateIdByPid($cateId) ?: $cateId;
  330. $cateList = StoreCategory::pidByCategory($pid, 'id') ?: [];
  331. $cid = [$pid];
  332. foreach ($cateList as $cate) {
  333. $cid[] = $cate['id'];
  334. }
  335. $model = self::where('cate_id', 'IN', $cid)->where('is_show', 1)->where('is_del', 0)
  336. ->field($field)->order('sort DESC,id DESC');
  337. if ($limit) $model->limit($limit);
  338. return $model->select();
  339. }
  340. public static function isValidProduct($productId)
  341. {
  342. return self::be(['id' => $productId, 'is_del' => 0, 'is_show' => 1]) > 0;
  343. }
  344. public static function getProductStock($productId, $uniqueId = '')
  345. {
  346. return $uniqueId == '' ?
  347. self::where('id', $productId)->value('stock') ?: 0
  348. : StoreProductAttr::uniqueByStock($uniqueId);
  349. }
  350. /**
  351. * 加销量减销量
  352. * @param $num
  353. * @param $productId
  354. * @param string $unique
  355. * @return bool
  356. */
  357. public static function decProductStock($num, $productId, $unique = '')
  358. {
  359. if ($unique) {
  360. $res = false !== StoreProductAttrValueModel::decProductAttrStock($productId, $unique, $num, 0);
  361. $res = $res && self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
  362. } else {
  363. $res = false !== self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
  364. }
  365. if ($res) {
  366. $stock = self::where('id', $productId)->value('stock');
  367. $replenishment_num = sys_config('store_stock') ?? 0;//库存预警界限
  368. if ($replenishment_num >= $stock) {
  369. try {
  370. ChannelService::instance()->send('STORE_STOCK', ['id' => $productId]);
  371. } catch (\Exception $e) {
  372. }
  373. }
  374. }
  375. return $res;
  376. }
  377. /**
  378. * 减少销量,增加库存
  379. * @param int $num 增加库存数量
  380. * @param int $productId 产品id
  381. * @param string $unique 属性唯一值
  382. * @return boolean
  383. */
  384. public static function incProductStock($num, $productId, $unique = '')
  385. {
  386. $product = self::where('id', $productId)->field(['sales', 'stock'])->find();
  387. if (!$product) return true;
  388. if ($product->sales > 0) $product->sales = bcsub($product->sales, $num, 0);
  389. if ($product->sales < 0) $product->sales = 0;
  390. if ($unique) {
  391. $res = false !== StoreProductAttrValueModel::incProductAttrStock($productId, $unique, $num);
  392. //没有修改销量则直接返回
  393. if ($product->sales == 0) return true;
  394. $res = $res && $product->save();
  395. } else {
  396. $product->stock = bcadd($product->stock, $num, 0);
  397. $res = false !== $product->save();
  398. }
  399. return $res;
  400. }
  401. /**
  402. * 获取产品分销佣金最低和最高
  403. * @param $storeInfo
  404. * @param $productValue
  405. * @return int|string
  406. */
  407. public static function getPacketPrice($storeInfo, $productValue)
  408. {
  409. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  410. $store_brokerage_ratio = bcdiv($store_brokerage_ratio, 100, 2);
  411. if (isset($storeInfo['is_sub']) && $storeInfo['is_sub'] == 1) {
  412. $Maxkey = self::getArrayMax($productValue, 'brokerage');
  413. $Minkey = self::getArrayMin($productValue, 'brokerage');
  414. $maxPrice = bcadd(isset($productValue[$Maxkey]) ? $productValue[$Maxkey]['brokerage'] : 0, 0, 0);
  415. $minPrice = bcadd(isset($productValue[$Minkey]) ? $productValue[$Minkey]['brokerage'] : 0, 0, 0);
  416. } else {
  417. $Maxkey = self::getArrayMax($productValue, 'price');
  418. $Minkey = self::getArrayMin($productValue, 'price');
  419. $maxPrice = bcmul($store_brokerage_ratio, bcadd(isset($productValue[$Maxkey]) ? $productValue[$Maxkey]['price'] : 0, 0, 0), 0);
  420. $minPrice = bcmul($store_brokerage_ratio, bcadd(isset($productValue[$Minkey]) ? $productValue[$Minkey]['price'] : 0, 0, 0), 0);
  421. }
  422. if ($minPrice == 0 && $maxPrice == 0)
  423. return 0;
  424. else if ($minPrice == 0 && $maxPrice)
  425. return $maxPrice;
  426. else if ($maxPrice == 0 && $minPrice)
  427. return $minPrice;
  428. else if ($maxPrice == $minPrice && $minPrice)
  429. return $maxPrice;
  430. else
  431. return $minPrice . '~' . $maxPrice;
  432. }
  433. /**
  434. * 获取二维数组中最大的值
  435. * @param $arr
  436. * @param $field
  437. * @return int|string
  438. */
  439. public static function getArrayMax($arr, $field)
  440. {
  441. $temp = [];
  442. foreach ($arr as $k => $v) {
  443. $temp[] = $v[$field];
  444. }
  445. if (!count($temp)) return 0;
  446. $maxNumber = max($temp);
  447. foreach ($arr as $k => $v) {
  448. if ($maxNumber == $v[$field]) return $k;
  449. }
  450. return 0;
  451. }
  452. /**
  453. * 获取二维数组中最小的值
  454. * @param $arr
  455. * @param $field
  456. * @return int|string
  457. */
  458. public static function getArrayMin($arr, $field)
  459. {
  460. $temp = [];
  461. foreach ($arr as $k => $v) {
  462. $temp[] = $v[$field];
  463. }
  464. if (!count($temp)) return 0;
  465. $minNumber = min($temp);
  466. foreach ($arr as $k => $v) {
  467. if ($minNumber == $v[$field]) return $k;
  468. }
  469. return 0;
  470. }
  471. /**
  472. * 产品名称 图片
  473. * @param array $productIds
  474. * @return array
  475. */
  476. public static function getProductStoreNameOrImage(array $productIds)
  477. {
  478. return self::whereIn('id', $productIds)->column('store_name,image', 'id');
  479. }
  480. /**
  481. * TODO 获取某个字段值
  482. * @param $id
  483. * @param string $field
  484. * @return mixed
  485. */
  486. public static function getProductField($id, $field = 'store_name')
  487. {
  488. if (is_array($id))
  489. return self::where('id', 'in', $id)->field($field)->select();
  490. else
  491. return self::where('id', $id)->value($field);
  492. }
  493. /**
  494. * 获取产品返佣金额
  495. * @param array $cartId
  496. * @param bool $type true = 一级返佣, fasle = 二级返佣
  497. * @return array
  498. */
  499. public static function getProductBrokerage(array $cartId, bool $type = true)
  500. {
  501. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $cartId)->column('cart_info');
  502. $oneBrokerageReal = 0;//一级返佣金额
  503. $twoBrokerageReal = 0;//二级返佣金额
  504. $sumProductPriceReal = 0;//非指定返佣商品总金额
  505. $oneBrokerageVirtual = 0;//一级返佣金额
  506. $twoBrokerageVirtual = 0;//二级返佣金额
  507. $sumProductPriceVirtual = 0;//非指定返佣商品总金额
  508. foreach ($cartInfo as $value) {
  509. $product = json_decode($value, true);
  510. $cartNum = $product['cart_num'] ?? 0;
  511. if (isset($product['productInfo'])) {
  512. $productInfo = $product['productInfo'];
  513. //指定返佣金额
  514. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  515. if ($productInfo['store_type'] == 1) {
  516. $oneBrokerageReal = bcadd($oneBrokerageReal, bcmul($cartNum, $productInfo['attrInfo']['brokerage'] ?? 0, 2), 2);
  517. $twoBrokerageReal = bcadd($twoBrokerageReal, bcmul($cartNum, $productInfo['attrInfo']['brokerage_two'] ?? 0, 2), 2);
  518. } else {
  519. $oneBrokerageVirtual = bcadd($oneBrokerageVirtual, bcmul($cartNum, $productInfo['attrInfo']['brokerage'] ?? 0, 2), 2);
  520. $twoBrokerageVirtual = bcadd($twoBrokerageVirtual, bcmul($cartNum, $productInfo['attrInfo']['brokerage_two'] ?? 0, 2), 2);
  521. }
  522. } else {
  523. //比例返佣
  524. if ($productInfo['store_type'] == 1) {
  525. if (isset($productInfo['attrInfo'])) {
  526. $sumProductPriceReal = bcadd($sumProductPriceReal, bcmul($cartNum, $productInfo['attrInfo']['price'] ?? 0, 2), 2);
  527. } else {
  528. $sumProductPriceReal = bcadd($sumProductPriceReal, bcmul($cartNum, $productInfo['price'] ?? 0, 2), 2);
  529. }
  530. } else {
  531. if (isset($productInfo['attrInfo'])) {
  532. $sumProductPriceVirtual = bcadd($sumProductPriceVirtual, bcmul($cartNum, $productInfo['attrInfo']['price'] ?? 0, 2), 2);
  533. } else {
  534. $sumProductPriceVirtual = bcadd($sumProductPriceVirtual, bcmul($cartNum, $productInfo['price'] ?? 0, 2), 2);
  535. }
  536. }
  537. }
  538. }
  539. }
  540. if ($type) {
  541. //获取后台一级返佣比例
  542. $storeBrokerageRatio = sys_config('store_brokerage_ratio');
  543. //一级返佣比例 小于等于零时直接返回 不返佣
  544. if ($storeBrokerageRatio <= 0) {
  545. return [$oneBrokerageReal, $oneBrokerageVirtual];
  546. }
  547. //计算获取一级返佣比例
  548. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 2);
  549. $brokeragePriceReal = bcmul($sumProductPriceReal, $brokerageRatio, 2);
  550. $brokeragePriceVirtual = bcmul($sumProductPriceVirtual, $brokerageRatio, 2);
  551. //固定返佣 + 比例返佣 = 一级总返佣金额
  552. return [bcadd($oneBrokerageReal, $brokeragePriceReal, 2), bcadd($oneBrokerageVirtual, $brokeragePriceVirtual, 2)];
  553. } else {
  554. //获取二级返佣比例
  555. $storeBrokerageTwo = sys_config('store_brokerage_two');
  556. //二级返佣比例小于等于0 直接返回
  557. if ($storeBrokerageTwo <= 0) {
  558. return [$twoBrokerageReal, $twoBrokerageVirtual];
  559. }
  560. //计算获取二级返佣比例
  561. $brokerageRatio = bcdiv($storeBrokerageTwo, 100, 2);
  562. $brokeragePriceReal = bcmul($sumProductPriceReal, $brokerageRatio, 2);
  563. $brokeragePriceVirtual = bcmul($sumProductPriceVirtual, $brokerageRatio, 2);
  564. //固定返佣 + 比例返佣 = 二级总返佣金额
  565. return [bcadd($twoBrokerageReal, $brokeragePriceReal, 2), bcadd($twoBrokerageVirtual, $brokeragePriceVirtual, 2)];
  566. }
  567. }
  568. /**
  569. * 获取商品在此时段活动优先类型
  570. */
  571. public static function activity($id, $status = true)
  572. {
  573. $activity = self::where('id', $id)->value('activity');
  574. if (!$activity) $activity = '1,2,3';//如果老商品没有活动顺序,默认活动顺序,秒杀-砍价-拼团
  575. $activity = explode(',', $activity);
  576. $activityId = [];
  577. $time = 0;
  578. $seckillId = StoreSeckill::where('is_del', 0)->where('status', 1)->where('start_time', '<=', time())->where('stop_time', '>=', time() - 86400)->where('product_id', $id)->field('id,time_id')->select();
  579. if ($seckillId) {
  580. foreach ($seckillId as $v) {
  581. $timeInfo = GroupDataService::getDataNumber((int)$v['time_id']);
  582. if ($timeInfo && isset($timeInfo['time']) && isset($timeInfo['continued'])) {
  583. if (date('H') >= $timeInfo['time'] && date('H') < ($timeInfo['time'] + $timeInfo['continued'])) {
  584. $activityId[1] = $v['id'];
  585. $time = strtotime(date("Y-m-d"), time()) + 3600 * ($timeInfo['time'] + $timeInfo['continued']);
  586. }
  587. }
  588. }
  589. }
  590. $bargainId = StoreBargain::where('is_del', 0)->where('status', 1)->where('start_time', '<=', time())->where('stop_time', '>=', time())->where('product_id', $id)->value('id');
  591. if ($bargainId) $activityId[2] = $bargainId;
  592. $combinationId = StoreCombination::where('is_del', 0)->where('is_show', 1)->where('start_time', '<=', time())->where('stop_time', '>=', time())->where('product_id', $id)->value('id');
  593. if ($combinationId) $activityId[3] = $combinationId;
  594. $data = [];
  595. foreach ($activity as $k => $v) {
  596. if (array_key_exists($v, $activityId)) {
  597. if ($status) {
  598. $data['type'] = $v;
  599. $data['id'] = $activityId[$v];
  600. if ($v == 1) $data['time'] = $time;
  601. break;
  602. } else {
  603. $arr['type'] = $v;
  604. $arr['id'] = $activityId[$v];
  605. if ($v == 1) $arr['time'] = $time;
  606. $data[] = $arr;
  607. }
  608. }
  609. }
  610. return $data;
  611. }
  612. }