StoreProduct.php 26 KB

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