ProductGroupDao.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\ProductGroup;
  14. use app\common\repositories\store\product\SpuRepository;
  15. class ProductGroupDao extends BaseDao
  16. {
  17. public function getModel(): string
  18. {
  19. return ProductGroup::class;
  20. }
  21. /**
  22. * 商品分组查询
  23. * @param $where
  24. * @return \think\db\BaseQuery
  25. * @author wuhaotian
  26. * @email 442384644@qq.com
  27. * @date 2024/7/17
  28. */
  29. public function search($where)
  30. {
  31. $query = ProductGroup::hasWhere('product',function($query)use($where){
  32. $query->where('status',1);
  33. $query->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  34. $query->whereLike('store_name',"%{$where['keyword']}%");
  35. });
  36. });
  37. $query->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  38. $query->where('ProductGroup.is_show',$where['is_show']);
  39. })
  40. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  41. if($where['product_status'] == -1){
  42. $query->where('ProductGroup.product_status','in',[-1,-2]);
  43. }else{
  44. $query->where('ProductGroup.product_status',$where['product_status']);
  45. }
  46. })
  47. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  48. $query->where('ProductGroup.status',$where['status']);
  49. })
  50. ->when(isset($where['end_time']) && $where['end_time'] !== '',function($query)use($where){
  51. $query->whereTime('ProductGroup.end_time','>',$where['end_time']);
  52. })
  53. ->when(isset($where['active_type']) && $where['active_type'] !== '',function($query)use($where){
  54. $query->where('ProductGroup.action_status',$where['active_type']);
  55. })
  56. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  57. $query->join('Merchant M','M.mer_id = ProductGroup.mer_id')->where('is_trader',$where['is_trader']);
  58. })
  59. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  60. $query->where('ProductGroup.mer_id',$where['mer_id']);
  61. })
  62. ->when(isset($where['product_group_id']) && $where['product_group_id'] !== '',function($query)use($where){
  63. $query->where('ProductGroup.product_group_id',$where['product_group_id']);
  64. })
  65. ->when(isset($where['store_category_id']) && $where['store_category_id'] !== '',function($query)use($where){
  66. $query->join('StoreCategory C','Product.cate_id = C.store_category_id')
  67. ->whereLike('path',"/{$where['store_category_id']}/%");
  68. })
  69. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  70. if($where['us_status'] == 0) {
  71. $query->where('ProductGroup.is_show',0)->where('ProductGroup.status',1)->where('ProductGroup.product_status',1);
  72. }
  73. if($where['us_status'] == 1) {
  74. $query->where('ProductGroup.is_show',1)->where('ProductGroup.status',1)->where('ProductGroup.product_status',1);
  75. }
  76. if($where['us_status'] == -1) {
  77. $query->where(function($query){
  78. $query->where('ProductGroup.status',0)->whereOr('ProductGroup.product_status','<>',1);
  79. });
  80. }
  81. });
  82. $query->join('StoreSpu U','ProductGroup.product_group_id = U.activity_id')->where('U.product_type',4);
  83. $query->when(isset($where['star']) && $where['star'] !== '',function($query)use($where){
  84. $query->where('U.star',$where['star']);
  85. })
  86. ->when(isset($where['level']) && $where['level'] !== '',function($query)use($where) {
  87. $query->where('U.star',$where['level']);
  88. })
  89. ->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) {
  90. $query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%");
  91. })
  92. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) {
  93. $query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%");
  94. });
  95. if(isset($where['order'])) {
  96. switch ($where['order']) {
  97. case 'sort':
  98. $order = 'U.sort DESC';
  99. break;
  100. case 'rank':
  101. $order = 'U.rank DESC';
  102. break;
  103. case 'star':
  104. $order = 'U.star DESC,U.rank DESC';
  105. break;
  106. default:
  107. $order = 'U.star DESC,U.rank DESC,U.sort DES';
  108. break;
  109. }
  110. $query->order($order.',ProductGroup.create_time DESC');
  111. }
  112. return $query->where('ProductGroup.is_del',0);
  113. }
  114. /**
  115. * 展示动作的执行结果
  116. *
  117. * 本函数用于返回一个表示展示状态的数组,包含多个状态标识和当前时间。
  118. * 这些状态标识用于表示展示是否应该显示、动作是否成功、产品状态等。
  119. * 返回的数组包含以下键值对:
  120. * - is_show: 表示展示是否应该显示,值为1表示应该显示。
  121. * - action_status: 表示动作的状态,值为1表示动作执行成功。
  122. * - product_status: 表示产品的状态,值为1表示产品处于有效状态。
  123. * - status: 通用状态标识,值为1表示一切正常。
  124. * - end_time: 表示当前时间,以Unix时间戳形式表示,用于记录动作的结束时间。
  125. *
  126. * @return array 包含状态信息的数组。
  127. */
  128. public function actionShow()
  129. {
  130. // 返回包含状态信息的数组
  131. return [
  132. 'is_show' => 1,
  133. 'action_status' => 1,
  134. 'product_status' => 1,
  135. 'status' => 1,
  136. 'end_time' => time(),
  137. ];
  138. }
  139. /**
  140. * 获取展示状态为正常且可购买的产品分类路径列表
  141. *
  142. * 本函数通过查询产品组(ProductGroup)关联的产品(StoreProduct)及其分类(StoreCategory)
  143. * 来获取展示状态、动作状态及产品状态都为正常的产品的分类路径。
  144. * 使用了别名简化查询语句,通过连接(join)产品组、产品和分类表,筛选出符合条件的产品,
  145. * 并按产品ID分组,最后返回每个产品的分类路径列表。
  146. *
  147. * @return array 返回符合条件的产品的分类路径列表
  148. */
  149. public function category()
  150. {
  151. // 使用别名简化表名,并连接产品组、产品和分类表
  152. $query = ProductGroup::alias('G')->join('StoreProduct P','G.product_id = P.product_id')
  153. ->join('StoreCategory C','P.cate_id = C.store_category_id');
  154. // 筛选展示状态、动作状态及产品状态都为正常的产品
  155. $query->where('G.is_show',1)->where('G.action_status',1)->where('G.product_status',1);
  156. // 按产品ID分组,以确保每个产品只返回一个分类路径
  157. $query->group('G.product_id');
  158. // 返回查询结果中产品的分类路径列表
  159. return $query->column('path');
  160. }
  161. /**
  162. * 检查并更新过期活动的状态
  163. * 该方法用于定期检查所有活动的结束时间,如果活动已经结束,则将其状态设置为失效。
  164. * 同时,此方法还会更新关联的SPU的状态,将这些SPU的状态设置为不可用。
  165. *
  166. * @return void
  167. */
  168. public function valActiveStatus()
  169. {
  170. // 查询已结束且状态为有效的活动的ID
  171. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  172. $id = $query->column($this->getPk());
  173. // 如果查询到已结束的活动ID
  174. if($id) {
  175. // 更新这些活动的状态为失效
  176. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  177. // 准备更新关联SPU的状态
  178. $where = [
  179. 'product_type' => 4,
  180. 'activity_ids' => $id
  181. ];
  182. // 更新所有关联到这些活动的SPU的状态为不可用
  183. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  184. }
  185. }
  186. /**
  187. * 软删除商户的所有商品
  188. * @param $merId
  189. * @author Qinii
  190. * @day 5/15/21
  191. */
  192. public function clearProduct($merId)
  193. {
  194. $this->getModel()::getDb()->where('mer_id', $merId)->update(['is_del' => 1]);
  195. }
  196. }