Bläddra i källkod

优惠券配置处理

林海涛 1 år sedan
förälder
incheckning
1002b7b6f9

+ 108 - 0
app/adminapi/controller/coupon/CouponRulesController.php

@@ -0,0 +1,108 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+
+namespace app\adminapi\controller\coupon;
+
+
+use app\adminapi\controller\BaseAdminController;
+use app\adminapi\lists\coupon\CouponRulesLists;
+use app\adminapi\logic\coupon\CouponRulesLogic;
+use app\adminapi\validate\coupon\CouponRulesValidate;
+
+
+/**
+ * CouponRules控制器
+ * Class CouponRulesController
+ * @package app\adminapi\controller\coupon
+ */
+class CouponRulesController extends BaseAdminController
+{
+
+
+    /**
+     * @notes 获取列表
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function lists()
+    {
+        return $this->dataLists(new CouponRulesLists());
+    }
+
+
+    /**
+     * @notes 添加
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function add()
+    {
+        $params = (new CouponRulesValidate())->post()->goCheck('add');
+        $result = CouponRulesLogic::add($params);
+        if (true === $result) {
+            return $this->success('添加成功', [], 1, 1);
+        }
+        return $this->fail(CouponRulesLogic::getError());
+    }
+
+
+    /**
+     * @notes 编辑
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function edit()
+    {
+        $params = (new CouponRulesValidate())->post()->goCheck('edit');
+        $result = CouponRulesLogic::edit($params);
+        if (true === $result) {
+            return $this->success('编辑成功', [], 1, 1);
+        }
+        return $this->fail(CouponRulesLogic::getError());
+    }
+
+
+    /**
+     * @notes 删除
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function delete()
+    {
+        $params = (new CouponRulesValidate())->post()->goCheck('delete');
+        CouponRulesLogic::delete($params);
+        return $this->success('删除成功', [], 1, 1);
+    }
+
+
+    /**
+     * @notes 获取详情
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function detail()
+    {
+        $params = (new CouponRulesValidate())->goCheck('detail');
+        $result = CouponRulesLogic::detail($params);
+        return $this->data($result);
+    }
+
+
+}

+ 116 - 0
app/adminapi/lists/coupon/CouponRulesLists.php

@@ -0,0 +1,116 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\adminapi\lists\coupon;
+
+
+use app\adminapi\lists\BaseAdminDataLists;
+use app\common\model\coupon\CouponCategory;
+use app\common\model\coupon\CouponRules;
+use app\common\lists\ListsSearchInterface;
+use think\db\Query;
+
+
+/**
+ * CouponRules列表
+ * Class CouponRulesLists
+ * @package app\adminapi\listscoupon
+ */
+class CouponRulesLists extends BaseAdminDataLists implements ListsSearchInterface
+{
+
+
+    /**
+     * @notes 设置搜索条件
+     * @return \string[][]
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['amount', 'amount_require', 'begin_use', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_status', 'voucher_count'],
+
+        ];
+    }
+
+    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', '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();
+    }
+
+}

+ 21 - 2
app/adminapi/lists/effective/EffectiveRulesLists.php

@@ -44,7 +44,25 @@ class EffectiveRulesLists extends BaseAdminDataLists implements ListsSearchInter
         ];
         ];
     }
     }
 
 
-
+    public function queryWhere()
+    {
+        $where = [];
+        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 = EffectiveRules::alias('er')
+                ->join('effective_category ec','er.id = ec.effective_id')
+                ->whereIn('ec.goods_category_id',$categoryIds)->distinct(true)->field('ec.effective_id')->column('ec.effective_id');
+            if(empty($ids)){
+                $ids = [0];
+            }
+            $where[] = ['id','IN',$ids];
+        }
+        return $where;
+    }
     /**
     /**
      * @notes 获取列表
      * @notes 获取列表
      * @return array
      * @return array
@@ -60,6 +78,7 @@ class EffectiveRulesLists extends BaseAdminDataLists implements ListsSearchInter
             $query->field('id,name');
             $query->field('id,name');
         }])
         }])
             ->where($this->searchWhere)
             ->where($this->searchWhere)
+            ->where($this->queryWhere())
             ->field(['id', 'effective_num', 'effective_unit', 'remark'])
             ->field(['id', 'effective_num', 'effective_unit', 'remark'])
             ->limit($this->limitOffset, $this->limitLength)
             ->limit($this->limitOffset, $this->limitLength)
             ->order(['id' => 'desc'])
             ->order(['id' => 'desc'])
@@ -81,7 +100,7 @@ class EffectiveRulesLists extends BaseAdminDataLists implements ListsSearchInter
      */
      */
     public function count(): int
     public function count(): int
     {
     {
-        return EffectiveRules::where($this->searchWhere)->count();
+        return EffectiveRules::where($this->searchWhere)->where($this->queryWhere())->count();
     }
     }
 
 
 }
 }

+ 159 - 0
app/adminapi/logic/coupon/CouponRulesLogic.php

@@ -0,0 +1,159 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\adminapi\logic\coupon;
+
+
+use app\common\model\coupon\CouponCategory;
+use app\common\model\coupon\CouponRules;
+use app\common\logic\BaseLogic;
+use think\db\Query;
+use think\facade\Db;
+
+
+/**
+ * CouponRules逻辑
+ * Class CouponRulesLogic
+ * @package app\adminapi\logic\coupon
+ */
+class CouponRulesLogic extends BaseLogic
+{
+
+
+    /**
+     * @notes 添加
+     * @param array $params
+     * @return bool
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public static function add(array $params): bool
+    {
+        Db::startTrans();
+        try {
+            $model = CouponRules::create([
+                'amount' => $params['amount'],
+                'amount_require' => $params['amount_require'],
+                'begin_use' => $params['begin_use'] ? strtotime($params['begin_use']) :0,
+                'discount_ratio' => $params['discount_ratio'],
+                'event_name' => $params['event_name'],
+                'expire_time' =>  $params['expire_time']  ? strtotime($params['expire_time']) :0,
+                'max_deductible_price' => $params['max_deductible_price'],
+                'mold_type' => $params['mold_type'],
+                'server_category_name' => $params['server_category_name'],
+                'voucher_status' => $params['voucher_status'],
+                'voucher_count' => $params['voucher_count'],
+            ]);
+            if(!empty($params['goods_category_ids'])){
+                $categoryArr = [];
+                foreach($params['goods_category_ids'] as $v){
+                    $categoryArr[] = end($v);
+                }
+                $model->couponWithCategory()->saveAll($categoryArr);
+            }
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 编辑
+     * @param array $params
+     * @return bool
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public static function edit(array $params): bool
+    {
+        Db::startTrans();
+        try {
+            CouponRules::where('id', $params['id'])->update([
+                'amount' => $params['amount'],
+                'amount_require' => $params['amount_require'],
+                'begin_use' => $params['begin_use'] ? strtotime($params['begin_use']) :0,
+                'discount_ratio' => $params['discount_ratio'],
+                'event_name' => $params['event_name'],
+                'expire_time' => $params['expire_time']  ? strtotime($params['expire_time']) :0,
+                'max_deductible_price' => $params['max_deductible_price'],
+                'mold_type' => $params['mold_type'],
+                'server_category_name' => $params['server_category_name'],
+                'voucher_status' => $params['voucher_status'],
+                'voucher_count' => $params['voucher_count'],
+            ]);
+            CouponCategory::where('coupon_id',$params['id'])->delete();
+            if(!empty($params['goods_category_ids'])){
+                $categoryArr = [];
+                foreach($params['goods_category_ids'] as $v){
+                    if(!is_array($v)){
+                        $categoryArr[] = $v;
+                        continue;
+                    }
+                    $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);
+            }
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 删除
+     * @param array $params
+     * @return bool
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public static function delete(array $params): bool
+    {
+        CouponCategory::where('coupon_id',$params['id'])->delete();
+        return CouponRules::destroy($params['id']);
+    }
+
+
+    /**
+     * @notes 获取详情
+     * @param $params
+     * @return array
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public static function detail($params): array
+    {
+        $rules = CouponRules::findOrEmpty($params['id'])->toArray();
+        if(!empty($rules)){
+            $rules['goods_category_ids'] =  CouponCategory::where('coupon_id',$params['id'])->column('goods_category_id');
+        }
+        return $rules;
+    }
+}

+ 5 - 1
app/adminapi/logic/effective/EffectiveRulesLogic.php

@@ -88,7 +88,11 @@ class EffectiveRulesLogic extends BaseLogic
             $effectiveCategoryArr = [];
             $effectiveCategoryArr = [];
             if(!empty($params['goods_category_ids'])){
             if(!empty($params['goods_category_ids'])){
                 foreach($params['goods_category_ids'] as $v){
                 foreach($params['goods_category_ids'] as $v){
-                    $effectiveCategoryArr[] =end($v);
+                    if(!is_array($v)){
+                        $effectiveCategoryArr[] = $v;
+                        continue;
+                    }
+                    $effectiveCategoryArr[] = end($v);
                 }
                 }
                 $categoryIds = EffectiveCategory::where([['goods_category_id','in',$effectiveCategoryArr],['effective_id','<>', $params['id']]])->column('goods_category_id');
                 $categoryIds = EffectiveCategory::where([['goods_category_id','in',$effectiveCategoryArr],['effective_id','<>', $params['id']]])->column('goods_category_id');
                 if(!empty($categoryIds)){
                 if(!empty($categoryIds)){

+ 123 - 0
app/adminapi/validate/coupon/CouponRulesValidate.php

@@ -0,0 +1,123 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\adminapi\validate\coupon;
+
+
+use app\common\validate\BaseValidate;
+
+
+/**
+ * CouponRules验证器
+ * Class CouponRulesValidate
+ * @package app\adminapi\validate\coupon
+ */
+class CouponRulesValidate extends BaseValidate
+{
+
+     /**
+      * 设置校验规则
+      * @var string[]
+      */
+    protected $rule = [
+        'id' => 'require',
+        'amount_require' => 'require',
+        'event_name' => 'require',
+        'expire_time' => 'require',
+        'goods_category_ids' => 'require',
+        'mold_type' => 'require|checkMoldType',
+        'voucher_count' => 'require',
+
+    ];
+
+
+    /**
+     * 参数描述
+     * @var string[]
+     */
+    protected $field = [
+        'id' => 'id',
+        'amount_require' => '满减金额',
+        'event_name' => '优惠卷名称',
+        'expire_time' => '到期时间',
+        'mold_type' => '折扣方式',
+        'voucher_count' => '优惠卷数量',
+        'goods_category_ids' => '服务类目不能为空',
+    ];
+
+
+   public function checkMoldType($moldType, $rule, $data)
+   {
+       if ($moldType == 1) {
+           if (!isset($data['discount_ratio']) || empty($data['discount_ratio'])) {
+               return '请输入折扣';
+           }
+           if($data['discount_ratio'] <0 || $data['discount_ratio'] > 10){
+               return '折扣在0~10之间';
+           }
+       } else if($moldType ==2){
+           if(!isset($data['amount']) || empty($data['amount'])){
+               return '请输入折扣金额';
+           }
+       }
+       return true;
+   }
+    /**
+     * @notes 添加场景
+     * @return CouponRulesValidate
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function sceneAdd()
+    {
+        return $this->only(['amount_require','event_name','expire_time','mold_type','voucher_count']);
+    }
+
+
+    /**
+     * @notes 编辑场景
+     * @return CouponRulesValidate
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function sceneEdit()
+    {
+        return $this->only(['id','amount_require','event_name','expire_time','mold_type','voucher_count']);
+    }
+
+
+    /**
+     * @notes 删除场景
+     * @return CouponRulesValidate
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 详情场景
+     * @return CouponRulesValidate
+     * @author likeadmin
+     * @date 2024/07/18 10:11
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+}

+ 14 - 0
app/common/model/coupon/CouponCategory.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace app\common\model\coupon;
+use think\model\Pivot;
+
+/**
+ * @author 林海涛
+ * @date 2024/7/18 上午11:25
+ */
+class CouponCategory  extends Pivot{
+    protected $name = 'coupon_category';
+
+
+}

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

@@ -0,0 +1,37 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\common\model\coupon;
+
+
+use app\common\model\BaseModel;
+use app\common\model\goods_category\GoodsCategory;
+
+
+/**
+ * CouponRules模型
+ * Class CouponRules
+ * @package app\common\model\coupon
+ */
+class CouponRules extends BaseModel
+{
+
+    protected $name = 'coupon_rules';
+
+    public function couponWithCategory()
+    {
+        return $this->belongsToMany(GoodsCategory::class,CouponCategory::class,'goods_category_id','coupon_id');
+    }
+    
+}