package com.red.circle.mq.rocket.business.producer;


import com.red.circle.component.mq.MessageEvent;
import com.red.circle.component.mq.service.FixedTopic;
import com.red.circle.mq.business.model.event.team.TeamBillSettleEvent;
import com.red.circle.mq.business.model.event.team.TeamExchangeSurplusTargetEvent;
import com.red.circle.mq.business.model.event.team.TeamSalaryCountEvent;
import com.red.circle.mq.business.model.event.team.TeamSalaryPaymentEvent;
import com.red.circle.mq.business.model.event.team.TeamSalaryRevokeEvent;
import com.red.circle.mq.rocket.business.service.MessageSenderService;
import com.red.circle.mq.rocket.business.streams.TeamBillSettleSink;
import com.red.circle.mq.rocket.business.streams.TeamExchangeSurplusTargetSink;
import com.red.circle.mq.rocket.business.streams.TeamSalaryCountSink;
import com.red.circle.mq.rocket.business.streams.TeamSalaryPaymentSink;
import com.red.circle.mq.rocket.business.streams.TeamSalaryRevokeSink;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.DateFormatConstant;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.sql.Timestamp;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 团队工资实时MQ.
 *
 * @author pengshigang on 2023/4/10
 */
@Component
@RequiredArgsConstructor
public class
TeamSalaryMqMessage {

  private final MessageSenderService senderService;

  /**
   * 团队工资实时统计 - 业务处理.
   */
  public void sendSalaryCount(Set<Long> teamIds) {

    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .topic(FixedTopic.RC_DEFAULT_APP_ORDINARY)
        .tag(TeamSalaryCountSink.TAG)
        .body(new TeamSalaryCountEvent()
            .setTeamIds(teamIds)
            .setTimestamp(TimestampUtils.now())
        )
        .build());
  }

  /**
   * 团队工资实时发送 - 业务处理.
   */
  public void sendSalary(Set<Long> teamIds, Boolean thisMonthFirstDay) {

    if (CollectionUtils.isEmpty(teamIds)) {
      return;
    }

    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .topic(FixedTopic.RC_DEFAULT_APP_ORDINARY)
        .tag(TeamSalaryPaymentSink.TAG)
        .body(new TeamSalaryPaymentEvent()
            .setTeamIds(teamIds)
            .setLastMonth(Integer.parseInt(ZonedDateTimeAsiaRiyadhUtils.now().minusMonths(1)
                .format(DateTimeFormatter.ofPattern(DateFormatConstant.yyyyMM))))
            .setThisMonthFirstDay(thisMonthFirstDay)
            .setTimestamp(TimestampUtils.now())
        )
        .build());
  }

  /**
   * 用户退款处理团队工资问题 - 业务处理.
   *
   * @param mapAcceptAnchors list 主播数据(key 主播， val 退款涉及目标数量)
   * @param purchaseDate     订单支付时间.
   * @param refundUserId     退款用户ID.
   */
  public void recycleSalary(List<Map<Long, Long>> mapAcceptAnchors, Timestamp purchaseDate,
      String sysOrigin, Long refundUserId) {

    if (CollectionUtils.isEmpty(mapAcceptAnchors) || Objects.isNull(purchaseDate) || Objects
        .isNull(refundUserId)) {
      return;
    }

    mapAcceptAnchors.forEach(acceptMap ->
        senderService.sendEventMessage(MessageEvent.builder()
            .consumeId(IdWorkerUtils.getIdStr())
            .topic(FixedTopic.RC_DEFAULT_APP_ORDINARY)
            .tag(TeamSalaryRevokeSink.TAG)
            .body(new TeamSalaryRevokeEvent()
                .setPurchaseDate(purchaseDate)
                .setAnchorMap(acceptMap)
                .setSysOrigin(sysOrigin)
                .setRefundUserId(refundUserId)
                .setTimestamp(TimestampUtils.now())
            )
            .build()));

  }

  /**
   * 超出政策后的剩余目标兑换美元 - 业务处理.
   */
  public void sendExchangeSurplusTarget(Set<Long> teamIds, Integer lastMonth, String code) {

    if (CollectionUtils.isEmpty(teamIds)) {
      return;
    }

    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .topic(FixedTopic.RC_DEFAULT_APP_ORDINARY)
        .tag(TeamExchangeSurplusTargetSink.TAG)
        .body(new TeamExchangeSurplusTargetEvent()
            .setTeamIds(teamIds)
            .setCode(code)
            .setLastMonth(lastMonth)
            .setTimestamp(TimestampUtils.now())
        )
        .build());
  }

  /**
   * 团队账单结算.
   */
  public void teamBillSettle(TeamBillSettleEvent event) {
    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(Objects.toString(event.getTeamId()))
        .topic(FixedTopic.RC_DEFAULT_APP_ORDINARY)
        .tag(TeamBillSettleSink.TAG)
        .body(event)
        .build());
  }
}
