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

import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.common.business.core.enums.DynamicEventTypeEnum;
import com.red.circle.common.business.dto.cmd.IdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.mq.business.model.event.dynamic.DynamicEvent;
import com.red.circle.mq.rocket.business.producer.DynamicMqMessage;
import com.red.circle.other.app.enums.violation.ViolationTypeEnum;
import com.red.circle.other.infra.database.mongo.service.dynamic.DynamicPopularService;
import com.red.circle.other.infra.database.rds.entity.approval.ApprovalDynamicContent;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicContent;
import com.red.circle.other.infra.database.rds.service.approval.ApprovalDynamicContentService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicContentService;
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.asserts.DynamicErrorCode;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 发布者删除自己的动态内容.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-11
 */
@Component
@RequiredArgsConstructor
public class DynamicContentDelCmdExe {

  private final DynamicMqMessage dynamicMqMessage;
  private final DynamicContentService dynamicContentService;
  private final DynamicPopularService dynamicPopularService;
  private final ApprovalDynamicContentService approvalDynamicContentService;
  private final AdministratorService administratorService;
  private final AdministratorAuthService administratorAuthService;

  public void execute(IdLongCmd cmd) {

    DynamicContent dynamicContent = dynamicContentService.getById(cmd.getId());
    ResponseAssert.notNull(DynamicErrorCode.CONTENT_IS_NULL, dynamicContent);

    boolean isOwn = Objects.equals(dynamicContent.getCreateUser(), cmd.getReqUserId());
    boolean isAdminAuth = administratorService.existsAdmin(cmd.getReqUserId()) &&
            administratorAuthService.existsAuth(cmd.requiredReqUserId(), ViolationTypeEnum.DELETE_CONTENT.name());
    ResponseAssert.isTrue(CommonErrorCode.INSUFFICIENT_PERMISSION, isOwn || isAdminAuth);

    // 管理员删除别人的 送入待审核
    if (isAdminAuth && !isOwn) {
      ApprovalDynamicContent approvalDynamicContent = new ApprovalDynamicContent();
      approvalDynamicContent.setUserId(cmd.getReqUserId());
      approvalDynamicContent.setDynamicId(dynamicContent.getId());
      approvalDynamicContent.setApproveStatus(ApprovalStatusEnum.PENDING.name());
      approvalDynamicContent.setSysOrigin(dynamicContent.getSysOrigin());
      approvalDynamicContent.setCreateTime(TimestampUtils.now());
      approvalDynamicContent.setCreateUser(cmd.getReqUserId());
      approvalDynamicContentService.save(approvalDynamicContent);
      return;
    }


    dynamicContent.setDel(Boolean.TRUE);
    dynamicContent.setUpdateUser(cmd.getReqUserId());
    dynamicContentService.updateSelectiveById(dynamicContent);

    dynamicPopularService.removeByDynamicId(dynamicContent.getId());

    //删除审批内容
    approvalDynamicContentService.deleteByDynamicId(dynamicContent.getId());
    //处理删除动态队列
    sendMessage(dynamicContent.getId());

  }

  /**
   * 处理业务
   *
   * @param contentId 动态ID
   */
  private void sendMessage(Long contentId) {
    dynamicMqMessage.sendDynamic(IdWorkerUtils.getIdStr(),
        new DynamicEvent()
            .setDynamicContentId(contentId)
            .setType(DynamicEventTypeEnum.DELETE)
    );

  }


}
