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

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.cmd.dynamic.DynamicBlacklistAddCmd;
import com.red.circle.other.app.enums.violation.ViolationTypeEnum;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicBlacklistService;
import com.red.circle.other.infra.database.rds.service.sys.AdministratorAuthService;
import com.red.circle.other.infra.database.rds.service.sys.AdministratorService;
import com.red.circle.other.inner.model.cmd.dynamic.DynamicBlacklistCmd;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 添加黑名单执行器.
 *
 * @author pengshigang
 * @since 2025-01-29
 */
@Component
@RequiredArgsConstructor
public class DynamicBlacklistAddCmdExe {

  private final AdministratorService administratorService;
  private final AdministratorAuthService administratorAuthService;
  private final DynamicBlacklistService dynamicBlacklistService;

  public void execute(DynamicBlacklistAddCmd cmd) {
    // 验证管理员权限
    ViolationTypeEnum violationTypeEnum = ViolationTypeEnum.CREATE_CONTENT;
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION,
        administratorService.existsAdmin(cmd.getReqUserId()) &&
            administratorAuthService.existsAuth(cmd.requiredReqUserId(), violationTypeEnum.name()));

    // 添加黑名单
    DynamicBlacklistCmd blacklistCmd = new DynamicBlacklistCmd();
    blacklistCmd.setUserId(cmd.getUserId());
    blacklistCmd.setReqUserId(cmd.getReqUserId());
    blacklistCmd.setSysOrigin(cmd.getReqSysOrigin().getOrigin());
    dynamicBlacklistService.add(blacklistCmd);
  }

}
