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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.rds.entity.user.user.UserBeautifulNumberApply;
import com.red.circle.other.infra.database.rds.service.user.user.UserBeautifulNumberApplyService;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 用户靓号申请 实现.
 *
 * @author pengshiang on 2021/8/18
 */
@Component
@RequiredArgsConstructor
public class UserBeautifulNumberApplyCmdExe {

  private final UserProfileGateway userProfileGateway;
  private final UserBeautifulNumberApplyService userBeautifulNumberApplyService;

  public void execute(AppExtCommand cmd) {

    ResponseAssert.isTrue(
        UserErrorCode.INSUFFICIENT_LEVEL_30_ERROR, getUserLevelExperience(cmd) >= 30);

    ResponseAssert.isFalse(CommonErrorCode.REPEATED_SUBMISSION, isExistPending(cmd));

    userBeautifulNumberApplyService.save(new UserBeautifulNumberApply()
        .setId(IdWorkerUtils.getId())
        .setApplyUser(cmd.requiredReqUserId())
        .setState(0)
        .setSysOrigin(cmd.requireReqSysOrigin())
    );
  }

  private Boolean isExistPending(AppExtCommand cmd) {
    return userBeautifulNumberApplyService.isExistPending(cmd.requiredReqUserId());
  }

  private Integer getUserLevelExperience(AppExtCommand cmd) {
    return userProfileGateway.getUserConsumptionLevel(cmd.requireReqSysOriginEnum(),
            cmd.requiredReqUserId())
        .getWealthLevel();
  }

}
