SpuDao.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\common\dao\store\product;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\store\product\Spu;
  5. use app\common\model\store\StoreCategory;
  6. use app\common\repositories\store\StoreCategoryRepository;
  7. use ln\services\VicWordService;
  8. class SpuDao extends BaseDao
  9. {
  10. public function getModel(): string
  11. {
  12. return Spu::class;
  13. }
  14. public function search($where)
  15. {
  16. $order = 'P.sort DESC';
  17. if(isset($where['order'])){
  18. if(in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sales'])){
  19. if ($where['order'] == 'price_asc') {
  20. $order = 'S.price ASC';
  21. } else if ($where['order'] == 'price_desc') {
  22. $order = 'S.price DESC';
  23. } else {
  24. $order = 'P.'.$where['order'] . ' DESC';
  25. }
  26. }elseif($where['order'] == 'star'){
  27. $order = 'S.star DESC,S.rank DESC';
  28. }else{
  29. $order = 'S.'.$where['order'].' DESC';
  30. }
  31. }
  32. $order .= ',S.create_time DESC';
  33. $query = Spu::getDB()->alias('S')->join('StoreProduct P','S.product_id = P.product_id', 'left');
  34. $query->when(isset($where['is_del']) && $where['is_del'] !== '',function($query)use($where){
  35. $query->where('P.is_del',$where['is_del']);
  36. })
  37. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  38. $query->where('P.mer_id',$where['mer_id']);
  39. })
  40. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  41. if (is_numeric($where['keyword'])) {
  42. $query->whereLike("S.store_name|S.keyword|S.product_id", "%{$where['keyword']}%");
  43. } else {
  44. $word = app()->make(VicWordService::class)->getWord($where['keyword']);
  45. $query->where(function ($query) use ($word) {
  46. foreach ($word as $item) {
  47. $query->whereOr('S.store_name|S.keyword', 'LIKE', "%$item%");
  48. }
  49. });
  50. }
  51. })
  52. ->when(isset($where['cate_pid']) && $where['cate_pid'], function ($query) use ($where) {
  53. $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
  54. $cate = array_merge($storeCategoryRepository->findChildrenId((int)$where['cate_pid']), [(int)$where['cate_pid']]);
  55. $query->whereIn('P.cate_id', $cate);
  56. })
  57. ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
  58. is_array($where['cate_id']) ? $query->whereIn('P.cate_id', $where['cate_id']) : $query->where('P.cate_id', $where['cate_id']);
  59. })
  60. ->when(isset($where['common']) && $where['common'] !== '', function ($query) use ($where) {
  61. $query->whereIn('P.product_type', [0, 1]);
  62. })
  63. ->when(isset($where['price_on']) && $where['price_on'] !== '',function($query)use($where){
  64. $query->where('S.price','>=',$where['price_on']);
  65. })
  66. ->when(isset($where['price_off']) && $where['price_off'] !== '',function($query)use($where){
  67. $query->where('S.price','<=',$where['price_off']);
  68. })
  69. ->when(isset($where['brand_id']) && $where['brand_id'] !== '', function ($query) use ($where) {
  70. $query->whereIn('P.brand_id', array_map('intval', explode(',', $where['brand_id'])));
  71. })
  72. ->when(isset($where['is_gift_bag']) && $where['is_gift_bag'] !== '',function($query)use($where){
  73. $query->where('P.is_gift_bag',$where['is_gift_bag']);
  74. })
  75. ->when(isset($where['product_type']) && $where['product_type'] !== '',function($query)use($where){
  76. $query->where('S.product_type',$where['product_type']);
  77. })
  78. ->when(isset($where['action']) && $where['action'] !== '',function($query)use($where){
  79. $query->where('S.product_type','>',0);
  80. })
  81. ->when(isset($where['mer_cate_id']) && $where['mer_cate_id'] !== '',function($query)use($where){
  82. $ids = (StoreCategory::where('path','like','%/'.$where['mer_cate_id'].'/%'))->column('store_category_id');
  83. $ids[] = intval($where['mer_cate_id']);
  84. $query->join('StoreProductCate C','S.product_id = C.product_id')->where('mer_cate_id','in',$ids);
  85. })
  86. ->when(isset($where['mer_status']) && $where['mer_status'] !== '',function($query)use($where){
  87. $query->where('mer_status',$where['mer_status']);
  88. })
  89. ->when(isset($where['spu_status']) && $where['spu_status'] !== '',function($query)use($where){
  90. $query->where('S.status',$where['spu_status']);
  91. })
  92. ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  93. $query->join('StoreCategory CT','P.cate_id = CT.store_category_id')->where('CT.pid',$where['pid']);
  94. })
  95. ->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) {
  96. if ($where['hot_type'] == 'new') $query->where('P.is_new', 1);
  97. else if ($where['hot_type'] == 'hot') $query->where('P.is_hot', 1);
  98. else if ($where['hot_type'] == 'best') $query->where('P.is_best', 1);
  99. else if ($where['hot_type'] == 'good') $query->where('P.is_benefit', 1);
  100. });
  101. return $query->order($order);
  102. }
  103. public function findOrCreateAll(array $where)
  104. {
  105. foreach ($where as $item) {
  106. $item['activity_id'] = $item['activity_id'] ?? 0;
  107. $data = $this->getModel()::getDB()->where('product_id', $item['product_id'])
  108. ->where('product_type', $item['product_type'])
  109. ->where('activity_id', $item['activity_id'])
  110. ->find();
  111. if (!$data) $this->create($item);
  112. }
  113. }
  114. public function delProduct($id, $isDel = 1)
  115. {
  116. $this->getModel()::getDb()->where('product_id', $id)->update(['is_del' => $isDel]);
  117. }
  118. public function getActivecategory($type)
  119. {
  120. $query = Spu::getDB()->alias('S')->join('StoreProduct P','S.product_id = P.product_id')
  121. ->join('StoreCategory C','C.store_category_id = P.cate_id');
  122. $query->where('S.status',1)->where('S.product_type',$type)->where('C.is_show',1);
  123. return $query->group('S.product_id')->column('C.path');
  124. }
  125. /**
  126. * TODO 软删除商户的所有商品
  127. * @param $merId
  128. * @author Qinii
  129. * @day 5/15/21
  130. */
  131. public function clearProduct($merId)
  132. {
  133. $this->getModel()::getDb()->where('mer_id',$merId)->update(['is_del' => 1]);
  134. }
  135. }