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


import com.red.circle.common.business.core.ImageSizeConst;
import com.red.circle.external.inner.endpoint.oss.OssServiceClient;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.cmd.user.user.UserLogoutApplyCmd;
import com.red.circle.other.infra.database.rds.entity.user.user.LogoutApply;
import com.red.circle.other.infra.database.rds.service.user.user.LogoutApplyService;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
/**
 * 用户注销
 *
 * @author zongpubin on 2023/9/12
 */
@Component
@RequiredArgsConstructor
public class UserLogoutApplyAddCmdExe {

  private final OssServiceClient ossServiceClient;
  private final LogoutApplyService logoutApplyService;

  public void execute(UserLogoutApplyCmd cmd) {
    ResponseAssert.isFalse(UserErrorCode.ACCOUNT_IS_DELETE,
        logoutApplyService.getByUserAccount(cmd.getUserAccount(), cmd.getLogoutType()));

    logoutApplyService.save(new LogoutApply()
        .setId(IdWorkerUtils.getId())
        .setUserAccount(cmd.getUserAccount())
        .setUserName(cmd.getUserName())
        .setUploadCover(ResponseAssert.requiredSuccess(
            ossServiceClient.processImgSaveAsCompressZoom(cmd.getUploadCover(),
                ImageSizeConst.COVER_HEIGHT)
        ))
        .setLogoutType(cmd.getLogoutType())
        .setSysOrigin(cmd.getSysOrigin())
    );
  }
}
