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

import com.red.circle.other.infra.database.mongo.entity.team.team.TeamNotice;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamNoticeService;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.List;
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/15
 */
@Service
@RequiredArgsConstructor
public class TeamNoticeServiceImpl implements TeamNoticeService {

  private final MongoTemplate mongoTemplate;

  @Override
  public List<TeamNotice> listByTeamId(Long teamId) {

    Query query = new Query();
    query.addCriteria(Criteria.where("teamId").is(teamId))
        .with(Sort.by(Sort.Order.desc("createTime")));
    return mongoTemplate.find(query, TeamNotice.class);
  }

  @Override
  public void add(TeamNotice card) {
    card.setExpiredTime(TimestampUtils.nowPlusDays(30));
    mongoTemplate.save(card);
  }

  @Override
  public void deleteById(Long id) {
    mongoTemplate.remove(Query.query(Criteria.where("id").is(id)), TeamNotice.class);
  }

  @Override
  public void removeBatchByTeamId(List<Long> teamIds) {
    mongoTemplate.remove(Query.query(Criteria.where("teamId").in(teamIds)), TeamNotice.class);
  }

}
