| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace App\Module\Point\Models;
- use App\Module\Point\Enums\POINT_TYPE;
- use UCore\ModelCore;
- /**
- * 积分转账记录表
- *
- * 记录不同用户间的积分转账操作
- * 专注于整数积分处理,不涉及小数运算
- *
- * field start
- * @property int $id 自增
- * @property int $from_user_id 转出用户ID
- * @property int $to_user_id 转入用户ID
- * @property \App\Module\Point\Enums\POINT_TYPE $point_id 积分类型ID
- * @property int $amount 转账积分数量
- * @property string $remark 备注
- * @property int $status 状态:0-待处理,1-已完成,2-已失败
- * @property int $create_time 创建时间
- * @property int $update_time 更新时间
- * field end
- */
- class PointTransferModel extends ModelCore
- {
- protected $table = 'point_transfer';
- // attrlist start
- protected $fillable = [
- 'id',
- 'from_user_id',
- 'to_user_id',
- 'point_id',
- 'amount',
- 'remark',
- 'status',
- 'create_time',
- 'update_time',
- ];
- // attrlist end
- public $timestamps = false;
- protected $casts = [
- 'point_id' => POINT_TYPE::class,
- ];
- // 状态常量
- const STATUS_PENDING = 0; // 待处理
- const STATUS_COMPLETED = 1; // 已完成
- const STATUS_FAILED = 2; // 已失败
- /**
- * 创建积分转账记录
- *
- * @param int $fromUserId 转出用户ID
- * @param int $toUserId 转入用户ID
- * @param int $pointId 积分类型ID
- * @param int $amount 转账积分数量
- * @param string $remark 备注
- * @return PointTransferModel|false 成功返回模型,失败返回false
- */
- public static function createRecord(
- int $fromUserId,
- int $toUserId,
- int $pointId,
- int $amount,
- string $remark
- ) {
- $record = new self();
- $record->from_user_id = $fromUserId;
- $record->to_user_id = $toUserId;
- $record->point_id = $pointId;
- $record->amount = $amount;
- $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(function($query) use ($userId) {
- $query->where('from_user_id', $userId)
- ->orWhere('to_user_id', $userId);
- })
- ->orderBy('create_time', 'desc')
- ->limit($limit)
- ->get();
- }
- /**
- * 获取用户转出记录
- *
- * @param int $userId 用户ID
- * @param int $limit 限制数量
- * @return \Illuminate\Database\Eloquent\Collection 记录集合
- */
- public static function getUserTransferOutRecords(int $userId, int $limit = 50)
- {
- return self::where('from_user_id', $userId)
- ->orderBy('create_time', 'desc')
- ->limit($limit)
- ->get();
- }
- /**
- * 获取用户转入记录
- *
- * @param int $userId 用户ID
- * @param int $limit 限制数量
- * @return \Illuminate\Database\Eloquent\Collection 记录集合
- */
- public static function getUserTransferInRecords(int $userId, int $limit = 50)
- {
- return self::where('to_user_id', $userId)
- ->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 getPointTypeName(): string
- {
- return $this->point_id->getTypeName();
- }
- /**
- * 判断用户是否为转出方
- *
- * @param int $userId 用户ID
- * @return bool 是否为转出方
- */
- public function isTransferOut(int $userId): bool
- {
- return $this->from_user_id === $userId;
- }
- /**
- * 判断用户是否为转入方
- *
- * @param int $userId 用户ID
- * @return bool 是否为转入方
- */
- public function isTransferIn(int $userId): bool
- {
- return $this->to_user_id === $userId;
- }
- }
|