Sfoglia il codice sorgente

Merge branch 'master' of e.coding.net:zdap/weixiu/weixiu_api into activity-m

liugc 1 anno fa
parent
commit
2db3480031

+ 7 - 2
app/adminapi/lists/coupon/CouponRulesLists.php

@@ -19,6 +19,7 @@ use app\adminapi\lists\BaseAdminDataLists;
 use app\common\model\coupon\CouponCategory;
 use app\common\model\coupon\CouponRules;
 use app\common\lists\ListsSearchInterface;
+use app\common\model\goods\Goods;
 use think\db\Query;
 
 
@@ -40,7 +41,7 @@ class CouponRulesLists extends BaseAdminDataLists implements ListsSearchInterfac
     public function setSearch(): array
     {
         return [
-            '=' => ['amount',  'amount_require',  'max_deductible_price', 'mold_type', 'server_category_name', 'coupon_type','voucher_status'],
+            '=' => ['amount',  'amount_require',  'max_deductible_price', 'mold_type', 'server_category_name', 'coupon_type','voucher_status','coupon_target'],
             '%like%' => ['event_name','code'],
         ];
     }
@@ -102,15 +103,19 @@ class CouponRulesLists extends BaseAdminDataLists implements ListsSearchInterfac
     {
         $data =  CouponRules::with(['couponWithCategory'=>function(Query $query){
             $query->field('id,name');
+        },'couponWithGoods'=>function(Query $query){
+            $query->field('id,goods_name');
         }])->where($this->searchWhere)
             ->where($this->queryWhere())
-            ->field(['id','code', 'amount', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count','remaining_count','property_activity_id'])
+            ->field(['id','code', 'amount', 'coupon_target', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count','remaining_count','property_activity_id'])
             ->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['goods'] = $v['couponWithGoods'];
+            $v['goods_id'] = array_column($v['couponWithGoods'],'id');
             $data[$k] = $v;
         }
         return $data;

+ 25 - 8
app/adminapi/logic/coupon/CouponRulesLogic.php

@@ -16,8 +16,10 @@ namespace app\adminapi\logic\coupon;
 
 
 use app\common\model\coupon\CouponCategory;
+use app\common\model\coupon\CouponGoods;
 use app\common\model\coupon\CouponRules;
 use app\common\logic\BaseLogic;
+use app\common\model\goods\Goods;
 use think\db\Query;
 use think\facade\Db;
 
@@ -44,6 +46,7 @@ class CouponRulesLogic extends BaseLogic
         try {
             $model = CouponRules::create([
                 'coupon_type'=> $params['coupon_type'],
+                'coupon_target'=>!empty($params['coupon_target'])?$params['coupon_target']:'category',
                 'code' => generate_sn(CouponRules::class,'code'),
                 'amount' => $params['amount'],
                 'amount_require' => $params['amount_require'],
@@ -65,6 +68,13 @@ class CouponRulesLogic extends BaseLogic
                 }
                 $model->couponWithCategory()->saveAll($categoryArr);
             }
+            if(!empty($params['goods_id'])){
+                $goodsArr = [];
+                foreach($params['goods_id'] as $v){
+                    $goodsArr[] = end($v);
+                }
+                $model->couponWithGoods()->saveAll($goodsArr);
+            }
             Db::commit();
             return true;
         } catch (\Exception $e) {
@@ -88,6 +98,7 @@ class CouponRulesLogic extends BaseLogic
         try {
             CouponRules::where('id', $params['id'])->update([
                 'coupon_type'=> $params['coupon_type'],
+                'coupon_target'=>!empty($params['coupon_target'])?$params['coupon_target']:'category',
                 'amount' => $params['amount'],
                 'amount_require' => $params['amount_require'],
                 'discount_ratio' => $params['discount_ratio'],
@@ -111,17 +122,22 @@ class CouponRulesLogic extends BaseLogic
                     }
                     $categoryArr[] = end($v);
                 }
-//                $exitCategory = CouponRules::hasWhere("couponWithCategory",function(Query $query)use($categoryArr){
-//                    $query->whereIn('goods_category_id',$categoryArr);
-//                })->where('id','<>',$params['id'])
-//                    ->where('begin_use','>=',time())
-//                    ->where('expire_time','<=','expire_time')
-//                    ->where('voucher_status', '<>',3)
-//                    ->findOrEmpty();
-
                 $model = CouponRules::find($params['id']);
                 $model->couponWithCategory()->saveAll($categoryArr);
             }
+            CouponGoods::where('coupon_id',$params['id'])->delete();
+            if(!empty($params['goods_id'])){
+                $goodsArr = [];
+                foreach($params['goods_id'] as $v){
+                    if(!is_array($v)){
+                        $goodsArr[] = $v;
+                        continue;
+                    }
+                    $goodsArr[] = end($v);
+                }
+                $model = CouponRules::find($params['id']);
+                $model->couponWithGoods()->saveAll($goodsArr);
+            }
             Db::commit();
             return true;
         } catch (\Exception $e) {
@@ -158,6 +174,7 @@ class CouponRulesLogic extends BaseLogic
         $rules = CouponRules::findOrEmpty($params['id'])->toArray();
         if(!empty($rules)){
             $rules['goods_category_ids'] =  CouponCategory::where('coupon_id',$params['id'])->column('goods_category_id');
+            $rules['goods'] =  Goods::whereIn('goods_id',CouponGoods::where('coupon_id',$params['id'])->column('goods_id'))->column('*');
         }
         return $rules;
     }

+ 32 - 13
app/api/logic/UserCouponLogic.php

@@ -2,6 +2,7 @@
 namespace app\api\logic;
 use app\common\logic\BaseLogic;
 use app\common\model\coupon\CouponCategory;
+use app\common\model\coupon\CouponGoods;
 use app\common\model\coupon\CouponRules;
 use app\common\model\coupon\UserCoupon;
 use app\common\model\goods\Goods;
@@ -172,14 +173,30 @@ class UserCouponLogic extends BaseLogic
     public static function categoryWithAmountLists($params)
     {
         try{
-            $coupon_all_ids =UserCoupon::where('user_id',$params['user_id'])
-                ->where('voucher_count','>',0)
-                ->where('voucher_status',0)
-                ->where('expire_time','>',time())
-                ->where('amount_require','<=',$params['amount'])
-                ->column('coupon_id');
+            // 构建查询条件
+            $query = UserCoupon::where('user_id', $params['user_id'])
+                ->where('voucher_count', '>', 0)
+                ->where('voucher_status', 0)
+                ->where('expire_time', '>', time())
+                ->where('amount_require', '<=', $params['amount']);
+            // 执行查询并获取结果集
+            $coupons = $query->select()->toArray(); // 使用 toArray() 获取数组形式的结果
+            $filteredCoupons = array_filter($coupons, function ($item) use ($params) {
+                if ($item['coupon_target'] == 'category') {
+                    $coupon_id = CouponCategory::where('coupon_id', $item['coupon_id'])
+                        ->where('goods_category_id', $params['goods_category_id'])
+                        ->value('coupon_id'); // 获取单个值
+                } elseif ($item['coupon_target'] == 'goods') {
+                    $coupon_id = CouponGoods::where('coupon_id', $item['coupon_id'])
+                        ->where('goods_id', $params['goods_id'])
+                        ->value('coupon_id'); // 获取单个值
+                }
+                // 如果 $coupon_id 不为空,则保留该项
+                return !empty($coupon_id);
+            });
 
-            $coupon_ids = CouponCategory::where('goods_category_id',$params['goods_category_id'])->whereIn('coupon_id',$coupon_all_ids)->column('coupon_id');
+            // 提取所有 coupon_id 到数组
+            $coupon_ids = array_column($filteredCoupons, 'coupon_id');
 
             $data = UserCoupon::where('user_id',$params['user_id'])
                 ->where('voucher_count','>',0)
@@ -200,12 +217,14 @@ class UserCouponLogic extends BaseLogic
             $data = $data->append(['discount_ratio_text'])
                 ->where('expire_time','>',time())
                 ->visible(['id','coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
-                ->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'] );
-                $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
-            }
+                ->order('amount desc')
+                ->select()
+                ->each(function (&$item){
+                    $item['begin_use'] = date("Y-m-d H:i:s",$item['begin_use'] );
+                    $item['expire_time'] = date("Y-m-d H:i:s",$item['expire_time'] );
+                    $item['amount_require'] = '满'.$item['amount_require'].'可用';
+                })
+                ->toArray();
             return $data;
         } catch(\Exception $e){
             self::setError($e->getMessage());

+ 10 - 0
app/common/model/coupon/CouponGoods.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace app\common\model\coupon;
+use think\model\Pivot;
+
+class CouponGoods  extends Pivot{
+    protected $name = 'coupon_goods';
+
+
+}

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

@@ -16,6 +16,7 @@ namespace app\common\model\coupon;
 
 
 use app\common\model\BaseModel;
+use app\common\model\goods\Goods;
 use app\common\model\goods_category\GoodsCategory;
 
 
@@ -34,6 +35,11 @@ class CouponRules extends BaseModel
         return $this->belongsToMany(GoodsCategory::class,CouponCategory::class,'goods_category_id','coupon_id');
     }
 
+    public function couponWithGoods()
+    {
+        return $this->belongsToMany(Goods::class,CouponGoods::class,'goods_id','coupon_id');
+    }
+
     public function couponCategory()
     {
         return $this->hasMany(CouponCategory::class, 'coupon_id', 'goods_category_id');