package com.cf.imes.module.executor.service.funds.bill;


import cn.hutool.core.collection.CollUtil;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.executor.controller.admin.funds.balancedetails.vo.BalanceDetailsPageReqVO;
import com.cf.imes.module.executor.controller.admin.funds.bill.vo.BillDetailsRespVO;
import com.cf.imes.module.executor.dal.dataobject.funds.balancedetails.IncomeExpenseDetailsDO;
import com.cf.imes.module.executor.dal.mysql.funds.balancedetails.IncomeExpenseDetailsMapper;
import com.cf.imes.module.executor.enums.amount.IncomeExpenseTypeEnum;
import com.cf.imes.module.executor.enums.amount.TradeTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * @author 账单/总额明细接口 实现类
 */
@Slf4j
@Service
public class BillServiceImpl implements BillService{


    @Resource
    private IncomeExpenseDetailsMapper incomeExpenseDetailsMapper;

    @Override
    public PageResult<BillDetailsRespVO> getBillDetailsPage(BalanceDetailsPageReqVO pageReqVO) {

        PageResult<IncomeExpenseDetailsDO> pageResult = incomeExpenseDetailsMapper.selectDetailsPage(pageReqVO,null);

        if(CollUtil.isNotEmpty(pageResult.getList())){
            return new PageResult<>();
        }

        PageResult<BillDetailsRespVO> result = BeanUtils.toBean(pageResult, BillDetailsRespVO.class);

        result.getList().forEach(f->{
            // 收支类型
            f.setIncomeExpenseType(IncomeExpenseTypeEnum.EXPENSES.getType());
            // 支出账户余额
            f.setExpenditureAccountBalance(f.getAvailableAmount());
            if(f.getTradeType().equals(TradeTypeEnum.INCOME.getType())){
                // 充值金额
                f.setRechargeAmount(f.getCashAmountChange());
                // 支出现金
                f.setCashExpenditure(f.getCashAmountChange());
                // 实际支出总费用
                f.setActualTotalExpenditure(f.getCashAmountChange());

            }else if(f.getTradeType().equals(TradeTypeEnum.EXPENSES.getType())){
                // 产品金额
                f.setProductAmount(f.getCashAmountChange().add(f.getBonusAmountChange()));
                // 支出现金
                f.setCashExpenditure(f.getCashAmountChange());
                // 实际支出总费用
                f.setActualTotalExpenditure(f.getCashAmountChange().add(f.getBonusAmountChange()));

            }
        });

        return result;

    }



}
