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

import com.red.circle.other.infra.database.mongo.entity.team.team.TeamImportanceLog;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamImportanceLogService;
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.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

/**
 * 团队关键日志.
 *
 * @author pengshigang on 2023/4/12
 */
@Service
@RequiredArgsConstructor
public class TeamImportanceLogServiceImpl implements TeamImportanceLogService {

  private final MongoTemplate mongoTemplate;

  @Override
  public void add(TeamImportanceLog param) {
    param.setId(IdWorkerUtils.getId());
    if (Objects.isNull(param.getExpiredTime())) {
      param.setExpiredTime(TimestampUtils.nowPlusDays(60));
    }
    param.setCreateTime(TimestampUtils.now());
    mongoTemplate.save(param);
  }

  @Override
  public void addBatch(List<TeamImportanceLog> 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()), TeamImportanceLog.class);
  }

}
