package com.red.circle.console.infra.database.mongo.app.service.bank.impl;

import com.red.circle.console.infra.database.mongo.app.entity.SysUserBankRunningWater;
import com.red.circle.console.infra.database.mongo.app.service.bank.SysUserBankRunningWaterService;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.tool.core.date.TimestampUtils;
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;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户银行卡流水 实现.
 *
 * @author pengliang on 2023/2/15
 */
@Service
@RequiredArgsConstructor
public class SysUserBankRunningWaterServiceImpl implements SysUserBankRunningWaterService {

  private final MongoTemplate mongoTemplate;

  @Override
  public void add(SysUserBankRunningWater runningWater) {
    if (Objects.isNull(runningWater.getCreateTime())) {
      runningWater.setCreateTime(TimestampUtils.now());
    }
    if (Objects.isNull(runningWater.getUpdateTime())) {
      runningWater.setUpdateTime(TimestampUtils.now());
    }
    mongoTemplate.save(runningWater);
  }

  @Override
  public void addBatch(List<SysUserBankRunningWater> runningWaters) {
    mongoTemplate.insert(runningWaters.stream().peek(runningWater -> {
      if (Objects.isNull(runningWater.getCreateTime())) {
        runningWater.setCreateTime(TimestampUtils.now());
      }
      if (Objects.isNull(runningWater.getUpdateTime())) {
        runningWater.setUpdateTime(TimestampUtils.now());
      }
    }).collect(Collectors.toList()), SysUserBankRunningWater.class);
  }

  @Override
  public String getTrackIdById(Long id) {
    Query query = Query.query(Criteria.where("id").is(id));
    query.fields().include("trackId");
    return Optional.ofNullable(
        mongoTemplate.findOne(query, SysUserBankRunningWater.class)
    ).map(SysUserBankRunningWater::getTrackId).orElse(null);
  }

  @Override
  public List<SysUserBankRunningWater> listLast(Long userId, Integer size) {
    return mongoTemplate.find(
        Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.desc("id")))
            .limit(Objects.nonNull(size) ? size : PageConstant.MAX_LIMIT_SIZE),
        SysUserBankRunningWater.class);
  }

  @Override
  public boolean existsTrackId(String trackId) {
    return mongoTemplate.exists(Query.query(Criteria.where("trackId").is(trackId)),
        SysUserBankRunningWater.class);
  }

  @Override
  public boolean existsTrackId(Long userId, String event, String trackId) {
    return mongoTemplate.exists(Query.query(Criteria.where("trackId").is(trackId)
            .and("userId").is(userId)
            .and("event").is(event)),
        SysUserBankRunningWater.class);
  }

}
