StoreProduct.php 30 KB

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