package com.jeeplus.test.reimbursementsys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.DictUtils;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.reimbursementsys.domain.dto.*;
import com.jeeplus.test.reimbursementsys.mapper.ReimbursementBusinessMapper;
import com.jeeplus.test.reimbursementsys.mapper.ReimbursementSysMapper;
import com.jeeplus.test.reimbursementsys.mapper.ReimbursementUserMapper;
import com.jeeplus.test.reimbursementsys.mapper.WuHanReimbursementSysMapper;
import com.jeeplus.test.reimbursementsys.service.ReimbursementBusinessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author li-peike
 * @version 1.0.0
 * @ClassName ReimbursementSysServiceImpl.java
 * @Description TODO
 * @createTime 2022年05月06日 10:38:00
 */
@Service
public class ReimbursementBusinessServiceImpl implements ReimbursementBusinessService {

    @Resource
    private ReimbursementBusinessMapper mapper;
    @Resource
    private ReimbursementSysMapper reimbursementSysMapper;
    @Resource
    private WuHanReimbursementSysMapper wuHanReimbursementSysMapper;
    @Resource
    private ReimbursementUserMapper reimbursementUserMapper;


    @Override
    public ReimbursementBusinessDTO queryById(String id) {
        return mapper.queryById(id);
    }

    @Override
    public ReimbursementDTO getBusinessById(String id) {
        ReimbursementDTO reimbursement = new ReimbursementDTO();
        reimbursement.setBusinessCodeId(id);
        ReimbursementBusinessDTO reimbursementBusiness = new ReimbursementBusinessDTO();
        reimbursementBusiness.setBusinessCodeId(id);
        reimbursementBusiness.setReimbursementAddress("0");
        //根据业务id查询已收款的发票信息收款总额
        String getReceiptInvoiceByReimbursementId = reimbursementSysMapper.getReceiptInvoiceIncomeByReimbursementId(id);
        BigDecimal income = new BigDecimal(getReceiptInvoiceByReimbursementId);    //发票收入
        //根据业务id查询报销比例
        String getReimbursementRatioByReimbursementId = reimbursementSysMapper.getReimbursementRatioByReimbursementId(id);
        if(StringUtils.isBlank(getReimbursementRatioByReimbursementId)){
            getReimbursementRatioByReimbursementId = "0";
            reimbursement.setReimbursementRatio("0");
        }
        BigDecimal reimbursementRatio = new BigDecimal(getReimbursementRatioByReimbursementId);    //报销比例
        BigDecimal canReimbursementAmount = income.multiply(reimbursementRatio).divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);    //该项目可报销额度计算
        //查询该业务总报销额度
        String alreadyReimbursementQuotaByBusinessCodeId = mapper.getAlreadyReimbursementQuotaByBusinessCodeId(reimbursementBusiness);
        BigDecimal allAlreadyReimbursementQuota = new BigDecimal(alreadyReimbursementQuotaByBusinessCodeId);    //总报销额度
        reimbursement.setAllAlreadyReimbursementQuota(allAlreadyReimbursementQuota.toString());
        reimbursement.setCanReimbursementAmount(canReimbursementAmount.toString());
        reimbursement.setSurplusReimbursementAmount(canReimbursementAmount.subtract(allAlreadyReimbursementQuota).toString());
        return reimbursement;
    }


    @Override
    public ReimbursementDTO getBindingBusinessById(String id,String reimbursementAddress) {
        ReimbursementDTO reimbursement = new ReimbursementDTO();
        reimbursement.setBusinessCodeId(id);
        ReimbursementBusinessDTO reimbursementBusiness = new ReimbursementBusinessDTO();
        reimbursementBusiness.setBusinessCodeId(id);
        reimbursementBusiness.setReimbursementAddress(reimbursementAddress);
        //根据业务id查询已收款的发票信息收款总额
        String getReceiptInvoiceByReimbursementId = reimbursementSysMapper.getReceiptInvoiceIncomeByBindingReimbursementId(id,reimbursementAddress);
        BigDecimal income = new BigDecimal(getReceiptInvoiceByReimbursementId);    //发票收入
        //根据业务id查询报销比例
        String getReimbursementRatioByReimbursementId = reimbursementSysMapper.getReimbursementRatioByBindingReimbursementId(id,reimbursementAddress);
        if(StringUtils.isBlank(getReimbursementRatioByReimbursementId)){
            getReimbursementRatioByReimbursementId = "0";
            reimbursement.setReimbursementRatio("0");
        }
        BigDecimal reimbursementRatio = new BigDecimal(getReimbursementRatioByReimbursementId);    //报销比例
        BigDecimal canReimbursementAmount = income.multiply(reimbursementRatio).divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);    //该项目可报销额度计算
        //查询该业务总报销额度
        String alreadyReimbursementQuotaByBusinessCodeId = mapper.getAlreadyReimbursementQuotaByBusinessCodeId(reimbursementBusiness);
        BigDecimal allAlreadyReimbursementQuota = new BigDecimal(alreadyReimbursementQuotaByBusinessCodeId);    //总报销额度
        reimbursement.setAllAlreadyReimbursementQuota(allAlreadyReimbursementQuota.toString());
        reimbursement.setCanReimbursementAmount(canReimbursementAmount.toString());
        reimbursement.setSurplusReimbursementAmount(canReimbursementAmount.subtract(allAlreadyReimbursementQuota).toString());
        return reimbursement;
    }

    //武汉
    @Override
    public WuHanReimbursementDTO getWuHanBusinessById(String id) {
        WuHanReimbursementDTO reimbursement = new WuHanReimbursementDTO();
        reimbursement.setBusinessCodeId(id);
        ReimbursementBusinessDTO reimbursementBusiness = new ReimbursementBusinessDTO();
        reimbursementBusiness.setBusinessCodeId(id);
        reimbursementBusiness.setReimbursementAddress("1");
        //根据业务id查询已收款的发票信息收款总额
        String getReceiptInvoiceByReimbursementId = wuHanReimbursementSysMapper.getReceiptInvoiceIncomeByReimbursementId(id);
        BigDecimal income = new BigDecimal(getReceiptInvoiceByReimbursementId);    //发票收入
        //根据业务id查询报销比例
        String getReimbursementRatioByReimbursementId = wuHanReimbursementSysMapper.getReimbursementRatioByReimbursementId(id);
        if(StringUtils.isBlank(getReimbursementRatioByReimbursementId)){
            getReimbursementRatioByReimbursementId = "0";
            reimbursement.setReimbursementRatio("0");
        }
        BigDecimal reimbursementRatio = new BigDecimal(getReimbursementRatioByReimbursementId);    //报销比例
        BigDecimal canReimbursementAmount = income.multiply(reimbursementRatio).divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);    //该项目可报销额度计算
        //查询该业务总报销额度
        String alreadyReimbursementQuotaByBusinessCodeId = mapper.getAlreadyReimbursementQuotaByBusinessCodeId(reimbursementBusiness);
        BigDecimal allAlreadyReimbursementQuota = new BigDecimal(alreadyReimbursementQuotaByBusinessCodeId);    //总报销额度
        reimbursement.setAllAlreadyReimbursementQuota(allAlreadyReimbursementQuota.toString());
        reimbursement.setCanReimbursementAmount(canReimbursementAmount.toString());
        reimbursement.setSurplusReimbursementAmount(canReimbursementAmount.subtract(allAlreadyReimbursementQuota).toString());
        return reimbursement;
    }

    @Override
    public ReimbursementUserDTO queryByUserIdAndYear(ReimbursementBusinessDTO reimbursementUser) {
        return mapper.queryByUserIdAndYear(reimbursementUser);
    }


    @Override
    public IPage<ReimbursementUserDTO> findList(Page<ReimbursementUserDTO> page, ReimbursementUserDTO reimbursementUser) {
        return mapper.findList (page, reimbursementUser);
    }

    @Override
    public IPage<ReimbursementUserDTO> getYearList(Page<ReimbursementUserDTO> page, ReimbursementUserDTO reimbursementUser) {
        IPage<ReimbursementUserDTO> yearList = mapper.getYearList(page, reimbursementUser);
        List<ReimbursementUserDTO> list = yearList.getRecords();
        if(list.size()>0){
            List<String> yearStrList = Lists.newArrayList();
            for (ReimbursementUserDTO info : list) {
                yearStrList.add(info.getYear());
            }
            String years = StringUtils.join(yearStrList, ',');
            reimbursementUser.setYear(years);
            //根据人员和年份查询他的已报金额
            List<ReimbursementUserDTO> reimbursementMoneyByYear = mapper.getReimbursementMoneyByYear(reimbursementUser);
            for (ReimbursementUserDTO info : list) {
                for (ReimbursementUserDTO yearInfo : reimbursementMoneyByYear) {
                    if(info.getYear().equals(yearInfo.getYear())){
                        //根据人员和年份查询报销人年度报销天数信息
                        ReimbursementUserDTO reimbursementUserDTO = new ReimbursementUserDTO();
                        reimbursementUserDTO.setUserId(reimbursementUser.getUserId());
                        reimbursementUserDTO.setYear(info.getYear());
                        ReimbursementUserDTO userInfo = reimbursementUserMapper.queryByUserIdAndYear(reimbursementUserDTO);
                        if(null != userInfo.getReimbursementAllDay()){
                            //计算剩余天数
                            if(StringUtils.isBlank(yearInfo.getAlreadyReimbursementDay())){
                                yearInfo.setAlreadyReimbursementDay("0");
                            }
                            Integer surplusReimbursementDay = userInfo.getReimbursementAllDay() - Integer.parseInt(yearInfo.getAlreadyReimbursementDay());
                            info.setSurplusReimbursementDay(surplusReimbursementDay.toString());
                        }else{
                            info.setSurplusReimbursementDay("");
                        }
                        //获取当年总报销额度
                        BigDecimal allAlreadyReimbursementQuota = new BigDecimal(info.getReimbursementQuota());
                        //获取当年已报销额度
                        BigDecimal alreadyReimbursementQuota = new BigDecimal(yearInfo.getAlreadyReimbursementAmount());
                        info.setAlreadyReimbursementAmount(alreadyReimbursementQuota.toString());
                        //计算出当年所剩报销额度
                        info.setRemainReimbursementAmount(allAlreadyReimbursementQuota.subtract(alreadyReimbursementQuota).toString());
                        break;
                    }
                }
                //如果没有剩余报销天数 则将年报销天数添加到剩余报销天数中
                if(StringUtils.isBlank(info.getSurplusReimbursementDay())){
                    info.setSurplusReimbursementDay(info.getReimbursementAllDay().toString());
                }
            }

        }
        return yearList;
    }

    /**
     * 保存
     * @param reimbursementUser
     */
    @Override
    public Map<String,Object> save(ReimbursementBusinessDTO reimbursementUser) {
        Map<String,Object> map = new HashMap<>();

        if(StringUtils.isBlank(reimbursementUser.getUserId())){
            map.put("success", false);
            map.put("message", "报销人员为空，无法进行添加报销信息!");
            return map;
        }
        if(StringUtils.isBlank(reimbursementUser.getYear())){
            map.put("success", false);
            map.put("message", "报销年份不正确，请重新填写!");
            return map;
        }
        if(StringUtils.isBlank(reimbursementUser.getReimbursementType())){
            map.put("success", false);
            map.put("message", "请选择报销类型!");
            return map;
        }else{
            if("1".equals(reimbursementUser.getReimbursementType()) && (StringUtils.isBlank(reimbursementUser.getReimbursementDay()) || !StringUtils.isNumeric(reimbursementUser.getReimbursementDay()))){
                map.put("success", false);
                map.put("message", "报销天数不正确，请重新填写!");
                return map;
            }
        }
        if(StringUtils.isBlank(reimbursementUser.getReimbursementFsalary())){
            map.put("success", false);
            map.put("message", "报销类型不正确，请重新填写!");
            return map;
        }
        //根据人员id和报销年份查询该年度的报销信息
        ReimbursementUserDTO reimbursementUserByUserIdAndYear = mapper.reimbursementUserByUserIdAndYear(reimbursementUser);
        if(null == reimbursementUserByUserIdAndYear){
            map.put("success", false);
            map.put("message", "该员工" + reimbursementUser.getYear() + "年度未添加报销信息，无法进行报销");
            return map;
        }
        //查询该人员该年度已报销额度(正常报销额度)
        reimbursementUser.setReimbursementAddress("0");
        String alreadyReimbursementQuota = mapper.getAlreadyReimbursementQuotaByUserIdAndYear(reimbursementUser);
        //判断本地报销额度是否超过该年度所剩余报销额度，如果超过，则不允许报销，并抛出。否则进行报销并保存报销数据
        BigDecimal alreadyReimbursementQuotaB = new BigDecimal(alreadyReimbursementQuota);    //已报销额度
        BigDecimal reimbursementAmount = null;
        if("1".equals(reimbursementUser.getReimbursementType())){
            BigDecimal reimbursementQuotaDay = new BigDecimal(reimbursementUser.getReimbursementQuotaDay());    //日报销额度
            BigDecimal reimbursementDay = new BigDecimal(reimbursementUser.getReimbursementDay());  //报销天数
            reimbursementAmount = reimbursementQuotaDay.multiply(reimbursementDay).setScale(2, BigDecimal.ROUND_HALF_UP);    //报销额度

            //往期加这次的总报销额度和当年的总报销额度进行比较，小于当年报销额度 则个人可以进行报销，否则，无法进行报销
            if(alreadyReimbursementQuotaB.add(reimbursementAmount).compareTo(new BigDecimal(reimbursementUser.getReimbursementQuota())) == 1){
                map.put("success", false);
                map.put("message", "本次报销金额已超出当年报销额度所剩余额，无法进行报销");
                return map;
            }
        }else{
            reimbursementAmount = new BigDecimal(reimbursementUser.getReimbursementAmount());
        }


        //根据业务id查询已收款的发票信息收款总额
        String getReceiptInvoiceByReimbursementId = reimbursementSysMapper.getReceiptInvoiceIncomeByReimbursementId(reimbursementUser.getBusinessCodeId());
        BigDecimal income = new BigDecimal(getReceiptInvoiceByReimbursementId);    //发票收入
        if(new BigDecimal("0").compareTo(income) == 0){
            map.put("success", false);
            map.put("message", "该业务可报销额度为0，无法进行报销");
            return map;
        }
        //根据业务id查询报销比例
        String getReimbursementRatioByReimbursementId = reimbursementSysMapper.getReimbursementRatioByReimbursementId(reimbursementUser.getBusinessCodeId());
        if(StringUtils.isBlank(getReimbursementRatioByReimbursementId)){
            map.put("success", false);
            map.put("message", "该业务报销比例为0，无法进行报销");
            return map;
        }
        BigDecimal reimbursementRatio = new BigDecimal(getReimbursementRatioByReimbursementId);    //报销比例
        BigDecimal canReimbursementAmount = income.multiply(reimbursementRatio).divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);    //该项目可报销额度计算
        //查询该业务总报销额度
        String alreadyReimbursementQuotaByBusinessCodeId = mapper.getAlreadyReimbursementQuotaByBusinessCodeId(reimbursementUser);
        String gatheringStatus = mapper.getGatheringStatusById(reimbursementUser);
        BigDecimal allAlreadyReimbursementQuota = new BigDecimal(alreadyReimbursementQuotaByBusinessCodeId);    //总报销额度
        //往期加这次的总报销额度和当年的总报销额度进行比较，小于当年报销额度 则个人可以进行报销，否则，无法进行报销
        if(allAlreadyReimbursementQuota.add(reimbursementAmount).compareTo(canReimbursementAmount) == 1 && "1".equals(reimbursementUser.getReimbursementType())){
            map.put("success", false);
            map.put("message", "本次报销金额已超出该业务可报销额度所剩余额，无法进行报销");
            return map;
        } else if (allAlreadyReimbursementQuota.add(reimbursementAmount).compareTo(canReimbursementAmount) == 0 && "1".equals(gatheringStatus)){
            //如果报销额度和剩余报销额度相同
            ReimbursementDTO reimbursementDTO = new ReimbursementDTO();
            reimbursementDTO.setId(reimbursementUser.getBusinessCodeId());
            reimbursementDTO.setReimbursementStatus("1");

            mapper.updateReimbursementInfo(reimbursementDTO);
        }

        ReimbursementDTO reimbursement = new ReimbursementDTO();
        reimbursement.setBusinessCodeId(reimbursementUser.getBusinessCodeId());
        //根据该条发票信息查询同业务编号下的没有收款的发票信息
        List<ReimbursementDTO> notGatheringTimeInvoice = reimbursementSysMapper.getNotGatheringTimeInvoiceBybusinessCodeId(reimbursement);
        if(notGatheringTimeInvoice.size() == 0){
            //查询该发票信息
            //如果所有的发票均已收款，则将业务信息添加全部收款状态
            reimbursementSysMapper.updateGatheringStatusById(reimbursementUser.getBusinessCodeId());
        }


        UserDTO user = UserUtils.get(reimbursementUser.getUserId());

        reimbursementUser.setReimbursementAmount(reimbursementAmount.toString());
        if(StringUtils.isNotBlank(reimbursementUser.getId())){
            reimbursementUser.preUpdate();
            //新增
            mapper.update(reimbursementUser);
        }else{
            reimbursementUser.preInsert();
            //新增
            mapper.save(reimbursementUser);
        }

        map.put("success", true);
        map.put("message", user.getName() + "报销" + reimbursementUser.getReimbursementAmount() + "金额成功");
        return map;
    }

    /**
     * 武汉保存
     * @param reimbursementUser
     */
    @Override
    public Map<String,Object> wuHanSave(ReimbursementBusinessDTO reimbursementUser) {
        Map<String,Object> map = new HashMap<>();

        if(StringUtils.isBlank(reimbursementUser.getUserId())){
            map.put("success", false);
            map.put("message", "报销人员为空，无法进行添加报销信息!");
            return map;
        }
        if(StringUtils.isBlank(reimbursementUser.getYear())){
            map.put("success", false);
            map.put("message", "报销年份不正确，请重新填写!");
            return map;
        }
        if(StringUtils.isBlank(reimbursementUser.getReimbursementType())){
            map.put("success", false);
            map.put("message", "请选择报销类型!");
            return map;
        }else{
            if("1".equals(reimbursementUser.getReimbursementType()) && (StringUtils.isBlank(reimbursementUser.getReimbursementDay()) || !StringUtils.isNumeric(reimbursementUser.getReimbursementDay()))){
                map.put("success", false);
                map.put("message", "报销天数不正确，请重新填写!");
                return map;
            }
        }
        //根据人员id和报销年份查询该年度的报销信息
        ReimbursementUserDTO reimbursementUserByUserIdAndYear = mapper.reimbursementUserByUserIdAndYear(reimbursementUser);
        if(null == reimbursementUserByUserIdAndYear){
            map.put("success", false);
            map.put("message", "该员工" + reimbursementUser.getYear() + "年度未添加报销信息，无法进行报销");
            return map;
        }
        //查询该人员该年度已报销额度(正常报销额度)
        reimbursementUser.setReimbursementAddress("0");
        String alreadyReimbursementQuota = mapper.getAlreadyReimbursementQuotaByUserIdAndYear(reimbursementUser);
        //判断本地报销额度是否超过该年度所剩余报销额度，如果超过，则不允许报销，并抛出。否则进行报销并保存报销数据
        BigDecimal alreadyReimbursementQuotaB = new BigDecimal(alreadyReimbursementQuota);    //已报销额度
        BigDecimal reimbursementAmount = null;
        if("1".equals(reimbursementUser.getReimbursementType())){
            BigDecimal reimbursementQuotaDay = new BigDecimal(reimbursementUser.getReimbursementQuotaDay());    //日报销额度
            BigDecimal reimbursementDay = new BigDecimal(reimbursementUser.getReimbursementDay());  //报销天数
            reimbursementAmount = reimbursementQuotaDay.multiply(reimbursementDay).setScale(2, BigDecimal.ROUND_HALF_UP);    //报销额度

            //往期加这次的总报销额度和当年的总报销额度进行比较，小于当年报销额度 则个人可以进行报销，否则，无法进行报销
            if(alreadyReimbursementQuotaB.add(reimbursementAmount).compareTo(new BigDecimal(reimbursementUser.getReimbursementQuota())) == 1){
                map.put("success", false);
                map.put("message", "本次报销金额已超出当年报销额度所剩余额，无法进行报销");
                return map;
            }
        }else{
            reimbursementAmount = new BigDecimal(reimbursementUser.getReimbursementAmount());
        }


        //根据业务id查询已收款的发票信息收款总额
        String getReceiptInvoiceByReimbursementId = wuHanReimbursementSysMapper.getReceiptInvoiceIncomeByReimbursementId(reimbursementUser.getBusinessCodeId());
        BigDecimal income = new BigDecimal(getReceiptInvoiceByReimbursementId);    //发票收入
        if(new BigDecimal("0").compareTo(income) == 0){
            map.put("success", false);
            map.put("message", "该业务可报销额度为0，无法进行报销");
            return map;
        }
        //根据业务id查询报销比例
        String getReimbursementRatioByReimbursementId = wuHanReimbursementSysMapper.getReimbursementRatioByReimbursementId(reimbursementUser.getBusinessCodeId());
        if(StringUtils.isBlank(getReimbursementRatioByReimbursementId)){
            map.put("success", false);
            map.put("message", "该业务报销比例为0，无法进行报销");
            return map;
        }
        BigDecimal reimbursementRatio = new BigDecimal(getReimbursementRatioByReimbursementId);    //报销比例
        BigDecimal canReimbursementAmount = income.multiply(reimbursementRatio).divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);    //该项目可报销额度计算
        //查询该业务总报销额度
        String alreadyReimbursementQuotaByBusinessCodeId = mapper.getAlreadyReimbursementQuotaByBusinessCodeId(reimbursementUser);
        String gatheringStatus = mapper.getWuHanGatheringStatusById(reimbursementUser);
        BigDecimal allAlreadyReimbursementQuota = new BigDecimal(alreadyReimbursementQuotaByBusinessCodeId);    //总报销额度
        //往期加这次的总报销额度和当年的总报销额度进行比较，小于当年报销额度 则个人可以进行报销，否则，无法进行报销
        if(allAlreadyReimbursementQuota.add(reimbursementAmount).compareTo(canReimbursementAmount) == 1 && "1".equals(reimbursementUser.getReimbursementType())){
            map.put("success", false);
            map.put("message", "本次报销金额已超出该业务可报销额度所剩余额，无法进行报销");
            return map;
        } else if (allAlreadyReimbursementQuota.add(reimbursementAmount).compareTo(canReimbursementAmount) == 0 && "1".equals(gatheringStatus)){
            //如果报销额度和剩余报销额度相同
            WuHanReimbursementDTO reimbursementDTO = new WuHanReimbursementDTO();
            reimbursementDTO.setId(reimbursementUser.getBusinessCodeId());
            reimbursementDTO.setReimbursementStatus("1");

            mapper.updateWuHanReimbursementInfo(reimbursementDTO);
        }

        WuHanReimbursementDTO reimbursement = new WuHanReimbursementDTO();
        reimbursement.setBusinessCodeId(reimbursementUser.getBusinessCodeId());
        //根据该条发票信息查询同业务编号下的没有收款的发票信息
        List<WuHanReimbursementDTO> notGatheringTimeInvoice = wuHanReimbursementSysMapper.getNotGatheringTimeInvoiceBybusinessCodeId(reimbursement);
        if(notGatheringTimeInvoice.size() == 0){
            //查询该发票信息
            //如果所有的发票均已收款，则将业务信息添加全部收款状态
            wuHanReimbursementSysMapper.updateGatheringStatusById(reimbursementUser.getBusinessCodeId());
        }


        UserDTO user = UserUtils.get(reimbursementUser.getUserId());

        reimbursementUser.setReimbursementAmount(reimbursementAmount.toString());
        if(StringUtils.isNotBlank(reimbursementUser.getId())){
            reimbursementUser.preUpdate();
            //新增
            mapper.update(reimbursementUser);
        }else{
            reimbursementUser.preInsert();
            //新增
            mapper.save(reimbursementUser);
        }

        map.put("success", true);
        map.put("message", user.getName() + "报销" + reimbursementUser.getReimbursementAmount() + "金额成功");
        return map;
    }

    @Override
    public void update(ReimbursementBusinessDTO reimbursementUser) {
        mapper.update(reimbursementUser);
    }

    @Override
    public void delete(List<String> idList) {
        //删除发票信息
        if(idList.size()>0){
            mapper.delete(idList);
        }
    }


    @Override
    public ReimbursementBusinessReturnDTO findListByBusinessCodeId(ReimbursementBusinessDTO reimbursementBusiness) {
        //查询报销批次信息
        ReimbursementDTO reimbursementDTO = reimbursementSysMapper.queryBusinessById(reimbursementBusiness.getBusinessCodeId());

        reimbursementBusiness.setReimbursementAddress("0");
        List<ReimbursementBusinessDTO> list = mapper.findListByBusinessCodeId(reimbursementBusiness);
        for (ReimbursementBusinessDTO info : list) {
            info.setReimbursementType(DictUtils.getDictLabel(info.getReimbursementType(), "reimbursement_type", null));
            info.setReimbursementFsalary(DictUtils.getDictLabel(info.getReimbursementFsalary(), "reimbursement_fsalary", null));
            info.setOfficeType(DictUtils.getDictLabel(info.getOfficeType(), "reimbursement_office_type", null));
        }
        ReimbursementBusinessReturnDTO reimbursementBusinessReturnDTO = new ReimbursementBusinessReturnDTO();
        //获取业务报销信息
        ReimbursementDTO business = this.getBusinessById(reimbursementBusiness.getBusinessCodeId());
        reimbursementBusinessReturnDTO.setList(list);
        //剩余报销额度
        reimbursementBusinessReturnDTO.setSurplusReimbursementAmount(business.getSurplusReimbursementAmount());
        //已报销额度
        reimbursementBusinessReturnDTO.setAlreadyReimbursementAmount(business.getAllAlreadyReimbursementQuota());
        //总报销额度
        reimbursementBusinessReturnDTO.setReimbursementQuota(business.getCanReimbursementAmount());
        reimbursementBusinessReturnDTO.setYear(reimbursementDTO.getYear());
        reimbursementBusinessReturnDTO.setBusinessCode(reimbursementDTO.getBusinessCode());
        return reimbursementBusinessReturnDTO;
    }

}
