['amount', 'amount_require', 'max_deductible_price', 'mold_type', 'server_category_name', 'coupon_type','voucher_status', 'voucher_count'], '%like%' => ['event_name'], ]; } public function queryWhere() { $where = []; if(isset($this->params['begin_use']) && !empty($this->params['begin_use'])){ $time = [strtotime($this->params['begin_use'][0]), strtotime($this->params['begin_use'][1])]; $where[] = ['begin_use', 'between', $time]; } if(isset($this->params['expire_time']) && !empty($this->params['expire_time'])){ $time = [strtotime($this->params['expire_time'][0]), strtotime($this->params['expire_time'][1])]; $where[] = ['expire_time', 'between', $time]; } if(isset($this->params['goods_category_ids']) && !empty($this->params['goods_category_ids'])){ $categoryIds =[]; foreach($this->params['goods_category_ids'] as $v){ $v = json_decode($v,true)?? (array)$v; $categoryIds[] = end($v); } $ids = CouponRules::alias('cr') ->join('coupon_category cc','cr.id = cc.coupon_id') ->whereIn('cc.goods_category_id',$categoryIds)->distinct(true)->field('cc.coupon_id')->column('cc.coupon_id'); if(empty($ids)){ $ids = [0]; } $where[] = ['id','IN',$ids]; } return $where; } /** * @notes 获取列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author likeadmin * @date 2024/07/18 10:11 */ public function lists(): array { $data = CouponRules::with(['couponWithCategory'=>function(Query $query){ $query->field('id,name'); }])->where($this->searchWhere) ->where($this->queryWhere()) ->field(['id', 'amount', 'coupon_type','amount_require', 'begin_use', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) ->select() ->toArray(); foreach($data as $k => $v){ $v['goods_category_ids'] = array_column($v['couponWithCategory'],'id'); $v['begin_use'] = $v['begin_use'] ? date('Y-m-d H:i:s',$v['begin_use']) :null; $v['expire_time'] = $v['begin_use'] ?date('Y-m-d H:i:s',$v['expire_time']) :null; $data[$k] = $v; } return $data; } /** * @notes 获取数量 * @return int * @author likeadmin * @date 2024/07/18 10:11 */ public function count(): int { return CouponRules::where($this->searchWhere)->where($this->queryWhere())->count(); } }