StoreProduct.php 24 KB

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