SpuDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\ProductCate;
  14. use app\common\model\store\product\Spu;
  15. use app\common\model\store\StoreCategory;
  16. use app\common\repositories\store\StoreCategoryRepository;
  17. use app\common\repositories\system\merchant\MerchantRepository;
  18. use crmeb\services\VicWordService;
  19. class SpuDao extends BaseDao
  20. {
  21. public function getModel(): string
  22. {
  23. return Spu::class;
  24. }
  25. public function search($where)
  26. {
  27. $order = 'P.sort DESC';
  28. if(isset($where['order'])){
  29. if(in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sales'])){
  30. if ($where['order'] == 'price_asc') {
  31. $order = 'S.price ASC';
  32. } else if ($where['order'] == 'price_desc') {
  33. $order = 'S.price DESC';
  34. } else {
  35. $order = 'P.'.$where['order'] . ' DESC';
  36. }
  37. }elseif($where['order'] == 'star'){
  38. $order = 'S.star DESC,S.rank DESC';
  39. }else{
  40. $order = 'S.'. (($where['order'] !== '') ?$where['order']: 'star' ).' DESC';
  41. }
  42. }
  43. $order .= ',S.create_time DESC';
  44. if(isset($where['order']) && $where['order'] === 'none'){
  45. $order = '';
  46. }
  47. $query = Spu::getDB()->alias('S')->join('StoreProduct P','S.product_id = P.product_id', 'left');
  48. $query->when(isset($where['is_del']) && $where['is_del'] !== '',function($query)use($where){
  49. $query->where('P.is_del',$where['is_del']);
  50. })
  51. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  52. $query->where('P.mer_id',$where['mer_id']);
  53. })
  54. ->when(isset($where['mer_ids']) && $where['mer_ids'] !== '',function($query)use($where){
  55. $query->whereIn('P.mer_id',$where['mer_ids']);
  56. })
  57. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  58. if (is_numeric($where['keyword'])) {
  59. $query->whereLike("S.store_name|S.keyword|S.product_id", "%{$where['keyword']}%");
  60. } else {
  61. $word = app()->make(VicWordService::class)->getWord($where['keyword']);
  62. $query->where(function ($query) use ($word) {
  63. foreach ($word as $item) {
  64. $query->whereOr('S.store_name|S.keyword', 'LIKE', "%$item%");
  65. }
  66. });
  67. }
  68. })
  69. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  70. $merId = app()->make(MerchantRepository::class)->search([
  71. 'is_trader' => $where['is_trader'],
  72. 'status' => 1,
  73. 'mer_state' => 1,
  74. 'is_del' => 1,
  75. ])->column('mer_id');
  76. $query->whereIn('P.mer_id',$merId);
  77. })
  78. ->when(isset($where['cate_pid']) && $where['cate_pid'], function ($query) use ($where) {
  79. $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
  80. if (is_array($where['cate_pid'])) {
  81. $cateIds = $storeCategoryRepository->selectChildrenId($where['cate_pid']);
  82. } else {
  83. $cateIds = $storeCategoryRepository->findChildrenId((int)$where['cate_pid']);
  84. $where['cate_pid'] = [$where['cate_pid']];
  85. }
  86. $cate = array_merge($cateIds, $where['cate_pid']);
  87. $query->whereIn('P.cate_id', $cate);
  88. })
  89. ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
  90. is_array($where['cate_id']) ? $query->whereIn('P.cate_id', $where['cate_id']) : $query->where('P.cate_id', $where['cate_id']);
  91. })
  92. ->when(isset($where['spu_id']) && $where['spu_id'] !== '', function ($query) use ($where) {
  93. $query->where('S.spu_id',$where['spu_id']);
  94. })
  95. ->when(isset($where['spu_ids']) && $where['spu_ids'] !== '', function ($query) use ($where) {
  96. $query->whereIn('S.spu_id',$where['spu_ids']);
  97. })
  98. ->when(isset($where['product_ids']) && !empty($where['product_ids']), function ($query) use ($where) {
  99. $query->whereIn('P.product_id',$where['product_ids']);
  100. })
  101. ->when(isset($where['is_stock']) && !empty($where['is_stock']), function ($query) use ($where) {
  102. $query->where('P.stock','>',0);
  103. })
  104. ->when(isset($where['is_coupon']) && !empty($where['is_coupon']), function ($query) use ($where) {
  105. $query->whereIn('P.product_type','0,2');
  106. })
  107. ->when(isset($where['common']) && $where['common'] !== '', function ($query) use ($where) {
  108. $query->whereIn('S.product_type', [0, 1]);
  109. })
  110. ->when(isset($where['price_on']) && $where['price_on'] !== '',function($query)use($where){
  111. $query->where('S.price','>=',$where['price_on']);
  112. })
  113. ->when(isset($where['price_off']) && $where['price_off'] !== '',function($query)use($where){
  114. $query->where('S.price','<=',$where['price_off']);
  115. })
  116. ->when(isset($where['brand_id']) && $where['brand_id'] !== '', function ($query) use ($where) {
  117. $query->whereIn('P.brand_id', array_map('intval', explode(',', $where['brand_id'])));
  118. })
  119. ->when(isset($where['is_gift_bag']) && $where['is_gift_bag'] !== '',function($query)use($where){
  120. $query->where('P.is_gift_bag',$where['is_gift_bag']);
  121. })
  122. ->when(isset($where['product_type']) && $where['product_type'] !== '',function($query)use($where){
  123. $query->where('S.product_type',$where['product_type']);
  124. })
  125. ->when(isset($where['action']) && $where['action'] !== '',function($query)use($where){
  126. $query->where('S.product_type','>',0);
  127. })
  128. ->when(isset($where['mer_cate_id']) && $where['mer_cate_id'] !== '',function($query)use($where){
  129. $ids = (StoreCategory::where('path','like','%/'.$where['mer_cate_id'].'/%'))->column('store_category_id');
  130. $ids[] = intval($where['mer_cate_id']);
  131. $ids = array_unique($ids);
  132. $productId = ProductCate::where('mer_cate_id', 'in', $ids)->column('product_id');
  133. $productId = array_unique($productId);
  134. $query->where('P.product_id','in',$productId);
  135. })
  136. ->when(isset($where['mer_status']) && $where['mer_status'] !== '',function($query)use($where){
  137. $query->where('mer_status',$where['mer_status']);
  138. })
  139. ->when(isset($where['spu_status']) && $where['spu_status'] !== '',function($query)use($where){
  140. $query->where('S.status',$where['spu_status']);
  141. })
  142. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '',function($query)use($where){
  143. $query->whereLike('S.sys_labels',"%,{$where['sys_labels']},%");
  144. })
  145. ->when(isset($where['mer_labels']) && $where['mer_labels'] !== '',function($query)use($where){
  146. $query->whereLike('S.mer_labels',"%,{$where['mer_labels']},%");
  147. })
  148. ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  149. $query->join('StoreCategory CT','P.cate_id = CT.store_category_id')->where('CT.pid',$where['pid']);
  150. })
  151. ->when(isset($where['delivery_way']) && $where['delivery_way'] !== '', function ($query) use ($where) {
  152. $query->whereLike('P.delivery_way',"%{$where['delivery_way']}%");
  153. })
  154. ->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) {
  155. if ($where['hot_type'] == 'new') $query->where('P.is_new', 1);
  156. else if ($where['hot_type'] == 'hot') $query->where('P.is_hot', 1);
  157. else if ($where['hot_type'] == 'best') $query->where('P.is_best', 1);
  158. else if ($where['hot_type'] == 'good') $query->where('P.is_benefit', 1);
  159. })
  160. ->when(isset($where['svip']) && $where['svip'] !== '',function($query)use($where){
  161. $query->where('svip_price_type','>',0)->where('mer_svip_status',1);
  162. });
  163. return $query->order($order);
  164. }
  165. public function findOrCreateAll(array $where)
  166. {
  167. foreach ($where as $item) {
  168. $item['activity_id'] = $item['activity_id'] ?? 0;
  169. $data = $this->getModel()::getDB()->where('product_id', $item['product_id'])
  170. ->where('product_type', $item['product_type'])
  171. ->where('activity_id', $item['activity_id'])
  172. ->find();
  173. if (!$data) $this->create($item);
  174. }
  175. }
  176. public function delProduct($id, $isDel = 1)
  177. {
  178. $this->getModel()::getDb()->where('product_id', $id)->update(['is_del' => $isDel]);
  179. }
  180. public function getActivecategory($type)
  181. {
  182. $query = Spu::getDB()->alias('S')->join('StoreProduct P','S.product_id = P.product_id')
  183. ->join('StoreCategory C','C.store_category_id = P.cate_id');
  184. $query->where('S.status',1)->where('S.product_type',$type)->where('C.is_show',1);
  185. return $query->group('S.product_id')->column('C.path');
  186. }
  187. /**
  188. * TODO 软删除商户的所有商品
  189. * @param $merId
  190. * @author Qinii
  191. * @day 5/15/21
  192. */
  193. public function clearProduct($merId)
  194. {
  195. $this->getModel()::getDb()->where('mer_id', $merId)->update(['is_del' => 1]);
  196. }
  197. }