Bladeren bron

商品优惠卷

whitefang 1 jaar geleden
bovenliggende
commit
0a4d8c6319

+ 17 - 1
app/api/controller/GoodsController.php

@@ -3,7 +3,9 @@ namespace app\api\controller;
 
 use app\api\lists\GoodsLists;
 use app\api\logic\GoodsLogic;
+use app\api\logic\UserCouponLogic;
 use app\api\validate\GoodsValidate;
+use app\api\validate\UserCouponValidate;
 
 /**
  * 用户控制器
@@ -12,7 +14,7 @@ use app\api\validate\GoodsValidate;
  */
 class GoodsController extends BaseApiController
 {
-    public array $notNeedLogin = ['lists','hotData','categoryDetail','goodsDetail'];
+    public array $notNeedLogin = ['lists','hotData','categoryDetail','goodsDetail','categoryCouponList'];
 
 
     public function lists()
@@ -38,4 +40,18 @@ class GoodsController extends BaseApiController
         $result = GoodsLogic::detail($params['id'],'goods');
         return $this->data($result);
     }
+
+
+    //商品优惠卷
+    public function categoryCouponList()
+    {
+        $params = (new UserCouponValidate())->goCheck('category',[
+            'user_id'=>$this->userId
+        ]);
+        $result = UserCouponLogic::categoryCouponList($params);
+        if (false === $result) {
+            return $this->fail(UserCouponLogic::getError());
+        }
+        return $this->data($result);
+    }
 }

+ 3 - 1
app/api/lists/UserCouponLists.php

@@ -7,6 +7,7 @@ use think\db\Query;
 
 class UserCouponLists extends  BaseApiDataLists implements ListsSearchInterface
 {
+    protected $count = 0;
     public function setSearch(): array
     {
         return [
@@ -52,11 +53,12 @@ class UserCouponLists extends  BaseApiDataLists implements ListsSearchInterface
         if(!empty($couponIds)){
             UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]);
         }
+        $this->count = count($data);
         return $data;
     }
 
     public function count(): int
     {
-        return UserCoupon::where($this->searchWhere)->count();
+        return $this->count;
     }
 }

+ 29 - 0
app/api/logic/UserCouponLogic.php

@@ -1,8 +1,11 @@
 <?php
 namespace app\api\logic;
 use app\common\logic\BaseLogic;
+use app\common\model\coupon\CouponCategory;
 use app\common\model\coupon\CouponRules;
 use app\common\model\coupon\UserCoupon;
+use think\db\Query;
+use think\db\Where;
 use think\facade\Db;
 
 /**
@@ -52,4 +55,30 @@ class UserCouponLogic extends BaseLogic
         }
 
     }
+
+    /**
+     * @param $params
+     * @return array|false
+    */
+    public static function categoryCouponList($params)
+    {
+        try{
+            $data =  CouponRules::whereIn('id',CouponCategory::where('goods_category_id',$params['goods_category_id'])->group('coupon_id')->column('coupon_id'))
+                ->where(['coupon_type'=>1])
+                ->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'])
+                ->order(['id' => 'desc'])
+                ->select()
+                ->toArray();
+            foreach($data as $k => $v){
+                $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;
+        }catch(\Exception $e){
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
 }

+ 7 - 0
app/api/validate/UserCouponValidate.php

@@ -9,12 +9,19 @@ use app\common\validate\BaseValidate;
 class UserCouponValidate extends BaseValidate {
     protected $rule = [
         'coupon_ids' => 'require',
+        'goods_category_id'=> 'require'
     ];
     protected $field = [
         'coupon_ids' => '优惠券ID组',
+        'goods_category_id'=>'分类ID'
     ];
     public function sceneAdd()
     {
         return $this->only(['coupon_ids']);
     }
+
+    public function sceneCategory()
+    {
+        return $this->only(['goods_category_id']);
+    }
 }