liugc 10 месяцев назад
Родитель
Сommit
8fcb939400

+ 108 - 0
app/adminapi/controller/coupon/UserCouponController.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\UserCouponLists;
+use app\adminapi\logic\coupon\UserCouponLogic;
+use app\adminapi\validate\coupon\UserCouponValidate;
+
+
+/**
+ * UserCoupon控制器
+ * Class UserCouponController
+ * @package app\adminapi\controller
+ */
+class UserCouponController extends BaseAdminController
+{
+
+
+    /**
+     * @notes 获取列表
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function lists()
+    {
+        return $this->dataLists(new UserCouponLists());
+    }
+
+
+    /**
+     * @notes 添加
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function add()
+    {
+        $params = (new UserCouponValidate())->post()->goCheck('add');
+        $result = UserCouponLogic::add($params);
+        if (true === $result) {
+            return $this->success('添加成功', [], 1, 1);
+        }
+        return $this->fail(UserCouponLogic::getError());
+    }
+
+
+    /**
+     * @notes 编辑
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function edit()
+    {
+        $params = (new UserCouponValidate())->post()->goCheck('edit');
+        $result = UserCouponLogic::edit($params);
+        if (true === $result) {
+            return $this->success('编辑成功', [], 1, 1);
+        }
+        return $this->fail(UserCouponLogic::getError());
+    }
+
+
+    /**
+     * @notes 删除
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function delete()
+    {
+        $params = (new UserCouponValidate())->post()->goCheck('delete');
+        UserCouponLogic::delete($params);
+        return $this->success('删除成功', [], 1, 1);
+    }
+
+
+    /**
+     * @notes 获取详情
+     * @return \think\response\Json
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function detail()
+    {
+        $params = (new UserCouponValidate())->goCheck('detail');
+        $result = UserCouponLogic::detail($params);
+        return $this->data($result);
+    }
+
+
+}

+ 87 - 0
app/adminapi/lists/coupon/UserCouponLists.php

@@ -0,0 +1,87 @@
+<?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\UserCoupon;
+use app\common\lists\ListsSearchInterface;
+use app\common\model\user\User;
+
+
+/**
+ * UserCoupon列表
+ * Class UserCouponLists
+ * @package app\adminapi\lists
+ */
+class UserCouponLists extends BaseAdminDataLists implements ListsSearchInterface
+{
+
+
+    /**
+     * @notes 设置搜索条件
+     * @return \string[][]
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['user_id', 'coupon_id', 'coupon_target',  'voucher_status', 'voucher_count', 'amount', 'amount_require', 'begin_use', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'property_activity_id'],
+            '%like%' => ['code','event_name'],
+        ];
+    }
+    public function queryWhere()
+    {
+        $where = [];
+        if (isset($this->params['mobile']) && !empty($this->params['mobile'])) {
+            $user_ids = User::where([['mobile|real_name', 'like','%' .$this->params['mobile'] . '%']])->column('id')??[0];
+            $where[] = ['user_id','in' ,$user_ids];
+        }
+        return $where;
+    }
+
+    /**
+     * @notes 获取列表
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function lists(): array
+    {
+        return UserCoupon::with(['user'])->where($this->searchWhere)->where($this->queryWhere())
+            ->field(['*'])
+            ->limit($this->limitOffset, $this->limitLength)
+            ->order(['id' => 'desc'])
+            ->select()
+            ->toArray();
+    }
+
+
+    /**
+     * @notes 获取数量
+     * @return int
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function count(): int
+    {
+        return UserCoupon::where($this->searchWhere)->where($this->queryWhere())->count();
+    }
+
+}

+ 124 - 0
app/adminapi/logic/coupon/UserCouponLogic.php

@@ -0,0 +1,124 @@
+<?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\adminapi\logic\user\UserLogic;
+use app\common\model\coupon\UserCoupon;
+use app\common\logic\BaseLogic;
+use think\facade\Db;
+
+
+/**
+ * UserCoupon逻辑
+ * Class UserCouponLogic
+ * @package app\adminapi\logic
+ */
+class UserCouponLogic extends BaseLogic
+{
+
+
+    /**
+     * @notes 添加
+     * @param array $params
+     * @return bool
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public static function add(array $params): bool
+    {
+        Db::startTrans();
+        try {
+            //注册用户
+            $userId = UserLogic::getUserIdByMobile($params['mobile']);
+            $codes = [$params['code']];
+            if($codes){
+                \app\api\logic\UserCouponLogic::grant($codes,$userId,1);
+            }
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 编辑
+     * @param array $params
+     * @return bool
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public static function edit(array $params): bool
+    {
+        Db::startTrans();
+        try {
+            /*UserCoupon::where('id', $params['id'])->update([
+                'user_id' => $params['user_id'],
+                'coupon_id' => $params['coupon_id'],
+                'coupon_target' => $params['coupon_target'],
+                'code' => $params['code'],
+                'voucher_status' => $params['voucher_status'],
+                'voucher_count' => $params['voucher_count'],
+                'amount' => $params['amount'],
+                'amount_require' => $params['amount_require'],
+                'begin_use' => $params['begin_use'],
+                'discount_ratio' => $params['discount_ratio'],
+                'event_name' => $params['event_name'],
+                'expire_time' => $params['expire_time'],
+                'max_deductible_price' => $params['max_deductible_price'],
+                'mold_type' => $params['mold_type'],
+                'server_category_name' => $params['server_category_name'],
+                'property_activity_id' => $params['property_activity_id'],
+            ]);*/
+
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 删除
+     * @param array $params
+     * @return bool
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public static function delete(array $params): bool
+    {
+        return UserCoupon::destroy($params['id']);
+    }
+
+
+    /**
+     * @notes 获取详情
+     * @param $params
+     * @return array
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public static function detail($params): array
+    {
+        return UserCoupon::findOrEmpty($params['id'])->toArray();
+    }
+}

+ 1 - 13
app/adminapi/logic/works/ServiceWorkLogic.php

@@ -1286,19 +1286,7 @@ class ServiceWorkLogic extends BaseLogic
                     $coupon_data['code'] && $codes[] = $coupon_data['code'];
                 }
                 if($codes){
-                    $userCouponLogic = UserCouponLogic::add(['codes'=>$codes,'user_id'=>$serviceWork->user_id]);
-                    Log::info('工单中给用户发券:'.$serviceWork->user_id.'--'.json_encode($codes).'--'.json_encode($userCouponLogic));
-                    if($userCouponLogic === false) throw new \Exception(UserCouponLogic::getError());
-                    if(empty(UserCouponLogic::getError())){
-                        // 通知用户公众号 跳转优惠券
-                        event('Notice',  [
-                            'scene_id' => 129,
-                            'params' => [
-                                'user_id' => $serviceWork->user_id,
-                                'order_id' => $serviceWork->id,
-                            ]
-                        ]);
-                    }
+                    UserCouponLogic::grant($codes,$serviceWork->user_id,$serviceWork->id);
                 }
             }
             return true;

+ 116 - 0
app/adminapi/validate/coupon/UserCouponValidate.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\validate\coupon;
+
+
+use app\common\validate\BaseValidate;
+
+
+/**
+ * UserCoupon验证器
+ * Class UserCouponValidate
+ * @package app\adminapi\validate
+ */
+class UserCouponValidate extends BaseValidate
+{
+
+     /**
+      * 设置校验规则
+      * @var string[]
+      */
+    protected $rule = [
+        'id' => 'require',
+        'user_id' => 'require',
+        'coupon_id' => 'require',
+        'coupon_target' => 'require',
+        'code' => 'require',
+        'voucher_status' => 'require',
+        'amount_require' => 'require',
+        'begin_use' => 'require',
+        'event_name' => 'require',
+        'expire_time' => 'require',
+        'mold_type' => 'require',
+
+    ];
+
+
+    /**
+     * 参数描述
+     * @var string[]
+     */
+    protected $field = [
+        'id' => 'id',
+        'user_id' => '用户ID',
+        'coupon_id' => '优惠卷Id',
+        'coupon_target' => '优惠券使用目标',
+        'code' => '优惠券编码',
+        'voucher_status' => '使用状态',
+        'amount_require' => '满减金额',
+        'begin_use' => '生效时间',
+        'event_name' => '优惠卷名称',
+        'expire_time' => '到期时间',
+        'mold_type' => '折扣方式:优惠卷折扣现金卷方式',
+
+    ];
+
+
+    /**
+     * @notes 添加场景
+     * @return UserCouponValidate
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function sceneAdd()
+    {
+        return $this->only(['mobile','code']);
+    }
+
+
+    /**
+     * @notes 编辑场景
+     * @return UserCouponValidate
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function sceneEdit()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 删除场景
+     * @return UserCouponValidate
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function sceneDelete()
+    {
+        return $this->only(['id']);
+    }
+
+
+    /**
+     * @notes 详情场景
+     * @return UserCouponValidate
+     * @author likeadmin
+     * @date 2025/05/28 11:42
+     */
+    public function sceneDetail()
+    {
+        return $this->only(['id']);
+    }
+
+}

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

@@ -243,4 +243,37 @@ class UserCouponLogic extends BaseLogic
             return false;
         }
     }
+
+    public static function grant($codes = [],$user_id = 0,$order_id = 0)
+    {
+        try{
+            if($codes){
+                $userCouponLogic = UserCouponLogic::add(['codes'=>$codes,'user_id'=>$user_id]);
+                if($userCouponLogic === false) throw new \Exception(UserCouponLogic::getError());
+                if(empty(UserCouponLogic::getError())){
+                    // 通知用户公众号 跳转优惠券
+                    event('Notice',  [
+                        'scene_id' => 129,
+                        'params' => [
+                            'user_id' => $user_id??0,
+                            'order_id' => $order_id??0,
+                        ]
+                    ]);
+                }
+            }
+            return true;
+        }catch(\Exception $e){
+            throw new \Exception($e->getMessage());
+        }
+    }
+
+
+
+
+
+
+
+
+
+
 }

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

@@ -3,6 +3,7 @@
 namespace app\common\model\coupon;
 use app\common\model\BaseModel;
 use app\common\model\dict\DictData;
+use app\common\model\user\User;
 use think\facade\Cache;
 
 /**
@@ -15,6 +16,10 @@ class UserCoupon extends BaseModel
 
     protected $name = 'user_coupon';
 
+    public function user()
+    {
+        return $this->hasOne(User::class, 'id', 'user_id');
+    }
     public function couponCategory()
     {
         return $this->hasMany(CouponCategory::class, 'coupon_id', 'goods_category_id');