StoreProduct.php 25 KB

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