StoreProduct.php 24 KB

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