StoreProduct.php 26 KB

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