StoreProduct.php 27 KB

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