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

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.other.infra.database.mongo.entity.game.trumpet.GameTrumpetCount;
import com.red.circle.other.infra.database.mongo.entity.game.trumpet.query.GameTrumpetQuery;
import com.red.circle.other.infra.database.mongo.service.game.GameTrumpetCountService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Objects;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 喇叭 前端控制器.
 * </p>
 *
 * @author pengshigang
 * @since 2022-8-9
 */
@Service
@RequiredArgsConstructor
public class GameTrumpetCountServiceImpl implements GameTrumpetCountService {

  private final MongoTemplate mongoTemplate;

  @Override
  public void add(GameTrumpetCount gameBarrageCount) {
    mongoTemplate.save(gameBarrageCount);
  }

  @Override
  public List<GameTrumpetCount> listLatestTopSize(String sysOrigin, List<String> languages,
      Integer topSize) {

    Criteria criteria = new Criteria();
    if (StringUtils.isNotBlank(sysOrigin)) {
      criteria.and("sysOrigin").is(sysOrigin);
    }
    criteria.and("del").ne(Boolean.TRUE);

    if (CollectionUtils.isNotEmpty(languages)) {
      criteria.and("language").in(languages);
    }

    return mongoTemplate.find(Query.query(criteria)
            .with(Sort.by(Sort.Order.desc("createTime")))
            .limit(topSize),
        GameTrumpetCount.class);
  }

  @Override
  public void deleteById(Long id, Long updateUser) {

    mongoTemplate.updateFirst(Query.query(Criteria.where("id").is(id)),
        new Update()
            .set("updateUser", updateUser)
            .set("updateTime", TimestampUtils.now())
            .set("del", Boolean.TRUE),
        GameTrumpetCount.class);
  }

  @Override
  public List<GameTrumpetCount> list(GameTrumpetQuery query) {

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

    if (Objects.nonNull(query.getLastId())) {
      criteria.and("id").lt(query.getLastId());
    }
    if (Objects.nonNull(query.getUserId())) {
      criteria.and("userId").is(query.getUserId());
    }
    if (Boolean.TRUE.equals(query.getDel())) {
      criteria.and("del").is(Boolean.TRUE);
    } else {
      criteria.and("del").ne(Boolean.TRUE);
    }

    if (Objects.isNull(query.getLimit())) {
      query.setLimit(PageConstant.DEFAULT_LIMIT_SIZE);
    }
    return mongoTemplate.find(Query.query(criteria)
            .with(Sort.by(Sort.Order.desc("id")))
            .limit(query.getLimit())
        , GameTrumpetCount.class);
  }

}
