ProductDao.php 23 KB

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