belongsTo('LotteryDrawRecord', 'draw_record_id'); } /** * 关联活动 */ public function activity() { return $this->belongsTo('LotteryActivity', 'activity_id'); } /** * 关联用户 */ public function user() { return $this->belongsTo('app\common\model\User', 'user_id'); } /** * 关联奖品 */ public function prize() { return $this->belongsTo('LotteryPrize', 'prize_id'); } /** * 获取发放状态文本 */ public function getDeliverStatusTextAttr($value, $data) { $status = [ self::DELIVER_STATUS_PENDING => '待发放', self::DELIVER_STATUS_SUCCESS => '已发放', self::DELIVER_STATUS_FAILED => '发放失败', self::DELIVER_STATUS_CANCELLED => '已取消' ]; return isset($status[$data['deliver_status']]) ? $status[$data['deliver_status']] : '未知'; } /** * 获取奖品信息数据 */ public function getPrizeValueDataAttr($value, $data) { return !empty($data['prize_value']) ? json_decode($data['prize_value'], true) : []; } /** * 设置奖品信息 */ public function setPrizeValueAttr($value) { return is_array($value) ? json_encode($value) : $value; } /** * 获取发放信息数据 */ public function getDeliverInfoDataAttr($value, $data) { return !empty($data['deliver_info']) ? json_decode($data['deliver_info'], true) : []; } /** * 设置发放信息 */ public function setDeliverInfoAttr($value) { return is_array($value) ? json_encode($value) : $value; } /** * 创建中奖记录 */ public static function createWinRecord($drawRecordId, $activityId, $userId, $prizeId, $prizeName, $prizeType, $prizeValue = []) { $data = [ 'draw_record_id' => $drawRecordId, 'activity_id' => $activityId, 'user_id' => $userId, 'prize_id' => $prizeId, 'prize_name' => $prizeName, 'prize_type' => $prizeType, 'prize_value' => is_array($prizeValue) ? json_encode($prizeValue) : $prizeValue, 'deliver_status' => self::DELIVER_STATUS_PENDING ]; return static::create($data); } /** * 标记为发放成功 */ public function markAsDelivered($deliverInfo = []) { $this->deliver_status = self::DELIVER_STATUS_SUCCESS; $this->deliver_time = time(); if ($deliverInfo) { $this->deliver_info = json_encode($deliverInfo); } return $this->save(); } /** * 标记为发放失败 */ public function markAsFailed($reason = '') { $this->deliver_status = self::DELIVER_STATUS_FAILED; $this->fail_reason = $reason; return $this->save(); } /** * 设置收货地址 */ public function setDeliveryAddress($name, $mobile, $address) { $this->receiver_name = $name; $this->receiver_mobile = $mobile; $this->receiver_address = $address; return $this->save(); } /** * 设置快递信息 */ public function setExpressInfo($company, $number) { $this->express_company = $company; $this->express_number = $number; return $this->save(); } /** * 设置兑换码 */ public function setExchangeCode($code) { $this->exchange_code = $code; return $this->save(); } /** * 标记兑换码已使用 */ public function markCodeUsed() { $this->code_used_time = time(); return $this->save(); } /** * 获取待发放的记录 */ public static function getPendingRecords($limit = 100) { return static::where('deliver_status', self::DELIVER_STATUS_PENDING) ->order('createtime', 'asc') ->limit($limit) ->select(); } /** * 获取用户中奖记录 */ public static function getUserWinRecords($userId, $page = 1, $limit = 20) { return static::where('user_id', $userId) ->with(['activity', 'prize']) ->order('createtime', 'desc') ->page($page, $limit) ->select(); } /** * 获取活动中奖统计 */ public static function getActivityWinStats($activityId) { return [ 'total_win' => static::where('activity_id', $activityId)->count(), 'delivered' => static::where('activity_id', $activityId) ->where('deliver_status', self::DELIVER_STATUS_SUCCESS) ->count(), 'pending' => static::where('activity_id', $activityId) ->where('deliver_status', self::DELIVER_STATUS_PENDING) ->count(), 'failed' => static::where('activity_id', $activityId) ->where('deliver_status', self::DELIVER_STATUS_FAILED) ->count() ]; } }