CheckUserPointValidator.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace App\Module\Point\Validators;
  3. use App\Module\Point\Models\PointModel;
  4. use App\Module\Point\Enums\POINT_TYPE;
  5. /**
  6. * 用户积分检查验证器
  7. *
  8. * 用于验证用户积分相关操作的有效性
  9. */
  10. class CheckUserPointValidator
  11. {
  12. /**
  13. * 错误信息列表
  14. */
  15. private array $errors = [];
  16. /**
  17. * 验证用户积分余额是否足够
  18. *
  19. * @param int $userId 用户ID
  20. * @param int $pointId 积分类型ID
  21. * @param int $amount 需要的积分数量
  22. * @param string $field 字段名(用于错误信息)
  23. * @return bool 是否验证通过
  24. */
  25. public function validateBalance(int $userId, int $pointId, int $amount, string $field = 'amount'): bool
  26. {
  27. if ($userId <= 0) {
  28. $this->addError($field, '用户ID无效');
  29. return false;
  30. }
  31. if (!POINT_TYPE::isValid($pointId)) {
  32. $this->addError($field, '积分类型无效');
  33. return false;
  34. }
  35. if ($amount <= 0) {
  36. $this->addError($field, '积分数量必须大于0');
  37. return false;
  38. }
  39. $balance = PointModel::getBalance($userId, $pointId);
  40. if ($balance < $amount) {
  41. $pointType = POINT_TYPE::from($pointId);
  42. $typeName = $pointType->getTypeName();
  43. $this->addError($field, "积分余额不足,当前{$typeName}余额:{$balance},需要:{$amount}");
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * 验证积分操作参数
  50. *
  51. * @param int $userId 用户ID
  52. * @param int $pointId 积分类型ID
  53. * @param int $amount 积分数量
  54. * @param string $operateId 操作ID
  55. * @param string $remark 备注
  56. * @return bool 是否验证通过
  57. */
  58. public function validatePointOperation(
  59. int $userId,
  60. int $pointId,
  61. int $amount,
  62. string $operateId,
  63. string $remark
  64. ): bool {
  65. $isValid = true;
  66. if ($userId <= 0) {
  67. $this->addError('user_id', '用户ID无效');
  68. $isValid = false;
  69. }
  70. if (!POINT_TYPE::isValid($pointId)) {
  71. $this->addError('point_id', '积分类型无效');
  72. $isValid = false;
  73. }
  74. if ($amount == 0) {
  75. $this->addError('amount', '积分数量不能为0');
  76. $isValid = false;
  77. }
  78. if (empty($operateId)) {
  79. $this->addError('operate_id', '操作ID不能为空');
  80. $isValid = false;
  81. }
  82. if (empty($remark)) {
  83. $this->addError('remark', '备注不能为空');
  84. $isValid = false;
  85. }
  86. return $isValid;
  87. }
  88. /**
  89. * 验证积分流转参数
  90. *
  91. * @param int $userId 用户ID
  92. * @param int $fromPointId 源积分类型ID
  93. * @param int $toPointId 目标积分类型ID
  94. * @param int $amount 流转积分数量
  95. * @return bool 是否验证通过
  96. */
  97. public function validateCirculation(
  98. int $userId,
  99. int $fromPointId,
  100. int $toPointId,
  101. int $amount
  102. ): bool {
  103. $isValid = true;
  104. if ($userId <= 0) {
  105. $this->addError('user_id', '用户ID无效');
  106. $isValid = false;
  107. }
  108. if (!POINT_TYPE::isValid($fromPointId)) {
  109. $this->addError('from_point_id', '源积分类型无效');
  110. $isValid = false;
  111. }
  112. if (!POINT_TYPE::isValid($toPointId)) {
  113. $this->addError('to_point_id', '目标积分类型无效');
  114. $isValid = false;
  115. }
  116. if ($fromPointId === $toPointId) {
  117. $this->addError('to_point_id', '源积分类型和目标积分类型不能相同');
  118. $isValid = false;
  119. }
  120. if ($amount <= 0) {
  121. $this->addError('amount', '流转积分数量必须大于0');
  122. $isValid = false;
  123. }
  124. // 检查源积分余额
  125. if ($isValid && !$this->validateBalance($userId, $fromPointId, $amount, 'amount')) {
  126. $isValid = false;
  127. }
  128. return $isValid;
  129. }
  130. /**
  131. * 验证积分转账参数
  132. *
  133. * @param int $fromUserId 转出用户ID
  134. * @param int $toUserId 转入用户ID
  135. * @param int $pointId 积分类型ID
  136. * @param int $amount 转账积分数量
  137. * @return bool 是否验证通过
  138. */
  139. public function validateTransfer(
  140. int $fromUserId,
  141. int $toUserId,
  142. int $pointId,
  143. int $amount
  144. ): bool {
  145. $isValid = true;
  146. if ($fromUserId <= 0) {
  147. $this->addError('from_user_id', '转出用户ID无效');
  148. $isValid = false;
  149. }
  150. if ($toUserId <= 0) {
  151. $this->addError('to_user_id', '转入用户ID无效');
  152. $isValid = false;
  153. }
  154. if ($fromUserId === $toUserId) {
  155. $this->addError('to_user_id', '不能向自己转账');
  156. $isValid = false;
  157. }
  158. if (!POINT_TYPE::isValid($pointId)) {
  159. $this->addError('point_id', '积分类型无效');
  160. $isValid = false;
  161. }
  162. if ($amount <= 0) {
  163. $this->addError('amount', '转账积分数量必须大于0');
  164. $isValid = false;
  165. }
  166. // 检查转出方积分余额
  167. if ($isValid && !$this->validateBalance($fromUserId, $pointId, $amount, 'amount')) {
  168. $isValid = false;
  169. }
  170. return $isValid;
  171. }
  172. /**
  173. * 批量验证用户积分余额
  174. *
  175. * @param array $operations 操作数组,每个元素包含user_id、point_id、amount
  176. * @return bool 是否全部验证通过
  177. */
  178. public function batchValidateBalance(array $operations): bool
  179. {
  180. $isValid = true;
  181. foreach ($operations as $index => $operation) {
  182. if (!isset($operation['user_id'], $operation['point_id'], $operation['amount'])) {
  183. $this->addError("operations.{$index}", '操作参数不完整');
  184. $isValid = false;
  185. continue;
  186. }
  187. if (!$this->validateBalance(
  188. $operation['user_id'],
  189. $operation['point_id'],
  190. $operation['amount'],
  191. "operations.{$index}.amount"
  192. )) {
  193. $isValid = false;
  194. }
  195. }
  196. return $isValid;
  197. }
  198. /**
  199. * 添加错误信息
  200. *
  201. * @param string $field 字段名
  202. * @param string $message 错误信息
  203. */
  204. public function addError(string $field, string $message): void
  205. {
  206. if (!isset($this->errors[$field])) {
  207. $this->errors[$field] = [];
  208. }
  209. $this->errors[$field][] = $message;
  210. }
  211. /**
  212. * 获取所有错误信息
  213. *
  214. * @return array 错误信息数组
  215. */
  216. public function getErrors(): array
  217. {
  218. return $this->errors;
  219. }
  220. /**
  221. * 获取指定字段的错误信息
  222. *
  223. * @param string $field 字段名
  224. * @return array 错误信息数组
  225. */
  226. public function getFieldErrors(string $field): array
  227. {
  228. return $this->errors[$field] ?? [];
  229. }
  230. /**
  231. * 检查是否有错误
  232. *
  233. * @return bool 是否有错误
  234. */
  235. public function hasErrors(): bool
  236. {
  237. return !empty($this->errors);
  238. }
  239. /**
  240. * 清空错误信息
  241. */
  242. public function clearErrors(): void
  243. {
  244. $this->errors = [];
  245. }
  246. /**
  247. * 获取第一个错误信息
  248. *
  249. * @return string 错误信息
  250. */
  251. public function getFirstError(): string
  252. {
  253. foreach ($this->errors as $fieldErrors) {
  254. if (!empty($fieldErrors)) {
  255. return $fieldErrors[0];
  256. }
  257. }
  258. return '';
  259. }
  260. }