quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
 
namespace app\operations\model\plus\coupon;
 
use app\common\exception\BaseException;
use app\operations\model\user\User;
use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
 
/**
 * 用户优惠券模型
 */
class UserCoupon extends UserCouponModel
{
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $shop_supplier_ids = \app\operations\model\supplier\Supplier::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $query->where($query->getTable() . '.shop_supplier_id', -1);
        }else{
            $query->where($query->getTable() . '.shop_supplier_id', 'in', $shop_supplier_ids);
        }
    }
    /**
     * 获取优惠券列表
     */
    public function getList($limit = 20)
    {
        return $this->with(['user'])
            ->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)
    {
        $send_type = $data['send_type'];
        $coupon_id = $data['coupon_id'];
        $user_level = $data['user_level'];
        $user_ids = $data['user_ids'];
        $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'] * 10;
            $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]['coupon_project'] = $coupon['coupon_project'];
            $data[$k]['coupon_rule'] = $coupon['coupon_rule'];
            $data[$k]['shop_supplier_id'] = $coupon['shop_supplier_id'];
            $data[$k]['use_permission'] = empty($coupon['use_permission']) ? 0 : $coupon['use_permission'];
        }
        return $data;
    }
}