StoreProductDao.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\product\product;
  12. use app\dao\BaseDao;
  13. use app\model\product\product\StoreProduct;
  14. use think\facade\Config;
  15. /**
  16. * Class StoreProductDao
  17. * @package app\dao\product\product
  18. */
  19. class StoreProductDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return StoreProduct::class;
  28. }
  29. /**
  30. * @param array $where
  31. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  32. */
  33. public function search(array $where = [])
  34. {
  35. return parent::search($where)
  36. ->when(isset($where['collate_code_id']) && $where['collate_code_id'], function ($query) use ($where) {
  37. $query->where('product_type', 0)->where('is_presale_product', 0)->where('system_form_id', 0);
  38. })->when(isset($where['is_integral']) && $where['is_integral'], function ($query) use ($where) {
  39. $query->where('product_type', 'in', [0, 1, 2, 3]);
  40. })
  41. ->when(isset($where['store_name']) && $where['store_name'], function ($query) use ($where) {
  42. if (isset($where['field_key']) && $where['field_key'] && in_array($where['field_key'], ['product_id', 'bar_code'])) {
  43. if ($where['field_key'] == 'product_id') {
  44. $query->where('id', trim($where['store_name']));
  45. } else {
  46. $query->where(function ($query) use ($where) {
  47. $query->where('bar_code', trim($where['store_name']))->whereOr('id', 'IN', function ($q) use ($where) {
  48. $q->name('store_product_attr_value')->field('product_id')->where('bar_code', trim($where['store_name']))->select();
  49. });
  50. });
  51. }
  52. } else {
  53. $query->where(function ($q) use ($where) {
  54. $q->where('id|store_name|bar_code|keyword', 'LIKE', '%' . trim($where['store_name']) . '%')->whereOr('id', 'IN', function ($q) use ($where) {
  55. $q->name('store_product_attr_value')->field('product_id')->where('bar_code', trim($where['store_name']))->select();
  56. });
  57. });
  58. }
  59. })->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  60. $query->whereIn('id', function ($query) use ($where) {
  61. $query->name('store_product_relation')->where('type', 1)->where('relation_id', $where['sid'])->field('product_id')->select();
  62. });
  63. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  64. $query->whereIn('id', function ($query) use ($where) {
  65. $query->name('store_product_relation')->where('type', 1)->whereIn('relation_id', function ($query) use ($where) {
  66. $query->name('store_product_category')->where('id|pid', $where['cid'])->field('id')->select();
  67. })->field('product_id')->select();
  68. });
  69. })->when(isset($where['brand_id']) && $where['brand_id'], function ($query) use ($where) {
  70. $query->whereIn('id', function ($query) use ($where) {
  71. $query->name('store_product_relation')->where('type', 2)->whereIn('relation_id', $where['brand_id'])->field('product_id')->select();
  72. });
  73. })->when(isset($where['store_label_id']) && $where['store_label_id'], function ($query) use ($where) {
  74. $query->whereIn('id', function ($query) use ($where) {
  75. $query->name('store_product_relation')->where('type', 3)->whereIn('relation_id', $where['store_label_id'])->field('product_id')->select();
  76. });
  77. })->when(isset($where['is_live']) && $where['is_live'] == 1, function ($query) use ($where) {
  78. $query->whereNotIn('id', function ($query) {
  79. $query->name('live_goods')->where('is_del', 0)->where('audit_status', '<>', 3)->field('product_id')->select();
  80. });
  81. })->when(isset($where['is_supplier']) && in_array((int)$where['is_supplier'], [0, 1]), function ($query) use ($where) {
  82. if ($where['is_supplier'] == 1) {//查询供应商商品
  83. $query->where('type', 2);
  84. } else {
  85. $query->where('type', '<>', 2);
  86. }
  87. });
  88. }
  89. /**
  90. * 条件获取数量
  91. * @param array $where
  92. * @return int
  93. */
  94. public function getCount(array $where)
  95. {
  96. return $this->search($where)
  97. ->when(isset($where['unit_name']) && $where['unit_name'] !== '', function ($query) use ($where) {
  98. $query->where('unit_name', $where['unit_name']);
  99. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  100. if (!isset($where['type'])) $query->where('id', 'in', $where['ids']);
  101. })->when(isset($where['not_ids']) && $where['not_ids'], function ($query) use ($where) {
  102. $query->whereNotIn('id', $where['not_ids']);
  103. })->when(isset($where['pids']) && $where['pids'], function ($query) use ($where) {
  104. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  105. $query->whereIn('pid', $where['pids']);
  106. } else {
  107. $query->whereIn('pid', $where['pids'])->orderField('pid', $where['pids'], 'asc');
  108. }
  109. })->when(isset($where['not_pids']) && $where['not_pids'], function ($query) use ($where) {
  110. $query->whereNotIn('pid', $where['not_pids']);
  111. })->count();
  112. }
  113. /**
  114. * 获取商品列表
  115. * @param array $where
  116. * @param int $page
  117. * @param int $limit
  118. * @param string $order
  119. * @param array $with
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function getList(array $where, int $page = 0, int $limit = 0, string $order = '', array $with = [])
  126. {
  127. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  128. return $this->search($where)->order(($order ? $order . ' ,' : '') . 'sort desc,id desc')
  129. ->when(count($with), function ($query) use ($with) {
  130. $query->with($with);
  131. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  132. $query->page($page, $limit);
  133. })->when(isset($where['store_id']) && $where['store_id'], function ($query) use ($where) {
  134. $query->with(['storeBranchProduct' => function ($querys) use ($where) {
  135. $querys->where('store_id', $where['store_id'])->bind([
  136. 'branch_sales' => 'sales',
  137. 'branch_stock' => 'stock',
  138. 'branch_is_show' => 'is_show'
  139. ]);
  140. }]);
  141. })->when(isset($where['ids']), function ($query) use ($where) {
  142. $query->where('id', 'in', $where['ids']);
  143. })->field([
  144. '*',
  145. '(SELECT count(*) FROM `' . $prefix . 'user_relation` WHERE `relation_id` = `' . $prefix . 'store_product`.`id` AND `category` = \'product\' AND `type` = \'collect\') as collect',
  146. '(SELECT count(*) FROM `' . $prefix . 'user_relation` WHERE `relation_id` = `' . $prefix . 'store_product`.`id` AND `category` = \'product\' AND `type` = \'like\') as likes',
  147. '(SELECT SUM(stock) FROM `' . $prefix . 'store_product_attr_value` WHERE `product_id` = `' . $prefix . 'store_product`.`id` AND `type` = 0) as stock',
  148. // '(SELECT SUM(sales) FROM `' . $prefix . 'store_product_attr_value` WHERE `product_id` = `' . $prefix . 'store_product`.`id` AND `type` = 0) as sales',
  149. '(SELECT count(*) FROM `' . $prefix . 'store_visit` WHERE `product_id` = `' . $prefix . 'store_product`.`id` AND `product_type` = \'product\') as visitor',
  150. ])->select()->toArray();
  151. }
  152. /**
  153. * 获取商品详情
  154. * @param int $id
  155. * @return array|\think\Model|null
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public function getInfo(int $id)
  161. {
  162. return $this->search()->with('coupons')->find($id);
  163. }
  164. /**
  165. * 获取门店商品
  166. * @param $where
  167. * @return array|\think\Model|null
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. */
  172. public function getBranchProduct($where)
  173. {
  174. return $this->search($where)->find();
  175. }
  176. /**
  177. * 条件获取商品列表
  178. * @param array $where
  179. * @param int $page
  180. * @param int $limit
  181. * @param array $field
  182. * @param string $order
  183. * @param array $with
  184. * @return array
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function getSearchList(array $where, int $page = 0, int $limit = 0, array $field = ['*'], string $order = '', array $with = ['couponId', 'descriptions'])
  190. {
  191. if (isset($where['star'])) $with[] = 'star';
  192. return $this->search($where)->with($with)->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  193. $query->page($page, $limit);
  194. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  195. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  196. $query->whereIn('id', $where['ids']);
  197. } else {
  198. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  199. }
  200. })->when(isset($where['not_ids']) && $where['not_ids'], function ($query) use ($where) {
  201. $query->whereNotIn('id', $where['not_ids']);
  202. })->when(isset($where['pids']) && $where['pids'], function ($query) use ($where) {
  203. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  204. $query->whereIn('pid', $where['pids']);
  205. } else {
  206. $query->whereIn('pid', $where['pids'])->orderField('pid', $where['pids'], 'asc');
  207. }
  208. })->when(isset($where['not_pids']) && $where['not_pids'], function ($query) use ($where) {
  209. $query->whereNotIn('pid', $where['not_pids']);
  210. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  211. if ($where['priceOrder'] === 'desc') {
  212. $query->order("price desc");
  213. } else {
  214. $query->order("price asc");
  215. }
  216. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  217. if ($where['salesOrder'] === 'desc') {
  218. $query->order("sales desc");
  219. } else {
  220. $query->order("sales asc");
  221. }
  222. })->when(!isset($where['ids']), function ($query) use ($where, $order) {
  223. if (isset($where['timeOrder']) && $where['timeOrder'] == 1) {
  224. $query->order('id desc');
  225. } else if ($order == 'rand') {
  226. $query->orderRand();
  227. } else if ($order) {
  228. $query->orderRaw($order);
  229. } else {
  230. $query->order('sort desc,id desc');
  231. }
  232. })->when(isset($where['use_min_price']) && $where['use_min_price'], function ($query) use ($where) {
  233. if (is_array($where['use_min_price']) && count($where['use_min_price']) == 2) {
  234. $query->where('price', $where['use_min_price'][0] ?? '=', $where['use_min_price'][1] ?? 0);
  235. }
  236. })->when(isset($where['award']) && $where['award'] >= 0, function ($query) use ($where) {
  237. if ($where['award'])
  238. $query->where('award_price', '>', 0);
  239. else
  240. $query->where('award_price', 0);
  241. })->when(!$page && $limit, function ($query) use ($limit) {
  242. $query->limit($limit);
  243. })->field($field)->select()->toArray();
  244. }
  245. /**商品列表
  246. * @param array $where
  247. * @param $limit
  248. * @param $field
  249. * @return array
  250. * @throws \think\db\exception\DataNotFoundException
  251. * @throws \think\db\exception\DbException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. */
  254. public function getProductLimit(array $where, $limit, $field)
  255. {
  256. return $this->search($where)->field($field)->order('val', 'desc')->limit($limit)->select()->toArray();
  257. }
  258. /**
  259. * 根据id获取商品数据
  260. * @param array $ids
  261. * @param string $field
  262. * @return array
  263. * @throws \think\db\exception\DataNotFoundException
  264. * @throws \think\db\exception\DbException
  265. * @throws \think\db\exception\ModelNotFoundException
  266. */
  267. public function idByProductList(array $ids, string $field)
  268. {
  269. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  270. }
  271. /**
  272. * 获取推荐商品
  273. * @param array $where
  274. * @param array $field
  275. * @param int $num
  276. * @param int $page
  277. * @param int $limit
  278. * @param array $with
  279. * @return array
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\DbException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. */
  284. public function getRecommendProduct(array $where, array $field = ['*'], int $num = 0, int $page = 0, int $limit = 0, array $with = ['couponId', 'star'])
  285. {
  286. return $this->search($where)->field($field)
  287. ->when(count($with), function ($query) use ($with) {
  288. $query->with($with);
  289. })->when($num, function ($query) use ($num) {
  290. $query->limit($num);
  291. })->when($page, function ($query) use ($page, $limit) {
  292. $query->page($page, $limit);
  293. })->when($limit, function ($query) use ($limit) {
  294. $query->limit($limit);
  295. })->order('sort DESC, id DESC')->select()->toArray();
  296. }
  297. /**
  298. * 获取加入购物车的商品
  299. * @param array $where
  300. * @param int $page
  301. * @param int $limit
  302. * @return array
  303. * @throws \think\db\exception\DataNotFoundException
  304. * @throws \think\db\exception\DbException
  305. * @throws \think\db\exception\ModelNotFoundException
  306. */
  307. public function getProductCartList(array $where, int $page, int $limit, array $field = ['*'])
  308. {
  309. $where['is_verify'] = 1;
  310. $where['is_show'] = 1;
  311. $where['is_del'] = 0;
  312. return $this->search($where)->when($page, function ($query) use ($page, $limit) {
  313. $query->page($page, $limit);
  314. })->field($field)->order('sort DESC,id DESC')->select()->toArray();
  315. }
  316. /**
  317. * 获取用户购买热销榜单
  318. * @param array $where
  319. * @param int $limit
  320. * @return array
  321. * @throws \think\db\exception\DataNotFoundException
  322. * @throws \think\db\exception\DbException
  323. * @throws \think\db\exception\ModelNotFoundException
  324. */
  325. public function getUserProductHotSale(array $where, int $limit = 20)
  326. {
  327. return $this->search($where)->field(['IFNULL(sales,0) + IFNULL(ficti,0) as sales', 'store_name', 'image', 'id', 'price', 'ot_price', 'stock'])->limit($limit)->order('sales desc')->select()->toArray();
  328. }
  329. /**
  330. * 通过商品id获取商品分类
  331. * @param array $productIds
  332. * @return array
  333. * @throws \think\db\exception\DataNotFoundException
  334. * @throws \think\db\exception\DbException
  335. * @throws \think\db\exception\ModelNotFoundException
  336. */
  337. public function productIdByCateId(array $productIds)
  338. {
  339. return $this->search(['id' => $productIds])->with('cateName')->field('id')->select()->toArray();
  340. }
  341. /**
  342. * @param array $where
  343. * @param $field
  344. * @return array
  345. * @throws \think\db\exception\DataNotFoundException
  346. * @throws \think\db\exception\DbException
  347. * @throws \think\db\exception\ModelNotFoundException
  348. */
  349. public function getProductListByWhere(array $where, $field)
  350. {
  351. return $this->search($where)->field($field)->select()->toArray();
  352. }
  353. /**
  354. * 搜索条件获取字段column
  355. * @param array $where
  356. * @param string $field
  357. * @param string $key
  358. * @return array
  359. */
  360. public function getColumnList(array $where, string $field = 'brand_id', string $key = 'id')
  361. {
  362. return $this->search($where)
  363. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  364. $query->whereIn('id', function ($query) use ($where) {
  365. $query->name('store_product_relation')->where('type', 1)->where('relation_id', $where['sid'])->field('product_id')->select();
  366. });
  367. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  368. $query->whereIn('id', function ($query) use ($where) {
  369. $query->name('store_product_relation')->where('type', 1)->whereIn('relation_id', function ($query) use ($where) {
  370. $query->name('store_product_category')->where('id|pid', $where['cid'])->field('id')->select();
  371. })->field('product_id')->select();
  372. });
  373. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  374. $query->whereIn('id', $where['ids']);
  375. })->field($field)->column($field, $key);
  376. }
  377. /**
  378. * 自动上下架
  379. * @param int $is_show
  380. * @return \crmeb\basic\BaseModel
  381. */
  382. public function overUpperShelves($is_show = 0)
  383. {
  384. return $this->getModel()->where(['is_del' => 0])->where('is_verify', 1)
  385. ->when(in_array($is_show, [0, 1]), function ($query) use ($is_show) {
  386. if ($is_show == 1) {
  387. $query->where('is_show', 0)->where('auto_on_time', '<>', 0)->where('auto_on_time', '<=', time());
  388. } else {
  389. $query->where('is_show', 1)->where('auto_off_time', '<>', 0)->where('auto_off_time', '<', time());
  390. }
  391. })->update(['is_show' => $is_show]);
  392. }
  393. /**
  394. * 获取预售列表
  395. * @param array $where
  396. * @param int $page
  397. * @param int $limit
  398. * @param string $field
  399. * @return array
  400. * @throws \think\db\exception\DataNotFoundException
  401. * @throws \think\db\exception\DbException
  402. * @throws \think\db\exception\ModelNotFoundException
  403. */
  404. public function getPresaleList(array $where, int $page, int $limit, string $field = '*')
  405. {
  406. $model = $this->getModel()->field($field)
  407. ->where('is_presale_product', 1)->where('is_del', 0)->where('is_show', 1)
  408. ->where(function ($query) use ($where) {
  409. switch ($where['time_type']) {
  410. case 1:
  411. $query->where('presale_start_time', '>', time());
  412. break;
  413. case 2:
  414. $query->where('presale_start_time', '<=', time())->where('presale_end_time', '>=', time());
  415. break;
  416. case 3:
  417. $query->where('presale_end_time', '<', time());
  418. break;
  419. }
  420. if ($where['type']) $query->whereIn('type', $where['type']);
  421. });
  422. $count = $model->count();
  423. $list = $model->when($page && $limit, function ($query) use ($page, $limit) {
  424. $query->page($page, $limit);
  425. })->order('add_time desc')->select()->toArray();
  426. return compact('list', 'count');
  427. }
  428. /**
  429. * 获取使用某服务保障商品数量
  430. * @param int $ensure_id
  431. * @return int
  432. */
  433. public function getUseEnsureCount(int $ensure_id)
  434. {
  435. return $this->getModel()->whereFindInSet('ensure_id', $ensure_id)->count();
  436. }
  437. /**
  438. * 保存数据
  439. * @param array $data
  440. * @return mixed|\think\Collection
  441. * @throws \Exception
  442. */
  443. public function saveAll(array $data)
  444. {
  445. return $this->getModel()->saveAll($data);
  446. }
  447. /**
  448. * 同步商品保存获取id
  449. * @param $data
  450. * @return int|string
  451. */
  452. public function ErpProductSave($data)
  453. {
  454. return $this->getModel()->insertGetId($data);
  455. }
  456. }