StoreProduct.php 27 KB

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