StoreProduct.php 26 KB

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