whitefang 1 год назад
Родитель
Сommit
2b81382cfd

+ 16 - 22
app/api/logic/UserCouponLogic.php

@@ -41,6 +41,7 @@ class UserCouponLogic extends BaseLogic
                 $updateData[$v->id] = ['remaining_count' => --$v->remaining_count];
                 $createData[] = [
                     'user_id'=>$params['user_id'],
+                    'coupon_id' => $v->id,
                     'code' => $v->code,
                     'voucher_status' => 0,
                     'voucher_count' =>1,
@@ -129,14 +130,13 @@ class UserCouponLogic extends BaseLogic
     public static function categoryCouponLists($params)
     {
         try{
-            $data =  CouponRules::alias('cr')
-                ->leftJoin('coupon_category cc','cc.coupon_id = cr.id')
-                ->where('cc.goods_category_id',$params['goods_category_id'])
-                ->where('cr.remaining_count','>',0)
-                ->where('cr.voucher_status',1)
-                ->order(['cr.id' => 'desc'])
-                ->field(['cr.id','cr.code','cr.amount','cr.amount_require','cr.discount_ratio','cr.server_category_name',
-                    'cr.event_name','cr.expire_time','cr.max_deductible_price','cr.mold_type',])
+            $data =  CouponRules::with(['couponCategory'=>function ($query) use ($params) {
+                $query->where('goods_category_id',$params['goods_category_id']);
+            }])->where('remaining_count','>',0)
+                ->where('voucher_status',1)
+                ->order(['id' => 'desc'])
+                ->field(['code','amount','amount_require','discount_ratio','server_category_name',
+                    'event_name','expire_time','max_deductible_price','mold_type',])
                 ->select();
             $codes = $data->column('code');
             $userCouponCodes = [];
@@ -155,20 +155,14 @@ class UserCouponLogic extends BaseLogic
     public static function categoryWithAmountLists($params)
     {
         try{
-            $data = UserCoupon::alias('uc')
-                ->leftJoin('coupon_rules cr','uc.code=cr.code')
-                ->leftJoin('coupon_category cc','cc.coupon_id=cr.id')
-                ->where('cc.goods_category_id',$params['goods_category_id'])
-                ->where('uc.user_id',$params['user_id'])
-                ->where('uc.begin_use','<=',time())
-                ->where('uc.expire_time','>',time())
-                ->where('uc.voucher_count','>',0)
-                ->where('uc.voucher_status',0)
-                ->where('uc.amount_require','<=',$params['amount'])
-                ->field(['uc.id','uc.code','uc.amount','uc.amount_require','uc.discount_ratio','uc.server_category_name',
-                    'uc.event_name','uc.expire_time','uc.begin_use','uc.max_deductible_price','uc.mold_type','uc.server_category_name'])
-                ->select()
-                ->toArray();
+            $data = UserCoupon::with(['couponCategory'=>function ($query) use ($params) {
+                $query->where('goods_category_id',$params['goods_category_id']);
+            }])->where('user_id',$params['user_id'])
+                ->where('voucher_count','>',0)
+                ->where('voucher_status',0)
+                ->where('amount_require','<=',$params['amount'])
+                ->visible(['coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name'])
+                ->select()->toArray();
             foreach($data as $k => $v){
                 $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
                 $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );

+ 5 - 0
app/common/model/coupon/CouponRules.php

@@ -33,4 +33,9 @@ class CouponRules extends BaseModel
     {
         return $this->belongsToMany(GoodsCategory::class,CouponCategory::class,'goods_category_id','coupon_id');
     }
+
+    public function couponCategory()
+    {
+        return $this->hasMany(CouponCategory::class, 'coupon_id', 'goods_category_id');
+    }
 }

+ 5 - 0
app/common/model/coupon/UserCoupon.php

@@ -15,6 +15,11 @@ class UserCoupon extends BaseModel
 
     protected $name = 'user_coupon';
 
+    public function couponCategory()
+    {
+        return $this->hasMany(CouponCategory::class, 'coupon_id', 'goods_category_id');
+    }
+
     public function couponRules()
     {
         return $this->hasOne(CouponRules::class,'code','code');