POINT_TYPE::class, 'to_point_id' => POINT_TYPE::class, ]; // 状态常量 const STATUS_PENDING = 0; // 待处理 const STATUS_COMPLETED = 1; // 已完成 const STATUS_FAILED = 2; // 已失败 /** * 创建积分流转记录 * * @param int $userId 用户ID * @param int $fromPointId 源积分类型ID * @param int $toPointId 目标积分类型ID * @param int $amount 流转积分数量 * @param int $reId 关联ID * @param string $reType 关联类型 * @param string $remark 备注 * @return PointCirculationModel|false 成功返回模型,失败返回false */ public static function createRecord( int $userId, int $fromPointId, int $toPointId, int $amount, int $reId, string $reType, string $remark ) { $record = new self(); $record->user_id = $userId; $record->from_point_id = $fromPointId; $record->to_point_id = $toPointId; $record->amount = $amount; $record->re_id = $reId; $record->re_type = $reType; $record->remark = $remark; $record->status = self::STATUS_PENDING; $record->create_time = time(); $record->update_time = time(); if ($record->save()) { return $record; } return false; } /** * 更新记录状态 * * @param int $status 状态 * @return bool 是否成功 */ public function updateStatus(int $status): bool { $this->status = $status; $this->update_time = time(); return $this->save(); } /** * 标记为已完成 * * @return bool 是否成功 */ public function markCompleted(): bool { return $this->updateStatus(self::STATUS_COMPLETED); } /** * 标记为已失败 * * @return bool 是否成功 */ public function markFailed(): bool { return $this->updateStatus(self::STATUS_FAILED); } /** * 获取用户积分流转记录 * * @param int $userId 用户ID * @param int $limit 限制数量 * @return \Illuminate\Database\Eloquent\Collection 记录集合 */ public static function getUserRecords(int $userId, int $limit = 50) { return self::where('user_id', $userId) ->orderBy('create_time', 'desc') ->limit($limit) ->get(); } /** * 获取指定积分类型的流转记录 * * @param int $userId 用户ID * @param int $pointId 积分类型ID * @param int $limit 限制数量 * @return \Illuminate\Database\Eloquent\Collection 记录集合 */ public static function getPointRecords(int $userId, int $pointId, int $limit = 50) { return self::where('user_id', $userId) ->where(function($query) use ($pointId) { $query->where('from_point_id', $pointId) ->orWhere('to_point_id', $pointId); }) ->orderBy('create_time', 'desc') ->limit($limit) ->get(); } /** * 获取状态名称 * * @return string 状态名称 */ public function getStatusName(): string { return match($this->status) { self::STATUS_PENDING => '待处理', self::STATUS_COMPLETED => '已完成', self::STATUS_FAILED => '已失败', default => '未知状态', }; } /** * 判断是否为待处理状态 * * @return bool 是否为待处理 */ public function isPending(): bool { return $this->status === self::STATUS_PENDING; } /** * 判断是否为已完成状态 * * @return bool 是否为已完成 */ public function isCompleted(): bool { return $this->status === self::STATUS_COMPLETED; } /** * 判断是否为已失败状态 * * @return bool 是否为已失败 */ public function isFailed(): bool { return $this->status === self::STATUS_FAILED; } /** * 获取源积分类型名称 * * @return string 源积分类型名称 */ public function getFromPointTypeName(): string { return $this->from_point_id->getTypeName(); } /** * 获取目标积分类型名称 * * @return string 目标积分类型名称 */ public function getToPointTypeName(): string { return $this->to_point_id->getTypeName(); } }