package com.cf.imes.module.system.service.funds.incomeandexpense;

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.system.controller.admin.funds.balancedetails.vo.BalanceDetailsPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.balancedetails.vo.BalanceDetailsRespVO;
import com.cf.imes.module.system.dal.dataobject.incomeandexpense.IncomeExpenseDetailsDO;
import com.cf.imes.module.system.dal.mysql.incomeandexpense.IncomeExpenseDetailsMapper;
import com.cf.imes.module.system.enums.pay.IncomeExpenseTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 收支明细接口实现类
 *
 * @author Gqr
 * @since 2025/8/13 10:29
 */
@Service
public class IncomeExpenseServiceImpl implements IncomeExpenseService {
    @Resource
    private IncomeExpenseDetailsMapper incomeExpenseDetailsMapper;

    @Override
    public void createDetail(IncomeExpenseDetailsDO incomeExpenseDetailsDO) {
        incomeExpenseDetailsMapper.insert(incomeExpenseDetailsDO);
    }

    @Override
    public PageResult<BalanceDetailsRespVO> getBalanceDetailsPage(BalanceDetailsPageReqVO pageReqVO) {
        PageResult<IncomeExpenseDetailsDO> pageResult = incomeExpenseDetailsMapper.selectDetailsPage(pageReqVO,null);
        if(CollUtil.isEmpty(pageResult.getList())){
            return new PageResult<>();
        }

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

        // 根据收支类型赋值对应的 支出/收入字段
        result.getList().forEach(f -> {
            if (IncomeExpenseTypeEnum.INCOME.getCode().equals(f.getIncomeExpenseType())) {
                f.setEntryAmount(f.getCashAmountChange());
                f.setEntryGiftAmount(f.getGiftAmountChange());
            } else {
                f.setExpensesAmount(f.getCashAmountChange());
                f.setExpensesGiftAmount(f.getGiftAmountChange());
            }
        });
        return result;
    }
}
