StoreProduct.php 23 KB

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