package com.red.circle.wallet.infra.database.rds.service.diamond.impl;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.wallet.infra.database.rds.dao.DiamondRunningWaterDAO;
import com.red.circle.wallet.infra.database.rds.entity.diamond.DiamondRunningWater;
import com.red.circle.wallet.infra.database.rds.service.diamond.DiamondRunningWaterService;
import com.red.circle.wallet.inner.model.cmd.UserDiamondOriginQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserDiamondRunWaterQryCmd;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户钻石流水(20230718) 服务实现类.
 * </p>
 *
 * @author pengshigang on 2023-07-18 16:14
 */
@Service
public class DiamondRunningWaterServiceImpl extends
    BaseServiceImpl<DiamondRunningWaterDAO, DiamondRunningWater>
    implements DiamondRunningWaterService {

  @Override
  public boolean existsId(Long id) {

    return Objects.nonNull(query()
        .select(DiamondRunningWater::getId)
        .eq(DiamondRunningWater::getId, id)
        .last(PageConstant.LIMIT_ONE)
        .getOne());
  }

  @Override
  public void add(DiamondRunningWater runningWater) {

    runningWater.setId(IdWorkerUtils.getId());
    runningWater.setCreateTime(TimestampUtils.now());
    runningWater.setCreateUser(runningWater.getUserId());
    super.save(runningWater);
  }

  @Override
  public List<DiamondRunningWater> listUserLatestOneMonths(Long userId, Integer type, Integer size,
      Long lastId) {

    return query()
        .eq(Objects.nonNull(type), DiamondRunningWater::getType, type)
        .eq(Objects.nonNull(userId), DiamondRunningWater::getUserId, userId)
        .lt(Objects.nonNull(lastId), DiamondRunningWater::getId, lastId)
        .orderByDesc(DiamondRunningWater::getId)
        .last(PageConstant.formatLimit(size))
        .list();
  }


  @Override
  public PageResult<DiamondRunningWater> pageUserDiamondRunWater(UserDiamondRunWaterQryCmd query) {

    return query()
        .eq(Objects.nonNull(query.getUserId()), DiamondRunningWater::getOriginUserId,
            query.getUserId())
        .eq(Objects.nonNull(query.getType()), DiamondRunningWater::getType, query.getType())
        .eq(StringUtils.isNotBlank(query.getSysOrigin()), DiamondRunningWater::getSysOrigin,
            query.getSysOrigin())
        .orderByDesc(DiamondRunningWater::getCreateTime)
        .page(query.getPageQuery());
  }

  @Override
  public PageResult<DiamondRunningWater> pageUserDiamondOriginDetails(
      UserDiamondOriginQryCmd query) {
    return query()
        .eq(Objects.nonNull(query.getUserId()), DiamondRunningWater::getUserId, query.getUserId())
        .eq(StringUtils.isNotBlank(query.getOrigin()), DiamondRunningWater::getOrigin,
            query.getOrigin())
        .ge(Objects.nonNull(query.startTimeToLocalDateTime()), DiamondRunningWater::getCreateTime,
            query.startTimeToLocalDateTime())
        .le(Objects.nonNull(query.endTimeToLocalDateTime()), DiamondRunningWater::getCreateTime,
            query.endTimeToLocalDateTime())
        .orderByDesc(DiamondRunningWater::getCreateTime)
        .page(query.getPageQuery());
  }

}
