package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.dto.admin.incomeStatis.AccountCashCountDTO;
import com.ruoyi.cai.dto.admin.incomeStatis.RechargeOrderCountDTO;
import com.ruoyi.cai.dto.admin.incomeStatis.VipOrderCountDTO;
import com.ruoyi.cai.domain.DayIncomeStatistics;
import com.ruoyi.cai.mapper.AccountCashMapper;
import com.ruoyi.cai.mapper.DayIncomeStatisticsMapper;
import com.ruoyi.cai.mapper.RechargeOrderMapper;
import com.ruoyi.cai.mapper.VipOrderMapper;
import com.ruoyi.cai.service.DayIncomeStatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 每日账单统计Service业务层处理
 *
 * @author 77
 * @date 2024-01-22
 */
@Service
public class DayIncomeStatisticsServiceImpl extends ServiceImpl<DayIncomeStatisticsMapper,DayIncomeStatistics> implements DayIncomeStatisticsService {

    @Resource
    private RechargeOrderMapper rechargeOrderMapper;
    @Resource
    private VipOrderMapper vipOrderMapper;
    @Resource
    private AccountCashMapper accountCashMapper;

    @Override
    public void refreshByDate(LocalDate date){
        LocalDateTime startTime = date.atTime(LocalTime.MIN);
        LocalDateTime endTime = date.atTime(LocalTime.MAX);
        DayIncomeStatistics one = this.getOne(Wrappers.lambdaQuery(DayIncomeStatistics.class).eq(DayIncomeStatistics::getDate, date));
        if(one == null){
            ReentrantLock lock = new ReentrantLock();
            try {
                one = this.getOne(Wrappers.lambdaQuery(DayIncomeStatistics.class).eq(DayIncomeStatistics::getDate, date));
                if(one == null){
                    one = new DayIncomeStatistics();
                    one.setDate(date);
                    this.save(one);
                    lock.lock();
                }
            }finally {
                lock.unlock();
            }
        }
        RechargeOrderCountDTO recharge = rechargeOrderMapper.incomeStatistics(startTime,endTime);
        VipOrderCountDTO vip = vipOrderMapper.incomeStatistics(startTime,endTime);
        AccountCashCountDTO cash = accountCashMapper.incomeStatistics(startTime,endTime);
        DayIncomeStatistics update = new DayIncomeStatistics();
        update.setId(one.getId());
        if(recharge != null){
            update.setExpInMoney(recharge.getExpInMoney()==null? BigDecimal.ZERO:recharge.getExpInMoney());
            update.setOrderCount(recharge.getOrderCount()==null?0:recharge.getOrderCount());
            update.setModifyCoinAdd(recharge.getModifyCoinAdd()==null?0:recharge.getModifyCoinAdd());
            update.setModifyCoinSub(recharge.getModifyCoinSub()==null?0:recharge.getModifyCoinSub());
            update.setModifyIncomeAdd(recharge.getModifyIncomeAdd()==null?0:recharge.getModifyIncomeAdd());
            update.setModifyIncomeSub(recharge.getModifyIncomeSub()==null?0:recharge.getModifyIncomeSub());
        }
        if(cash != null){
            update.setOutMoney(cash.getCashMoney()==null?BigDecimal.ZERO:cash.getCashMoney());
            update.setCashCount(cash.getCashCount()==null?0:cash.getCashCount());
        }
        if(vip != null){
            update.setVipInMoney(vip.getVipMoney()==null?BigDecimal.ZERO:vip.getVipMoney());
            update.setVipCount(vip.getVipCount()==null?0:vip.getVipCount());
        }
        update.setUpdateTime(LocalDateTime.now());
        this.updateById(update);
    }

}
