StoreProduct.php 28 KB

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