SpuDao.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\model\store\parameter\ParameterProduct;
  17. use app\common\repositories\store\StoreCategoryRepository;
  18. use app\common\repositories\system\merchant\MerchantRepository;
  19. use crmeb\services\VicWordService;
  20. class SpuDao extends BaseDao
  21. {
  22. public function getModel(): string
  23. {
  24. return Spu::class;
  25. }
  26. /**
  27. * spu搜索
  28. * @param $where
  29. * @return mixed
  30. * @author wuhaotian
  31. * @email 442384644@qq.com
  32. * @date 2024/7/17
  33. */
  34. public function search($where)
  35. {
  36. $order = 'P.sort DESC';
  37. if(isset($where['order']) && $where['order'] !== 'range_asc'){
  38. if(in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sort', 'sales','ot_price','ot_price_desc','ot_price_asc'])){
  39. switch ($where['order']) {
  40. case 'price_asc':
  41. $order = 'S.price ASC';
  42. break;
  43. case 'price_desc':
  44. $order = 'S.price DESC';
  45. break;
  46. case 'ot_price_asc':
  47. $order = 'S.ot_price ASC';
  48. break;
  49. case 'ot_price_desc':
  50. $order = 'S.ot_price DESC';
  51. break;
  52. default:
  53. $order = 'P.'.$where['order'] . ' DESC';
  54. break;
  55. }
  56. }elseif($where['order'] == 'star'){
  57. $order = 'S.star DESC,S.rank DESC';
  58. }else{
  59. $order = 'S.'. (($where['order'] !== '') ? $where['order']: 'star' ).' DESC';
  60. }
  61. }
  62. $order .= ',S.create_time DESC';
  63. if(isset($where['order']) && $where['order'] === 'none'){
  64. $order = '';
  65. }
  66. $query = Spu::getDB()->alias('S')->join('StoreProduct P','S.product_id = P.product_id', 'left');
  67. $query->when(isset($where['filter_params']) && $where['filter_params'] !== '',function($query) use($where){
  68. $query->join('ParameterProduct T','S.product_id = T.product_id')->whereIn('T.parameter_value_id', $where['filter_params']);
  69. });
  70. $query->when(isset($where['is_del']) && $where['is_del'] !== '',function($query)use($where){
  71. $query->where('P.is_del',$where['is_del']);
  72. })
  73. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  74. $query->where('P.mer_id',$where['mer_id']);
  75. })
  76. ->when(isset($where['mer_ids']) && $where['mer_ids'] !== '',function($query)use($where){
  77. $query->whereIn('P.mer_id',$where['mer_ids']);
  78. })
  79. ->when(isset($where['product_ids']) && $where['product_ids'] !== '',function($query)use($where){
  80. $query->whereIn('P.product_id',$where['product_ids']);
  81. })
  82. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  83. if (is_numeric($where['keyword'])) {
  84. $query->whereLike("S.store_name|S.keyword|S.product_id", "%{$where['keyword']}%");
  85. } else {
  86. $word = app()->make(VicWordService::class)->getWord($where['keyword']);
  87. $query->where(function ($query) use ($word) {
  88. foreach ($word as $item) {
  89. $query->whereOr('S.store_name|S.keyword', 'LIKE', "%$item%");
  90. }
  91. });
  92. }
  93. })
  94. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  95. $merId = app()->make(MerchantRepository::class)->search([
  96. 'is_trader' => $where['is_trader'],
  97. 'status' => 1,
  98. 'mer_state' => 1,
  99. 'is_del' => 1,
  100. ])->column('mer_id');
  101. $query->whereIn('P.mer_id',$merId);
  102. })
  103. ->when(isset($where['mer_type_id']) && $where['mer_type_id'] !== '',function($query)use($where){
  104. $merId = app()->make(MerchantRepository::class)->search([
  105. 'type_id' => $where['mer_type_id'],
  106. 'status' => 1,
  107. 'mer_state' => 1,
  108. 'is_del' => 1,
  109. ])->column('mer_id');
  110. $query->whereIn('P.mer_id',$merId);
  111. })
  112. ->when(isset($where['cate_pid']) && $where['cate_pid'], function ($query) use ($where) {
  113. $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
  114. if (is_array($where['cate_pid'])) {
  115. $cateIds = $storeCategoryRepository->selectChildrenId($where['cate_pid']);
  116. } else {
  117. $cateIds = $storeCategoryRepository->findChildrenId((int)$where['cate_pid']);
  118. $where['cate_pid'] = [$where['cate_pid']];
  119. }
  120. $cate = array_merge($cateIds, $where['cate_pid']);
  121. $query->whereIn('P.cate_id', $cate);
  122. })
  123. ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
  124. is_array($where['cate_id']) ? $query->whereIn('P.cate_id', $where['cate_id']) : $query->where('P.cate_id', $where['cate_id']);
  125. })
  126. ->when(isset($where['spu_id']) && $where['spu_id'] !== '', function ($query) use ($where) {
  127. $query->where('S.spu_id',$where['spu_id']);
  128. })
  129. ->when(isset($where['spu_ids']) && $where['spu_ids'] !== '', function ($query) use ($where) {
  130. $query->whereIn('S.spu_id',$where['spu_ids']);
  131. })
  132. ->when(isset($where['is_stock']) && !empty($where['is_stock']), function ($query) use ($where) {
  133. $query->where('P.stock','>',0);
  134. })
  135. ->when(isset($where['is_coupon']) && !empty($where['is_coupon']), function ($query) use ($where) {
  136. $query->whereIn('P.product_type','0,2');
  137. })
  138. ->when(isset($where['common']) && $where['common'] !== '', function ($query) use ($where) {
  139. $query->whereIn('S.product_type', [0, 1]);
  140. })
  141. ->when(isset($where['price_on']) && $where['price_on'] !== '',function($query)use($where){
  142. $query->where('S.price','>=',$where['price_on']);
  143. })
  144. ->when(isset($where['price_off']) && $where['price_off'] !== '',function($query)use($where){
  145. $query->where('S.price','<=',$where['price_off']);
  146. })
  147. ->when(isset($where['brand_id']) && $where['brand_id'] !== '', function ($query) use ($where) {
  148. $query->whereIn('P.brand_id', array_map('intval', explode(',', $where['brand_id'])));
  149. })
  150. ->when(isset($where['is_gift_bag']) && $where['is_gift_bag'] !== '',function($query)use($where){
  151. $query->where('P.is_gift_bag',$where['is_gift_bag']);
  152. })
  153. ->when(isset($where['product_type']) && $where['product_type'] !== '',function($query)use($where){
  154. $query->where('S.product_type',$where['product_type']);
  155. })
  156. ->when(isset($where['not_type']) && $where['not_type'] !== '',function($query)use($where){
  157. $query->whereNotIn('S.product_type',$where['not_type']);
  158. })
  159. ->when(isset($where['action']) && $where['action'] !== '',function($query)use($where){
  160. $query->where('S.product_type','>',0)->where('S.mer_id','<>',0);
  161. })
  162. ->when(isset($where['mer_cate_id']) && $where['mer_cate_id'] !== '',function($query)use($where){
  163. $ids = (StoreCategory::where('path','like','%/'.$where['mer_cate_id'].'/%'))->column('store_category_id');
  164. $ids[] = intval($where['mer_cate_id']);
  165. $ids = array_unique($ids);
  166. $productId = ProductCate::where('mer_cate_id', 'in', $ids)->column('product_id');
  167. $productId = array_unique($productId);
  168. $query->where('P.product_id','in',$productId);
  169. })
  170. ->when(isset($where['mer_status']) && $where['mer_status'] !== '',function($query)use($where){
  171. $query->where('mer_status',$where['mer_status']);
  172. })
  173. ->when(isset($where['spu_status']) && $where['spu_status'] !== '',function($query)use($where){
  174. $query->where('S.status',$where['spu_status']);
  175. })
  176. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '',function($query)use($where){
  177. $query->whereLike('S.sys_labels',"%,{$where['sys_labels']},%");
  178. })
  179. ->when(isset($where['mer_labels']) && $where['mer_labels'] !== '',function($query)use($where){
  180. $query->whereLike('S.mer_labels',"%,{$where['mer_labels']},%");
  181. })
  182. ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  183. $query->join('StoreCategory CT','P.cate_id = CT.store_category_id')->where('CT.pid',$where['pid']);
  184. })
  185. ->when(isset($where['delivery_way']) && $where['delivery_way'] !== '', function ($query) use ($where) {
  186. $query->whereLike('P.delivery_way',"%{$where['delivery_way']}%");
  187. })
  188. ->when(isset($where['scope']) && $where['scope'] !== '', function ($query) use ($where) {
  189. $scope = explode(',', $where['scope']);
  190. if ($scope[1] <= 0) {
  191. $query->where('S.ot_price','<',$where['scope']);
  192. } else {
  193. $query->where('S.ot_price','between',$scope);
  194. }
  195. })
  196. ->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) {
  197. if ($where['hot_type'] == 'new') $query->where('P.is_new', 1);
  198. else if ($where['hot_type'] == 'hot') $query->where('P.is_hot', 1);
  199. else if ($where['hot_type'] == 'best') $query->where('P.is_best', 1);
  200. else if ($where['hot_type'] == 'good') $query->where('P.is_benefit', 1);
  201. })
  202. ->when(isset($where['svip']) && $where['svip'] !== '',function($query)use($where){
  203. $query->where('svip_price_type','>',0)->where('mer_svip_status',1);
  204. })
  205. ;
  206. return $query->order($order);
  207. }
  208. /**
  209. * 根据给定的条件查找或创建数据。
  210. * 该方法遍历一个条件数组,并对每个条件尝试查找对应的数据记录。
  211. * 如果找不到,则根据条件创建新记录。
  212. * @param array $where 包含多个查询条件的数组,每个条件可能包含product_id, product_type, activity_id。
  213. */
  214. public function findOrCreateAll(array $where)
  215. {
  216. // 遍历条件数组
  217. foreach ($where as $item) {
  218. // 确保activity_id有值,如果未指定,则默认为0
  219. $item['activity_id'] = $item['activity_id'] ?? 0;
  220. // 根据条件查询数据库,尝试找到匹配的记录
  221. $data = $this->getModel()::getDB()->where('product_id', $item['product_id'])
  222. ->where('product_type', $item['product_type'])
  223. ->where('activity_id', $item['activity_id'])
  224. ->find();
  225. // 如果查询结果为空,则意味着该条件下的记录不存在,需要创建新记录
  226. if (!$data) {
  227. $this->create($item);
  228. }
  229. }
  230. }
  231. /**
  232. * 删除产品记录
  233. *
  234. * 本函数用于更新数据库中指定产品ID的记录的is_del字段,标记该产品为已删除。
  235. * 默认情况下,is_del字段被设置为1,表示产品被逻辑删除。可以通过传入不同的参数来修改这个行为。
  236. *
  237. * @param int $id 产品的唯一标识ID
  238. * @param int $isDel 标记删除状态的字段值,默认为1,表示已删除。可以根据需要传入其他值。
  239. */
  240. public function delProduct($id, $isDel = 1)
  241. {
  242. // 通过getModel方法获取数据库操作对象,并根据产品ID更新is_del字段
  243. $this->getModel()::getDb()->where('product_id', $id)->update(['is_del' => $isDel]);
  244. }
  245. /**
  246. * 获取指定类型下激活状态的商品分类路径
  247. *
  248. * 本函数用于查询特定类型的商品所属于的激活状态分类的路径。
  249. * 通过JOIN操作关联了Spu、StoreProduct和StoreCategory三个表,
  250. * 以获取商品ID和分类ID之间的关联,并筛选出状态为激活且显示状态为是的分类。
  251. *
  252. * @param string $type 商品类型标识
  253. * @return array 返回一个包含分类路径的数组
  254. */
  255. public function getActivecategory($type)
  256. {
  257. // 初始化查询,设置表别名为S,并JOIN产品和分类表
  258. $query = Spu::getDB()->alias('S')->join('StoreProduct P','S.product_id = P.product_id')
  259. ->join('StoreCategory C','C.store_category_id = P.cate_id');
  260. // 筛选条件:商品状态为激活,商品类型为参数类型,分类显示状态为是
  261. $query->where('S.status',1)->where('S.product_type',$type)->where('C.is_show',1);
  262. // 分组查询以确保每个产品ID只出现一次,并返回分类路径列
  263. return $query->group('S.product_id')->column('C.path');
  264. }
  265. /**
  266. * 清理指定商户的产品数据
  267. *
  268. * 本函数用于将指定商户的所有产品标记为删除状态。它通过修改产品表中相应记录的is_del字段来实现。
  269. * 这里选择不直接物理删除记录,以防止数据误删导致的不可恢复后果。标记删除允许后续有条件地恢复数据,
  270. * 同时也保持了数据的完整性。
  271. *
  272. * @param int $merId 商户ID,用于指定要清理的产品所属的商户。
  273. */
  274. public function clearProduct($merId)
  275. {
  276. // 根据$merId更新产品表中is_del字段为1,标记这些产品为删除状态
  277. $this->getModel()::getDb()->where('mer_id', $merId)->update(['is_del' => 1]);
  278. }
  279. /**
  280. * 清除特定字段中具有指定ID的记录。
  281. *
  282. * 此方法通过提供的ID和字段名称,从数据库中删除符合条件的记录。
  283. * 它首先获取模型对应的数据库实例,然后使用提供的字段和ID构建删除条件,
  284. * 最后执行删除操作。
  285. *
  286. * @param int $id 主键ID,用于指定要删除的记录。
  287. * @param string $field 要用于删除条件的字段名称。
  288. */
  289. public function clear(int $id, string $field)
  290. {
  291. $this->getModel()::getDB()->where($field, $id)->delete();
  292. }
  293. /**
  294. * 更新商品价格
  295. * 本函数用于特定商家ID和商品ID的情况下,更新商品的价格。
  296. * 这是通过查询数据库中匹配给定商家ID和商品ID的商品,并将其价格更新为新的价格来实现的。
  297. *
  298. * @param int $mer_id 商家ID,用于限定查询的商家范围。
  299. * @param int $product_id 商品ID,用于指定需要更新价格的具体商品。
  300. * @param float $price 新的商品价格,这是需要更新到数据库的值。
  301. * @return bool 返回更新操作的结果,成功为true,失败为false。
  302. */
  303. public function updatePrice($mer_id, $product_id, $price)
  304. {
  305. // 使用模型获取数据库实例,并构建更新查询条件,特定商家ID、商品ID和商品类型为0,然后更新商品价格
  306. return $this->getModel()::getDB()->where('mer_id', $mer_id)->where('product_id', $product_id)->where('product_type', 0)->update(['price' => $price]);
  307. }
  308. }