package com.red.circle.other.app.command.team;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.other.app.dto.cmd.team.DbInviteMessageProcessCmd;
import com.red.circle.other.infra.database.mongo.entity.team.bd.BdInviteAgentMessage;
import com.red.circle.other.infra.database.mongo.entity.team.bd.MessageInviteGroup;
import com.red.circle.other.infra.database.mongo.service.team.bd.BdInviteAgentMessageService;
import com.red.circle.other.infra.database.rds.entity.team.AdminRechargeAgentRelation;
import com.red.circle.other.infra.database.rds.service.sys.AdministratorService;
import com.red.circle.other.infra.database.rds.service.team.AdminRechargeAgentRelationService;
import com.red.circle.other.inner.asserts.team.TeamErrorCode;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.wallet.inner.endpoint.freight.FreightGoldClient;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 处理管理员邀请充值代理消息.
 *
 * @author tf
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AdminInviteRechargeAgentProcessExe {

  private final AdministratorService administratorService;
  private final BdInviteAgentMessageService bdInviteAgentMessageService;
  private final AdminRechargeAgentRelationService adminRechargeAgentRelationService;
  private final FreightGoldClient freightGoldClient;

  public void execute(DbInviteMessageProcessCmd cmd) {
    ResponseAssert.isTrue(ResponseErrorCode.REQUEST_PARAMETER_ERROR, cmd.getStatus() > 0);
    BdInviteAgentMessage message = bdInviteAgentMessageService.getById(cmd.getId());
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, message);

    ResponseAssert.isTrue(CommonErrorCode.NOT_FOUND_RECORD_INFO,
        Objects.equals(message.getGroup(), MessageInviteGroup.ADMIN_INVITE_RECHARGE_AGENT.name()));

    // 被邀请用户必须是充值代理
    ResponseAssert.isTrue(TeamErrorCode.USER_IS_NOT_RECHARGE_AGENT,
        ResponseAssert.requiredSuccess(freightGoldClient.checkFreightAgent(cmd.requiredReqUserId())));

    // 该用户已经被邀请过了
    ResponseAssert.isFalse(TeamErrorCode.USER_IS_ALREADY_A_RECHARGE_AGENT,
        adminRechargeAgentRelationService.checkRechargeAgent(cmd.requiredReqUserId()));

    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        Objects.equals(message.getInviteUserId(), cmd.requiredReqUserId()));
    ResponseAssert.isTrue(TeamErrorCode.MESSAGE_PROCESSED, Objects.equals(message.getStatus(), 0));

    // 验证发起邀请的用户是否是管理员
    ResponseAssert.isTrue(TeamErrorCode.USER_IS_NOT_ADMIN,
        administratorService.existsAdmin(message.getUserId()));

    ResponseAssert.isTrue(CommonErrorCode.OPERATING_FAILURE, bdInviteAgentMessageService.
        updateStatus(cmd.getId(), message.getStatus(), cmd.getStatus(),
            MessageInviteGroup.ADMIN_INVITE_RECHARGE_AGENT.name()));

    if (Objects.equals(cmd.getStatus(), 1)) {

      // 创建管理员与充值代理关系
      adminRechargeAgentRelationService.add(new AdminRechargeAgentRelation()
          .setId(IdWorkerUtils.getId())
          .setSysOrigin(cmd.requireReqSysOrigin())
          .setAdminUserId(message.getUserId())
          .setUserId(cmd.requiredReqUserId())
      );
    }
  }

}
