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

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.cmd.dynamic.DynamicReportCmd;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicContent;
import com.red.circle.other.infra.database.rds.entity.dynamic.DynamicReport;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicContentService;
import com.red.circle.other.infra.database.rds.service.dynamic.DynamicReportService;
import com.red.circle.other.inner.asserts.DynamicErrorCode;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 投诉动态.
 * </p>
 *
 * @author pengshigang
 * @since 2022-04-18
 */
@Component
@RequiredArgsConstructor
public class DynamicReportCmdExe {

  private final DynamicReportService dynamicReportService;
  private final DynamicContentService dynamicContentService;

  public void execute(DynamicReportCmd cmd) {

    DynamicContent content = dynamicContentService.getById(cmd.getDynamicContentId());
    ResponseAssert.notNull(DynamicErrorCode.CONTENT_IS_NULL, content);
    ResponseAssert.isFalse(DynamicErrorCode.CONTENT_DELETE, content.getDel());

    ResponseAssert.isFalse(DynamicErrorCode.REPEAT_COMPLAINT_ERROR,
        dynamicReportService.exist(cmd.getDynamicContentId(), cmd.getReqUserId()));

    dynamicReportService.save(new DynamicReport()
        .setApprovalStatus(0)
        .setDynamicContentId(content.getId())
        .setReportedUserId(content.getCreateUser())
        .setReportUserId(cmd.getReqUserId())
        .setReportType(cmd.getReportType())
        .setSysOrigin(cmd.getReqSysOrigin().getOrigin())
        .setCreateTime(TimestampUtils.now())
        .setCreateUser(cmd.getReqUserId())
    );
  }

}
