package com.dayou.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.dayou.common.BaseEntity;
import com.dayou.entity.FinanceClaim;
import com.dayou.entity.FinanceRealFund;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.FinanceRealFundMapper;
import com.dayou.service.IFinanceClaimService;
import com.dayou.service.IFinanceRealFundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.LoginContext;
import com.dayou.vo.FinanceRealFundVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 财务实收款 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-12-08
 */
@Service
public class FinanceRealFundServiceImpl extends ServiceImpl<FinanceRealFundMapper, FinanceRealFund> implements IFinanceRealFundService {

    @Autowired
    private FinanceRealFundMapper financeRealFundMapper;

    @Autowired
    private IFinanceClaimService financeClaimService;

    @Override
    @SuppressWarnings("unchecked")
    public Page<FinanceRealFundVO> selectPage(Page page, FinanceRealFund financeRealFund,String keyword){
        Page<FinanceRealFundVO> ret = financeRealFundMapper.getPage(page,financeRealFund,keyword);
        return ret;
    }


    @Override
    public FinanceRealFund detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(FinanceRealFund financeRealFund){
        financeRealFund.setCreatorId(LoginContext.getCurrentUserId());
        return  this.save(financeRealFund);
    }

    @Override
    public Boolean update(FinanceRealFund financeRealFund){
        return  this.updateById(financeRealFund);
    }

    @Override
    public Boolean delete(Long id){
        List<FinanceClaim> list = financeClaimService.list(new LambdaQueryWrapper<FinanceClaim>().eq(FinanceClaim::getRealFundId, id).eq(BaseEntity::getDeleted, Boolean.FALSE));
        if (CollectionUtil.isNotEmpty(list)){
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"此笔实收款，已有订单认领金额，无法删除。");
            return Boolean.FALSE;
        }
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public BigDecimal getNotClaimAmount(Long realFundId) {
        BigDecimal claimAmount = financeRealFundMapper.getNotClaimAmount(realFundId);
        return claimAmount;
    }
}
