StoreProduct.php 25 KB

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