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
<?php
 
namespace app\common\model\user;
 
use app\common\library\helper;
use app\common\model\BaseModel;
use app\common\model\plus\shareholder\Apply as shareholderApply;
use app\common\model\plus\team\Apply as teamApply;
 
/**
 * 激活码模型
 * Class ActivationCode
 * @package app\common\model\user
 */
class ActivationCode extends BaseModel
{
    protected $name = 'activation_code';
    protected $append = ['type_text'];
 
    /**
     * 根据使用订单取消激活码的使用状态
     *
     * 该函数将指定激活码的使用状态重置为未使用状态,
     * 包括清空使用用户ID、使用订单ID和使用时间
     *
     * @param string $activation_code 激活码
     * @return mixed 更新操作的返回结果
     */
    public static function unuseActivationCodeByUseOrder($activation_code)
    {
        return static::where('activation_code', $activation_code)
            ->update(['use_user_id' => 0, 'use_order_id' => 0, 'use_time' => 0]);
    }
 
    /**
     * 关联用户模型
     * @return \think\model\relation\BelongsTo 用户关联关系
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\user\User', 'user_id', 'user_id');
    }
 
    public function getUseTimeAttr($value, $data)
    {
        if ($value > 0) {
            return date('Y-m-d H:i:s', $value);
        }
        return 0;
    }
 
    /**
     * 关联使用用户模型
     * @return \think\model\relation\BelongsTo 使用用户关联关系
     */
    public function useUser()
    {
        return $this->belongsTo('app\common\model\user\User', 'use_user_id', 'user_id');
    }
 
    /**
     * 关联使用订单模型
     * @return \think\model\relation\BelongsTo 使用订单关联关系
     */
    public function useOrder()
    {
        return $this->belongsTo('app\common\model\order\Order', 'use_order_id', 'order_id');
    }
 
    /**
     * 获取类型文本属性
     * @param mixed $value 属性值
     * @param array $data 数据数组
     * @return string 类型对应的文本描述
     */
    public function getTypeTextAttr($value, $data)
    {
        $type = [10 => '产品', 20 => '项目'];
        return $type[$data['type']];
    }
 
    /**
     * 生成激活码
     * @param int $num 生成数量
     * @param int $type 类型(10:产品 20:项目)
     * @param int $appId 小程序ID
     * @return bool|\think\Collection
     */
    public function generateCodes($order)
    {
        $data = [];
        foreach ($order['product'] as $product) {
            if ($product['is_activation_code'] > 0) {
                for ($i = 0; $i < $product['activation_code_num']; $i++) {
                    $data[] = [
                        'activation_code' => $this->createUniqueCode(),
                        'status' => 0,
                        'type' => 10,
                        'user_id' => $order['user_id'],
                        'order_id' => $order['order_id'],
                        'app_id' => $order['app_id'],
                    ];
                }
            }
        }
        //$this->upgrade($order);
        if (!$data) {
            return true;
        }
        return $this->saveAll($data);
    }
 
    /**
     * 生成唯一的激活码
     * @return string
     */
    private function createUniqueCode()
    {
        $code = strtoupper(substr(md5(uniqid(microtime(true), true)), 0, 16));
        // 确保唯一性
        if (self::where('activation_code', $code)->find()) {
            return $this->createUniqueCode();
        }
        return $code;
    }
 
    public function upgrade($order)
    {
        $productIds = helper::getArrayColumn($order['product'], 'product_id');
        event('AgentUserGrade', $order['user_id']);
        (new teamApply())->becomeTeamByAgentUser($order['user_id'], $productIds, $order['app_id'], $order);
        (new shareholderApply())->becomeShareholderTeam($order['user_id'], $productIds, $order['app_id'], $order);
        return true;
    }
 
    /**
     * 标记激活码已使用
     * @param string $code 激活码
     * @param int $userId 使用激活码的用户ID
     * @param int $useOrderId 使用激活码的订单号
     * @return ActivationCode|bool
     */
    public function markAsUsed($code, $userId, $useOrderId = null)
    {
        $time = time();
        return $this->where('activation_code', $code)
            ->where('status', 0) // 确保激活码未被使用
            ->update([
                'status' => 1,
                'use_user_id' => $userId,
                'use_order_id' => $useOrderId,
                'use_time' => $time
            ]);
    }
 
    /**
     * 检查激活码是否有效(存在且未使用)
     * @param string $code 激活码
     * @return mixed|null
     */
    public function checkValidCode($code)
    {
        return $this->where('activation_code', $code)
            ->where('status', 0)
            ->find();
    }
 
    /**
     * 获取用户激活码列表
     * @param array $params
     * @return \think\Collection|\think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($params = [])
    {
        $model = $this;
        if (!empty($params['type'])) {
            if ($params['type'] >=0){
                $model = $model->where('type', $params['type']);
            }
        }
        if (!empty($params['user_id'])) {
            $model = $model->where('user_id', $params['user_id']);
        }
        if (!empty($params['activation_code'])) {
            $model = $model->where('activation_code', 'like', '%' . $params['activation_code'] . '%');
        }
        if (!empty($params['order_id'])) {
            $model = $model->where('order_id', $params['order_id']);
        }
        if (isset($params['status'])) {
            if ($params['status'] >=0){
                $model = $model->where('status', $params['status']);
            }
        }
        if (!empty($params['is_shipment'])) {
                $model = $model->where('is_shipment', $params['is_shipment']);
        }
        return $model
            ->with(['useUser', 'useOrder', 'user', 'order'])
            ->order(['status', 'create_time' => 'desc', 'activation_code_id' => 'desc'])
            ->paginate($params, false);
 
    }
 
    /**
     * 关联用户订单模型
     * @return \think\model\relation\BelongsTo 订单关联关系
     */
    public function order()
    {
        return $this->belongsTo('app\common\model\order\Order', 'order_id', 'order_id');
    }
    public function getStatusNum($status=0,$user_id)
    {
        return $this->where('status', $status)->where('user_id', $user_id)->count();
    }
    /**
     * 修改激活码是否已发货
     * @param string $code 激活码
     * @return bool
     */
    public function upIsShipment($code)
    {
        $detail=$this->where('activation_code', $code)->find();
        if (!$detail){
            return false;
        }
        return $detail->save(['is_shipment'=>$detail['is_shipment']==1?0:1]);
    }
}