userId = $userId; $this->pointId = $pointId; } /** * 积分流转 * (同用户,不同积分账户) * * @param POINT_TYPE $toPointId 目标积分类型 * @param int $amount 积分数量 * @param int $reId 关联ID * @param string $reType 关联类型 * @param string $remark 备注 * @return CirculationDto|string 成功返回DTO,失败返回错误信息 */ public function circulation(POINT_TYPE $toPointId, int $amount, int $reId, string $reType, string $remark) { # 检查积分类型是否相同 if ($this->pointId === $toPointId->valueInt()) { return '源积分类型和目标积分类型不能相同'; } # 检查积分数量 if ($amount <= 0) { return '积分数量必须大于0'; } Helper::check_tr(); # 先进行流转记录 $circulation_id = Circulation::handle($this->userId, $this->pointId, $toPointId->valueInt(), $amount, $reId, $reType, $remark); if (is_string($circulation_id)) { return $circulation_id; } # 进行双方的积分修改 # 先减少源积分 $result1 = User::handle($this->userId, $this->pointId, -$amount, LOG_TYPE::CIRCULATION, $circulation_id, $remark); if (is_string($result1)) { return $result1; } # 再增加目标积分 $result2 = User::handle($this->userId, $toPointId->valueInt(), $amount, LOG_TYPE::CIRCULATION, $circulation_id, $remark); if (is_string($result2)) { return $result2; } # 返回流转结果 return new CirculationDto([ 'circulation_id' => $circulation_id, 'from_point_id' => $this->pointId, 'to_point_id' => $toPointId->valueInt(), 'amount' => $amount, 'user_id' => $this->userId, 'remark' => $remark ]); } /** * 转账 * (不同人,同积分类型) * * @param int $toUserId 接收用户ID * @param int $amount 积分数量 * @param string $remark 备注 * @return TransferResultDto|string 成功返回DTO,失败返回错误信息 */ public function transfer(int $toUserId, int $amount, string $remark) { # 检查参数 if ($this->userId === $toUserId) { return '不能向自己转账'; } if ($amount <= 0) { return '积分数量必须大于0'; } # 开启事务 DB::beginTransaction(); # 先进行转账记录 $transfer_id = Transfer::to_user($this->userId, $this->pointId, $toUserId, $amount, $remark); if (is_string($transfer_id)) { DB::rollBack(); return $transfer_id; } # 进行双方的积分修改 # 先减少转出方积分 $result1 = User::handle($this->userId, $this->pointId, -$amount, LOG_TYPE::TRANSFER, $transfer_id, $remark); if (is_string($result1)) { DB::rollBack(); return $result1; } # 再增加转入方积分 $result2 = User::handle($toUserId, $this->pointId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark); if (is_string($result2)) { DB::rollBack(); return $result2; } DB::commit(); # 返回转账结果 return new TransferResultDto([ 'transfer_id' => $transfer_id, 'from_user_id' => $this->userId, 'to_user_id' => $toUserId, 'point_id' => $this->pointId, 'amount' => $amount, 'remark' => $remark ]); } /** * 增加积分 * * @param int $amount 积分数量 * @param LOG_TYPE $logType 日志类型 * @param string $operateId 操作ID * @param string $remark 备注 * @return TradeResultDto|string 成功返回DTO,失败返回错误信息 */ public function increase(int $amount, LOG_TYPE $logType, string $operateId, string $remark) { if ($amount <= 0) { return '积分数量必须大于0'; } $result = User::handle($this->userId, $this->pointId, $amount, $logType, $operateId, $remark); if (is_string($result)) { return $result; } return new TradeResultDto([ 'user_id' => $this->userId, 'point_id' => $this->pointId, 'amount' => $amount, 'log_type' => $logType, 'operate_id' => $operateId, 'remark' => $remark, 'success' => true ]); } /** * 减少积分 * * @param int $amount 积分数量 * @param LOG_TYPE $logType 日志类型 * @param string $operateId 操作ID * @param string $remark 备注 * @return TradeResultDto|string 成功返回DTO,失败返回错误信息 */ public function decrease(int $amount, LOG_TYPE $logType, string $operateId, string $remark) { if ($amount <= 0) { return '积分数量必须大于0'; } $result = User::handle($this->userId, $this->pointId, -$amount, $logType, $operateId, $remark); if (is_string($result)) { return $result; } return new TradeResultDto([ 'user_id' => $this->userId, 'point_id' => $this->pointId, 'amount' => -$amount, 'log_type' => $logType, 'operate_id' => $operateId, 'remark' => $remark, 'success' => true ]); } /** * 获取积分余额 * * @return int 积分余额 */ public function getBalance(): int { return PointModel::getBalance($this->userId, $this->pointId); } /** * 检查积分余额是否足够 * * @param int $amount 需要的积分数量 * @return bool 是否足够 */ public function checkBalance(int $amount): bool { return PointModel::checkBalance($this->userId, $this->pointId, $amount); } /** * 获取用户积分账户信息 * * @return PointAccountDto 积分账户DTO */ public function getAccountInfo(): PointAccountDto { $account = PointModel::getAccount($this->userId, $this->pointId); return new PointAccountDto([ 'user_id' => $this->userId, 'point_id' => $this->pointId, 'balance' => $account ? $account->balance : 0, 'create_time' => $account ? $account->create_time : 0, 'update_time' => $account ? $account->update_time : 0, ]); } /** * Admin 积分操作 * * @param int $admin_id 管理员ID * @param POINT_TYPE $point_id 积分类型 * @param int $point_amount 积分数量 * @param string $remark 备注 * @return PointAdminDto|string 成功返回DTO,失败返回错误信息 */ public function admin_operate(int $admin_id, POINT_TYPE $point_id, int $point_amount, string $remark) { $data = [ 'total_points' => $point_amount, 'status' => 1, 'user_id' => $this->userId, 'point_id' => $point_id->valueInt(), 'admin_id' => $admin_id, 'create_time' => time(), 'remark' => $remark ]; # 启动事务 DB::beginTransaction(); # 写日志 $point_admin = new PointAdminModel(); $point_admin->setData($data); if ($point_admin->save() === false) { DB::rollBack(); return '_Model-error'; } # 进行积分操作 $result = User::handle($this->userId, $point_id->valueInt(), $point_amount, LOG_TYPE::ADMIN_OPERATE, $point_admin->id, $remark); if (is_string($result)) { DB::rollBack(); return $result; } DB::commit(); return new PointAdminDto([ 'id' => $point_admin->id, 'user_id' => $this->userId, 'point_id' => $point_id->valueInt(), 'admin_id' => $admin_id, 'total_points' => $point_amount, 'remark' => $remark, 'create_time' => time() ]); } }