package com.ruoyi.ss.balancePeriod.service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.balancePeriod.domain.SmBalancePeriod;
import com.ruoyi.ss.balancePeriod.domain.SmBalancePeriodQuery;
import com.ruoyi.ss.balancePeriod.mapper.SmBalancePeriodMapper;
import com.ruoyi.dashboard.domain.vo.BillCountVo;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillGroupBy;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus;
import com.ruoyi.ss.transactionBill.mapper.TransactionBillMapper;
import com.ruoyi.ss.user.domain.SmUserQuery;
import com.ruoyi.ss.user.mapper.SmUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 余额时段记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-16
 */
@Service("smBalancePeriodService")
public class SmBalancePeriodServiceImpl implements ISmBalancePeriodService
{
    @Autowired
    private SmBalancePeriodMapper smBalancePeriodMapper;

    @Autowired
    private SmUserMapper smUserMapper;

    @Autowired
    private TransactionBillMapper transactionBillMapper;

    /**
     * 查询余额时段记录
     *
     * @param id 余额时段记录主键
     * @return 余额时段记录
     */
    @Override
    public SmBalancePeriod selectSmBalancePeriodById(Long id)
    {
        return smBalancePeriodMapper.selectSmBalancePeriodById(id);
    }

    /**
     * 查询余额时段记录列表
     *
     * @param smBalancePeriod 余额时段记录
     * @return 余额时段记录
     */
    @Override
    public List<SmBalancePeriod> selectSmBalancePeriodList(SmBalancePeriodQuery smBalancePeriod)
    {
        return smBalancePeriodMapper.selectSmBalancePeriodList(smBalancePeriod);
    }

    /**
     * 新增余额时段记录
     *
     * @param smBalancePeriod 余额时段记录
     * @return 结果
     */
    @Override
    public int insertSmBalancePeriod(SmBalancePeriod smBalancePeriod)
    {
        smBalancePeriod.setCreateTime(DateUtils.getNowDate());
        return smBalancePeriodMapper.insertSmBalancePeriod(smBalancePeriod);
    }

    /**
     * 修改余额时段记录
     *
     * @param smBalancePeriod 余额时段记录
     * @return 结果
     */
    @Override
    public int updateSmBalancePeriod(SmBalancePeriod smBalancePeriod)
    {
        return smBalancePeriodMapper.updateSmBalancePeriod(smBalancePeriod);
    }

    /**
     * 批量删除余额时段记录
     *
     * @param ids 需要删除的余额时段记录主键
     * @return 结果
     */
    @Override
    public int deleteSmBalancePeriodByIds(Long[] ids)
    {
        return smBalancePeriodMapper.deleteSmBalancePeriodByIds(ids);
    }

    /**
     * 删除余额时段记录信息
     *
     * @param id 余额时段记录主键
     * @return 结果
     */
    @Override
    public int deleteSmBalancePeriodById(Long id)
    {
        return smBalancePeriodMapper.deleteSmBalancePeriodById(id);
    }



    @Override
    @Transactional
    public void recordCurrentBalance() {
        Date now = new Date();
        SmBalancePeriod data = new SmBalancePeriod();

        // 用户总余额
        data.setUserBalance(smUserMapper.selectBalanceCount(new SmUserQuery()));

        // 平台总余额
        List<BillCountVo> platFormBillCount = transactionBillMapper.selectCount(new TransactionBillQuery());
        data.setPlatformBalance(CollectionUtils.isEmpty(platFormBillCount) ? BigDecimal.ZERO : platFormBillCount.get(0).getServiceCharge());

        // 充值
        TransactionBillQuery dto = new TransactionBillQuery();
        dto.setCreateDate(now);
        dto.setHour(now.getHours());
        dto.setGroupBy(TransactionBillGroupBy.create_hour.name());
        dto.setStatusList(TransactionBillStatus.payedOrder());
        List<BillCountVo> billCount = transactionBillMapper.selectCount(dto);
        data.setRecharge(CollectionUtils.isEmpty(billCount) ? BigDecimal.ZERO : billCount.get(0).getRechargeAmount());

        data.setCreateTime(now);
        smBalancePeriodMapper.insertSmBalancePeriod(data);
    }

    @Override
    public boolean isExist(Date date, Integer hour) {
        SmBalancePeriodQuery balanceDto = new SmBalancePeriodQuery();
        balanceDto.setCreateDate(date);
        balanceDto.setCreateHour(hour);
        List<SmBalancePeriod> smBalancePeriods = smBalancePeriodMapper.selectSmBalancePeriodList(balanceDto);
        return !CollectionUtils.isEmpty(smBalancePeriods);
    }
}
