SpuDao.php 7.2 KB

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