StoreProduct.php 24 KB

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