package com.red.circle.order.infra.database.rds.service.count.impl;

import com.red.circle.framework.dto.PageQuery;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.convertor.MybatisConvertor;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.order.infra.database.rds.dao.count.MonthlyRechargeUserDAO;
import com.red.circle.order.infra.database.rds.entity.count.MonthlyRechargeUser;
import com.red.circle.order.infra.database.rds.service.count.MonthlyRechargeUserService;
import com.red.circle.order.inner.model.cmd.UserRechargeMonthQryCmd;
import com.red.circle.order.inner.model.dto.MonthlyRechargeUserDTO;
import com.red.circle.tool.core.date.LocalDateUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 每月充值用户 服务实现类.
 * </p>
 *
 * @author pengshigang
 * @since 2022-05-27
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MonthlyRechargeUserServiceImpl extends
    BaseServiceImpl<MonthlyRechargeUserDAO, MonthlyRechargeUser> implements
    MonthlyRechargeUserService {

  private final MonthlyRechargeUserDAO monthlyRechargeUserDAO;

  @Override
  public List<MonthlyRechargeUserDTO> listMonthlyRecharge(UserRechargeMonthQryCmd query) {
    return monthlyRechargeUserDAO.listMonthlyRecharge(query);
  }

  @Override
  public PageResult<MonthlyRechargeUser> opsPageQuery(PageQuery pageQuery,
      String sysOrigin, Long userId, Integer rechargeDate) {
    return MybatisConvertor.toPageResult(
        monthlyRechargeUserDAO.opsPageQuery(MybatisConvertor.toPage(pageQuery), sysOrigin,
            userId, rechargeDate)
    );
  }

  @Override
  public PageResult<MonthlyRechargeUser> opsPageQuery(PageQuery pageQuery,
      Long userId) {
    return query()
        .eq(MonthlyRechargeUser::getUserId, userId)
        .orderByDesc(MonthlyRechargeUser::getRechargeDate)
        .page(pageQuery);
  }

  @Override
  public void incAmount(Long userId, BigDecimal amount) {

    int nowThisMonth = nowThisMonth();

    monthlyRechargeUserDAO.saveIfAbsent(new MonthlyRechargeUser()
        .setId(IdWorkerUtils.getId())
        .setUserId(userId)
        .setRechargeDate(nowThisMonth)
    );

    update()
        .setSql("amount=amount+" + amount)
        .eq(MonthlyRechargeUser::getRechargeDate, nowThisMonth)
        .eq(MonthlyRechargeUser::getUserId, userId)
        .execute();
  }

  @Override
  public void decr(Long userId, BigDecimal amount) {

    MonthlyRechargeUser rechargeUser = query()
        .eq(MonthlyRechargeUser::getUserId, userId)
        .eq(MonthlyRechargeUser::getRechargeDate, nowThisMonth())
        .last(PageConstant.LIMIT_ONE)
        .getOne();

    if (Objects.isNull(rechargeUser)) {
      return;
    }

    update()
        .setSql("amount=amount-" + amount)
        .eq(MonthlyRechargeUser::getId, rechargeUser.getId())
        .last(PageConstant.LIMIT_ONE)
        .execute();


  }

  private int nowThisMonth() {
    return LocalDateUtils.nowThisMonthToInteger();
  }


}
