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
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
<?php
 
namespace app\operations\model\user;
 
use app\common\model\user\Card as CardModel;
use app\operations\controller\plus\queue\Record;
use app\operations\model\user\CardRecord as CardRecordModel;
use app\operations\model\plus\coupon\UserCoupon as UserCouponModel;
use app\common\enum\user\balanceLog\BalanceLogSceneEnum;
use app\common\model\user\BalanceLog as BalanceLogModel;
 
/**
 * 会员卡模型
 */
class Card extends CardModel
{
    /**
     * 获取列表记录
     */
    public function getList($data)
    {
        $model = $this;
        if ($data['card_name']) {
            $model = $model->where('card_name', 'like', '%' . $data['card_name'] . '%');
        }
        if ($data['status'] >= 0) {
            $model = $model->where('status', '=', $data['status']);
        }
        $list = $model->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($data);
        return $list;
    }
 
    /**
     * 获取列表记录
     */
    public function getDeleteList($data)
    {
        $model = $this;
        if ($data['card_name']) {
            $model = $model->where('card_name', 'like', '%' . $data['card_name'] . '%');
        }
        $list = $model->where('is_delete', '=', 1)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->paginate($data);
        return $list;
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        $data = json_decode($data, 1);
        $data['app_id'] = self::$app_id;
        return $this->save($data);
    }
 
    /**
     * 发卡
     */
    public function put($data)
    {
        $userId = $data['user_ids'];
        if (empty($userId)) {
            $this->error = "请选择会员";
            return false;
        }
        //是否存在会员卡
        $isExist = (new CardRecord())->checkExistByUserId($userId);
        if ($isExist) {
            $this->error = "该会员已存在会员卡,勿重复发放";
            return false;
        }
        $detail = self::detail($data['card_id']);
        $this->startTrans();
        try {
            //添加会员卡
            $record = [
                'user_id' => $userId,
                'card_id' => $data['card_id'],
                'expire_time' => $detail['expire'] ? (time() + $detail['expire'] * 86400 * 30) : 0,
                'money' => $detail['money'],
                'discount' => $detail['is_discount'] ? $detail['discount'] : 0,
                'open_points' => $detail['open_points'],
                'open_points_num' => $detail['open_points_num'],
                'open_coupon' => $detail['open_coupon'],
                'open_coupons' => $detail['open_coupons'],
                'open_money' => $detail['open_money'],
                'open_money_num' => $detail['open_money_num'],
                'pay_status' => 20,
                'pay_type' => 30,
                'pay_time' => time(),
                'app_id' => self::$app_id,
            ];
            $CardRecordModel = new CardRecordModel;
            $CardRecordModel->save($record);
            $user = (new User)::detail($userId);
            //赠送积分
            if ($detail['open_points'] && $detail['open_points_num']) {
                $user->setIncPoints($detail['open_points_num'], '发会员卡获取积分');
            }
            //赠送优惠券
            if ($detail['open_coupon'] && $detail['open_coupons']) {
                (new UserCouponModel)->addUserCardCoupon($detail['open_coupons'], $user, $CardRecordModel['order_id']);
            }
            //赠送余额
            if ($detail['open_money'] && $detail['open_money_num']) {
                (new User())->where('user_id', '=', $user['user_id'])
                    ->inc('balance', $detail['open_money_num'])
                    ->update();
                BalanceLogModel::add(BalanceLogSceneEnum::RECHARGE, [
                    'user_id' => $user['user_id'],
                    'money' => $detail['open_money_num'],
                ], ['order_no' => '后台发放会员卡赠送']);
            }
            $detail->save(['receive_num' => $detail['receive_num'] + 1]);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 撤销
     */
    public function cancel($data)
    {
        $CardRecordModel = new CardRecordModel;
        $detail = $CardRecordModel::detail($data['order_id']);
        $cardDetail = self::detail($detail['card_id']);
        if (!$detail || $detail['is_delete'] != 0) {
            $this->error = "记录不存在";
            return false;
        }
        $this->startTrans();
        try {
            $detail->save(['is_delete' => 1]);
            $user = (new User)::detail($detail['user_id']);
            //撤销积分
            if ($detail['open_points'] && $detail['open_points_num']) {
                $user->setIncPoints(-$detail['open_points_num'], '撤销会员卡减少积分');
            }
            //撤销优惠券
            if ($detail['open_coupon'] && $detail['open_coupons']) {
                (new UserCouponModel)->cancelUserCardCoupon($detail['open_coupons'], $user, $detail['order_id']);
            }
            if ($detail['open_money'] && $detail['open_money_num']) {
                (new User())->where('user_id', '=', $user['user_id'])
                    ->dec('balance', $detail['open_money_num'])
                    ->update();
                BalanceLogModel::add(BalanceLogSceneEnum::ADMIN, [
                    'user_id' => $user['user_id'],
                    'money' => -$detail['open_money_num'],
                ], ['order_no' => '撤销会员卡减少余额']);
            }
            $cardDetail->save(['receive_num' => $cardDetail['receive_num'] - 1]);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 编辑记录
     */
    public function edit($data)
    {
        $data = json_decode($data, 1);
        unset($data['create_time']);
        unset($data['update_time']);
        return $this->save($data);
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        // 判断该卡下是否存在会员
        if (CardRecordModel::checkExistByRecordId($this['card_id'])) {
            return false;
        }
        return $this->save(['is_delete' => 1]);
    }
 
}