PointTransferModel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace App\Module\Point\Models;
  3. use App\Module\Point\Enums\POINT_TYPE;
  4. use UCore\ModelCore;
  5. /**
  6. * 积分转账记录表
  7. *
  8. * 记录不同用户间的积分转账操作
  9. * 专注于整数积分处理,不涉及小数运算
  10. *
  11. * field start
  12. * @property int $id 自增
  13. * @property int $from_user_id 转出用户ID
  14. * @property int $to_user_id 转入用户ID
  15. * @property \App\Module\Point\Enums\POINT_TYPE $point_id 积分类型ID
  16. * @property int $amount 转账积分数量
  17. * @property string $remark 备注
  18. * @property int $status 状态:0-待处理,1-已完成,2-已失败
  19. * @property int $create_time 创建时间
  20. * @property int $update_time 更新时间
  21. * field end
  22. */
  23. class PointTransferModel extends ModelCore
  24. {
  25. protected $table = 'point_transfer';
  26. // attrlist start
  27. protected $fillable = [
  28. 'id',
  29. 'from_user_id',
  30. 'to_user_id',
  31. 'point_id',
  32. 'amount',
  33. 'remark',
  34. 'status',
  35. 'create_time',
  36. 'update_time',
  37. ];
  38. // attrlist end
  39. public $timestamps = false;
  40. protected $casts = [
  41. 'point_id' => POINT_TYPE::class,
  42. ];
  43. // 状态常量
  44. const STATUS_PENDING = 0; // 待处理
  45. const STATUS_COMPLETED = 1; // 已完成
  46. const STATUS_FAILED = 2; // 已失败
  47. /**
  48. * 创建积分转账记录
  49. *
  50. * @param int $fromUserId 转出用户ID
  51. * @param int $toUserId 转入用户ID
  52. * @param int $pointId 积分类型ID
  53. * @param int $amount 转账积分数量
  54. * @param string $remark 备注
  55. * @return PointTransferModel|false 成功返回模型,失败返回false
  56. */
  57. public static function createRecord(
  58. int $fromUserId,
  59. int $toUserId,
  60. int $pointId,
  61. int $amount,
  62. string $remark
  63. ) {
  64. $record = new self();
  65. $record->from_user_id = $fromUserId;
  66. $record->to_user_id = $toUserId;
  67. $record->point_id = $pointId;
  68. $record->amount = $amount;
  69. $record->remark = $remark;
  70. $record->status = self::STATUS_PENDING;
  71. $record->create_time = time();
  72. $record->update_time = time();
  73. if ($record->save()) {
  74. return $record;
  75. }
  76. return false;
  77. }
  78. /**
  79. * 更新记录状态
  80. *
  81. * @param int $status 状态
  82. * @return bool 是否成功
  83. */
  84. public function updateStatus(int $status): bool
  85. {
  86. $this->status = $status;
  87. $this->update_time = time();
  88. return $this->save();
  89. }
  90. /**
  91. * 标记为已完成
  92. *
  93. * @return bool 是否成功
  94. */
  95. public function markCompleted(): bool
  96. {
  97. return $this->updateStatus(self::STATUS_COMPLETED);
  98. }
  99. /**
  100. * 标记为已失败
  101. *
  102. * @return bool 是否成功
  103. */
  104. public function markFailed(): bool
  105. {
  106. return $this->updateStatus(self::STATUS_FAILED);
  107. }
  108. /**
  109. * 获取用户转账记录(包括转出和转入)
  110. *
  111. * @param int $userId 用户ID
  112. * @param int $limit 限制数量
  113. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  114. */
  115. public static function getUserRecords(int $userId, int $limit = 50)
  116. {
  117. return self::where(function($query) use ($userId) {
  118. $query->where('from_user_id', $userId)
  119. ->orWhere('to_user_id', $userId);
  120. })
  121. ->orderBy('create_time', 'desc')
  122. ->limit($limit)
  123. ->get();
  124. }
  125. /**
  126. * 获取用户转出记录
  127. *
  128. * @param int $userId 用户ID
  129. * @param int $limit 限制数量
  130. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  131. */
  132. public static function getUserTransferOutRecords(int $userId, int $limit = 50)
  133. {
  134. return self::where('from_user_id', $userId)
  135. ->orderBy('create_time', 'desc')
  136. ->limit($limit)
  137. ->get();
  138. }
  139. /**
  140. * 获取用户转入记录
  141. *
  142. * @param int $userId 用户ID
  143. * @param int $limit 限制数量
  144. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  145. */
  146. public static function getUserTransferInRecords(int $userId, int $limit = 50)
  147. {
  148. return self::where('to_user_id', $userId)
  149. ->orderBy('create_time', 'desc')
  150. ->limit($limit)
  151. ->get();
  152. }
  153. /**
  154. * 获取状态名称
  155. *
  156. * @return string 状态名称
  157. */
  158. public function getStatusName(): string
  159. {
  160. return match($this->status) {
  161. self::STATUS_PENDING => '待处理',
  162. self::STATUS_COMPLETED => '已完成',
  163. self::STATUS_FAILED => '已失败',
  164. default => '未知状态',
  165. };
  166. }
  167. /**
  168. * 判断是否为待处理状态
  169. *
  170. * @return bool 是否为待处理
  171. */
  172. public function isPending(): bool
  173. {
  174. return $this->status === self::STATUS_PENDING;
  175. }
  176. /**
  177. * 判断是否为已完成状态
  178. *
  179. * @return bool 是否为已完成
  180. */
  181. public function isCompleted(): bool
  182. {
  183. return $this->status === self::STATUS_COMPLETED;
  184. }
  185. /**
  186. * 判断是否为已失败状态
  187. *
  188. * @return bool 是否为已失败
  189. */
  190. public function isFailed(): bool
  191. {
  192. return $this->status === self::STATUS_FAILED;
  193. }
  194. /**
  195. * 获取积分类型名称
  196. *
  197. * @return string 积分类型名称
  198. */
  199. public function getPointTypeName(): string
  200. {
  201. return $this->point_id->getTypeName();
  202. }
  203. /**
  204. * 判断用户是否为转出方
  205. *
  206. * @param int $userId 用户ID
  207. * @return bool 是否为转出方
  208. */
  209. public function isTransferOut(int $userId): bool
  210. {
  211. return $this->from_user_id === $userId;
  212. }
  213. /**
  214. * 判断用户是否为转入方
  215. *
  216. * @param int $userId 用户ID
  217. * @return bool 是否为转入方
  218. */
  219. public function isTransferIn(int $userId): bool
  220. {
  221. return $this->to_user_id === $userId;
  222. }
  223. }