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]); } }