package com.red.circle.other.infra.database.mongo.service.team.team.impl;

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamApplicationProcessApproval;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamApplicationProcessApprovalService;
import com.red.circle.other.inner.model.cmd.team.TeamApplicationProcessApprovalQryCmd;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * 团队申请流程日志.
 *
 * @author pengshigang on 2022/12/19
 */
@Service
@RequiredArgsConstructor
public class TeamApplicationProcessApprovalServiceImpl implements
    TeamApplicationProcessApprovalService {

  private final MongoTemplate mongoTemplate;

  @Override
  public TeamApplicationProcessApproval add(TeamApplicationProcessApproval param) {
    if (Objects.isNull(param)) {
      throw new IllegalArgumentException("add param isNull");
    }
    if (Objects.isNull(param.getExpiredTime())) {
      param.setExpiredTime(TimestampUtils.nowPlusDays(60));
    }
    param.setId(IdWorkerUtils.getId());
    return mongoTemplate.save(param);
  }

  @Override
  public void addBatch(List<TeamApplicationProcessApproval> params) {
    if (CollectionUtils.isEmpty(params)) {
      throw new IllegalArgumentException("addBatch param isEmpty");
    }

    mongoTemplate.insert(params.stream().peek(param -> {
      param.setId(IdWorkerUtils.getId());
      if (Objects.isNull(param.getExpiredTime())) {
        param.setId(IdWorkerUtils.getId());
        param.setExpiredTime(TimestampUtils.nowPlusDays(60));
      }
    }).collect(Collectors.toList()), TeamApplicationProcessApproval.class);
  }

  @Override
  public List<TeamApplicationProcessApproval> listByCondition(
      TeamApplicationProcessApprovalQryCmd query) {

    Criteria criteria = Criteria.where("sysOrigin").is(query.getSysOrigin());

    if (Objects.nonNull(query.getAssociateId())) {
      criteria.and("associateId").is(query.getAssociateId());
    }

    if (Objects.nonNull(query.getReason())) {
      criteria.and("reason").is(query.getReason());
    }

    if (Objects.nonNull(query.getBeProcessUserId())) {
      criteria.and("beProcessUserId").is(query.getBeProcessUserId());
    }

    if (Objects.nonNull(query.getType())) {
      criteria.and("type").is(query.getType());
    }

    if (Objects.nonNull(query.getLastId())) {
      criteria.and("id").lt(query.getLastId());
    }

    return mongoTemplate.find(Query.query(criteria)
            .with(Sort.by(Sort.Order.desc("id")))
            .limit(PageConstant.DEFAULT_LIMIT_SIZE),
        TeamApplicationProcessApproval.class);

  }

  @Override
  public void removeBatchByAssociateId(List<Long> associateIds) {
    mongoTemplate.remove(Query.query(Criteria.where("associateId").in(associateIds)),
        TeamApplicationProcessApproval.class);
  }

}
