quanwei
2 days ago 73b874c72ad55eb9eef21c36160ac0de58f0189e
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
 
namespace app\api\model\plus\coupon;
 
use app\common\library\helper;
use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
use app\api\model\plus\coupon\Coupon as CouponModel;
use app\common\model\user\CardRecord as CardRecordModel; // by lyzflash
use app\common\model\settings\Setting as SettingModel; // by lyzflash
 
/**
 * 用户优惠券模型
 */
class UserCoupon extends UserCouponModel
{
    /**
     * 获取用户优惠券详情
     */
    public static function getDetail($user_coupon_id)
    {
        // return (new self)->with(['supplier', 'coupon'])->where('user_coupon_id', '=', $user_coupon_id)->find();
        $detail = (new static())->with(['supplier', 'coupon'])->find($user_coupon_id);
        return $detail;
    }
    
    /**
     * 获取用户优惠券列表
     */
    public function getList($user_id, $shop_supplier_id = -1, $is_use = false, $is_expire = false, $coupon_type = 'all',$use_permission=0)
    {
        $model = $this;
        if ($shop_supplier_id != -1) {
            $model = $model->where('shop_supplier_id', 'in', [0,$shop_supplier_id]);
        }
        if ($coupon_type == 'duihuan') {
            $model = $model->where('coupon_type', '=', 30);
        }
        if ($coupon_type == 'daijin') {
            $model = $model->where('coupon_type', '<>', 30);
        }
        if ($use_permission > 0 ) {
            //排除线上使用或线下使用 by yj 2024.1.16
            $model = $model->where('use_permission', '<>', $use_permission);
        }
        return $model->with(['supplier', 'coupon'])->where('user_id', '=', $user_id)
            ->where('is_use', '=', $is_use ? 1 : 0)
            ->where('is_expire', '=', $is_expire ? 1 : 0)
            ->select();
    }
 
    /**
     * 获取用户优惠券总数量(可用)
     */
    public function getCount($user_id, $coupon_type = 'all')
    {
        $model = $this;
        $model = $model->where('user_id', '=', $user_id)
            ->where('is_use', '=', 0)
            ->where('is_expire', '=', 0);
        if ($coupon_type == 'duihuan') {
            $model = $model->where('coupon_type', '=', 30);
        }
        if ($coupon_type == 'daijin') {
            $model = $model->where('coupon_type', '<>', 30);
        }
        return $model->count();
    }
 
    /**
     * 获取用户优惠券ID集
     */
    public function getUserCouponIds($user_id)
    {
        return $this->where('user_id', '=', $user_id)->column('coupon_id');
    }
 
    /**
     * 领取优惠券
     */
    public function receive($user, $coupon_id)
    {
        // 获取优惠券信息
        $coupon = Coupon::detail($coupon_id);
        // 验证优惠券是否可领取
        if (!$this->checkReceive($user, $coupon)) {
            return false;
        }
        // 添加领取记录
        return $this->add($user, $coupon);
    }
 
    /**
     * 批量领取优惠券
     */
    public function receiveList($user, $coupon_ids)
    {
        $coupon_arr = json_decode($coupon_ids, true);
        $model = new CouponModel();
        $list = $model->where('coupon_id', 'in', $coupon_arr)->select();
        // 开启事务
        $this->startTrans();
        try {
            $data = [];
            foreach ($list as $coupon) {
                // 验证优惠券是否可领取
                if ($this->checkReceive($user, $coupon)) {
                    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[] = [
                        'coupon_id' => $coupon['coupon_id'],
                        'name' => $coupon['name'],
                        'color' => $coupon['color']['value'],
                        'coupon_type' => $coupon['coupon_type']['value'],
                        'reduce_price' => $coupon['reduce_price'],
                        'discount' => $coupon->getData('discount'),
                        'min_price' => $coupon['min_price'],
                        'expire_type' => $coupon['expire_type'],
                        'expire_day' => $coupon['expire_day'],
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                        'apply_range' => $coupon['apply_range'],
                        'user_id' => $user['user_id'],
                        'pv' => $coupon['pv'],
                        'app_id' => self::$app_id,
                        // 返还金额
                        'back_money' => $coupon['back_money'],
                        // 兑换券价值金额
                        'product_price' => $coupon['product_price'],
                        // 兑换券使用次数 by lyzflash
                        'total_use_num' => $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0,
                        'limit_use_num' => $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0,
                        // 兑换券优惠项目
                        'coupon_project' => $coupon['coupon_project'],
                        // 使用规则
                        'coupon_rule' => $coupon['coupon_rule'],
                        // 每天核销上限
                        'verify_num' => $coupon['verify_num'],
                        // 优惠项目可用数量
                        'project_limit_num' => $coupon['project_limit_num'],
                        // 适用范围
                        'use_scope' => $coupon['use_scope'],
                        // 适用商品ID
                        'product_ids' => isset($coupon['product_ids']) ? $coupon['product_ids'] : '',
                        'product_image' => isset($coupon['product_image']) ? $coupon['product_image'] : '',
                        // 适用分类ID
                        'category_ids' => isset($coupon['category_ids']) ? $coupon['category_ids'] : '',
                    ];
                    // 更新优惠券领取数量
                    $coupon->setIncReceiveNum();
                }
            }
            $data && $this->saveAll($data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 添加领取记录
     */
    private function add($user, Coupon $coupon)
    {
        // 计算有效期
        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 = [
            'coupon_id' => $coupon['coupon_id'],
            'name' => $coupon['name'],
            'color' => $coupon['color']['value'],
            'coupon_type' => $coupon['coupon_type']['value'],
            'reduce_price' => $coupon['reduce_price'],
            'discount' => $coupon->getData('discount'),
            'min_price' => $coupon['min_price'],
            'expire_type' => $coupon['expire_type'],
            'expire_day' => $coupon['expire_day'],
            'start_time' => $start_time,
            'end_time' => $end_time,
            'apply_range' => $coupon['apply_range'],
            'user_id' => $user['user_id'],
            'app_id' => self::$app_id,
            'shop_supplier_id' => $coupon['shop_supplier_id'],
            // 返还金额 by lyzflash
            'back_money' => $coupon['back_money'],
            // 兑换券价值金额 by lyzflash
            'product_price' => $coupon['product_price'],
            // 兑换券使用次数 by lyzflash
            'total_use_num' => $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0,
            'limit_use_num' => $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0,
            // 兑换券优惠项目
            'coupon_project' => $coupon['coupon_project'],
            // 使用规则
            'coupon_rule' => $coupon['coupon_rule'],
            // 每天核销上限
            'verify_num' => $coupon['verify_num'],
            // 优惠项目可用数量
            'project_limit_num' => $coupon['project_limit_num'],
            // 适用范围
            'use_scope' => $coupon['use_scope'],
            // 使用权限
            'use_permission' => empty($coupon['use_permission']) ? 0 : $coupon['use_permission'],
            // 适用商品ID
            'product_ids' => isset($coupon['product_ids']) ? $coupon['product_ids'] : '',
            'product_image' => isset($coupon['product_image']) ? $coupon['product_image'] : '',
            // 适用分类ID
            'category_ids' => isset($coupon['category_ids']) ? $coupon['category_ids'] : '',
        ];
        return $this->transaction(function () use ($data, $coupon) {
            // 添加领取记录
            $status = $this->save($data);
            if ($status) {
                // 更新优惠券领取数量
                $coupon->setIncReceiveNum();
            }
            return $status;
        });
    }
 
    /**
     * 邀请有礼优惠券奖励
     * @param $coupon_ids
     * @param $user_id
     */
    public function addUserCoupon($coupon_ids, $user_id)
    {
        $model = new CouponModel();
        $list = $model->where('coupon_id', 'in', $coupon_ids)->select();
        $data = [];
        foreach ($list as $coupon) {
            // 计算有效期
            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[] = [
                'coupon_id' => $coupon['coupon_id'],
                'name' => $coupon['name'],
                'color' => $coupon['color']['value'],
                'coupon_type' => $coupon['coupon_type']['value'],
                'reduce_price' => $coupon['reduce_price'],
                'discount' => $coupon->getData('discount'),
                'min_price' => $coupon['min_price'],
                'expire_type' => $coupon['expire_type'],
                'expire_day' => $coupon['expire_day'],
                'start_time' => $start_time,
                'end_time' => $end_time,
                'apply_range' => $coupon['apply_range'],
                'user_id' => $user_id,
                'app_id' => self::$app_id,
                'shop_supplier_id' => $coupon['shop_supplier_id'],
                // 返还金额 by lyzflash
                'back_money' => $coupon['back_money'],
                // 兑换券价值金额 by lyzflash
                'product_price' => $coupon['product_price'],
                // 兑换券使用次数 by lyzflash
                'total_use_num' => $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0,
                'limit_use_num' => $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0,
                // 兑换券优惠项目
                'coupon_project' => $coupon['coupon_project'],
                // 使用规则
                'coupon_rule' => $coupon['coupon_rule'],
                // 每天核销上限
                'verify_num' => $coupon['verify_num'],
                // 优惠项目可用数量
                'project_limit_num' => $coupon['project_limit_num'],
                // 适用范围
                'use_scope' => $coupon['use_scope'],
                // 适用商品ID
                'product_ids' => isset($coupon['product_ids']) ? $coupon['product_ids'] : '',
                'product_image' => isset($coupon['product_image']) ? $coupon['product_image'] : '',
                // 适用分类ID
                'category_ids' => isset($coupon['category_ids']) ? $coupon['category_ids'] : '',
            ];
        }
        $this->saveAll($data);
        return true;
    }
 
    /**
     * 验证优惠券是否可领取
     */
    private function checkReceive($user, $coupon)
    {
        if (!$coupon) {
            $this->error = '优惠券不存在';
            return false;
        }
        if (!$coupon->checkReceive()) {
            $this->error = $coupon->getError();
            return false;
        }
        // 验证是否仅会员卡会员可领取
        $config = SettingModel::getItem('store');
        if ($config['coupon_card_user'] && !CardRecordModel::checkExistByUserId($user['user_id'])) {
            $this->error = '您需要成为会员卡会员才能领取,请先到会员中心领取会员卡';
            return false;
        }
        // 验证是否已领取
        $userCouponIds = $this->getUserCouponIds($user['user_id']);
        if (in_array($coupon['coupon_id'], $userCouponIds)) {
            $this->error = '该优惠券已领取';
            return false;
        }
        return true;
    }
 
    /**
     * 订单结算优惠券列表
     */
    public static function getUserCouponList($user_id, $orderPayPrice, $shop_supplier_id)
    {
        //     新增筛选条件: 最低消费金额
        // 获取用户可用的优惠券列表 (排除 线下使用的优惠券 by yj 2024.1.16)
        $list = (new self)->getList($user_id, $shop_supplier_id, false,false,'daijin',2); // 排除兑换券 by lyzflash
        $data = [];
        foreach ($list as $coupon) {
            // 最低消费金额
            if ($orderPayPrice < $coupon['min_price']) continue;
            // 有效期范围内
            if ($coupon['start_time']['value'] > time()) continue;
            $key = $coupon['user_coupon_id'];
            $data[$key] = [
                'user_coupon_id' => $coupon['user_coupon_id'],
                'name' => $coupon['name'],
                'color' => $coupon['color'],
                'coupon_type' => $coupon['coupon_type'],
                'reduce_price' => $coupon['reduce_price'],
                'discount' => $coupon['discount'],
                'min_price' => $coupon['min_price'],
                'expire_type' => $coupon['expire_type'],
                'start_time' => $coupon['start_time'],
                'end_time' => $coupon['end_time'],
                'expire_day' => $coupon['expire_day'],
                'free_limit' => $coupon['coupon']['free_limit'],
                'apply_range' => $coupon['coupon']['apply_range'],
                'product_ids' => $coupon['coupon']['product_ids'],
                'category_ids' => $coupon['coupon']['category_ids'],
            ];
            // 计算打折金额
            if ($coupon['coupon_type']['value'] == 20) {
                $reducePrice = helper::bcmul($orderPayPrice, $coupon['discount'] / 10);
                $data[$key]['reduced_price'] = bcsub($orderPayPrice, $reducePrice, 2);
            } else
                $data[$key]['reduced_price'] = $coupon['reduce_price'];
        }
        // 根据折扣金额排序并返回
        return array_sort($data, 'reduced_price', true);
    }
 
    /**
     * @param $user_id int 用户id
     * @param $days int 连续签到天数
     * @param $sign_conf array 签到配置
     * @return int
     */
    public function setCoupon($user_id, $days, $sign_conf)
    {
        $coupon_num = 0;
        $arr = array_column($sign_conf['reward_data'], 'day');
        if (in_array($days, $arr)) {
            $key = array_search($days, $arr);
            if ($sign_conf['reward_data'][$key]['is_coupon'] == 'true') {
                $coupon = $sign_conf['reward_data'][$key]['coupon'];
                $coupon_arr = array_column($coupon, 'coupon_id');
                $coupon_str = implode(',', $coupon_arr);
                $model = new CouponModel();
                $res = $model->getWhereData($coupon_str)->toArray();
                $res_arr = array_column($res, 'coupon_id');
                $result = [];
                foreach ($coupon as $key => $val) {
                    $j = array_search($coupon[$key]['coupon_id'], $res_arr);
                    for ($i = 0; $i < $coupon[$key]['num']; $i++) {
                        $coupon_num = $coupon[$key]['num'];
                        $result[] = [
                            'coupon_id' => $res[$j]['coupon_id'],
                            'name' => $res[$j]['name'],
                            'color' => $res[$j]['color']['value'],
                            'coupon_type' => $res[$j]['coupon_type']['value'],
                            'reduce_price' => $res[$j]['reduce_price'],
                            'discount' => $res[$j]['discount'],
                            'min_price' => $res[$j]['min_price'],
                            'expire_type' => $res[$j]['expire_type'],
                            'expire_day' => $res[$j]['expire_day'],
                            'start_time' => $res[$j]['start_time']['value'],
                            'end_time' => $res[$j]['end_time']['value'],
                            'apply_range' => $res[$j]['apply_range'],
                            'user_id' => $user_id,
                            'app_id' => self::$app_id,
                            // 返还金额
                            'back_money' => $res[$j]['back_money'],
                            // 兑换券价值金额
                            'product_price' => $res[$j]['product_price'],
                            // 兑换券使用次数 by lyzflash
                            'total_use_num' => $res[$j]['coupon_type']['value'] == 30 ? $res[$j]['total_use_num'] : 0,
                            'limit_use_num' => $res[$j]['coupon_type']['value'] == 30 ? $res[$j]['total_use_num'] : 0,
                            // 兑换券优惠项目
                            'coupon_project' => $res[$j]['coupon_project'],
                            // 使用规则
                            'coupon_rule' => $res[$j]['coupon_rule'],
                            // 每天核销上限
                            'verify_num' => $res[$j]['verify_num'],
                            // 优惠项目可用数量
                            'project_limit_num' => $res[$j]['project_limit_num'],
                            // 适用范围
                            'use_scope' => $res[$j]['use_scope'],
                            // 适用商品ID
                            'product_ids' => isset($coupon['product_ids']) ? $coupon['product_ids'] : '',
                            'product_image' => isset($coupon['product_image']) ? $coupon['product_image'] : '',
                            // 适用分类ID
                            'category_ids' => isset($coupon['category_ids']) ? $coupon['category_ids'] : '',
                        ];
                    }
                }
                self::saveAll($result);
            }
        }
        return $coupon_num;
    }
 
}