StoreProduct.php 24 KB

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