quanwei
2025-12-10 898043fc97d2ab8b793fd317a049b874ed207c6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
 
namespace app\supplier\model\coupon;
 
use app\common\exception\BaseException;
use app\shop\model\user\User;
use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
 
/**
 * 用户优惠券模型
 */
class UserCoupon extends UserCouponModel
{
    /**
     * 获取优惠券列表
     */
    public function getList($limit = 20,$shop_supplier_id)
    {
        return $this->with(['user'])
            ->where('shop_supplier_id','=',$shop_supplier_id)
            ->order(['create_time' => 'desc'])
            ->paginate($limit);
    }
 
    /**
     * 发送优惠券
     * @param int $send_type 1给所有会员 2给指定等级的用户 3给指定用户发送
     * @param int $coupon_id
     * @param int $user_level
     * @param string $user_ids
     */
    public function SendCoupon($data,$shop_supplier_id)
    {
        $send_type = $data['send_type'];
        $coupon_id = $data['coupon_id'];
        $user_level = $data['user_level'];
        $user_ids = $data['user_ids'];
        $data['shop_supplier_id'] = $shop_supplier_id;
        $user = new User();
        $coupon = Coupon::detail($coupon_id);
        if (empty($coupon)) {
            throw new BaseException(['msg' => '未找到优惠券信息']);
            return false;
        }
        if ($send_type == 1) {
            $user_arr = $user->getUsers();
            if (count($user_arr) == 0) {
                throw new BaseException(['msg' => '没有符合条件的会员']);
                return false;
            }
            $data = $this->setData($coupon, $user_arr);
        } elseif ($send_type == 2) {
            $user_arr = $user->getUsers(['grade_id' => $user_level]);
            if (count($user_arr) == 0) {
                throw new BaseException(['msg' => '没有符合条件的会员']);
                return false;
            }
            $data = $this->setData($coupon, $user_arr);
        } elseif ($send_type == 3) {
            if ($user_ids == '') {
                throw new BaseException(['msg' => '请选择用户']);
                return false;
            }
            $user_ids = explode(',', $user_ids);
            $user_arr = [];
            foreach ($user_ids as $val) {
                $user_arr[]['user_id'] = $val;
            }
            $data = $this->setData($coupon, $user_arr);
        }
        return $this->saveAll($data);
    }
 
    /**
     * 数组重组
     * @param $coupon
     * @param $user_arr
     */
    public function setData($coupon, $user_arr)
    {
        $data = [];
        foreach ($user_arr as $k => $val) {
            if ($coupon['expire_type'] == 10) {
                $start_time = time();
                $end_time = $start_time + ($coupon['expire_day'] * 86400);
            } else {
                $start_time = $coupon['start_time']['value'];
                $end_time = $coupon['end_time']['value'];
            }
            $data[$k]['coupon_id'] = $coupon['coupon_id'];
            $data[$k]['name'] = $coupon['name'];
            $data[$k]['color'] = $coupon['color']['value'];
            $data[$k]['coupon_type'] = $coupon['coupon_type']['value'];
            $data[$k]['reduce_price'] = $coupon['reduce_price'];
            $data[$k]['discount'] = $coupon['discount'];
            $data[$k]['min_price'] = $coupon['min_price'];
            $data[$k]['expire_type'] = $coupon['expire_type'];
            $data[$k]['expire_day'] = $coupon['expire_day'];
            $data[$k]['start_time'] = $start_time;
            $data[$k]['end_time'] = $end_time;
            $data[$k]['apply_range'] = $coupon['apply_range'];
            $data[$k]['app_id'] = self::$app_id;
            $data[$k]['user_id'] = $val['user_id'];
            $data[$k]['shop_supplier_id'] = $coupon['shop_supplier_id'];
            $data[$k]['back_money'] = $coupon['back_money']; // 返还金额 by lyzflash
            // 兑换券相关 by lyzflash
            $data[$k]['product_price'] = $coupon['product_price']; // 兑换券价值金额 by lyzflash
            $data[$k]['total_use_num'] = $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0;
            $data[$k]['limit_use_num'] = $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0;
            // 兑换券优惠项目
            $data[$k]['coupon_project'] = $coupon['coupon_project'];
            // 使用规则
            $data[$k]['coupon_rule'] = $coupon['coupon_rule'];
            // 每天核销上限
            $data[$k]['verify_num'] = $coupon['verify_num'];
            // 优惠项目可用数量
            $data[$k]['project_limit_num'] = $coupon['project_limit_num'];
            // 适用范围
            $data[$k]['use_scope'] = $coupon['use_scope'];
        }
        return $data;
    }
 
    /**
     * 撤销卡作废优惠券 by lyzflash
     * @param $coupon_ids
     * @param $user_id
     */
    public function cancelUserCardCoupon($coupons, $user, $order_id)
    {
        foreach ($coupons as $item) {
            $data[] = [
                'data' => ['is_use' => 1],
                'where' => [
                    'coupon_id' => $item['coupon_id'],
                    'user_id' => $user['user_id'],
                    'order_id' => $order_id,
                ],
            ];
        }
        return $this->updateAll($data);
    }
 
    /**
     * 扣除兑换券使用次数 by lyzflash
     * @param $user_coupon_id
     * @param $user_id
     */
    public static function setUseNum($user_coupon_id, $user_id, $num = 1)
    {
        // return (new self)->where('user_coupon_id', '=', $user_coupon_id)->where('user_id', '=', $user_id)->dec('limit_use_num', $num)->update();
        $model = static::detail($user_coupon_id);
        $data['limit_use_num'] = $model['limit_use_num'] - $num;
        if ($data['limit_use_num'] <= 0) {
            $data['limit_use_num'] = 0;
            $data['is_use'] = 1;
        }
        return $model->save($data);
    }
 
    /**
     * 返还兑换券使用次数 by lyzflash
     * @param $user_coupon_id
     * @param $user_id
     */
    public static function backUseNum($user_coupon_id, $user_id, $num = 1)
    {
        // return (new self)->where('user_coupon_id', '=', $user_coupon_id)->where('user_id', '=', $user_id)->inc('limit_use_num', $num)->update();
        $model = static::detail($user_coupon_id);
        $data['limit_use_num'] = $model['limit_use_num'] + $num;
        if ($data['limit_use_num'] >=  $model['total_use_num']) {
            $data['total_use_num'] = $model['total_use_num'];
            $data['is_use'] = 0;
        }
        return $model->save($data);
    }
}