| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace App\Module\Point\Validators;
- use App\Module\Point\Models\PointModel;
- use App\Module\Point\Enums\POINT_TYPE;
- /**
- * 用户积分检查验证器
- *
- * 用于验证用户积分相关操作的有效性
- */
- class CheckUserPointValidator
- {
- /**
- * 错误信息列表
- */
- private array $errors = [];
- /**
- * 验证用户积分余额是否足够
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @param int $amount 需要的积分数量
- * @param string $field 字段名(用于错误信息)
- * @return bool 是否验证通过
- */
- public function validateBalance(int $userId, int $pointId, int $amount, string $field = 'amount'): bool
- {
- if ($userId <= 0) {
- $this->addError($field, '用户ID无效');
- return false;
- }
- if (!POINT_TYPE::isValid($pointId)) {
- $this->addError($field, '积分类型无效');
- return false;
- }
- if ($amount <= 0) {
- $this->addError($field, '积分数量必须大于0');
- return false;
- }
- $balance = PointModel::getBalance($userId, $pointId);
- if ($balance < $amount) {
- $pointType = POINT_TYPE::from($pointId);
- $typeName = $pointType->getTypeName();
- $this->addError($field, "积分余额不足,当前{$typeName}余额:{$balance},需要:{$amount}");
- return false;
- }
- return true;
- }
- /**
- * 验证积分操作参数
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @param int $amount 积分数量
- * @param string $operateId 操作ID
- * @param string $remark 备注
- * @return bool 是否验证通过
- */
- public function validatePointOperation(
- int $userId,
- int $pointId,
- int $amount,
- string $operateId,
- string $remark
- ): bool {
- $isValid = true;
- if ($userId <= 0) {
- $this->addError('user_id', '用户ID无效');
- $isValid = false;
- }
- if (!POINT_TYPE::isValid($pointId)) {
- $this->addError('point_id', '积分类型无效');
- $isValid = false;
- }
- if ($amount == 0) {
- $this->addError('amount', '积分数量不能为0');
- $isValid = false;
- }
- if (empty($operateId)) {
- $this->addError('operate_id', '操作ID不能为空');
- $isValid = false;
- }
- if (empty($remark)) {
- $this->addError('remark', '备注不能为空');
- $isValid = false;
- }
- return $isValid;
- }
- /**
- * 验证积分流转参数
- *
- * @param int $userId 用户ID
- * @param int $fromPointId 源积分类型ID
- * @param int $toPointId 目标积分类型ID
- * @param int $amount 流转积分数量
- * @return bool 是否验证通过
- */
- public function validateCirculation(
- int $userId,
- int $fromPointId,
- int $toPointId,
- int $amount
- ): bool {
- $isValid = true;
- if ($userId <= 0) {
- $this->addError('user_id', '用户ID无效');
- $isValid = false;
- }
- if (!POINT_TYPE::isValid($fromPointId)) {
- $this->addError('from_point_id', '源积分类型无效');
- $isValid = false;
- }
- if (!POINT_TYPE::isValid($toPointId)) {
- $this->addError('to_point_id', '目标积分类型无效');
- $isValid = false;
- }
- if ($fromPointId === $toPointId) {
- $this->addError('to_point_id', '源积分类型和目标积分类型不能相同');
- $isValid = false;
- }
- if ($amount <= 0) {
- $this->addError('amount', '流转积分数量必须大于0');
- $isValid = false;
- }
- // 检查源积分余额
- if ($isValid && !$this->validateBalance($userId, $fromPointId, $amount, 'amount')) {
- $isValid = false;
- }
- return $isValid;
- }
- /**
- * 验证积分转账参数
- *
- * @param int $fromUserId 转出用户ID
- * @param int $toUserId 转入用户ID
- * @param int $pointId 积分类型ID
- * @param int $amount 转账积分数量
- * @return bool 是否验证通过
- */
- public function validateTransfer(
- int $fromUserId,
- int $toUserId,
- int $pointId,
- int $amount
- ): bool {
- $isValid = true;
- if ($fromUserId <= 0) {
- $this->addError('from_user_id', '转出用户ID无效');
- $isValid = false;
- }
- if ($toUserId <= 0) {
- $this->addError('to_user_id', '转入用户ID无效');
- $isValid = false;
- }
- if ($fromUserId === $toUserId) {
- $this->addError('to_user_id', '不能向自己转账');
- $isValid = false;
- }
- if (!POINT_TYPE::isValid($pointId)) {
- $this->addError('point_id', '积分类型无效');
- $isValid = false;
- }
- if ($amount <= 0) {
- $this->addError('amount', '转账积分数量必须大于0');
- $isValid = false;
- }
- // 检查转出方积分余额
- if ($isValid && !$this->validateBalance($fromUserId, $pointId, $amount, 'amount')) {
- $isValid = false;
- }
- return $isValid;
- }
- /**
- * 批量验证用户积分余额
- *
- * @param array $operations 操作数组,每个元素包含user_id、point_id、amount
- * @return bool 是否全部验证通过
- */
- public function batchValidateBalance(array $operations): bool
- {
- $isValid = true;
- foreach ($operations as $index => $operation) {
- if (!isset($operation['user_id'], $operation['point_id'], $operation['amount'])) {
- $this->addError("operations.{$index}", '操作参数不完整');
- $isValid = false;
- continue;
- }
- if (!$this->validateBalance(
- $operation['user_id'],
- $operation['point_id'],
- $operation['amount'],
- "operations.{$index}.amount"
- )) {
- $isValid = false;
- }
- }
- return $isValid;
- }
- /**
- * 添加错误信息
- *
- * @param string $field 字段名
- * @param string $message 错误信息
- */
- public function addError(string $field, string $message): void
- {
- if (!isset($this->errors[$field])) {
- $this->errors[$field] = [];
- }
- $this->errors[$field][] = $message;
- }
- /**
- * 获取所有错误信息
- *
- * @return array 错误信息数组
- */
- public function getErrors(): array
- {
- return $this->errors;
- }
- /**
- * 获取指定字段的错误信息
- *
- * @param string $field 字段名
- * @return array 错误信息数组
- */
- public function getFieldErrors(string $field): array
- {
- return $this->errors[$field] ?? [];
- }
- /**
- * 检查是否有错误
- *
- * @return bool 是否有错误
- */
- public function hasErrors(): bool
- {
- return !empty($this->errors);
- }
- /**
- * 清空错误信息
- */
- public function clearErrors(): void
- {
- $this->errors = [];
- }
- /**
- * 获取第一个错误信息
- *
- * @return string 错误信息
- */
- public function getFirstError(): string
- {
- foreach ($this->errors as $fieldErrors) {
- if (!empty($fieldErrors)) {
- return $fieldErrors[0];
- }
- }
- return '';
- }
- }
|