PointCirculationModel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 $user_id 用户ID
  14. * @property \App\Module\Point\Enums\POINT_TYPE $from_point_id 源积分类型ID
  15. * @property \App\Module\Point\Enums\POINT_TYPE $to_point_id 目标积分类型ID
  16. * @property int $amount 流转积分数量
  17. * @property int $re_id 关联ID
  18. * @property string $re_type 关联类型
  19. * @property string $remark 备注
  20. * @property int $status 状态:0-待处理,1-已完成,2-已失败
  21. * @property int $create_time 创建时间
  22. * @property int $update_time 更新时间
  23. * field end
  24. */
  25. class PointCirculationModel extends ModelCore
  26. {
  27. protected $table = 'point_circulation';
  28. // attrlist start
  29. protected $fillable = [
  30. 'id',
  31. 'user_id',
  32. 'from_point_id',
  33. 'to_point_id',
  34. 'amount',
  35. 're_id',
  36. 're_type',
  37. 'remark',
  38. 'status',
  39. 'create_time',
  40. 'update_time',
  41. ];
  42. // attrlist end
  43. public $timestamps = false;
  44. protected $casts = [
  45. 'from_point_id' => POINT_TYPE::class,
  46. 'to_point_id' => POINT_TYPE::class,
  47. ];
  48. // 状态常量
  49. const STATUS_PENDING = 0; // 待处理
  50. const STATUS_COMPLETED = 1; // 已完成
  51. const STATUS_FAILED = 2; // 已失败
  52. /**
  53. * 创建积分流转记录
  54. *
  55. * @param int $userId 用户ID
  56. * @param int $fromPointId 源积分类型ID
  57. * @param int $toPointId 目标积分类型ID
  58. * @param int $amount 流转积分数量
  59. * @param int $reId 关联ID
  60. * @param string $reType 关联类型
  61. * @param string $remark 备注
  62. * @return PointCirculationModel|false 成功返回模型,失败返回false
  63. */
  64. public static function createRecord(
  65. int $userId,
  66. int $fromPointId,
  67. int $toPointId,
  68. int $amount,
  69. int $reId,
  70. string $reType,
  71. string $remark
  72. ) {
  73. $record = new self();
  74. $record->user_id = $userId;
  75. $record->from_point_id = $fromPointId;
  76. $record->to_point_id = $toPointId;
  77. $record->amount = $amount;
  78. $record->re_id = $reId;
  79. $record->re_type = $reType;
  80. $record->remark = $remark;
  81. $record->status = self::STATUS_PENDING;
  82. $record->create_time = time();
  83. $record->update_time = time();
  84. if ($record->save()) {
  85. return $record;
  86. }
  87. return false;
  88. }
  89. /**
  90. * 更新记录状态
  91. *
  92. * @param int $status 状态
  93. * @return bool 是否成功
  94. */
  95. public function updateStatus(int $status): bool
  96. {
  97. $this->status = $status;
  98. $this->update_time = time();
  99. return $this->save();
  100. }
  101. /**
  102. * 标记为已完成
  103. *
  104. * @return bool 是否成功
  105. */
  106. public function markCompleted(): bool
  107. {
  108. return $this->updateStatus(self::STATUS_COMPLETED);
  109. }
  110. /**
  111. * 标记为已失败
  112. *
  113. * @return bool 是否成功
  114. */
  115. public function markFailed(): bool
  116. {
  117. return $this->updateStatus(self::STATUS_FAILED);
  118. }
  119. /**
  120. * 获取用户积分流转记录
  121. *
  122. * @param int $userId 用户ID
  123. * @param int $limit 限制数量
  124. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  125. */
  126. public static function getUserRecords(int $userId, int $limit = 50)
  127. {
  128. return self::where('user_id', $userId)
  129. ->orderBy('create_time', 'desc')
  130. ->limit($limit)
  131. ->get();
  132. }
  133. /**
  134. * 获取指定积分类型的流转记录
  135. *
  136. * @param int $userId 用户ID
  137. * @param int $pointId 积分类型ID
  138. * @param int $limit 限制数量
  139. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  140. */
  141. public static function getPointRecords(int $userId, int $pointId, int $limit = 50)
  142. {
  143. return self::where('user_id', $userId)
  144. ->where(function($query) use ($pointId) {
  145. $query->where('from_point_id', $pointId)
  146. ->orWhere('to_point_id', $pointId);
  147. })
  148. ->orderBy('create_time', 'desc')
  149. ->limit($limit)
  150. ->get();
  151. }
  152. /**
  153. * 获取状态名称
  154. *
  155. * @return string 状态名称
  156. */
  157. public function getStatusName(): string
  158. {
  159. return match($this->status) {
  160. self::STATUS_PENDING => '待处理',
  161. self::STATUS_COMPLETED => '已完成',
  162. self::STATUS_FAILED => '已失败',
  163. default => '未知状态',
  164. };
  165. }
  166. /**
  167. * 判断是否为待处理状态
  168. *
  169. * @return bool 是否为待处理
  170. */
  171. public function isPending(): bool
  172. {
  173. return $this->status === self::STATUS_PENDING;
  174. }
  175. /**
  176. * 判断是否为已完成状态
  177. *
  178. * @return bool 是否为已完成
  179. */
  180. public function isCompleted(): bool
  181. {
  182. return $this->status === self::STATUS_COMPLETED;
  183. }
  184. /**
  185. * 判断是否为已失败状态
  186. *
  187. * @return bool 是否为已失败
  188. */
  189. public function isFailed(): bool
  190. {
  191. return $this->status === self::STATUS_FAILED;
  192. }
  193. /**
  194. * 获取源积分类型名称
  195. *
  196. * @return string 源积分类型名称
  197. */
  198. public function getFromPointTypeName(): string
  199. {
  200. return $this->from_point_id->getTypeName();
  201. }
  202. /**
  203. * 获取目标积分类型名称
  204. *
  205. * @return string 目标积分类型名称
  206. */
  207. public function getToPointTypeName(): string
  208. {
  209. return $this->to_point_id->getTypeName();
  210. }
  211. }