package com.red.circle.other.app.inner.service.approval.impl;

import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.common.business.core.enums.DataApprovalTypeEnum;
import com.red.circle.component.mq.MessageEvent;
import com.red.circle.mq.business.model.event.approval.ApprovalContentEvent;
import com.red.circle.mq.business.model.event.approval.ApprovalDynamicEvent;
import com.red.circle.mq.rocket.business.service.MessageSenderService;
import com.red.circle.mq.rocket.business.streams.ConsoleApprovalSink;
import com.red.circle.other.app.inner.service.approval.ApprovalDataInfoClientService;
import com.red.circle.other.infra.database.rds.service.approval.ApprovalUserSettingDataService;
import com.red.circle.other.inner.model.cmd.approval.ApprovalContentDateCmd;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDataCmd;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDateInfoCmd;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 资料审批
 *
 * @author zongpubin on 2024/1/22
 */
@Service
@RequiredArgsConstructor
public class ApprovalDataInfoClientServiceImpl implements ApprovalDataInfoClientService {

  private final MessageSenderService senderService;
  private final ApprovalUserSettingDataService approvalUserSettingDataService;

  @Override
  public void approval(ApprovalDateInfoCmd cmd) {

    approvalUserSettingDataService.updateStatus(new ApprovalDataCmd()
        .setUserIds(getWaitApprovalUserIds(cmd.getWaitApprovalUser()))
        .setApprovalUserId(cmd.getApprovalUserId())
        .setApprovalStatus(cmd.getApprovalStatus())
        .setApprovalType(cmd.getApprovalType())
    );

    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .tag(ConsoleApprovalSink.TAG)
        .body(new ApprovalDynamicEvent()
            .setApprovalType(DataApprovalTypeEnum.valueOf(cmd.getApprovalType()))
            .setOriginType(Boolean.FALSE)
            .setApprovalId(IdWorkerUtils.getId())
            .setApprovalUserId(cmd.getApprovalUserId())
            .setApprovalStatus(ApprovalStatusEnum.valueOf(cmd.getApprovalStatus()))
            .setWaitApprovalUser(
                cmd.getWaitApprovalUser().parallelStream()
                    .map(approvalContent -> new ApprovalContentEvent()
                        .setContent(approvalContent.getContent())
                        .setContentId(approvalContent.getContentId())
                        .setUserId(approvalContent.getUserId())
                        .setTags(approvalContent.getTags())).filter(Objects::nonNull).collect(
                        Collectors.toList()))).build());

  }

  private List<Long> getWaitApprovalUserIds(List<ApprovalContentDateCmd> waitApprovalUser) {

    return Optional.ofNullable(waitApprovalUser)
        .map(beApprovalList -> beApprovalList.stream().map(ApprovalContentDateCmd::getUserId)
            .distinct().collect(Collectors.toList()))
        .orElse(null);
  }
}
