| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace app\api\lists;
- use app\common\lists\ListsSearchInterface;
- use app\common\model\coupon\UserCoupon;
- use think\db\Query;
- class UserCouponLists extends BaseApiDataLists implements ListsSearchInterface
- {
- protected $count = 0;
- public function setSearch(): array
- {
- return [
- '=' => ['user_id'],
- ];
- }
- public function queryWhere(){
- $where = [];
- if(isset($this->params['voucher_status']) && is_array($this->params['voucher_status'])){
- $where[] = ['voucher_status','IN',$this->params['voucher_status']];
- }
- $where[] = ['user_id','=',$this->userId];
- return $where;
- }
- public function lists(): array
- {
- $data = UserCoupon::with(['couponRules'=>function(Query $query){
- $query->withoutField(['voucher_status','voucher_count']);
- $query->with(['couponWithCategory'=>function(Query $query){
- $query->field('id,name');
- }]);
- }])
- ->where($this->searchWhere)
- ->where($this->queryWhere())
- ->field(['id', 'user_id', 'coupon_id', 'voucher_status','voucher_count'])
- ->limit($this->limitOffset, $this->limitLength)
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- $couponIds = [];
- foreach($data as $k => $v){
- if($v['couponRules']){
- if($v['voucher_status'] !=2 &&($v['couponRules']['begin_use'] <time() || $data['couponRules']['expire_time'] > time())){
- $couponIds[] = $v['coupon_id'];
- $v['voucher_status'] = 2;
- }
- $v['couponRules']['begin_use'] = date("Y-m-d H:i:s",$v['couponRules']['begin_use'] );
- $v['couponRules']['expire_time'] = date("Y-m-d H:i:s",$v['couponRules']['expire_time'] );
- $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
- $data[$k] = $v;
- }
- }
- if(!empty($couponIds)){
- UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]);
- }
- $this->count = count($data);
- return $data;
- }
- public function count(): int
- {
- return $this->count;
- }
- }
|