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

import com.mongodb.MongoWriteException;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.other.infra.database.mongo.entity.game.pk.GameRoomPkCount;
import com.red.circle.other.infra.database.mongo.entity.game.pk.GameTeamPkCount;
import com.red.circle.other.infra.database.mongo.entity.game.pk.query.GameRoomPkCountQuery;
import com.red.circle.other.infra.database.mongo.entity.game.pk.query.GameTeamPkCountQuery;
import com.red.circle.other.infra.database.mongo.service.game.GamePkCountService;
import com.red.circle.other.inner.model.cmd.game.GameRoomPkRecordQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameTeamPkRecordQryCmd;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
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;

/**
 * <p>
 * 房间PK 前端控制器.
 * </p>
 *
 * @author pengshigang
 * @since 2022-9-14
 */
@Service
@RequiredArgsConstructor
public class GamePkCountServiceImpl implements GamePkCountService {

  private final MongoTemplate mongoTemplate;

  @Override
  public void addRoomPk(GameRoomPkCount count) {
    if (mongoTemplate.exists(Query.query(Criteria.where("id").is(count.getId())),
        GameRoomPkCount.class)) {
      return;
    }
    try {
      mongoTemplate.save(count);
    } catch (DuplicateKeyException | MongoWriteException ex) {
      // ignore
    }
  }

  @Override
  public List<GameRoomPkCount> listRoomPk(GameRoomPkRecordQryCmd query) {

    Criteria criteria = new Criteria();
    if (Objects.nonNull(query.getLastId())) {
      criteria.and("id").lt(query.getLastId());
    }
    if (StringUtils.isNotBlank(query.getSysOrigin())) {
      criteria.and("sysOrigin").is(query.getSysOrigin());
    }
    if (Objects.nonNull(query.getSponsorUserId())) {
      criteria.and("sponsorUserId").is(query.getSponsorUserId());
    }
    if (Objects.nonNull(query.getSponsorRoomId())) {
      criteria.and("sponsorRoomId").is(query.getSponsorRoomId());
    }
    if (Objects.nonNull(query.getRecipientUserId())) {
      criteria.and("recipientUserId").is(query.getRecipientUserId());
    }
    if (Objects.nonNull(query.getRecipientRoomId())) {
      criteria.and("recipientRoomId").is(query.getRecipientRoomId());
    }
    if (StringUtils.isNotBlank(query.getPkType())) {
      criteria.and("pkType").is(query.getPkType());
    }

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

  @Override
  public void addTeamPk(GameTeamPkCount count) {
    if (mongoTemplate.exists(Query.query(Criteria.where("id").is(count.getId())),
        GameTeamPkCount.class)) {
      return;
    }
    mongoTemplate.save(count);
  }

  @Override
  public List<GameTeamPkCount> listTeamPk(GameTeamPkRecordQryCmd query) {

    Criteria criteria = new Criteria();
    if (Objects.nonNull(query.getLastId())) {
      criteria.and("id").lt(query.getLastId());
    }
    if (StringUtils.isNotBlank(query.getSysOrigin())) {
      criteria.and("sysOrigin").is(query.getSysOrigin());
    }
    if (Objects.nonNull(query.getRoomId())) {
      criteria.and("roomId").is(query.getRoomId());
    }

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

  }
}
