package com.red.circle.other.app.inner.service.game.impl;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.game.GameTrumpetBlacklistConvertor;
import com.red.circle.other.app.inner.service.game.GameTrumpetBlacklistClientService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.rds.entity.game.GameTrumpetBlacklist;
import com.red.circle.other.infra.database.rds.service.game.GameTrumpetBlacklistService;
import com.red.circle.other.inner.asserts.GameErrorCode;
import com.red.circle.other.inner.model.cmd.game.GameTrumpetBlacklistParamCmd;
import com.red.circle.other.inner.model.cmd.game.GameTrumpetBlacklistQryCmd;
import com.red.circle.other.inner.model.dto.game.GameTrumpetBlacklistDTO;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 喇叭-黑名单
 *
 * @author zongpubin on 2024/1/25
 */
@Service
@RequiredArgsConstructor
public class GameTrumpetBlacklistClientServiceImpl implements GameTrumpetBlacklistClientService {

  private final UserProfileGateway userProfileGateway;
  private final GameTrumpetBlacklistService gameTrumpetBlacklistService;
  private final GameTrumpetBlacklistConvertor gameTrumpetBlacklistConvertor;

  @Override
  public PageResult<GameTrumpetBlacklistDTO> page(GameTrumpetBlacklistQryCmd query) {
    PageResult<GameTrumpetBlacklist> pageResult = gameTrumpetBlacklistService.page(query);
    return pageResult.convert(gameTrumpetBlacklistConvertor::toGameTrumpetBlacklistDTO);
  }

  @Override
  public void add(GameTrumpetBlacklistParamCmd param) {
    UserProfile userProfile = userProfileGateway.getByAccount(param.getSysOrigin(),
        param.getAccount());

    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, userProfile);

    ResponseAssert.isFalse(GameErrorCode.USER_ALREADY_EXISTS,
        gameTrumpetBlacklistService.exist(userProfile.getId()));
    gameTrumpetBlacklistService.save(new GameTrumpetBlacklist()
        .setUserId(userProfile.getId())
        .setSysOrigin(userProfile.getOriginSys())
        .setCreateUser(param.getCreateUser())
        .setCreateTime(TimestampUtils.now())
        .setUpdateTime(TimestampUtils.now()));

  }

  @Override
  public void deleteByUserId(Long userId) {
    gameTrumpetBlacklistService.deleteByUserId(userId);
  }
}
