package com.tofly.feesapi.common.dzfp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.dzfp.entity.*;
import com.tofly.feesapi.dzfp.service.*;
import com.tofly.feesapi.security.auth.ToflyUser;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.sfgl.entity.Plsfkhxx;
import com.tofly.feesapi.sfgl.entity.YyQfmx;
import com.tofly.feesapi.sfgl.entity.YySspjxx;
import com.tofly.feesapi.sfgl.service.YyQfmxService;
import com.tofly.feesapi.sfgl.service.YySspjxxService;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class InvoiceService {

    @Autowired
    private EiFpkjMxService eiFpkjMxService;
    @Autowired
    private EiFpkjRequestService eiFpkjRequestService;
    @Autowired
    private EiSpbmService eiSpbmService;
    @Autowired
    private YySspjxxService yySspjxxService;
    @Autowired
    private YyQfmxService yyQfmxService;
    @Autowired
    private EiSalerConfigService eiSalerConfigService;
    @Autowired
    private EiYkfpLogService eiYkfpLogService;

    public void addInvoiceData(List<YyQfmx> yyQfmxs, List<YySspjxx> yySspjxxs, YhKhjbxx yhKhjbxx, String ishbkp, String sfkp) {
        addInvoiceData(yyQfmxs, yySspjxxs, yhKhjbxx, ishbkp, sfkp, "0");
    }

    public void addInvoiceData(List<YyQfmx> yyQfmxs, List<YySspjxx> yySspjxxs, YhKhjbxx yhKhjbxx, String ishbkp, String sfkp, String fply) {
        //进行排序
        yyQfmxs = yyQfmxs.stream().sorted(Comparator.comparing(YyQfmx::getYsyf)).collect(Collectors.toList());
        BigDecimal scye = StringUtil.isNull(yySspjxxs) ? BigDecimal.ZERO : yySspjxxs.get(0).getScye();
        BigDecimal bcye = StringUtil.isNull(yySspjxxs) ? BigDecimal.ZERO : yySspjxxs.get(yySspjxxs.size() - 1).getBcye();
        String sfryxm = StringUtil.isNull(yySspjxxs) ? SecurityUtils.getUser().getRealName() : yySspjxxs.get(0).getSfryxm();
        String sfrybm = StringUtil.isNull(yySspjxxs) ? String.valueOf(SecurityUtils.getUserId()) : yySspjxxs.get(0).getSfrybm();

        // 是否合并开票
        if (!ishbkp.equals("2")) {
            ishbkp = "0";
            if (yyQfmxs.size() > 1) {
                ishbkp = "1";
            }
        }

        String sfid = GuidGenerator.getGuid();
        String lsbh = GuidGenerator.getGuid();
        InvoiceAmounts amounts = new InvoiceAmounts(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO);

        List<EiFpkjMx> eiFpkjMxList = new ArrayList<>();
        for (YyQfmx yyQfmx : yyQfmxs) {
            List<EiFpkjMx> fpkjMxList = getEiFpkjMxList(yyQfmx, lsbh);
            eiFpkjMxList.addAll(fpkjMxList);
        }

        // 合并相同的收费项目
        List<Map> mapList = eiFpkjMxList.stream().map(
                e -> {
                    Map map = new HashMap();
                    map.put("goodsname", e.getGoodsname());
                    map.put("goodscode", e.getGoodscode());
                    return map;
                }
        ).distinct().collect(Collectors.toList());

        for (Map map : mapList) {
            //获取相同商品名称、商品编码
            List<EiFpkjMx> eiFpkjMxes = eiFpkjMxList.stream().filter(
                    e -> e.getGoodsname().equals(map.get("goodsname"))
                            && e.getGoodscode().equals(map.get("goodscode"))
            ).collect(Collectors.toList());

            EiFpkjMx eiFpkjMx = eiFpkjMxes.get(0);
            eiFpkjMx.setMxid(GuidGenerator.getGuid());
            //BigDecimal taxExcludedAmount = eiFpkjMxes.stream().map(EiFpkjMx::getTaxexcludedamount).reduce(BigDecimal.ZERO, BigDecimal::add);
            //BigDecimal tax = eiFpkjMxes.stream().map(EiFpkjMx::getTax).reduce(BigDecimal.ZERO, BigDecimal::add);//合计税
            BigDecimal taxIncludedAmount = eiFpkjMxes.stream().map(EiFpkjMx::getTaxincludedamount).reduce(BigDecimal.ZERO, BigDecimal::add);
            //合并需要重新计税率，防止出现分开计算累加时误差（分开四舍五入出现多的）
            BigDecimal taxrate = eiFpkjMx.getTaxrate();
            BigDecimal tax = taxIncludedAmount.multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP);
            BigDecimal taxExcludedAmount = taxIncludedAmount.subtract(tax);
            amounts = new InvoiceAmounts(amounts, new InvoiceAmounts(
                    taxExcludedAmount,
                    tax,
                    taxIncludedAmount
            ));
            eiFpkjMx.setTax(tax);
            eiFpkjMx.setTaxexcludedamount(taxExcludedAmount);
            eiFpkjMx.setTaxincludedamount(taxIncludedAmount);
            // 判断是否为违约金
            if (eiFpkjMx.getGoodscode().equals("611") && eiFpkjMx.getGoodsname().equals("违约金")) {
                eiFpkjMx.setNum(BigDecimal.ONE);
                eiFpkjMx.setPrice(taxIncludedAmount);
            } else {
                eiFpkjMx.setNum(eiFpkjMxes.stream().map(EiFpkjMx::getNum).reduce(BigDecimal.ZERO, BigDecimal::add));
                eiFpkjMx.setPrice(eiFpkjMx.getTaxincludedamount().divide(eiFpkjMx.getNum(), 8, RoundingMode.HALF_UP));
            }
            eiFpkjMxService.save(eiFpkjMx);
        }
        if (StringUtil.isNotNull(yySspjxxs) && yySspjxxs.size() > 0) {
            List<String> ids = yySspjxxs.stream().map(YySspjxx::getId)
                    .filter(StringUtil::isNotEmpty).collect(Collectors.toList());
            if (!ids.isEmpty()) {
                yySspjxxService.lambdaUpdate()
                        .set(YySspjxx::getFpglh, sfid)
                        .in(YySspjxx::getId, ids)
                        .update();
            } else {
                yySspjxxService.lambdaUpdate()
                        .set(YySspjxx::getFpglh, sfid)
                        .eq(YySspjxx::getSflsbh, yySspjxxs.get(0).getSflsbh())
                        .update();
            }
        }
        // 重新组装需要的欠费明细字段
        YyQfmx yyQfmx = new YyQfmx();
        YyQfmx chargedQfmx = yyQfmxs.get(0);

        yyQfmx.setEiYkfpBz(StringUtil.isNull(chargedQfmx.getEiYkfpBz()) ? "0" : chargedQfmx.getEiYkfpBz());
        if (yyQfmx.getEiYkfpBz().equals("1")) {
            yyQfmxService.lambdaUpdate()
                    .set(YyQfmx::getFpglh, sfid)
                    .in(YyQfmx::getId, yyQfmxs.stream().map(YyQfmx::getId).collect(Collectors.toList()))
                    .update();
        }

        yyQfmx.setYhbh(yhKhjbxx.getYhbh());
        yyQfmx.setSsgs(yhKhjbxx.getSsgs());
        yyQfmx.setSscbpq(yhKhjbxx.getSscbpq());
        yyQfmx.setCbkh(chargedQfmx.getCbkh());
        yyQfmx.setEiYkfpBz(StringUtil.isNull(chargedQfmx.getEiYkfpBz()) ? "0" : chargedQfmx.getEiYkfpBz());
        yyQfmx.setCwrq(yyQfmxs.size() > 1 ? chargedQfmx.getCwrq() + "-" + yyQfmxs.get(yyQfmxs.size() - 1).getCwrq() : chargedQfmx.getCwrq());
        yyQfmx.setYsyf(yyQfmxs.size() > 1 ? chargedQfmx.getYsyf() + "-" + yyQfmxs.get(yyQfmxs.size() - 1).getYsyf() : chargedQfmx.getYsyf());
        yyQfmx.setScds(chargedQfmx.getScds());
        yyQfmx.setBcds(yyQfmxs.get(yyQfmxs.size() - 1).getBcds());
        yyQfmx.setSflsbh(chargedQfmx.getSflsbh());
        yyQfmx.setJjl(yyQfmxs.stream().map(YyQfmx::getJjl).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
        yyQfmx.setYssl(yyQfmxs.stream().map(YyQfmx::getYssl).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
        yyQfmx.setYsje(yyQfmxs.stream().map(YyQfmx::getYsje).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
        yyQfmx.setZnj(yyQfmxs.stream().map(YyQfmx::getZnj).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
        yyQfmx.setLjf(yyQfmxs.stream().map(YyQfmx::getLjf).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
        String remark = "用户编号：" + yyQfmx.getYhbh() + ",用水月份：" + yyQfmx.getYsyf() +
                ",本次缴费：" + yyQfmx.getYsje() +
                ",上次表止度：" + yyQfmx.getScds() + ",本次表止度：" + yyQfmx.getBcds() +
                ",上次结余：" + String.format("%.2f", scye) +
                //",垃圾费：" + yyQfmx.getLjf() +
                ",本次结余：" + String.format("%.2f", bcye);
        if (ishbkp.equals("2")) {
            remark = "用户编号：" + yyQfmx.getYhbh() + ",用水月份：" + yyQfmx.getYsyf() +
                    ",本次缴费：" + yyQfmx.getYsje() +
                    ",上次结余：" + String.format("%.2f", scye) +
                    //",垃圾费：" + yyQfmx.getLjf() +
                    ",本次结余：" + String.format("%.2f", bcye);
        }
        if ("1".equals(fply)) {
            remark = "用水月份：" + yyQfmx.getYsyf() +
                    ",本次缴费：" + yyQfmx.getYsje();
        }
        if (yyQfmx.getEiYkfpBz().equals("1")) {
            EiYkfpLog eiYkfpLog = new EiYkfpLog();
            eiYkfpLog.setYhbh(yhKhjbxx.getYhbh());
            eiYkfpLog.setYhxm(yhKhjbxx.getYhxm());
            eiYkfpLog.setFpglh(sfid);
            eiYkfpLog.setIshp(ishbkp.equals("0") ? "0" : "1");
            eiYkfpLog.setSfbz("0");
            eiYkfpLog.setKpje(amounts.getTaxIncludedAmount());
            eiYkfpLog.setKprybm(sfrybm);
            eiYkfpLog.setKpryxm(sfryxm);
            eiYkfpLogService.save(eiYkfpLog);
        }
        //判断是不是专票，若是则拆分水资源税出来单独开票
        if (StringUtil.isNotEmpty(yhKhjbxx.getFplx()) && yhKhjbxx.getFplx().equals("2")) {
            List<EiFpkjMx> eiFpkjMxes = eiFpkjMxService.lambdaQuery()
                    .eq(EiFpkjMx::getLsbh, lsbh)
                    .inSql(EiFpkjMx::getGoodsname, "select name from ei_spbm where code in ('07')")
                    .list();
            if (eiFpkjMxes.size() > 0) {
                BigDecimal taxExcludedAmount = eiFpkjMxes.stream().map(EiFpkjMx::getTaxexcludedamount).reduce(BigDecimal.ZERO, BigDecimal::add);//汇总不含税金额
                BigDecimal tax = eiFpkjMxes.stream().map(EiFpkjMx::getTax).reduce(BigDecimal.ZERO, BigDecimal::add);//合计税
                BigDecimal taxIncludedAmount = eiFpkjMxes.stream().map(EiFpkjMx::getTaxincludedamount).reduce(BigDecimal.ZERO, BigDecimal::add);//含税金额
                amounts = new InvoiceAmounts(amounts, new InvoiceAmounts(
                        taxExcludedAmount.multiply(BigDecimal.valueOf(-1)),
                        tax.multiply(BigDecimal.valueOf(-1)),
                        taxIncludedAmount.multiply(BigDecimal.valueOf(-1))
                ));
                String lsbh1 = GuidGenerator.getGuid();
                addInvoiceSummary(yyQfmx, yhKhjbxx, lsbh1, remark, sfryxm, ishbkp, "1",
                        taxExcludedAmount,
                        tax,
                        taxIncludedAmount,
                        "3", sfid, sfkp, sfrybm);
                //修改水资源费与污水费明细
                eiFpkjMxService.lambdaUpdate()
                        .set(EiFpkjMx::getLsbh, lsbh1)
                        .in(EiFpkjMx::getMxid, eiFpkjMxes.stream().map(EiFpkjMx::getMxid).collect(Collectors.toList()))
                        .update();
            }
        }
        //判断是否存在污水费，有则拆分污水费
        List<EiFpkjMx> eiFpkjMxs_wsf = eiFpkjMxService.lambdaQuery()
                .eq(EiFpkjMx::getLsbh, lsbh)
                .inSql(EiFpkjMx::getGoodsname, "select name from ei_spbm where code in ('05')")
                .list();
        if (!eiFpkjMxs_wsf.isEmpty()) {
            BigDecimal taxExcludedAmount = eiFpkjMxs_wsf.stream().map(EiFpkjMx::getTaxexcludedamount).reduce(BigDecimal.ZERO, BigDecimal::add);//汇总不含税金额
            BigDecimal tax = eiFpkjMxs_wsf.stream().map(EiFpkjMx::getTax).reduce(BigDecimal.ZERO, BigDecimal::add);//合计税
            BigDecimal taxIncludedAmount = eiFpkjMxs_wsf.stream().map(EiFpkjMx::getTaxincludedamount).reduce(BigDecimal.ZERO, BigDecimal::add);//含税金额
            amounts = new InvoiceAmounts(amounts, new InvoiceAmounts(
                    taxExcludedAmount.multiply(BigDecimal.valueOf(-1)),
                    tax.multiply(BigDecimal.valueOf(-1)),
                    taxIncludedAmount.multiply(BigDecimal.valueOf(-1))
            ));
            String lsbh1 = GuidGenerator.getGuid();
            addInvoiceSummary(yyQfmx, yhKhjbxx, lsbh1, remark, sfryxm, ishbkp, "1",
                    taxExcludedAmount,
                    tax,
                    taxIncludedAmount,
                    "2", sfid, sfkp, sfrybm);
            //修改水资源费与污水费明细
            eiFpkjMxService.lambdaUpdate()
                    .set(EiFpkjMx::getLsbh, lsbh1)
                    .in(EiFpkjMx::getMxid, eiFpkjMxs_wsf.stream().map(EiFpkjMx::getMxid).collect(Collectors.toList()))
                    .update();
        }
        addInvoiceSummary(yyQfmx, yhKhjbxx, lsbh, remark, sfryxm, ishbkp, yhKhjbxx.getFplx(),
                amounts.getTaxExcludedAmount(),
                amounts.getTax(),
                amounts.getTaxIncludedAmount(),
                "1", sfid, sfkp, sfrybm);
    }


    private List<EiFpkjMx> getEiFpkjMxList(YyQfmx yyQfmx, String lsbh) {
        List<EiFpkjMx> eiFpkjMxList = new ArrayList<>();

        // 1.添加发票水费明细
        if (yyQfmx.getYssl().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getYssf().compareTo(BigDecimal.ZERO) > 0) {
            EiFpkjMx eiFpkjMx = new EiFpkjMx();
            EiSpbm eiSpbm = eiSpbmService.getById("01");
            String sl = eiSpbm.getSl();
            if (StrUtil.isEmpty(sl) || new BigDecimal(sl).compareTo(BigDecimal.ONE) >= 0) {
                throw new FeesException("01商品税率配置错误", FeesErrorCode.BUSINESS_ERROR);
            }

            BigDecimal taxrate = new BigDecimal(sl);
            eiFpkjMx.setUnit("吨");
            eiFpkjMx.setTaxrate(taxrate);
            eiFpkjMx.setSpectype("无");
            eiFpkjMx.setLsbh(lsbh);
            eiFpkjMx.setGoodsname(eiSpbm.getName());
            eiFpkjMx.setGoodscode(eiSpbm.getSszlbm());
            eiFpkjMx.setWithtaxflag(eiSpbm.getDjHsbs());
            eiFpkjMx.setInvoicelineproperty("0");
            eiFpkjMx.setFavouredpolicyflag(eiSpbm.getYhZcbs());
            eiFpkjMx.setFavouredpolicyname(eiSpbm.getYhZcmc());
            eiFpkjMx.setZerorateflag(eiSpbm.getLslBs());

            // 判断居民还是非居0正常单价
            if (StringUtil.isNotEmpty(yyQfmx.getCbjsbz()) && yyQfmx.getCbjsbz().equals("0")) {
                eiFpkjMx.setPrice(yyQfmx.getSj());
                eiFpkjMx.setNum(yyQfmx.getSl());
                eiFpkjMx.setTax(yyQfmx.getSf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP));
                eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf().subtract(eiFpkjMx.getTax()));
                eiFpkjMx.setTaxincludedamount(yyQfmx.getSf());
//                addFpmxQtfy(yyQfmx, eiFpkjMx);
                eiFpkjMxList.add(Deserialization(eiFpkjMx));
            }
            // 1一表多价（不含阶梯）
            else if (StringUtil.isNotEmpty(yyQfmx.getCbjsbz()) && yyQfmx.getCbjsbz().equals("1")) {
                //水价一
                if (yyQfmx.getSl() != null && yyQfmx.getSl().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getSj()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getSl()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getSf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf().subtract(eiFpkjMx.getTax()));
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getSf());
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
                // 水价二
                if (yyQfmx.getSl1() != null && yyQfmx.getSl1().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getSj1());// 单价
                    eiFpkjMx.setNum(yyQfmx.getSl1());// 数量
                    eiFpkjMx.setTax(yyQfmx.getSf1().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP));// 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf1().subtract(eiFpkjMx.getTax()));
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getSf1());
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
                //水价三
                if (yyQfmx.getSl2() != null && yyQfmx.getSl2().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getSj2());// 单价
                    eiFpkjMx.setNum(yyQfmx.getSl2());// 数量
                    eiFpkjMx.setTax(yyQfmx.getSf2().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP));// 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf2().subtract(eiFpkjMx.getTax()));
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getSf2());
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
            }
            //2正常阶梯
            else if (StringUtil.isNotEmpty(yyQfmx.getCbjsbz()) && yyQfmx.getCbjsbz().equals("2")) {
                //阶梯1
                if (yyQfmx.getJt1sl() != null && yyQfmx.getJt1sl().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getJt1sj()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getJt1sl()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getJt1sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getJt1sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getJt1sf());
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
                //阶梯2
                if (yyQfmx.getJt2sl() != null && yyQfmx.getJt2sl().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getJt2sj()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getJt2sl()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getJt2sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getJt2sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getJt2sf()); // 含税金额
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
                //阶梯3
                if (yyQfmx.getJt3sl() != null && yyQfmx.getJt3sl().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getJt3sj()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getJt3sl()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getJt3sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getJt3sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getJt3sf()); // 含税金额
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
            }
            //3一表多价（含阶梯）
            else if (StringUtil.isNotEmpty(yyQfmx.getCbjsbz()) && yyQfmx.getCbjsbz().equals("3")) {
                //水价一
                if (yyQfmx.getSl() != null && yyQfmx.getSl().compareTo(BigDecimal.ZERO) > 0) {
                    //判断水价一是阶梯价还是非阶梯
                    if (yyQfmx.getSj() != null && yyQfmx.getSj().compareTo(BigDecimal.ZERO) > 0) {
                        eiFpkjMx.setPrice(yyQfmx.getSj());
                        eiFpkjMx.setNum(yyQfmx.getSl());
                        eiFpkjMx.setTax(yyQfmx.getSf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                        eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf().subtract(eiFpkjMx.getTax()));
                        eiFpkjMx.setTaxincludedamount(yyQfmx.getSf());
                        eiFpkjMxList.add(Deserialization(eiFpkjMx));
                    } else {
                        //阶梯1
                        if (yyQfmx.getJt1sl() != null && yyQfmx.getJt1sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getJt1sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getJt1sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getJt1sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getJt1sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getJt1sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                        //阶梯2
                        if (yyQfmx.getJt2sl() != null && yyQfmx.getJt2sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getJt2sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getJt2sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getJt2sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getJt2sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getJt2sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                        //阶梯3
                        if (yyQfmx.getJt3sl() != null && yyQfmx.getJt3sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getJt3sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getJt3sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getJt3sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getJt3sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getJt3sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                    }
                }
                //水价二
                if (yyQfmx.getSl1() != null && yyQfmx.getSl1().compareTo(BigDecimal.ZERO) > 0) {
                    //判断水价二是阶梯价还是非阶梯
                    if (yyQfmx.getSj1() != null && yyQfmx.getSj1().compareTo(BigDecimal.ZERO) > 0) {
                        eiFpkjMx.setPrice(yyQfmx.getSj1());// 单价
                        eiFpkjMx.setNum(yyQfmx.getSl1());// 数量
                        eiFpkjMx.setTax(yyQfmx.getSf1().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP));// 税额
                        eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf1().subtract(eiFpkjMx.getTax()));
                        eiFpkjMx.setTaxincludedamount(yyQfmx.getSf1());
                        eiFpkjMxList.add(Deserialization(eiFpkjMx));
                    } else {
                        //阶梯1
                        if (yyQfmx.getSj1Jt1sl() != null && yyQfmx.getSj1Jt1sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getSj1Jt1sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getSj1Jt1sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getSj1Jt1sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSj1Jt1sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getSj1Jt1sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                        //阶梯2
                        if (yyQfmx.getSj1Jt2sl() != null && yyQfmx.getSj1Jt2sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getSj1Jt2sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getSj1Jt2sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getSj1Jt2sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSj1Jt2sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getSj1Jt2sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                        //阶梯3
                        if (yyQfmx.getSj1Jt3sl() != null && yyQfmx.getSj1Jt3sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getSj1Jt3sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getSj1Jt3sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getSj1Jt3sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSj1Jt3sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getSj1Jt3sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                    }
                }
                //水价三
                if (yyQfmx.getSl2() != null && yyQfmx.getSl2().compareTo(BigDecimal.ZERO) > 0) {
                    //判断水价三是阶梯价还是非阶梯
                    if (yyQfmx.getSj2() != null && yyQfmx.getSj2().compareTo(BigDecimal.ZERO) > 0) {
                        eiFpkjMx.setPrice(yyQfmx.getSj2());// 单价
                        eiFpkjMx.setNum(yyQfmx.getSl2());// 数量
                        eiFpkjMx.setTax(yyQfmx.getSf2().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP));
                        eiFpkjMx.setTaxexcludedamount(yyQfmx.getSf2().subtract(eiFpkjMx.getTax()));
                        eiFpkjMx.setTaxincludedamount(yyQfmx.getSf2());
                        eiFpkjMxList.add(Deserialization(eiFpkjMx));
                    } else {
                        //阶梯1
                        if (yyQfmx.getSj2Jt1sl() != null && yyQfmx.getSj2Jt1sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getSj2Jt1sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getSj2Jt1sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getSj2Jt1sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSj2Jt1sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getSj2Jt1sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                        //阶梯2
                        if (yyQfmx.getSj2Jt2sl() != null && yyQfmx.getSj2Jt2sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getSj2Jt2sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getSj2Jt2sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getSj2Jt2sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSj2Jt2sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getSj2Jt2sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                        //阶梯3
                        if (yyQfmx.getSj2Jt3sl() != null && yyQfmx.getSj2Jt3sl().compareTo(BigDecimal.ZERO) > 0) {
                            eiFpkjMx.setPrice(yyQfmx.getSj2Jt3sj()); // 单价
                            eiFpkjMx.setNum(yyQfmx.getSj2Jt3sl()); // 数量
                            eiFpkjMx.setTax(yyQfmx.getSj2Jt3sf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSj2Jt3sf().subtract(eiFpkjMx.getTax())); // 不含税金额
                            eiFpkjMx.setTaxincludedamount(yyQfmx.getSj2Jt3sf()); // 含税金额
                            eiFpkjMxList.add(Deserialization(eiFpkjMx));
                        }
                    }
                }
            }
        }

        // 2.添加发票污水费明细 (部分收费时没有水量,取消水量判断)
        if (yyQfmx.getYssl().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getWsf().compareTo(BigDecimal.ZERO) > 0) {
            EiFpkjMx eiFpkjMx = new EiFpkjMx();
            EiSpbm eiSpbm = eiSpbmService.getById("05"); // 污水费
            String sl = eiSpbm.getSl();
            if (StrUtil.isEmpty(sl) || new BigDecimal(sl).compareTo(BigDecimal.ONE) >= 0) {
                throw new FeesException("05-污水费商品税率配置错误", FeesErrorCode.BUSINESS_ERROR);
            }

            BigDecimal taxrate = new BigDecimal(eiSpbm.getSl());
            eiFpkjMx.setLsbh(lsbh);//流水编号，与汇总表关联
            eiFpkjMx.setGoodsname(eiSpbm.getName()); // 商品名称
            eiFpkjMx.setGoodscode(eiSpbm.getSszlbm()); // 商品编码
            eiFpkjMx.setWithtaxflag(eiSpbm.getDjHsbs()); // 单价含税标识
            eiFpkjMx.setInvoicelineproperty("0"); // 发票行性质
            eiFpkjMx.setFavouredpolicyflag(eiSpbm.getYhZcbs()); // 优惠政策
            eiFpkjMx.setFavouredpolicyname(eiSpbm.getYhZcmc()); // 优惠政策名称
            eiFpkjMx.setZerorateflag("1"); // 零税率标识
            eiFpkjMx.setUnit(eiSpbm.getDw()); // 单位
            eiFpkjMx.setSpectype("无");//规格型号
            eiFpkjMx.setTaxrate(taxrate); // 税率

            eiFpkjMx.setPrice(yyQfmx.getWsfdj()); // 单价
            eiFpkjMx.setNum(yyQfmx.getSl()); // 数量
            eiFpkjMx.setTax(yyQfmx.getWsf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
            eiFpkjMx.setTaxexcludedamount(yyQfmx.getWsf().subtract(eiFpkjMx.getTax()));
            eiFpkjMx.setTaxincludedamount(yyQfmx.getWsf());
            eiFpkjMxList.add(Deserialization(eiFpkjMx));
            //一表多价
            if (StringUtil.isNotEmpty(yyQfmx.getCbjsbz()) &&
                    (yyQfmx.getCbjsbz().equals("1") || yyQfmx.getCbjsbz().equals("3"))) {
                if (yyQfmx.getSl1().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getWsf1().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getWsfdj1()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getSl1()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getWsf1().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getWsf1().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getWsf1()); // 含税金额
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
                if (yyQfmx.getSl2().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getWsf2().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getWsfdj2()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getSl2()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getWsf2().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getWsf2().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getWsf2()); // 含税金额
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
            }
        }

        // 3.添加发票水资源税明细
        if (yyQfmx.getSl().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getSzyf().compareTo(BigDecimal.ZERO) > 0) {
            EiFpkjMx eiFpkjMx = new EiFpkjMx();
            EiSpbm eiSpbm = eiSpbmService.getById("07");
            String sl = eiSpbm.getSl();
            if (StrUtil.isEmpty(sl) || new BigDecimal(sl).compareTo(BigDecimal.ONE) >= 0) {
                throw new FeesException("07-水资源税商品税率配置错误", FeesErrorCode.BUSINESS_ERROR);
            }

            BigDecimal taxrate = new BigDecimal(sl);
            eiFpkjMx.setLsbh(lsbh);//流水编号，与汇总表关联
            eiFpkjMx.setGoodsname(eiSpbm.getName()); // 商品名称
            eiFpkjMx.setGoodscode(eiSpbm.getSszlbm()); // 商品编码
            eiFpkjMx.setWithtaxflag(eiSpbm.getDjHsbs()); // 单价含税标识
            eiFpkjMx.setUnit(eiSpbm.getDw()); // 单位
            eiFpkjMx.setSpectype("无");//规格型号
            eiFpkjMx.setTaxrate(taxrate); // 税率
            eiFpkjMx.setInvoicelineproperty("0"); // 发票行性质
            eiFpkjMx.setFavouredpolicyflag(eiSpbm.getYhZcbs()); // 优惠政策
            eiFpkjMx.setFavouredpolicyname(eiSpbm.getYhZcmc()); // 优惠政策名称
            eiFpkjMx.setZerorateflag("1"); // 零税率标识

            eiFpkjMx.setPrice(yyQfmx.getSzyfDj()); // 单价
            eiFpkjMx.setNum(yyQfmx.getSl()); // 数量
            eiFpkjMx.setTax(yyQfmx.getSzyf().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
            eiFpkjMx.setTaxexcludedamount(yyQfmx.getSzyf().subtract(eiFpkjMx.getTax())); // 不含税金额
            eiFpkjMx.setTaxincludedamount(yyQfmx.getSzyf()); // 含税金额
            eiFpkjMxList.add(Deserialization(eiFpkjMx));
            //一表多价
            if (StringUtil.isNotEmpty(yyQfmx.getCbjsbz()) &&
                    (yyQfmx.getCbjsbz().equals("1") || yyQfmx.getCbjsbz().equals("3"))) {
                if (yyQfmx.getSl1().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getSzyf1().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getSzyfDj1()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getSl1()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getSzyf1().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getSzyf1().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getSzyf1()); // 含税金额
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
                if (yyQfmx.getSl2().compareTo(BigDecimal.ZERO) > 0 && yyQfmx.getSzyf2().compareTo(BigDecimal.ZERO) > 0) {
                    eiFpkjMx.setPrice(yyQfmx.getSzyfDj2()); // 单价
                    eiFpkjMx.setNum(yyQfmx.getSl2()); // 数量
                    eiFpkjMx.setTax(yyQfmx.getSzyf2().multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
                    eiFpkjMx.setTaxexcludedamount(yyQfmx.getSzyf2().subtract(eiFpkjMx.getTax())); // 不含税金额
                    eiFpkjMx.setTaxincludedamount(yyQfmx.getSzyf2()); // 含税金额
                    eiFpkjMxList.add(Deserialization(eiFpkjMx));
                }
            }
        }

        // 4.添加发票违约金明细
        if (yyQfmx.getZnj() != null && yyQfmx.getZnj().compareTo(BigDecimal.ZERO) > 0) {
            EiFpkjMx eiFpkjMx = new EiFpkjMx();
            EiSpbm eiSpbm = eiSpbmService.getById("02"); //违约金
            String sl = eiSpbm.getSl();
            if (StrUtil.isEmpty(sl) || new BigDecimal(sl).compareTo(BigDecimal.ONE) >= 0) {
                throw new FeesException("02-违约金商品税率配置错误", FeesErrorCode.BUSINESS_ERROR);
            }

            eiFpkjMx.setLsbh(lsbh);//流水编号，与汇总表关联
            eiFpkjMx.setGoodsname(eiSpbm.getName()); // 商品名称
            eiFpkjMx.setGoodscode(eiSpbm.getSszlbm()); // 商品编码
            eiFpkjMx.setWithtaxflag(eiSpbm.getDjHsbs()); // 单价含税标识
            eiFpkjMx.setPrice(BigDecimal.ZERO); // 单价
            eiFpkjMx.setNum(BigDecimal.ZERO); // 数量
            eiFpkjMx.setUnit("无"); // 单位
            eiFpkjMx.setSpectype("无");//规格型号
            eiFpkjMx.setTaxrate(BigDecimal.valueOf(Double.parseDouble(eiSpbm.getSl()))); // 税率
            eiFpkjMx.setTax(yyQfmx.getZnj().multiply(eiFpkjMx.getTaxrate()).divide(eiFpkjMx.getTaxrate().add(BigDecimal.ONE), 2, RoundingMode.HALF_UP)); // 税额
            eiFpkjMx.setTaxexcludedamount(yyQfmx.getZnj().subtract(eiFpkjMx.getTax())); // 不含税金额
            eiFpkjMx.setTaxincludedamount(yyQfmx.getZnj()); // 含税金额
            eiFpkjMx.setInvoicelineproperty("0"); // 发票行性质
            eiFpkjMx.setFavouredpolicyflag(eiSpbm.getYhZcbs()); // 优惠政策
            eiFpkjMx.setFavouredpolicyname(eiSpbm.getYhZcmc()); // 优惠政策名称
            eiFpkjMx.setZerorateflag("1"); // 零税率标识
            eiFpkjMxList.add(Deserialization(eiFpkjMx));
        }

        // 5.水费无法合并附加、加压、消防栓等其他费用,安水费开出发票
        BigDecimal ysfjf = NumberUtil.null2Zero(yyQfmx.getYsfjf());
        BigDecimal ysecjyf = NumberUtil.null2Zero(yyQfmx.getYsecjyf());
        BigDecimal ysqt = NumberUtil.null2Zero(yyQfmx.getYsqt());
        BigDecimal qtfy = ysfjf.add(ysecjyf).add(ysqt);
        if (qtfy.compareTo(BigDecimal.ZERO) > 0) {
            EiSpbm eiSpbm = eiSpbmService.getById("01");
            EiFpkjMx eiFpkjMx = new EiFpkjMx();
            String sl = eiSpbm.getSl();
            if (StrUtil.isEmpty(sl) || new BigDecimal(sl).compareTo(BigDecimal.ONE) >= 0) {
                throw new FeesException("01商品税率配置错误", FeesErrorCode.BUSINESS_ERROR);
            }
            BigDecimal taxrate = new BigDecimal(sl);
            eiFpkjMx.setUnit("吨");
            eiFpkjMx.setTaxrate(taxrate);
            eiFpkjMx.setSpectype("无");
            eiFpkjMx.setLsbh(lsbh);
            eiFpkjMx.setGoodsname(eiSpbm.getName());
            eiFpkjMx.setGoodscode(eiSpbm.getSszlbm());
            eiFpkjMx.setWithtaxflag(eiSpbm.getDjHsbs());
            eiFpkjMx.setInvoicelineproperty("0");
            eiFpkjMx.setFavouredpolicyflag(eiSpbm.getYhZcbs());
            eiFpkjMx.setFavouredpolicyname(eiSpbm.getYhZcmc());
            eiFpkjMx.setZerorateflag(eiSpbm.getLslBs());
            eiFpkjMx.setPrice(BigDecimal.ZERO);
            eiFpkjMx.setNum(BigDecimal.ZERO);
            eiFpkjMx.setTax(qtfy.multiply(taxrate).divide(taxrate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP));
            eiFpkjMx.setTaxexcludedamount(qtfy.subtract(eiFpkjMx.getTax()));
            eiFpkjMx.setTaxincludedamount(qtfy);
            eiFpkjMxList.add(eiFpkjMx);
        }
        return eiFpkjMxList;
    }

    private void addInvoiceSummary(YyQfmx yyQfmx, YhKhjbxx yhKhjbxx,
                                   String lsbh, String remark, String sfryxm, String fpsfhb, String fplx,
                                   BigDecimal taxExcludedAmount, BigDecimal tax, BigDecimal taxIncludedAmount,
                                   String kpywlx, String sfid, String sfkp, String sfrybm) {

        EiFpkjRequest eiFpkjRequest = new EiFpkjRequest();
        eiFpkjRequest.setLsbh(lsbh); // 流水编号
        eiFpkjRequest.setSfid(sfid); //用于关联欠费明细与实收明细表
        eiFpkjRequest.setSflsbh(yyQfmx.getSflsbh());//收费流水编号
        eiFpkjRequest.setSsgs(yyQfmx.getSsgs()); // 所属公司
        eiFpkjRequest.setYhSspq(yyQfmx.getSscbpq()); // 所属片区
        eiFpkjRequest.setYhbh(yyQfmx.getYhbh()); // 用户编号
        eiFpkjRequest.setCbkh(yyQfmx.getCbkh()); // 抄表卡号
        eiFpkjRequest.setCwrq(yyQfmx.getCwrq()); // 财务月份
        eiFpkjRequest.setSsje(yyQfmx.getYsje().add(yyQfmx.getZnj())); // 实收金额=应收金额+违约金
        eiFpkjRequest.setTaxamount(tax);//合计税额
        eiFpkjRequest.setOrderamount(taxIncludedAmount);//含税金额
        eiFpkjRequest.setExtaxamount(taxExcludedAmount);//不含税金额
        eiFpkjRequest.setSflx("1"); // 收费类型

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strorderno = sdf.format(new Date()) + GuidGenerator.getGuid().substring(0, 6);
        eiFpkjRequest.setOrderno(strorderno);//订单号
        eiFpkjRequest.setInvoicedate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));// 订单时间
        eiFpkjRequest.setSfry(sfryxm); // 收费人员
        eiFpkjRequest.setPayee(sfryxm);// 收款人员
        eiFpkjRequest.setClerk(sfryxm);// 开票人员
        eiFpkjRequest.setClerkid(sfrybm);// 开票人员id
        eiFpkjRequest.setChecker(sfryxm);// 复核人员

        eiFpkjRequest.setSfrq(new Date()); // 收费日期
        eiFpkjRequest.setRemark(remark); // 备注
        eiFpkjRequest.setBuyername(StringUtil.isNotEmpty(yhKhjbxx.getFpName()) ? yhKhjbxx.getFpName() : yhKhjbxx.getYhxm()); // 购买方
        eiFpkjRequest.setBuyertaxnum(yhKhjbxx.getFpNsrsbh()); // 购方税号
        eiFpkjRequest.setBuyertel(yhKhjbxx.getFpLxdh()); // 购方电话
        eiFpkjRequest.setBuyeraddress(yhKhjbxx.getFpDz()); // 购方地址
        eiFpkjRequest.setBuyeraccount(yhKhjbxx.getFpBankcode()); // 购方银行账号
        eiFpkjRequest.setBuyerbank(yhKhjbxx.getFpBankname()); // 购方银行
        eiFpkjRequest.setInvoicetype("1"); // 开票类型（蓝票）
        eiFpkjRequest.setIsykfp("0");
        //判断是否开票（用户档案）
        if (StringUtil.isEmpty(yhKhjbxx.getSfkp())) {
            eiFpkjRequest.setFpSfkp("0");
        } else {
            eiFpkjRequest.setFpSfkp(yhKhjbxx.getSfkp());
        }
        //判断是否开票（预开发票）
        if (yyQfmx.getEiYkfpBz().equals("1")) {
            eiFpkjRequest.setFpSfkp("1");
            eiFpkjRequest.setIsykfp("1");
        }
        //判断是否开票（营业厅收费）
        if (StringUtil.isNotEmpty(sfkp) && sfkp.equals("1")) {
            eiFpkjRequest.setFpSfkp("1");
        }
        if (StringUtil.isNotEmpty(yhKhjbxx.getPushmode())) {
            eiFpkjRequest.setPushmode(Integer.valueOf(yhKhjbxx.getPushmode()));//推送方式
        } else {
            eiFpkjRequest.setPushmode(-1);
        }
        if (StringUtil.isNotEmpty(fplx) && fplx.equals("2")) {
            eiFpkjRequest.setInvoiceline("bs");//发票种类(专票)
        } else {
            eiFpkjRequest.setInvoiceline("pc");//发票种类(普票)
        }
        eiFpkjRequest.setSpecificfactor("0");//特定要素
        eiFpkjRequest.setListflag("0");//清单标识

        EiSalerConfig fpConfig = eiSalerConfigService.getById(1);
        eiFpkjRequest.setSalertaxnum(fpConfig.getSalertaxnum());//销方税号
        eiFpkjRequest.setSalertel(fpConfig.getSalertel());//销方电话
        eiFpkjRequest.setSaleraddress(fpConfig.getSaleraddress());//销方地址
        eiFpkjRequest.setSaleraccount(fpConfig.getSaleraccount());//销方银行账号
        eiFpkjRequest.setSalerbank(fpConfig.getSalerbank());//销方银行
        eiFpkjRequest.setYhlx(1);
        eiFpkjRequest.setProxyinvoiceflag("0");//代开标识
        eiFpkjRequest.setStatus("0");//发票状态
        eiFpkjRequest.setClLock("0");//是否锁定
        eiFpkjRequest.setClCljg("0");//处理结果
        eiFpkjRequest.setZfZfbz("0");//作废标识
        eiFpkjRequest.setJfYxbz("0");//交付有效标识
        eiFpkjRequest.setCxJfbz("0");//重新交付标识
        eiFpkjRequest.setFpCkbz("0");//发票重开标识
        if (fpsfhb.equals("0")) {
            eiFpkjRequest.setFpSfHb("0");
            eiFpkjRequest.setFpHbLx("0");
        } else {
            eiFpkjRequest.setFpSfHb("1");//发票是否合并开票1是0否
            eiFpkjRequest.setFpHbLx(fpsfhb);//合并开票类型，1多笔欠费，2多用户欠费  目前默认1
        }
        if (StringUtil.isNotEmpty(yhKhjbxx.getKhbm()) || Integer.valueOf("1").equals(yhKhjbxx.getDxh())) {
            eiFpkjRequest.setDxh("1");
        } else {
            eiFpkjRequest.setDxh("0");
        }
        eiFpkjRequest.setKpywlx(kpywlx);
        eiFpkjRequestService.save(eiFpkjRequest);
    }


    public void InvoiceRed(List<EiFpkjRequest> eiFpkjRequestList) {
        for (EiFpkjRequest eiFpkjRequest : eiFpkjRequestList) {
            //判断是否是预开发票，非预开发票的发票才能冲红
            if (!eiFpkjRequest.getIsykfp().equals("1")) {
                eiFpkjRequestService.dzfpch(eiFpkjRequest);
            }
        }
    }


    private EiFpkjMx Deserialization(EiFpkjMx eiFpkjMx) {
        return JSONObject.parseObject(JSON.toJSONString(eiFpkjMx), EiFpkjMx.class);
    }

}
