PointAdminModel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 $point_id 积分类型ID
  15. * @property int $admin_id 管理员ID
  16. * @property int $total_points 操作积分数量
  17. * @property int $status 状态:0-待处理,1-已完成,2-已失败
  18. * @property string $remark 备注
  19. * @property int $create_time 创建时间
  20. * @property int $update_time 更新时间
  21. * field end
  22. */
  23. class PointAdminModel extends ModelCore
  24. {
  25. protected $table = 'point_admin';
  26. // attrlist start
  27. protected $fillable = [
  28. 'id',
  29. 'user_id',
  30. 'point_id',
  31. 'admin_id',
  32. 'total_points',
  33. 'status',
  34. 'remark',
  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 $userId 用户ID
  51. * @param int $pointId 积分类型ID
  52. * @param int $adminId 管理员ID
  53. * @param int $totalPoints 操作积分数量
  54. * @param string $remark 备注
  55. * @return PointAdminModel|false 成功返回模型,失败返回false
  56. */
  57. public static function createRecord(
  58. int $userId,
  59. int $pointId,
  60. int $adminId,
  61. int $totalPoints,
  62. string $remark
  63. ) {
  64. $record = new self();
  65. $record->user_id = $userId;
  66. $record->point_id = $pointId;
  67. $record->admin_id = $adminId;
  68. $record->total_points = $totalPoints;
  69. $record->status = self::STATUS_PENDING;
  70. $record->remark = $remark;
  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 $adminId 管理员ID
  112. * @param int $limit 限制数量
  113. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  114. */
  115. public static function getAdminRecords(int $adminId, int $limit = 50)
  116. {
  117. return self::where('admin_id', $adminId)
  118. ->orderBy('create_time', 'desc')
  119. ->limit($limit)
  120. ->get();
  121. }
  122. /**
  123. * 获取用户相关的管理员操作记录
  124. *
  125. * @param int $userId 用户ID
  126. * @param int $limit 限制数量
  127. * @return \Illuminate\Database\Eloquent\Collection 记录集合
  128. */
  129. public static function getUserRecords(int $userId, int $limit = 50)
  130. {
  131. return self::where('user_id', $userId)
  132. ->orderBy('create_time', 'desc')
  133. ->limit($limit)
  134. ->get();
  135. }
  136. /**
  137. * 获取状态名称
  138. *
  139. * @return string 状态名称
  140. */
  141. public function getStatusName(): string
  142. {
  143. return match($this->status) {
  144. self::STATUS_PENDING => '待处理',
  145. self::STATUS_COMPLETED => '已完成',
  146. self::STATUS_FAILED => '已失败',
  147. default => '未知状态',
  148. };
  149. }
  150. /**
  151. * 判断是否为待处理状态
  152. *
  153. * @return bool 是否为待处理
  154. */
  155. public function isPending(): bool
  156. {
  157. return $this->status === self::STATUS_PENDING;
  158. }
  159. /**
  160. * 判断是否为已完成状态
  161. *
  162. * @return bool 是否为已完成
  163. */
  164. public function isCompleted(): bool
  165. {
  166. return $this->status === self::STATUS_COMPLETED;
  167. }
  168. /**
  169. * 判断是否为已失败状态
  170. *
  171. * @return bool 是否为已失败
  172. */
  173. public function isFailed(): bool
  174. {
  175. return $this->status === self::STATUS_FAILED;
  176. }
  177. /**
  178. * 获取积分类型名称
  179. *
  180. * @return string 积分类型名称
  181. */
  182. public function getPointTypeName(): string
  183. {
  184. return $this->point_id->getTypeName();
  185. }
  186. /**
  187. * 判断是否为增加积分操作
  188. *
  189. * @return bool 是否为增加操作
  190. */
  191. public function isIncrease(): bool
  192. {
  193. return $this->total_points > 0;
  194. }
  195. /**
  196. * 判断是否为减少积分操作
  197. *
  198. * @return bool 是否为减少操作
  199. */
  200. public function isDecrease(): bool
  201. {
  202. return $this->total_points < 0;
  203. }
  204. }