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 ''; } }