ProductDao.php 21 KB

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