StoreProduct.php 28 KB

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