StoreProduct.php 27 KB

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