package com.ruoyi.hywz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hywz.common.enums.StampBusinessTypeEnum;
import com.ruoyi.hywz.entity.*;
import com.ruoyi.hywz.entity.po.DateQueryPO;
import com.ruoyi.hywz.entity.po.StampTaxComputeSavePO;
import com.ruoyi.hywz.entity.vo.stamp.tax.StampTaxBaseVo;
import com.ruoyi.hywz.mapper.AdsMatHyxtStampTaxComputeDMapper;
import com.ruoyi.hywz.service.*;
import com.ruoyi.hywz.utils.AmountUtil;
import com.ruoyi.hywz.utils.IdGen;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-07-14 10:07:56
 */
@Service
@AllArgsConstructor
public class AdsMatHyxtStampTaxComputeDServiceImpl extends ServiceImpl<AdsMatHyxtStampTaxComputeDMapper, AdsMatHyxtStampTaxComputeD> implements IAdsMatHyxtStampTaxComputeDService {

    private final IAdsMatHyxtStampTaxItemDService adsMatHyxtStampTaxItemDService;
    private final IAdsMatHyxtStampTaxContractDService adsMatHyxtStampTaxContractDService;
    private final IAdsMatHyxtStampTaxInvoiceDService adsMatHyxtStampTaxInvoiceDService;
    private final IAdsMatHyxtStampTaxComputeItemDService adsMatHyxtStampTaxComputeItemDService;

    @Override
    public R queryPage(Long pageSize, Long pageNo, AdsMatHyxtStampTaxComputeD param) {
        Page<AdsMatHyxtStampTaxComputeD> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        LambdaQueryWrapper<AdsMatHyxtStampTaxComputeD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getIsDeleted, 0);
        if (null != param) {
            queryWrapper.like(StringUtils.isNotBlank(param.getTaxPayer()), AdsMatHyxtStampTaxComputeD::getTaxPayer, param.getTaxPayer());
            if (ObjectUtils.isNotEmpty(param.getIsAuditPage()) && param.getIsAuditPage() == 1) {
                LoginUser loginUser = SecurityUtils.getLoginUser();
                queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getAuditStatus, 1);
                queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getAuditUser, loginUser.getUsername());
            }
        }
        queryWrapper.orderByDesc(AdsMatHyxtStampTaxComputeD::getCreateTime);
        return R.ok(this.page(page, queryWrapper));
    }

    @Override
    public List<StampTaxBaseVo> getStampTaxBaseTable(String startDate, String endDate, String orgNo) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 1.获取所有税目
        List<AdsMatHyxtStampTaxItemD> list = adsMatHyxtStampTaxItemDService.getList(false);

        // 2.获取指定发票、合同数据
        List<AdsMatHyxtStampTaxContractD> contractList = adsMatHyxtStampTaxContractDService.getList(startDate, endDate, orgNo);
        List<AdsMatHyxtStampTaxInvoiceD> invoiceList = adsMatHyxtStampTaxInvoiceDService.getList(startDate, endDate, orgNo);

        // 3.匹配
        List<StampTaxBaseVo> result = new ArrayList<>();
        BigDecimal contractTotalNum = BigDecimal.ZERO;
        BigDecimal invoiceTotalNum = BigDecimal.ZERO;
        BigDecimal totalNum = BigDecimal.ZERO;
        for (AdsMatHyxtStampTaxItemD taxItemD : list) {
            StampTaxBaseVo item = new StampTaxBaseVo();
            item.setTaxName(taxItemD.getItemName());
            item.setTaxRate(taxItemD.getTaxRate());

            // 合同取数
            item.setContractNum(this.matchingContractData(taxItemD.getItemName(), contractList));
            // 发票取数
            item.setInvoiceNum(this.matchingInvoiceData(taxItemD.getItemName(), invoiceList));

            // 计税金额合计
            BigDecimal amount1 = AmountUtil.convert(item.getContractNum());
            BigDecimal amount2 = AmountUtil.convert(item.getInvoiceNum());
            item.setTotalNum(amount1.add(amount2).toString());

            contractTotalNum = contractTotalNum.add(new BigDecimal(item.getContractNum()));
            invoiceTotalNum = invoiceTotalNum.add(new BigDecimal(item.getInvoiceNum()));
            totalNum = totalNum.add(new BigDecimal(item.getTotalNum()));

            result.add(item);
        }


        // 4.计算合计
        StampTaxBaseVo total = new StampTaxBaseVo();
        total.setTaxName("合计");
        total.setContractNum(contractTotalNum.toString());
        total.setInvoiceNum(invoiceTotalNum.toString());
        total.setTotalNum(totalNum.toString());
        total.setTaxRate(null);
        result.add(total);

        return result;
    }

    @Override
    public List<AdsMatHyxtStampTaxItemD> getStampTaxStatiss(String year, String orgNo) {
        // 1.获取所有税目
        List<AdsMatHyxtStampTaxItemD> list = adsMatHyxtStampTaxItemDService.getList(false);
        List<StampTaxBaseVo> stampTaxBaseVoList1 = this.calculateStampTax(year+"-01-01",year+"-03-31",orgNo,list);//第一季度
        List<StampTaxBaseVo> stampTaxBaseVoList2 = this.calculateStampTax(year+"-04-01",year+"-06-30",orgNo,list);//第二季度
        List<StampTaxBaseVo> stampTaxBaseVoList3 = this.calculateStampTax(year+"-07-01",year+"-09-30",orgNo,list);//第三季度
        List<StampTaxBaseVo> stampTaxBaseVoList4 = this.calculateStampTax(year+"-10-01",year+"-12-31",orgNo,list);//第四季度

        BigDecimal taxFee1TotalNum = BigDecimal.ZERO;
        BigDecimal taxFee2TotalNum = BigDecimal.ZERO;
        BigDecimal taxFee3TotalNum = BigDecimal.ZERO;
        BigDecimal taxFee4TotalNum = BigDecimal.ZERO;
        BigDecimal tax1TotalNum = BigDecimal.ZERO;
        BigDecimal tax2TotalNum = BigDecimal.ZERO;
        BigDecimal tax3TotalNum = BigDecimal.ZERO;
        BigDecimal tax4TotalNum = BigDecimal.ZERO;
        for (AdsMatHyxtStampTaxItemD ati : list){
            //第一季度
            for(StampTaxBaseVo stampTaxBaseVo1 : stampTaxBaseVoList1){
                if(ati.getItemName().equals(stampTaxBaseVo1.getTaxName())){
                    ati.setTax1Fee(stampTaxBaseVo1.getTotalNum());
                    ati.setTax1(stampTaxBaseVo1.getTaxNum());
                    taxFee1TotalNum = taxFee1TotalNum.add(new BigDecimal(ati.getTax1Fee()));
                    tax1TotalNum = tax1TotalNum.add(new BigDecimal(ati.getTax1()));
                }
            }
            //第二季度
            for(StampTaxBaseVo stampTaxBaseVo2 : stampTaxBaseVoList2){
                if(ati.getItemName().equals(stampTaxBaseVo2.getTaxName())){
                    ati.setTax2Fee(stampTaxBaseVo2.getTotalNum());
                    ati.setTax2(stampTaxBaseVo2.getTaxNum());
                    taxFee2TotalNum = taxFee2TotalNum.add(new BigDecimal(ati.getTax2Fee()));
                    tax2TotalNum = tax2TotalNum.add(new BigDecimal(ati.getTax2()));
                }
            }
            //第三季度
            for(StampTaxBaseVo stampTaxBaseVo3 : stampTaxBaseVoList3){
                if(ati.getItemName().equals(stampTaxBaseVo3.getTaxName())){
                    ati.setTax3Fee(stampTaxBaseVo3.getTotalNum());
                    ati.setTax3(stampTaxBaseVo3.getTaxNum());
                    taxFee3TotalNum = taxFee3TotalNum.add(new BigDecimal(ati.getTax3Fee()));
                    tax3TotalNum = tax3TotalNum.add(new BigDecimal(ati.getTax3()));
                }
            }
            //第四季度
            for(StampTaxBaseVo stampTaxBaseVo4 : stampTaxBaseVoList4){
                if(ati.getItemName().equals(stampTaxBaseVo4.getTaxName())){
                    ati.setTax4Fee(stampTaxBaseVo4.getTotalNum());
                    ati.setTax4(stampTaxBaseVo4.getTaxNum());
                    taxFee4TotalNum = taxFee4TotalNum.add(new BigDecimal(ati.getTax4Fee()));
                    tax4TotalNum = tax4TotalNum.add(new BigDecimal(ati.getTax4()));
                }
            }
        }
        AdsMatHyxtStampTaxItemD atiz = new AdsMatHyxtStampTaxItemD();
        atiz.setItemName("合计");
        atiz.setTax1Fee(taxFee1TotalNum.toString());
        atiz.setTax2Fee(taxFee2TotalNum.toString());
        atiz.setTax3Fee(taxFee3TotalNum.toString());
        atiz.setTax4Fee(taxFee4TotalNum.toString());
        atiz.setTax1(tax1TotalNum.toString());
        atiz.setTax2(tax2TotalNum.toString());
        atiz.setTax3(tax3TotalNum.toString());
        atiz.setTax4(tax4TotalNum.toString());
        list.add(atiz);
        return list;
    }


    private List<StampTaxBaseVo> calculateStampTax(String startDate, String endDate, String orgNo,List<AdsMatHyxtStampTaxItemD> list){
        List<AdsMatHyxtStampTaxContractD> contractList = adsMatHyxtStampTaxContractDService.getList(startDate, endDate, orgNo);
        List<AdsMatHyxtStampTaxInvoiceD> invoiceList = adsMatHyxtStampTaxInvoiceDService.getList(startDate, endDate, orgNo);

        // 3.匹配
        List<StampTaxBaseVo> result = new ArrayList<>();
        BigDecimal contractTotalNum = BigDecimal.ZERO;
        BigDecimal invoiceTotalNum = BigDecimal.ZERO;
        BigDecimal totalNum = BigDecimal.ZERO;
        for (AdsMatHyxtStampTaxItemD taxItemD : list) {
            StampTaxBaseVo item = new StampTaxBaseVo();
            item.setTaxName(taxItemD.getItemName());
            item.setTaxRate(taxItemD.getTaxRate());

            // 合同取数
            item.setContractNum(this.matchingContractData(taxItemD.getItemName(), contractList));
            // 发票取数
            item.setInvoiceNum(this.matchingInvoiceData(taxItemD.getItemName(), invoiceList));

            // 计税金额合计
            BigDecimal amount1 = AmountUtil.convert(item.getContractNum());
            BigDecimal amount2 = AmountUtil.convert(item.getInvoiceNum());
            item.setTotalNum(amount1.add(amount2).toString());

            contractTotalNum = contractTotalNum.add(new BigDecimal(item.getContractNum()));
            invoiceTotalNum = invoiceTotalNum.add(new BigDecimal(item.getInvoiceNum()));
            totalNum = totalNum.add(new BigDecimal(item.getTotalNum()));

            result.add(item);
        }


        for (StampTaxBaseVo vo : result) {
            if (ObjectUtils.isNotEmpty(vo.getTaxRate())) {
                // 设置应税金额
                BigDecimal totalNum1 = AmountUtil.convert(vo.getTotalNum());
                BigDecimal taxRate = AmountUtil.convert(vo.getTaxRate()).divide(new BigDecimal(1000));
                BigDecimal taxAmount = totalNum1.multiply(taxRate).setScale(2, RoundingMode.HALF_UP);
                vo.setTaxNum(taxAmount.toString());
            }
        }
        return result;
    }

    @Override
    public List<StampTaxBaseVo> getStampTaxComputeBill(String startDate, String endDate, String orgNo) {
        List<StampTaxBaseVo> list = this.getStampTaxBaseTable(startDate, endDate, orgNo);
        BigDecimal totalTaxNum = BigDecimal.ZERO;
        for (StampTaxBaseVo vo : list) {
            if (ObjectUtils.isNotEmpty(vo.getTaxRate())) {
                // 设置应税金额
                BigDecimal totalNum = AmountUtil.convert(vo.getTotalNum());
                BigDecimal taxRate = AmountUtil.convert(vo.getTaxRate()).divide(new BigDecimal(1000));

                BigDecimal taxAmount = totalNum.multiply(taxRate).setScale(2, RoundingMode.HALF_UP);
                vo.setTaxNum(taxAmount.toString());

                totalTaxNum = totalTaxNum.add(taxAmount);
            } else {
                vo.setTaxNum(totalTaxNum.toString());
            }
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R stampTaxComputeSave(StampTaxComputeSavePO param) {
        List<AdsMatHyxtStampTaxComputeItemD> itemList = param.getItemList();
        // 判断这个月是否已经存在计算单
        if (this.checkIsExist(param.getHomeOrgNo(), param.getHomeDate())) {
            return R.fail(param.getSelectName()+param.getHomeDate()+"月已经生成计算单，请作废后再重新生成！");
        }

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String computeId = IdGen.get32UUID();
        Date date = new Date();
        AdsMatHyxtStampTaxComputeD computeD = new AdsMatHyxtStampTaxComputeD();
        computeD.setId(computeId);
        computeD.setTaxPayer(param.getTaxPayer());
        computeD.setStartDate(param.getStartDate());
        computeD.setEndDate(param.getEndDate());
        computeD.setAmount(param.getAmount());
        computeD.setTaxAmount(itemList.get(itemList.size() -1).getTotalNum());
        computeD.setAuditStatus(0);

        // 设置年月
        computeD.setYm(param.getHomeDate());

        computeD.setCreateUser(loginUser.getUsername());
        computeD.setCreateUserId(loginUser.getUserId());
        computeD.setCreateUserName(loginUser.getUser().getNickName());
        computeD.setCreateUserOrgNo(param.getHomeOrgNo());
        computeD.setCreateUserOrgName(param.getSelectName());
        computeD.setCreateTime(date);
        this.save(computeD);

        int i = 0;
        for (AdsMatHyxtStampTaxComputeItemD itemD : itemList) {
            i++;
            itemD.setId(IdGen.get32UUID());
            itemD.setComputeId(computeId);
            itemD.setCreateTime(date);
            itemD.setSort(i);
        }
        adsMatHyxtStampTaxComputeItemDService.saveBatch(itemList);
        return R.ok();
    }

    @Override
    public AdsMatHyxtStampTaxComputeD getOne(String orgNo, String date) {
        LambdaQueryWrapper<AdsMatHyxtStampTaxComputeD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getCreateUserOrgNo, orgNo);
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getYm, date);
        queryWrapper.ne(AdsMatHyxtStampTaxComputeD::getStatus, 1);
        List<AdsMatHyxtStampTaxComputeD> list = baseMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    private boolean checkIsExist(String orgNo, String date) {
        LambdaQueryWrapper<AdsMatHyxtStampTaxComputeD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getCreateUserOrgNo, orgNo);
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getYm, date);
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getIsDeleted, 0);
        queryWrapper.eq(AdsMatHyxtStampTaxComputeD::getStatus, 0);
        return baseMapper.selectCount(queryWrapper) > 0;
    }


    /**
     * 【合同取数】
     * 优先取不含税金额，如果不含税金额为0，则取含税金额
     */
    private String matchingContractData(String itemName, List<AdsMatHyxtStampTaxContractD> contractList) {
        // 承揽合同 从发票取数
//        if (itemName.equals("承揽合同")) {
//            return "0";
//        }
        BigDecimal total = new BigDecimal(0);
        int i=0;
        for (AdsMatHyxtStampTaxContractD contractD : contractList) {
            if (StringUtils.isNotEmpty(contractD.getStampTaxItem()) && contractD.getStampTaxItem().equals(itemName)) {
                i++;
                // 含税金额
                String contractAmount = contractD.getContractAmount();
                BigDecimal amount1 = AmountUtil.convert(contractAmount);

                // 不含税金额
                String preTaxAmount = contractD.getPreTaxAmount();
                BigDecimal amount2 = AmountUtil.convert(preTaxAmount);

                // 默认取不含税金额，如果不含税金额为0，取含税金额
                int compare = amount2.compareTo(BigDecimal.ZERO);
                if (compare > 0) {
                    // 取不含税金额
                    total = total.add(amount2);
                } else {
                    // 取含税金额
                    total = total.add(amount1);
                }
//                System.out.println(i);
            }
        }
        return total.toString();
    }

    /**
     * 【发票取数】
     */
    private String matchingInvoiceData(String itemName, List<AdsMatHyxtStampTaxInvoiceD> invoiceList) {
        BigDecimal totalAmount = new BigDecimal(0);
        if (itemName.equals("买卖合同")) {
            List<AdsMatHyxtStampTaxInvoiceD> mm = new ArrayList<>();
            mm.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_1.getCode(), invoiceList));
            mm.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_2.getCode(), invoiceList));
            mm.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_3.getCode(), invoiceList));
            mm.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_4.getCode(), invoiceList));
            return this.sum(mm);
        } else if (itemName.equals("建设工程合同")) {
            List<AdsMatHyxtStampTaxInvoiceD> js = adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_5.getCode(), invoiceList);
            return this.sum(js);
        } else if (itemName.equals("承揽合同")) {
            List<AdsMatHyxtStampTaxInvoiceD> cl = new ArrayList<>();
            cl.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_6.getCode(), invoiceList));
            cl.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_7.getCode(), invoiceList));
            return this.sum(cl);
        } else if (itemName.equals("技术合同")) {
            List<AdsMatHyxtStampTaxInvoiceD> js = new ArrayList<>();
            js.addAll(adsMatHyxtStampTaxInvoiceDService.getByBusinessCode(StampBusinessTypeEnum.TYPE_8.getCode(), invoiceList));
            return this.sum(js);
        }

        return totalAmount.toString();
    }

    private String sum(List<AdsMatHyxtStampTaxInvoiceD> invoiceList) {
        BigDecimal totalAmount = new BigDecimal(0);
        for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
            totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
        }
        return totalAmount.toString();
    }
//    private String matchingInvoiceData(String itemName, List<AdsMatHyxtStampTaxContractD> contractList, List<AdsMatHyxtStampTaxInvoiceD> invoiceList) {
//        BigDecimal totalAmount = new BigDecimal(0);
//        if (itemName.equals("买卖合同")) {
////            for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
////                String spBusinessType = invoiceD.getSpBusinessType();
////                String remark = invoiceD.getRemark();
////                String itemDesc = invoiceD.getItemDesc();
////                // 1.发票信息表“备注”包含（电商采购）字段的发票“金额”合计
////                if (StringUtils.isNotEmpty(remark) && remark.contains("电商采购")) {
////                    totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
////                }
////
////                // 3.发票信息表“特定业务类型”为（成品油发票）的发票的“金额”合计
////                if (StringUtils.isNotEmpty(spBusinessType) && spBusinessType.contains("成品油发票")) {
////                    totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
////                }
////                // 4.发票信息表“货物或应税劳务名称”含有（发电）或（购电）字段下，“税率”不为（0%）和（免税）的发票“价税合计”合计
////                if (StringUtils.isNotEmpty(itemDesc) && !invoiceD.getTaxRate().contains("0") && (itemDesc.contains("发电") || itemDesc.contains("购电"))) {
////                    totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getTotalAmount()));
////                }
////            }
//            List<AdsMatHyxtStampTaxContractD> list = new ArrayList<>();
//            for (AdsMatHyxtStampTaxContractD contractD : contractList) {
//                if (StringUtils.isNotEmpty(contractD.getStampTaxItem()) && contractD.getStampTaxItem().contains("买卖合同")) {
//                    list.add(contractD);
//                }
//            }
//
//            // 2.合同信息表“合同类型”为（物资买卖类合同）字段且“不含税金额”等于0部分，以“合同对方”字段为条件搜索全量发票信息表“销方名称”为相同公司的发票“金额”合计
//            for (AdsMatHyxtStampTaxContractD contractD : list) {
//                String contractType = contractD.getContractType();  // 合同类型
//                String preTaxAmount = contractD.getPreTaxAmount();  // 不含税金额
//                String counterparty = contractD.getCounterparty();  // 合同对方
//                if (StringUtils.isNotEmpty(contractType) && contractType.contains("物资买卖类合同") && AmountUtil.convert(preTaxAmount).compareTo(BigDecimal.ZERO) == 0) {
//                    for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
//                        String sellerName = invoiceD.getSellerName();
//                        if (StringUtils.isNotEmpty(sellerName) && counterparty.contains(sellerName)) {
//                            totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
//                        }
//                    }
//                }
//            }
//            return totalAmount.toString();
//        } else if (itemName.equals("建设工程合同")) {
//            for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
//                String itemDesc = invoiceD.getItemDesc();
//                String taxRate = invoiceD.getTaxRate();
//                // 发票信息表“货物或应税劳务名称”为（设计服务）且“税率”为（0.06）的发票的“金额”合计
//                if (StringUtils.isNotEmpty(itemDesc) && itemDesc.contains("设计服务") && taxRate.contains("0.06")) {
//                    totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
//                }
//            }
//            return totalAmount.toString();
//        } else if (itemName.equals("承揽合同")) {
//            List<AdsMatHyxtStampTaxContractD> list = new ArrayList<>();
//            for (AdsMatHyxtStampTaxContractD contractD : contractList) {
//                if (StringUtils.isNotEmpty(contractD.getStampTaxItem()) && contractD.getStampTaxItem().contains("承揽合同")) {
//                    list.add(contractD);
//                }
//            }
//            // 合同信息表“合同名称”包含（宣传）字段，以“合同对方”字段为条件搜索全量发票信息表“销方名称”为相同公司的发票“金额”合计
//            for (AdsMatHyxtStampTaxContractD contractD : list) {
//                String contractName = contractD.getContractName();  // 合同名称
//                String counterparty = contractD.getCounterparty();  // 合同对方
////                if (StringUtils.isNotEmpty(contractName) && contractName.contains("宣传")) {
//                    for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
//
//                        String sellerName = invoiceD.getSellerName();   // 销方名称
//                        if (StringUtils.isNotEmpty(sellerName) && counterparty.contains(sellerName)) {
//                            totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
//                        }
//                    }
////                }
//            }
//
//            // 发票信息表“货物或应税劳务名称”为（印刷品）的发票的“金额”合计
////            for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
////                String itemDesc = invoiceD.getItemDesc();
////                if (StringUtils.isNotEmpty(itemDesc) && itemDesc.contains("印刷品")) {
////                    totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getTotalAmount()));
////                }
////            }
//            return totalAmount.toString();
//        } else if (itemName.equals("技术合同")) {
//            // 合同信息表“合同类型”为（技术服务类合同_研发）和（技术服务类合同_实施服务）类型下“不含税金额”等于0部分，以“合同对方”字段为条件搜索全量发票信息表“销方名称”为相同公司的发票“金额”合计
//            for (AdsMatHyxtStampTaxContractD contractD : contractList) {
//                String contractType = contractD.getContractType();  // 合同类型
//                String preTaxAmount = contractD.getPreTaxAmount();  // 不含税金额
//                String counterparty = contractD.getCounterparty();  // 合同对方
//                if (StringUtils.isNotEmpty(contractType) && (contractType.contains("技术服务类合同_研发") || contractType.contains("技术服务类合同_实施服务")) && AmountUtil.convert(preTaxAmount).compareTo(BigDecimal.ZERO) == 0) {
//                    for (AdsMatHyxtStampTaxInvoiceD invoiceD : invoiceList) {
//                        String sellerName = invoiceD.getSellerName();
//                        if (StringUtils.isNotEmpty(sellerName) && counterparty.contains(sellerName)) {
//                            totalAmount = totalAmount.add(AmountUtil.convert(invoiceD.getAmount()));
//                        }
//                    }
//                }
//            }
//            return totalAmount.toString();
//        }
//
//        // TODO
//        return totalAmount.toString();
//    }

}
