StoreProduct.php 30 KB

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