ProductDao.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. namespace app\common\dao\store\product;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\store\product\Product as model;
  5. use app\common\repositories\store\product\SpuRepository;
  6. use app\common\repositories\store\StoreCategoryRepository;
  7. use think\db\BaseQuery;
  8. use think\db\exception\DbException;
  9. use think\facade\Db;
  10. class ProductDao extends BaseDao
  11. {
  12. protected function getModel(): string
  13. {
  14. return model::class;
  15. }
  16. /**
  17. * @Author:Qinii
  18. * @Date: 2020/5/9
  19. * @param int $id
  20. * @param array $data
  21. */
  22. public function createAttr(int $id, array $data)
  23. {
  24. ($this->getModel()::withTrashed()->find($id))->attr()->saveAll($data);
  25. }
  26. /**
  27. * @Author:Qinii
  28. * @Date: 2020/5/9
  29. * @param int $id
  30. * @param array $data
  31. */
  32. public function createAttrValue(int $id, array $data)
  33. {
  34. ($this->getModel()::withTrashed()->find($id))->attrValue()->saveAll($data);
  35. }
  36. /**
  37. * @Author:Qinii
  38. * @Date: 2020/5/9
  39. * @param int $id
  40. * @param array $data
  41. */
  42. public function createContent(int $id, array $data)
  43. {
  44. ($this->getModel()::withTrashed()->find($id))->content()->save($data);
  45. }
  46. /**
  47. * @Author:Qinii
  48. * @Date: 2020/5/9
  49. * @param int $merId
  50. * @param $field
  51. * @param $value
  52. * @param null $except
  53. * @return bool
  54. */
  55. public function merFieldExists(?int $merId, $field, $value, $except = null)
  56. {
  57. return model::withTrashed()->when($except, function ($query, $except) use ($field) {
  58. $query->where($field, '<>', $except);
  59. })->when($merId, function ($query, $merId) {
  60. $query->where('mer_id', $merId);
  61. })->where($field, $value)->count() > 0;
  62. }
  63. public function apiFieldExists(int $merId, $field, $value, $except = null)
  64. {
  65. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  66. $query->where($field, '<>', $except);
  67. })->when($merId, function ($query, $merId) {
  68. $query->where('mer_id', $merId);
  69. })->where(['status' => 1])->where($field, $value)->count() > 0;
  70. }
  71. /**
  72. * @param int $merId
  73. * @param int $productId
  74. * @return bool
  75. * @author Qinii
  76. */
  77. public function getDeleteExists(int $merId, int $productId)
  78. {
  79. return ($this->getModel())::onlyTrashed()->where('mer_id', $merId)->where($this->getPk(), $productId)->count() > 0;
  80. }
  81. /**
  82. * @Author:Qinii
  83. * @Date: 2020/5/11
  84. * @param int $merId
  85. * @param array $where
  86. * @return mixed
  87. */
  88. public function search(?int $merId, array $where)
  89. {
  90. $keyArray = $whereArr = [];
  91. unset($where['type']);
  92. foreach ($where as $key => $item) {
  93. if ($item !== '' && $key != 'soft' && $key != 'us_status') {
  94. $keyArray[] = $key;
  95. $whereArr[$key] = $item;
  96. }
  97. }
  98. $query = isset($where['soft']) ? model::onlyTrashed()->alias('Product') : model::alias('Product');
  99. if (isset($where['is_trader']) && $where['is_trader'] !== '') {
  100. $query->hasWhere('merchant', function ($query) use ($where) {
  101. $query->where('is_trader', $where['is_trader']);
  102. });
  103. }
  104. $query->withSearch($keyArray, $whereArr)
  105. ->when(($merId !== null), function ($query) use ($merId) {
  106. $query->where('Product.mer_id', $merId);
  107. })
  108. ->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) {
  109. if ($where['hot_type'] == 'new')
  110. $query->where('is_new', 1);
  111. else if ($where['hot_type'] == 'hot')
  112. $query->where('is_hot', 1);
  113. else if ($where['hot_type'] == 'best')
  114. $query->where('is_best', 1);
  115. else if ($where['hot_type'] == 'good')
  116. $query->where('is_benefit', 1);
  117. })
  118. ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  119. $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
  120. $ids = array_merge($storeCategoryRepository->findChildrenId((int)$where['pid']), [(int)$where['pid']]);
  121. if (count($ids)) $query->whereIn('cate_id', $ids);
  122. })
  123. ->when(isset($where['us_status']) && $where['us_status'] !== '', function ($query) use ($where) {
  124. if ($where['us_status'] == 0) {
  125. $query->where('Product.is_show', 0)->where('Product.is_used', 1)->where('Product.status',1);
  126. }
  127. if ($where['us_status'] == 1) {
  128. $query->where('Product.is_show', 1)->where('Product.is_used', 1)->where('Product.status',1);
  129. }
  130. if ($where['us_status'] == -1) {
  131. $query->where(function($query){
  132. $query->where('Product.is_used',0)->whereOr('Product.status','<>',1);
  133. });
  134. }
  135. })
  136. ->when(isset($where['order']), function ($query) use ($where, $merId) {
  137. $query->when(
  138. in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sales']),
  139. function ($query) use ($where) {
  140. if ($where['order'] == 'price_asc') {
  141. $where['order'] = 'price ASC';
  142. } else if ($where['order'] == 'price_desc') {
  143. $where['order'] = 'price DESC';
  144. } else {
  145. $where['order'] = $where['order'] . ' DESC';
  146. }
  147. $query->order($where['order'] . ',rank DESC ,create_time DESC ');
  148. },
  149. function ($query) use ($merId) {
  150. if ($merId) $query->order('Product.sort DESC ,Product.create_time DESC');
  151. }
  152. );
  153. })
  154. ->when(isset($where['star']),function($query)use($where){
  155. $query->Join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', $where['product_type'] ?? 0);
  156. $query->when($where['star'] !== '', function ($query) use ($where) {
  157. $query->where('U.star', $where['star']);
  158. });
  159. $query->order('U.star DESC,U.rank DESC,Product.create_time DESC');
  160. });
  161. return $query;
  162. }
  163. /**
  164. * TODO
  165. * @param array $where
  166. * @return BaseQuery
  167. * @author Qinii
  168. * @day 2020-08-04
  169. */
  170. public function seckillSearch(array $where)
  171. {
  172. $query = model::hasWhere('seckillActive', function ($query) use ($where) {
  173. $query->where('status', 1);
  174. $query->whereTime('start_day', '<=', $where['day'])->whereTime('end_day', '>=', $where['day']);
  175. $query->where('start_time', '<=', $where['start_time'])
  176. ->where('end_time', '>', $where['start_time'])
  177. ->where('end_time', '<=', $where['end_time']);
  178. });
  179. $query->where([
  180. 'Product.is_show' => 1,
  181. 'Product.status' => 1,
  182. 'Product.is_used' => 1,
  183. 'Product.mer_status' => 1,
  184. 'Product.product_type' => 1,
  185. 'Product.is_gift_bag' => 0,
  186. ])
  187. ->when(isset($where['star']),function($query)use($where){
  188. $query->Join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', 1);
  189. $query->when($where['star'] !== '', function ($query) use ($where) {
  190. $query->where('U.star', $where['star']);
  191. });
  192. $query->order('U.star DESC,U.rank DESC');
  193. });
  194. return $query;
  195. }
  196. /**
  197. * @Author:Qinii
  198. * @Date: 2020/5/18
  199. * @param int $id
  200. * @param bool $soft
  201. * @return int|mixed
  202. */
  203. public function delete(int $id, $soft = false)
  204. {
  205. if ($soft) {
  206. (($this->getModel())::onlyTrashed()->find($id))->force()->delete();
  207. } else {
  208. $this->getModel()::where($this->getPk(), $id)->update(['is_del' => 1]);
  209. }
  210. app()->make(SpuRepository::class)->getSearch(['product_id' => $id])->update(['is_del' => 1, 'status' => 0]);
  211. }
  212. /**
  213. * TODO
  214. * @param $id
  215. * @return mixed
  216. * @author Qinii
  217. * @day 2020-07-03
  218. */
  219. public function restore($id)
  220. {
  221. $res = ($this->getModel())::onlyTrashed()->find($id);
  222. app()->make(SpuRepository::class)->delProduct($id, 0);
  223. return $res->restore();
  224. }
  225. /**
  226. * @Author:Qinii
  227. * @Date: 2020/5/18
  228. * @param int $id
  229. * @param array $status
  230. * @return mixed
  231. */
  232. public function switchStatus(int $id, array $status)
  233. {
  234. return ($this->getModel()::getDB())->where($this->getPk(), $id)->update($status);
  235. }
  236. /**
  237. * @param int $merId
  238. * @param array $productIds
  239. * @return array
  240. * @author zfy
  241. * @day 2020/5/26
  242. */
  243. public function productIdByImage(int $merId, array $productIds)
  244. {
  245. return model::getDB()->where('mer_id', $merId)->whereIn('product_id', $productIds)->column('product_id,image');
  246. }
  247. /**
  248. * @param array $ids
  249. * @return array
  250. * @author zfy
  251. * @day 2020/5/30
  252. */
  253. public function intersectionKey(array $ids): array
  254. {
  255. return model::getDB()->whereIn('product_id', $ids)->column('product_id');
  256. }
  257. /**
  258. * @Author:Qinii
  259. * @Date: 2020/5/30
  260. * @param $id
  261. * @return mixed
  262. */
  263. public function productIdByMerId($id)
  264. {
  265. return model::getDB()->where('product_id', $id)->value('mer_id');
  266. }
  267. /**
  268. * @param int $productId
  269. * @param int $desc
  270. * @return int
  271. * @throws DbException
  272. * @author zfy
  273. * @day 2020/6/8
  274. */
  275. public function descStock(int $productId, int $desc)
  276. {
  277. return model::getDB()->where('product_id', $productId)->update([
  278. 'stock' => Db::raw('stock-' . $desc),
  279. 'sales' => Db::raw('sales+' . $desc)
  280. ]);
  281. }
  282. /**
  283. * @param int $productId
  284. * @param int $inc
  285. * @return int
  286. * @throws DbException
  287. * @author zfy
  288. * @day 2020/6/8
  289. */
  290. public function incStock(int $productId, int $inc)
  291. {
  292. model::getDB()->where('product_id', $productId)->inc('stock', $inc)->update();
  293. model::getDB()->where('product_id', $productId)->where('sales', '>=', $inc)->dec('sales', $inc)->update();
  294. }
  295. public function descIntegral(int $productId, $integral_total, $integral_price_total)
  296. {
  297. return model::getDB()->where('product_id', $productId)->update([
  298. 'integral_total' => Db::raw('integral_total-' . $integral_total),
  299. 'integral_price_total' => Db::raw('integral_price_total-' . $integral_price_total),
  300. ]);
  301. }
  302. public function incIntegral(int $productId, $integral_total, $integral_price_total)
  303. {
  304. model::getDB()->where('product_id', $productId)->inc('integral_total', $integral_total)->inc('integral_price_total', $integral_price_total)->update();
  305. }
  306. public function visitProductGroup($date, $merId = null, $limit = 7)
  307. {
  308. return model::getDB()->alias('A')->leftJoin('UserRelation B', 'A.product_id = B.type_id')
  309. ->field(Db::raw('count(B.type_id) as total,A.product_id,A.store_name,A.image'))
  310. ->when($date, function ($query, $date) {
  311. getModelTime($query, $date, 'B.create_time');
  312. })->when($merId, function ($query, $merId) {
  313. $query->where('A.mer_id', $merId);
  314. })->where('B.type', 1)->group('A.product_id')->limit($limit)->order('total DESC')->select();
  315. }
  316. public function cartProductGroup($date, $merId = null, $limit = 7)
  317. {
  318. return model::getDB()->alias('A')->leftJoin('StoreCart B', 'A.product_id = B.product_id')
  319. ->field(Db::raw('sum(B.cart_num) as total,A.product_id,A.store_name,A.image'))
  320. ->when($date, function ($query, $date) {
  321. getModelTime($query, $date, 'B.create_time');
  322. })->when($merId, function ($query, $merId) {
  323. $query->where('A.mer_id', $merId);
  324. })->where('B.product_type', 0)->where('B.is_pay', 0)->where('B.is_del', 0)
  325. ->where('B.is_new', 0)->where('B.is_fail', 0)->group('A.product_id')->limit($limit)->order('total DESC')->select();
  326. }
  327. public function changeMerchantProduct($merId, $data)
  328. {
  329. ($this->getModel()::getDB())->where('mer_id', $merId)->update($data);
  330. }
  331. /**
  332. * TODO
  333. * @param int $productId
  334. * @author Qinii
  335. * @day 2020-07-09
  336. */
  337. public function incCareCount(int $productId)
  338. {
  339. ($this->getModel()::getDB())->where($this->getPk(), $productId)->inc('care_count', 1)->update();
  340. }
  341. /**
  342. * TODO
  343. * @param int $productId
  344. * @author Qinii
  345. * @day 2020-07-09
  346. */
  347. public function decCareCount(int $productId)
  348. {
  349. ($this->getModel()::getDB())->where($this->getPk(), $productId)->where('care_count', '>', 0)->dec('care_count', 1)->update();
  350. }
  351. /**
  352. * TODO api展示的商品条件
  353. * @return array
  354. * @author Qinii
  355. * @day 2020-08-18
  356. */
  357. public function productShow()
  358. {
  359. return [
  360. 'is_show' => 1,
  361. 'status' => 1,
  362. 'is_used' => 1,
  363. 'product_type' => 0,
  364. 'mer_status' => 1,
  365. 'is_gift_bag' => 0,
  366. ];
  367. }
  368. /**
  369. * TODO api展示的礼包商品条件
  370. * @return array
  371. * @author Qinii
  372. * @day 2020-08-18
  373. */
  374. public function bagShow()
  375. {
  376. return [
  377. 'is_show' => 1,
  378. 'status' => 1,
  379. 'is_used' => 1,
  380. 'mer_status' => 1,
  381. 'product_type' => 0,
  382. 'is_gift_bag' => 1,
  383. ];
  384. }
  385. /**
  386. * TODO api展示的秒杀商品条件
  387. * @return array
  388. * @author Qinii
  389. * @day 2020-08-18
  390. */
  391. public function seckillShow()
  392. {
  393. return [
  394. 'is_show' => 1,
  395. 'status' => 1,
  396. 'is_used' => 1,
  397. 'mer_status' => 1,
  398. 'product_type' => 1,
  399. 'is_gift_bag' => 0,
  400. ];
  401. }
  402. public function getProductTypeById(int $productId, ?int $exsistType)
  403. {
  404. $product_type = $this->getModel()::getDB()
  405. ->when($exsistType, function ($query) use ($exsistType) {
  406. $query->where('product_type', $exsistType);
  407. })
  408. ->where($this->getPk(), $productId)->where('is_del', 0)->value('product_type');
  409. return $product_type == 0 ? true : false;
  410. }
  411. public function getFailProduct(int $productId)
  412. {
  413. return $this->getModel()::withTrashed()->field('product_id,image,store_name,is_show,status,is_del,unit_name,price,mer_status,is_used')->find($productId);
  414. }
  415. public function geTrashedtProduct(int $id)
  416. {
  417. return model::withTrashed()->where($this->getPk(),$id);
  418. }
  419. /**
  420. * TODO 获取各种有效时间内的活动
  421. * @param int $productType
  422. * @return array
  423. * @author Qinii
  424. * @day 2/1/21
  425. */
  426. public function activitSearch(int $productType)
  427. {
  428. $query = model::getDB()->alias('P')
  429. ->where('P.is_del', 0)
  430. ->where('P.mer_status', 1)
  431. ->where('P.product_type', $productType);
  432. switch ($productType) {
  433. case 0:
  434. // $query->where('P.is_show',1)
  435. // ->where('P.is_used',1)
  436. // ->field('product_id,product_type,mer_id,store_name,keyword,price,rank,sort,image,status,temp_id');
  437. break;
  438. case 1:
  439. $query->join('StoreSeckillActive S', 'S.product_id = P.product_id')
  440. ->field('P.*,S.status,S.seckill_active_id,S.end_time');
  441. break;
  442. case 2:
  443. $query->join('StoreProductPresell R', 'R.product_id = P.product_id')
  444. ->where('R.is_del',0)
  445. ->field('P.*,R.product_presell_id,R.store_name,R.price,R.status,R.is_show,R.product_status,R.action_status');
  446. break;
  447. case 3:
  448. $query->join('StoreProductAssist A', 'A.product_id = P.product_id')
  449. ->where('A.is_del',0)
  450. ->field('P.*,A.product_assist_id,A.store_name,A.status,A.is_show,A.product_status,A.action_status');
  451. break;
  452. case 4:
  453. $query->join('StoreProductGroup G', 'G.product_id = P.product_id')
  454. ->where('G.is_del',0)
  455. ->field('P.*,G.product_group_id,G.price,G.status,G.is_show,G.product_status,G.action_status');
  456. break;
  457. default:
  458. break;
  459. }
  460. $data = $query->select()->toArray();
  461. $ret = $this->commandChangeProductStatus($data);
  462. return $ret;
  463. }
  464. public function commandChangeProductStatus($data)
  465. {
  466. $ret = [];
  467. foreach ($data as $item) {
  468. $status = 0;
  469. switch ($item['product_type']) {
  470. case 0:
  471. if ($item['is_show'] && $item['is_used']) $status = 1;
  472. $ret[] = [
  473. 'activity_id' => 0,
  474. 'product_id' => $item['product_id'],
  475. 'mer_id' => $item['mer_id'],
  476. 'keyword' => $item['keyword'],
  477. 'price' => $item['price'],
  478. 'rank' => $item['rank'],
  479. 'sort' => $item['sort'],
  480. 'image' => $item['image'],
  481. 'status' => $status,
  482. 'temp_id' => $item['temp_id'],
  483. 'store_name' => $item['store_name'],
  484. 'product_type' => $item['product_type'],
  485. ];
  486. break;
  487. case 1:
  488. if ($item['is_show'] && $item['is_used'] && $item['status'] && ($item['end_time'] > time())) $status = 1;
  489. $ret[] = [
  490. 'activity_id' => $item['seckill_active_id'],
  491. 'product_id' => $item['product_id'],
  492. 'mer_id' => $item['mer_id'],
  493. 'keyword' => $item['keyword'],
  494. 'price' => $item['price'],
  495. 'rank' => $item['rank'],
  496. 'sort' => $item['sort'],
  497. 'image' => $item['image'],
  498. 'status' => $status,
  499. 'temp_id' => $item['temp_id'],
  500. 'store_name' => $item['store_name'],
  501. 'product_type' => $item['product_type'],
  502. ];
  503. break;
  504. case 2:
  505. if ($item['is_show'] && $item['action_status'] && $item['status'] && $item['product_status']) $status = 1;
  506. $ret[] = [
  507. 'activity_id' => $item['product_presell_id'],
  508. 'product_id' => $item['product_id'],
  509. 'mer_id' => $item['mer_id'],
  510. 'keyword' => $item['keyword'],
  511. 'price' => $item['price'],
  512. 'rank' => $item['rank'],
  513. 'sort' => $item['sort'],
  514. 'image' => $item['image'],
  515. 'status' => $status,
  516. 'temp_id' => $item['temp_id'],
  517. 'store_name' => $item['store_name'],
  518. 'product_type' => $item['product_type'],
  519. ];
  520. break;
  521. case 3:
  522. if ($item['is_show'] && $item['action_status'] && $item['status'] && $item['product_status']) $status = 1;
  523. $ret[] = [
  524. 'activity_id' => $item['product_assist_id'],
  525. 'product_id' => $item['product_id'],
  526. 'mer_id' => $item['mer_id'],
  527. 'keyword' => $item['keyword'],
  528. 'price' => $item['price'],
  529. 'rank' => $item['rank'],
  530. 'sort' => $item['sort'],
  531. 'image' => $item['image'],
  532. 'status' => $status,
  533. 'temp_id' => $item['temp_id'],
  534. 'store_name' => $item['store_name'],
  535. 'product_type' => $item['product_type'],
  536. ];
  537. break;
  538. case 4:
  539. if ($item['is_show'] && $item['action_status'] && $item['status'] && $item['product_status']) $status = 1;
  540. $ret[] = [
  541. 'activity_id' => $item['product_group_id'],
  542. 'product_id' => $item['product_id'],
  543. 'mer_id' => $item['mer_id'],
  544. 'keyword' => $item['keyword'],
  545. 'price' => $item['price'],
  546. 'rank' => $item['rank'],
  547. 'sort' => $item['sort'],
  548. 'image' => $item['image'],
  549. 'status' => $status,
  550. 'temp_id' => $item['temp_id'],
  551. 'store_name' => $item['store_name'],
  552. 'product_type' => $item['product_type'],
  553. ];
  554. break;
  555. }
  556. }
  557. return $ret;
  558. }
  559. /**
  560. * TODO 软删除商户的所有商品
  561. * @param $merId
  562. * @author Qinii
  563. * @day 5/15/21
  564. */
  565. public function clearProduct($merId)
  566. {
  567. $this->getModel()::getDb()->where('mer_id',$merId)->update(['is_del' => 1]);
  568. }
  569. }