'integer', 'createtime' => 'integer', 'updatetime' => 'integer', 'condition_value' => 'float', 'recharge_amount' => 'float', ]; /** * 关联活动 */ public function activity() { return $this->belongsTo(LotteryActivity::class, 'activity_id', 'id'); } /** * 关联用户 */ public function user() { return $this->belongsTo('app\common\model\User', 'user_id', 'id'); } /** * 关联条件 */ public function condition() { return $this->belongsTo(LotteryCondition::class, 'condition_id', 'id'); } /** * 关联订单 */ public function order() { return $this->belongsTo('app\common\model\Order', 'order_id', 'id'); } /** * 关联管理员 */ public function admin() { return $this->belongsTo('app\common\model\Admin', 'admin_id', 'id'); } /** * 获取器 - 获取类型文本 */ public function getGetTypeTextAttr($value, $data) { return LotteryEnum::getChanceGetTypeText($data['get_type']); } /** * 获取器 - 获取时间格式化 */ public function getGetTimeTextAttr($value, $data) { return $data['get_time'] ? date('Y-m-d H:i:s', $data['get_time']) : ''; } /** * 获取器 - 创建时间格式化 */ public function getCreatetimeTextAttr($value, $data) { return $data['createtime'] ? date('Y-m-d H:i:s', $data['createtime']) : ''; } /** * 修改器 - 设置获取时间 */ public function setGetTimeAttr($value) { return $value ? strtotime($value) : time(); } /** * 获取指定活动的用户机会获取记录 * * @param int $activityId 活动ID * @param int $userId 用户ID * @param int $page 页码 * @param int $limit 每页数量 * @return array */ public static function getUserChanceRecords($activityId, $userId, $page = 1, $limit = 20) { return self::where('activity_id', $activityId) ->where('user_id', $userId) ->with(['condition', 'order', 'admin']) ->order('get_time desc') ->page($page, $limit) ->select(); } /** * 获取指定活动的机会获取统计 * * @param int $activityId 活动ID * @return array */ public static function getActivityChanceStats($activityId) { $records = self::where('activity_id', $activityId)->select(); $stats = [ 'total_records' => count($records), 'total_chances' => 0, 'type_stats' => [], ]; foreach ($records as $record) { $stats['total_chances'] += $record->chances; $getType = $record->get_type; if (!isset($stats['type_stats'][$getType])) { $stats['type_stats'][$getType] = [ 'type' => $getType, 'type_text' => LotteryEnum::getChanceGetTypeText($getType), 'count' => 0, 'chances' => 0, ]; } $stats['type_stats'][$getType]['count']++; $stats['type_stats'][$getType]['chances'] += $record->chances; } return $stats; } /** * 获取用户机会获取统计 * * @param int $userId 用户ID * @param int $activityId 活动ID(可选) * @return array */ public static function getUserChanceStats($userId, $activityId = null) { $query = self::where('user_id', $userId); if ($activityId) { $query->where('activity_id', $activityId); } $records = $query->select(); $stats = [ 'total_records' => count($records), 'total_chances' => 0, 'type_stats' => [], 'recent_records' => [], ]; foreach ($records as $record) { $stats['total_chances'] += $record->chances; $getType = $record->get_type; if (!isset($stats['type_stats'][$getType])) { $stats['type_stats'][$getType] = [ 'type' => $getType, 'type_text' => LotteryEnum::getChanceGetTypeText($getType), 'count' => 0, 'chances' => 0, ]; } $stats['type_stats'][$getType]['count']++; $stats['type_stats'][$getType]['chances'] += $record->chances; } // 获取最近的记录 $stats['recent_records'] = self::where('user_id', $userId) ->with(['activity']) ->order('get_time desc') ->limit(5) ->select(); return $stats; } /** * 批量创建机会获取记录 * * @param array $records 记录数组 * @return bool */ public static function batchCreateRecords($records) { if (empty($records)) { return false; } // 为每条记录添加创建时间 foreach ($records as &$record) { $record['createtime'] = time(); $record['get_time'] = $record['get_time'] ?? time(); } return self::insertAll($records); } /** * 验证记录数据 * * @param array $data 记录数据 * @return bool|string true表示验证通过,string表示错误信息 */ public static function validateRecord($data) { // 验证必填字段 if (empty($data['activity_id'])) { return '活动ID不能为空'; } if (empty($data['user_id'])) { return '用户ID不能为空'; } if (empty($data['get_type'])) { return '获取类型不能为空'; } if (empty($data['chances']) || $data['chances'] <= 0) { return '机会次数必须大于0'; } // 验证获取类型是否有效 if (!LotteryEnum::isValidChanceGetType($data['get_type'])) { return '无效的获取类型'; } // 根据获取类型验证相关字段 switch ($data['get_type']) { case LotteryEnum::CHANCE_GET_TYPE_BUY_GOODS: case LotteryEnum::CHANCE_GET_TYPE_ORDER_AMOUNT: if (empty($data['order_id'])) { return '订单ID不能为空'; } break; case LotteryEnum::CHANCE_GET_TYPE_RECHARGE: if (empty($data['recharge_amount']) || $data['recharge_amount'] <= 0) { return '充值金额必须大于0'; } break; case LotteryEnum::CHANCE_GET_TYPE_ADMIN_GRANT: if (empty($data['admin_id'])) { return '管理员ID不能为空'; } break; } return true; } }