package com.red.circle.mq.business.model.event.approval;


import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.common.business.core.enums.DataApprovalTypeEnum;
import com.red.circle.common.business.core.enums.OpUserType;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.experimental.Accessors;


/**
 * @author pengliang on 2020/9/21
 */
@Data
@Accessors(chain = true)
public class ApprovalEvent implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 审批id.
   */
  private Long approvalId;

  /**
   * 操作审批人.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private Long opsUserId;

  /**
   * 操作审批人类型.
   */
  private OpUserType opUserType;

  /**
   * 违规类型.
   */
  private DataApprovalTypeEnum approvalType;

  /**
   * 审批状态.
   */
  private ApprovalStatusEnum approvalStatus;

  /**
   * 待审批审批信息.
   */
  private List<ApprovalContent> waitApprovalUser;


  public List<Long> waitApprovalUserIds() {
    return Optional.ofNullable(waitApprovalUser)
        .map(beApprovalList -> beApprovalList.stream().map(ApprovalContent::getUserId).distinct()
            .toList())
        .orElse(null);
  }

  public Set<Long> waitApprovalUserIdsSet() {
    return Optional.ofNullable(waitApprovalUser)
        .map(beApprovalList -> beApprovalList.stream().map(ApprovalContent::getUserId)
            .collect(Collectors.toSet()))
        .orElse(null);
  }

  public List<Long> waitApprovalUserContentIds() {
    return Optional.ofNullable(waitApprovalUser)
        .map(beApprovalList -> beApprovalList.stream().map(ApprovalContent::getContentId)
            .distinct().collect(Collectors.toList()))
        .orElse(null);
  }

  public boolean approvalPass() {
    return Objects.equals(approvalStatus, ApprovalStatusEnum.PASS);
  }

  public boolean approvalNotPass() {
    return Objects.equals(approvalStatus, ApprovalStatusEnum.NOT_PASS);
  }

}
