package com.seeyon.apps.src_hxinvoice.util;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ext.workflow.vo.FieldDataVo;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.src_hxinvoice.dao.IHxinvoiceDao;
import com.seeyon.apps.src_hxinvoice.dao.impl.HxinvoiceDaoImpl;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.SystemEnvironment;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import www.seeyon.com.utils.StringUtil;

import java.io.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class ParamUtil {
    private static Log log = Log.get(ParamUtil.class);

    private EnumManager enumManagerNew;
    public void setEnumManagerNew(EnumManager enumManagerNew) {this.enumManagerNew = enumManagerNew; }
    public EnumManager getEnumManagerNew() {
        if (enumManagerNew == null) {enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");}return enumManagerNew;
    }

    public String handleRequestParam(FormDataVo formDataVo, String request, String nodeId) throws NoSuchFieldException, IOException, BusinessException {
        String param = request;
        switch(nodeId){
            case "AddGenerateQdBdczlInvoiceNode":
                param =handleRent(formDataVo,request);
                param = paramMoneyInvoiceFormat(param);
                break;
            case "AddGenerateQdBdczlxzInvoiceNode":
                param =signHandleRent(formDataVo,request);
                param = paramMoneyInvoiceFormat(param);
                break;
            case "AddGenerateQdInvoiceNode":
                param = addGenerateQdInvoiceParam(formDataVo,request);
                if(param.contains("{")){
                    param = paramMoneyInvoiceFormat(param);
                }
                break;
            case "AddDanYiGenerateQdInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddDanYiGenerateQdBdczlInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddDanYiGenerateQdlkysfwInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddDanYiParkingFeeGenerateQdBdczlInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddGenerateQdHwysInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddReopenGenerateQdHwysInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddReopenGenerateQdcpyInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddReopenGenerateQdBdczlInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddReopenGenerateQdlkysfwInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddReopenParkingFeeGenerateQdBdczlInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddGenerateQdcpyInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddHongZIGenerateQdInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddReopenGenerateQdInvoiceNode":
                param = paramMoneyInvoiceFormat(request);
                break;
            case "AddWuYeGenerateQdInvoiceNode":
                param = handlePropertyManagementFee(formDataVo,request,"物业费税编");
                param = paramMoneyInvoiceFormat(param);
                break;
            case "AddWuYexinzengGenerateQdInvoiceNode":
                param = handlePropertyManagementFee(formDataVo,request,"物业费税编");
                param = paramMoneyInvoiceFormat(param);
                break;
            case "AddShiChangGenerateQdInvoiceNode":
                param = handlePropertyManagementFee(formDataVo,request,"市场费税编");
                param = paramMoneyInvoiceFormat(param);
                break;
            case "AddShiChangxinzengGenerateQdInvoiceNode":
                param = handlePropertyManagementFee(formDataVo,request,"市场费税编");
                param = paramMoneyInvoiceFormat(param);
                break;
        }
        return param;
    }

    private String addGenerateQdInvoiceParam(FormDataVo formDataVo,String request) throws NoSuchFieldException, BusinessException, IOException {
        log.info("进行附件处理");
        FileUtil fileUtil = new FileUtil();
        IHxinvoiceDao hxinvoiceDao = new HxinvoiceDaoImpl();
//        参数转JSON
        JSONObject jsonObject = JSONObject.parseObject(request);
//        获取参数名称
        String DDZXXName = jsonObject.getString("DDZXX");
        log.info("获取参数附件ID"+DDZXXName);
//        根据参数名称获取附件控件对象
//        FieldDataVo DDZXXNameVo = formDataVo.getFieldData(DDZXXName);
//        获取附件控件ID
//        String DDZXXValue = DDZXXNameVo.getStringValue();
//        设置附件下载路径
        String path = SystemEnvironment.getApplicationFolder()+"/hxinvoiceFile/"+DDZXXName+"/";
        log.info("文件下载路径"+path);
//        将附件下载到服务器
        List<String> filePaths = fileUtil.fieldFileDownload(Long.parseLong(DDZXXName),path);
//        开票人信息
        FieldDataVo kprvo = formDataVo.getFieldData("开票人");
        String kpr = kprvo.getStringValue();
        FieldDataVo shxmcvo = formDataVo.getFieldData("公司名称");
        String shxmc = shxmcvo.getStringValue();
        FieldDataVo xhfnsrsbhvo = formDataVo.getFieldData("销售方税号");
        String xhfnsrsbh = xhfnsrsbhvo.getStringValue();
//        创建DDZXX对象
        JSONArray DDZXXS = new JSONArray();
        for(String filePath : filePaths){
            JSONArray jsonArray = fileUtil.getFileContent(filePath);
            for(int i = 0 ; i < jsonArray.size();i++){
                double jshj = 0;
                FieldDataVo ddqqlshvo = formDataVo.getFieldData("订单请求批次号");
                String ddqqlsh = ddqqlshvo.getStringValue();
                JSONObject DDZXX = new JSONObject();
                JSONObject param = jsonArray.getJSONObject(i);
                JSONObject DDTXX = new JSONObject();
                DDTXX.put("GMFLX","01");//购买方类型

                DDTXX.put("XHFSBH",xhfnsrsbh);//销货方纳税人识别号
                DDTXX.put("GMFDH",param.getString("GMFSJ"));//购买方联系方式

                DDTXX.put("HJJE","0");//合计金额
                DDTXX.put("HJSE","0");//合计税额
                DDTXX.put("KPR",kpr);//开票人
//                DDTXX.put("DDQQLSH",ddqqlsh+"");//请求订单流水号。自定义
                DDTXX.put("DDQQLSH",ddqqlsh+"_"+param.getString("DDH"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String timestamp = sdf.format(new Date());
                String uniqueId = FourDigitUniqueIdGenerator.generateId();
                FieldDataVo gsbhvo = formDataVo.getFieldData("公司编号");
                String gsbh = gsbhvo.getStringValue();
//                DDTXX.put("DDQQLSH","ZZKPPLDDH_"+gsbh+timestamp+uniqueId);//请求订单流水号。自定义
                if(StringUtil.isEmpty(param.getString("DDH"))){
                    return "订单号为空，请检查上传附件数据";
                }
                DDTXX.put("DDH","ZZKPPLDDH_"+gsbh+timestamp+uniqueId);//订单号
                DDTXX.put("GMFDZYX",param.getString("GMFYX"));//购买方邮箱地址
                DDTXX.put("SX_XTLY","SX_OA");//系统标识，固定为SX_OA
                DDTXX.put("KPLX","0");//开票类型
                DDTXX.put("XHFMC",shxmc);//销货方名称
                DDTXX.put("GMFSJH",param.getString("GMFSJ"));//购买方联系方式
                DDTXX.put("BZ",param.getString("BZ"));//备注
                DDTXX.put("GMFMC",param.getString("GMFMC"));//购买方名称
                DDTXX.put("GMFDZ","");//购买方地址
                DDTXX.put("GMFSBH",param.getString("GMFNSRSBH"));//购买方税号
                DDTXX.put("SFZSGFDZDH","Y");//购方是否展示编码
                DDTXX.put("SFZSGFYHZH","Y");//购方是否展示编码
                DDTXX.put("SFZSXFDZDH","Y");//购方是否展示编码
                DDTXX.put("SFZSXFYHZH","Y");//购方是否展示编码
                DDTXX.put("GMFYH","");//购买方银行
                DDTXX.put("GMFZH","");//购买方银行账号

                JSONArray SPS = param.getJSONArray("SPS");
                JSONArray DDMXXXS = new JSONArray();
                int  bzkh = 0;int zkh = 0;
                for(int n = 0 ; n < SPS.size();n++){
//                    遍历商品信息，折扣行和被折扣行数量是否相等
                    JSONObject SP = SPS.getJSONObject(n);
                    String jestr = SP.getString("JE");
                    double jedou = Double.parseDouble(jestr);
                    jshj = jshj+jedou;
                    JSONObject DDMXXX = new JSONObject();
                    DDMXXX.put("SPSL",SP.getString("NUM"));//数量
                    DDMXXX.put("HSBZ","1");
                    if("折扣行".equals(SP.getString("FXHXZ"))){
                        DDMXXX.put("FPHXZ","1");//发票行性质
                        zkh++;
                    }else if ("被折扣行".equals(SP.getString("FXHXZ"))){
                        DDMXXX.put("FPHXZ","2");//发票行性质
                        bzkh++;
                    }else{
                        DDMXXX.put("FPHXZ","0");//发票行性质
                    }

                    DDMXXX.put("SE","");//税额
                    DDMXXX.put("YHZCBS","0");//优惠政策标识
                    DDMXXX.put("DJ",SP.getString("SPDJ"));//单价
                    String je = SP.getString("JE");
                    double jedecimalValue = 0;
                    try {
                        NumberFormat format = NumberFormat.getInstance(Locale.US); // 使用美国格式（千分位是逗号）
                        Number number = format.parse(je);
                        jedecimalValue = number.doubleValue();
                    } catch (ParseException e) {
                        log.info("解析失败: " + e.getMessage());
                    }
                    DDMXXX.put("JE",jedecimalValue);//金额
                    DDMXXX.put("SPID","");//商品ID
                    String spcode = SP.getString("SPBM");
                    Map<String,Object> goods = hxinvoiceDao.getGoodsByCode(spcode);
//                    BigDecimal sl = (BigDecimal) goods.get("sl");
                    DDMXXX.put("SL",SP.getString("SL"));//税率
                    if(StringUtil.isNotEmpty(SP.getString("YHZCBS"))){
                        DDMXXX.put("LSLBS","1");//增值税特殊管理
                        DDMXXX.put("YHZCBS","1");//增值税特殊管理
                        DDMXXX.put("ZZSTSGL","免税");//增值税特殊管理
                    }
//                    else{
//                        DDMXXX.put("LSLBS","");//增值税特殊管理
//                        DDMXXX.put("YHZCBS","");//增值税特殊管理
//                        DDMXXX.put("ZZSTSGL","");//增值税特殊管理
//                    }
//                    DDMXXX.put("ZZSTSGL",goods.get("zzstsgl"));//增值税特殊管理
                    DDMXXX.put("SPBM",spcode);//商品编码/税目编码
                    DDMXXX.put("DW",SP.getString("DW"));//单位
                    DDMXXX.put("GGXH",SP.getString("GGXH"));//规格
                    DDMXXX.put("XMMC",SP.getString("XMMC"));//商品税目名称
                    DDMXXXS.add(DDMXXX);
                }
//                校验发票行标识
                if(zkh!=bzkh){
                    return "订单号："+param.getString("DDH")+"，折扣行与被折扣行必须成对出现，请检查数据";
                }
                DDZXX.put("DDMXXX",DDMXXXS);
                DDTXX.put("JSHJ",jshj+"");//价税合计
                DDZXX.put("DDTXX",DDTXX);
                DDZXXS.add(DDZXX);
            }
        }
        jsonObject.put("DDZXX",DDZXXS);
        return jsonObject.toString();
    }


    /**
     * 进行数字转化，转换为保留两位小数的文本
     * @param request
     * @return
     */
    private String paramMoneyInvoiceFormat(String request){
        JSONObject jsonObject = JSON.parseObject(request);
        JSONArray DDZXXS = jsonObject.getJSONArray("DDZXX");
        if(DDZXXS==null){
            return request;
        }
        for(int i = 0 ; i < DDZXXS.size(); i++){
            JSONObject DDZXX = DDZXXS.getJSONObject(i);
            JSONObject DDTXX = DDZXX.getJSONObject("DDTXX");
            if(DDTXX!=null){
                if (DDTXX.containsKey("JSHJ")) {
                    DDTXX.put("JSHJ",toDecimalFormat2(DDTXX.getString("JSHJ")));
                }
                if (DDTXX.containsKey("HJJE")) {
                    DDTXX.put("HJJE",toDecimalFormat2(DDTXX.getString("HJJE")));
                }
                if (DDTXX.containsKey("HJSE")) {
                    DDTXX.put("HJSE",toDecimalFormat2(DDTXX.getString("HJSE")));
                }
            }
            JSONObject QDTXX = DDZXX.getJSONObject("QDTXX");
            if(QDTXX != null){
                if (QDTXX.containsKey("CEZSLIST")) {
                    JSONArray CEZSLISTS = QDTXX.getJSONArray("CEZSLIST");
                    if(CEZSLISTS!=null){
                        for(int n = 0 ; n < CEZSLISTS.size() ; n ++){
                            JSONObject CEZSLIST = CEZSLISTS.getJSONObject(n);
                            if(CEZSLIST!=null){
                                if (CEZSLIST.containsKey("HJJE")) {
                                    CEZSLIST.put("HJJE",toDecimalFormat2(CEZSLIST.getString("HJJE")));
                                }
                                if (CEZSLIST.containsKey("KCE")) {
                                    CEZSLIST.put("KCE",toDecimalFormat2(CEZSLIST.getString("KCE")));
                                }
                                if (CEZSLIST.containsKey("BCKCJE")) {
                                    CEZSLIST.put("BCKCJE",toDecimalFormat2(CEZSLIST.getString("BCKCJE")));
                                }
                                if (CEZSLIST.containsKey("PZHJJE")) {
                                    CEZSLIST.put("PZHJJE",toDecimalFormat2(CEZSLIST.getString("PZHJJE")));
                                }
                            }
                        }
                    }
                }
            }
            JSONArray DDMXXXS = DDZXX.getJSONArray("DDMXXX");
            if(DDMXXXS!=null){
                for(int n = 0 ; n < DDMXXXS.size() ; n++){
                    JSONObject DDMXXX = DDMXXXS.getJSONObject(n);
                    if(DDMXXX!=null){
                        if (DDMXXX.containsKey("DJ")) {
                            DDMXXX.put("DJ",toDecimalFormat6(DDMXXX.getString("DJ")));
                        }
                        if (DDMXXX.containsKey("SPSL")) {
                            DDMXXX.put("SPSL",toDecimalFormat6(DDMXXX.getString("SPSL")));
                        }
                        if (DDMXXX.containsKey("JE")) {
                            DDMXXX.put("JE",toDecimalFormat2(DDMXXX.getString("JE")));
                        }
                        if (DDMXXX.containsKey("SE")) {
                            DDMXXX.put("SE",toDecimalFormat2(DDMXXX.getString("SE")));
                        }
                        if (DDMXXX.containsKey("KCE")) {
                            DDMXXX.put("KCE",toDecimalFormat2(DDMXXX.getString("KCE")));
                        }
                    }
                }
            }
        }
        String param = jsonObject.toString();
        return param;
    }

    /**
     * 创建租赁合同签订租金调用开票接口参数
     * @param formDataVo    表单数据
     * @param request   封装参数数据
     * @return
     * @throws NoSuchFieldException
     */
    private String signHandleRent(FormDataVo formDataVo,String request) throws NoSuchFieldException {
        log.info("进行租赁合同签订不动产参数调整");
        JSONObject jsonObject = JSONObject.parseObject(request);
//        获取流水号，按照规则生成新的流水号
        JSONObject DDPCXX = jsonObject.getJSONObject("DDPCXX");
        DDPCXX.put("DDQQPCH","ZJQ"+DDPCXX.getString("DDQQPCH"));
        jsonObject.put("DDPCXX",DDPCXX);
//        获取明细表数据
        String subTableName = jsonObject.getString("subTable");
        Map<String, List<FormDataVo>> subTables = formDataVo.getSubFormMap();
//        获取指定明细表数据
        List<FormDataVo> subFormDataVos = subTables.get(subTableName);
        JSONArray DDZXXs = new JSONArray();
//        遍历明细表行数据
        for (FormDataVo subFormDataVo:subFormDataVos) {
            JSONObject DDZXX = new JSONObject();
            JSONObject DDTXX = new JSONObject();
            FieldDataVo JSHJvo = subFormDataVo.getFieldData("已开票租金");
            String JSHJ = JSHJvo.getStringValue();
            if(isNumberNull(JSHJ)){
                continue;
            }
            DDTXX.put("JSHJ",JSHJ);
            FieldDataVo BDCDZvo = formDataVo.getFieldData("不动产地址");
            String BDCDZ = BDCDZvo.getStringValue();
            FieldDataVo ZLQQZvo = subFormDataVo.getFieldData("租赁期起止");
            String ZLQQZ = ZLQQZvo.getStringValue();
            FieldDataVo KDSBZvo = formDataVo.getFieldData("跨地市标志");
            String KDSBZ = KDSBZvo.getStringValue();

            FieldDataVo GMFLXvo = formDataVo.getFieldData("购买方类型编码");
            String GMFLX = GMFLXvo.getStringValue();
            DDTXX.put("GMFLX",GMFLX);
            FieldDataVo XHFSBHvo = formDataVo.getFieldData("销售方纳税人识别号");
            String XHFSBH = XHFSBHvo.getStringValue();
            DDTXX.put("XHFSBH",XHFSBH);
            DDTXX.put("GMFDH","");//购买方联系方式
            FieldDataVo KPRvo = formDataVo.getFieldData("开票人");
            String KPR = KPRvo.getStringValue();
            DDTXX.put("KPR",KPR);
            FieldDataVo XHFYHvo = formDataVo.getFieldData("销货银行名称");
            String XHFYH = XHFYHvo.getStringValue();
            DDTXX.put("XHFYH",XHFYH);
            FieldDataVo XHFZHvo = formDataVo.getFieldData("销货银行账号");
            String XHFZH = XHFZHvo.getStringValue();
            DDTXX.put("XHFZH",XHFZH);
            FieldDataVo DDQQLSHvo = subFormDataVo.getFieldData("租金开票请求流水号");
            String DDQQLSH = DDQQLSHvo.getStringValue();
            DDTXX.put("DDQQLSH",DDQQLSH);
            FieldDataVo GMFDZYXvo = formDataVo.getFieldData("购买方邮箱");
            String GMFDZYX = GMFDZYXvo.getStringValue();
            DDTXX.put("GMFDZYX",GMFDZYX);
            DDTXX.put("SX_XTLY","SX_OA");//系统来源标识
            FieldDataVo KPLXvo = formDataVo.getFieldData("开票类型编码");
            String KPLX = KPLXvo.getStringValue();
            DDTXX.put("KPLX",KPLX);
            FieldDataVo XHFMCvo = formDataVo.getFieldData("销售方名称");
            String XHFMC = XHFMCvo.getStringValue();
            DDTXX.put("XHFMC",XHFMC);
            DDTXX.put("HJSE","0");//合计税额
            DDTXX.put("HJJE","0");//合计金额
            DDTXX.put("GMFSJH","");//购买方手机号
            FieldDataVo GMFMCvo = formDataVo.getFieldData("购买方名称");
            String GMFMC = GMFMCvo.getStringValue();
            DDTXX.put("GMFMC",GMFMC);
            FieldDataVo GMFSBHvo = formDataVo.getFieldData("购买方纳税人识别号");
            String GMFSBH = GMFSBHvo.getStringValue();
            DDTXX.put("GMFSBH",GMFSBH);

            FieldDataVo GFSFZSvo = formDataVo.getFieldData("购方是否展示文本");
            String GFSFZS = GFSFZSvo.getStringValue();
            FieldDataVo XFSFZSvo = formDataVo.getFieldData("销方是否展示文本");
            String XFSFZS = XFSFZSvo.getStringValue();
            DDTXX.put("SFZSGFDZDH",GFSFZS);
            DDTXX.put("SFZSGFYHZH",GFSFZS);
            DDTXX.put("SFZSXFDZDH",XFSFZS);
            DDTXX.put("SFZSXFYHZH",XFSFZS);

            DDTXX.put("DDH",DDQQLSH);
//            不动产地址:湖北省宜昌市伍家岗区东艳路48号;
//            租赁期起止:2025-01-01 2025-01-31;跨地(市)标志:否;
            FieldDataVo BZvo = formDataVo.getFieldData("租金开票备注");
            String BZ = BZvo.getStringValue();
            DDTXX.put("BZ",BZ);
            DDZXX.put("DDTXX",DDTXX);
            JSONObject QDTXX = new JSONObject();
            JSONArray BDCZLXXLISTs = new JSONArray();
            JSONObject BDCZLXXLIST = new JSONObject();
            BDCZLXXLIST.put("XH","1");
            BDCZLXXLIST.put("ZLQQZ",ZLQQZ);
            BDCZLXXLIST.put("KDSBZ",KDSBZ);
            FieldDataVo MJDWvo = formDataVo.getFieldData("面积单位");
            String MJDW = MJDWvo.getStringValue();
            BDCZLXXLIST.put("MJDW",MJDW);

            BDCZLXXLIST.put("BDCDZ",BDCDZ);
            FieldDataVo CQZSHvo = formDataVo.getFieldData("房屋产权证书号");
            String CQZSH = CQZSHvo.getStringValue();
            BDCZLXXLIST.put("CQZSH",CQZSH);
            BDCZLXXLISTs.add(BDCZLXXLIST);
            QDTXX.put("BDCZLXXLIST",BDCZLXXLISTs);
            DDZXX.put("QDTXX",QDTXX);

            JSONArray DDMXXXs = new JSONArray();
            JSONObject DDMXXX = new JSONObject();
            DDMXXX.put("XH","1");
            FieldDataVo SLvo = formDataVo.getFieldData("租金税率");
            String SL = SLvo.getStringValue();
            DDMXXX.put("SL",SL);
            DDMXXX.put("SPSL","1");
            DDMXXX.put("HSBZ","1");
            DDMXXX.put("SE","");
            FieldDataVo XMMCvo = formDataVo.getFieldData("租金项目名称");
            String XMMC = XMMCvo.getStringValue();
            if(StringUtil.isNotEmpty(XMMC)){
                DDMXXX.put("XMMC",XMMC);
            }else{
                DDMXXX.put("XMMC","不动产租赁");
            }
            DDMXXX.put("YHZCBS","0");
            DDMXXX.put("DJ",JSHJ);
            DDMXXX.put("JE",JSHJ);
            DDMXXX.put("SPID","");
            FieldDataVo SPBMvo = formDataVo.getFieldData("租金税编");
            String SPBM = SPBMvo.getStringValue();
            DDMXXX.put("SPBM",SPBM);
            DDMXXX.put("FPHXZ","0");
            DDMXXXs.add(DDMXXX);
            DDZXX.put("DDMXXX",DDMXXXs);

            DDZXXs.add(DDZXX);
        }
        jsonObject.remove("subTable");
        jsonObject.put("DDZXX",DDZXXs);
        return jsonObject.toString();
    }


    /**
     * 创建租赁租金调用开票接口参数
     * @param formDataVo    表单数据
     * @param request   封装参数数据
     * @return
     * @throws NoSuchFieldException
     */
    private String handleRent(FormDataVo formDataVo,String request) throws NoSuchFieldException, BusinessException {
        log.info("进行不动产参数调整");
        JSONObject jsonObject = JSONObject.parseObject(request);
//        获取流水号，按照规则生成新的流水号
        JSONObject DDPCXX = jsonObject.getJSONObject("DDPCXX");
        DDPCXX.put("DDQQPCH","ZJ"+DDPCXX.getString("DDQQPCH"));
        jsonObject.put("DDPCXX",DDPCXX);
        String subTableName = jsonObject.getString("subTable");
        Map<String, List<FormDataVo>> subTables = formDataVo.getSubFormMap();
        List<FormDataVo> subFormDataVos = subTables.get(subTableName);
        JSONArray DDZXXs = new JSONArray();
        for (FormDataVo subFormDataVo:subFormDataVos) {
            JSONObject DDZXX = new JSONObject();
            JSONObject DDTXX = new JSONObject();
            FieldDataVo JSHJvo = subFormDataVo.getFieldData("已开票租金");
            String JSHJ = JSHJvo.getStringValue();
            if(isNumberNull(JSHJ)){
                continue;
            }
            DDTXX.put("JSHJ",JSHJ);
            FieldDataVo BDCDZvo = formDataVo.getFieldData("不动产地址");
            String BDCDZ = BDCDZvo.getStringValue();
            FieldDataVo ZLQQZvo = subFormDataVo.getFieldData("租赁期起止");
            String ZLQQZ = ZLQQZvo.getStringValue();
            FieldDataVo KDSBZvo = formDataVo.getFieldData("跨地市标志");
            String KDSBZ = KDSBZvo.getStringValue();

            FieldDataVo GMFLXvo = formDataVo.getFieldData("购买方类型编码");
            String GMFLX = GMFLXvo.getStringValue();
            DDTXX.put("GMFLX",GMFLX);
            FieldDataVo XHFSBHvo = formDataVo.getFieldData("销售方纳税人识别号");
            String XHFSBH = XHFSBHvo.getStringValue();
            DDTXX.put("XHFSBH",XHFSBH);
            DDTXX.put("GMFDH","");//购买方联系方式

            FieldDataVo KPRvo = formDataVo.getFieldData("开票人");
            String KPR = KPRvo.getStringValue();
            DDTXX.put("KPR",KPR);
            FieldDataVo XHFYHvo = formDataVo.getFieldData("销货银行名称");
            String XHFYH = XHFYHvo.getStringValue();
            DDTXX.put("XHFYH",XHFYH);
            FieldDataVo XHFZHvo = formDataVo.getFieldData("销货银行账号");
            String XHFZH = XHFZHvo.getStringValue();
            DDTXX.put("XHFZH",XHFZH);
            FieldDataVo DDQQLSHvo = subFormDataVo.getFieldData("租金开票请求流水号");
            String DDQQLSH = DDQQLSHvo.getStringValue();
            DDTXX.put("DDQQLSH",DDQQLSH);
            FieldDataVo GMFDZYXvo = formDataVo.getFieldData("购买方邮箱");
            String GMFDZYX = GMFDZYXvo.getStringValue();
            DDTXX.put("GMFDZYX",GMFDZYX);
            DDTXX.put("SX_XTLY","SX_OA");//系统来源标识
            FieldDataVo KPLXvo = formDataVo.getFieldData("开票类型编码");
            String KPLX = KPLXvo.getStringValue();
            DDTXX.put("KPLX",KPLX);
            FieldDataVo XHFMCvo = formDataVo.getFieldData("销售方名称");
            String XHFMC = XHFMCvo.getStringValue();
            DDTXX.put("XHFMC",XHFMC);
            DDTXX.put("HJSE","0");//合计税额
            DDTXX.put("HJJE","0");//合计金额
            DDTXX.put("GMFSJH","");//购买方手机号
            FieldDataVo GMFMCvo = formDataVo.getFieldData("购买方名称");
            String GMFMC = GMFMCvo.getStringValue();
            DDTXX.put("GMFMC",GMFMC);
            FieldDataVo GMFSBHvo = formDataVo.getFieldData("购买方纳税人识别号");
            String GMFSBH = GMFSBHvo.getStringValue();
            DDTXX.put("GMFSBH",GMFSBH);

            FieldDataVo GFSFZSvo = formDataVo.getFieldData("购方是否展示文本");
            String GFSFZS = GFSFZSvo.getStringValue();
            FieldDataVo XFSFZSvo = formDataVo.getFieldData("销方是否展示文本");
            String XFSFZS = XFSFZSvo.getStringValue();
            DDTXX.put("SFZSGFDZDH",GFSFZS);
            DDTXX.put("SFZSGFYHZH",GFSFZS);
            DDTXX.put("SFZSXFDZDH",XFSFZS);
            DDTXX.put("SFZSXFYHZH",XFSFZS);

            DDTXX.put("DDH",DDQQLSH);
//            不动产地址:湖北省宜昌市伍家岗区东艳路48号;
//            租赁期起止:2025-01-01 2025-01-31;跨地(市)标志:否;
            FieldDataVo BZvo = formDataVo.getFieldData("租金开票备注");
            String BZ = BZvo.getStringValue();
            DDTXX.put("BZ",BZ);
            DDZXX.put("DDTXX",DDTXX);
            JSONObject QDTXX = new JSONObject();
            JSONArray BDCZLXXLISTs = new JSONArray();
            JSONObject BDCZLXXLIST = new JSONObject();
            BDCZLXXLIST.put("XH","1");
            BDCZLXXLIST.put("ZLQQZ",ZLQQZ);
            BDCZLXXLIST.put("KDSBZ",KDSBZ);
            FieldDataVo MJDWvo = formDataVo.getFieldData("面积单位");
            String MJDW = MJDWvo.getStringValue();
            BDCZLXXLIST.put("MJDW",MJDW);

            BDCZLXXLIST.put("BDCDZ",BDCDZ);
            FieldDataVo CQZSHvo = formDataVo.getFieldData("房屋产权证书号");
            String CQZSH = CQZSHvo.getStringValue();
            BDCZLXXLIST.put("CQZSH",CQZSH);
            BDCZLXXLISTs.add(BDCZLXXLIST);
            QDTXX.put("BDCZLXXLIST",BDCZLXXLISTs);
            DDZXX.put("QDTXX",QDTXX);

            JSONArray DDMXXXs = new JSONArray();
            JSONObject DDMXXX = new JSONObject();
            DDMXXX.put("XH","1");
            FieldDataVo SLvo = formDataVo.getFieldData("租金税率");
            String SL = SLvo.getStringValue();
            DDMXXX.put("SL",SL);
            DDMXXX.put("SPSL","1");
            DDMXXX.put("HSBZ","1");
            DDMXXX.put("SE","");
            FieldDataVo XMMCvo = formDataVo.getFieldData("租金项目名称");
            String XMMC = XMMCvo.getStringValue();
            if(StringUtil.isNotEmpty(XMMC)){
                DDMXXX.put("XMMC",XMMC);
            }else{
                DDMXXX.put("XMMC","不动产租赁");
            }
            DDMXXX.put("YHZCBS","0");
            DDMXXX.put("DJ",JSHJ);
            DDMXXX.put("JE",JSHJ);
            DDMXXX.put("SPID","");
            FieldDataVo SPBMvo = formDataVo.getFieldData("租金税编");
            String SPBM = SPBMvo.getStringValue();
            DDMXXX.put("SPBM",SPBM);
            DDMXXX.put("FPHXZ","0");
            DDMXXXs.add(DDMXXX);
            DDZXX.put("DDMXXX",DDMXXXs);

            DDZXXs.add(DDZXX);
        }
        jsonObject.remove("subTable");
        jsonObject.put("DDZXX",DDZXXs);
        return jsonObject.toString();
    }


    /**
     * 市场费、物业费开票参数处理
     * @param formDataVo
     * @param request
     * @param spbmname
     * @return
     * @throws NoSuchFieldException
     */
    private String handlePropertyManagementFee(FormDataVo formDataVo,String request,String spbmname) throws NoSuchFieldException {
        IHxinvoiceDao hxinvoiceDao = new HxinvoiceDaoImpl();
        JSONObject jsonObject = JSONObject.parseObject(request);
        JSONObject DDPCXX = jsonObject.getJSONObject("DDPCXX");
//        处理请求批次号
        if("市场费税编".equals(spbmname)){
            DDPCXX.put("DDQQPCH","SCQ"+DDPCXX.getString("DDQQPCH"));
        }else if("物业费税编".equals(spbmname)){
            DDPCXX.put("DDQQPCH","WYQ"+DDPCXX.getString("DDQQPCH"));
        }
        jsonObject.put("DDPCXX",DDPCXX);
        JSONArray DDZXXs = jsonObject.getJSONArray("DDZXX");
        JSONObject res = new JSONObject();
        res.put("DDPCXX",jsonObject.getJSONObject("DDPCXX"));
        if(DDZXXs!=null){
            for(int i = 0 ; i < DDZXXs.size();i++){
                JSONObject DDZXX = DDZXXs.getJSONObject(i);
                JSONArray DDMXXXs = DDZXX.getJSONArray("DDMXXX");
                JSONObject DDTXX = DDZXX.getJSONObject("DDTXX");
                JSONArray newDDZXXs = new JSONArray();
                for(int n = 0 ; n < DDMXXXs.size();n++){
                    JSONObject DDMXXX = DDMXXXs.getJSONObject(n);
                    //            查询金额是否为空，不为空则记录订单参数
                    if(isNumberNull(DDMXXX.getString("JE"))){
                        continue;
                    }
                    JSONObject newDDZXX = new JSONObject();

                    JSONArray newDDMXXXs = new JSONArray();
                    JSONObject toDDMXXX = new JSONObject(DDMXXX);
                    String DDMXXXstr = toDDMXXX.toString();
                    JSONObject newDDMXXX = JSONObject.parseObject(DDMXXXstr);
                    FieldDataVo SPBMvo = formDataVo.getFieldData(spbmname);
                    String SPBM = SPBMvo.getStringValue();
                    newDDMXXX.put("SPBM",SPBM);//获取税目编码
                    Map<String,Object> good = hxinvoiceDao.getGoodsByCode(SPBM);
                    newDDMXXX.put("SL",good.get("sl").toString());//根据税目编码查询税率
                    newDDMXXX.put("XH","1");//序号默认为1
                    newDDMXXXs.add(newDDMXXX);
                    newDDZXX.put("DDMXXX",newDDMXXXs);

                    JSONObject toDDTXX = new JSONObject(DDTXX);
                    String DDTXXstr = toDDTXX.toString();
                    JSONObject newDDTXX = JSONObject.parseObject(DDTXXstr);
                    newDDTXX.put("JSHJ",DDMXXX.getString("JE"));//每行创建一条开票哦信息价税合计为明细表单个金额
                    String DDQQLSH = DDTXX.getString("DDQQLSH");
//                    String xh = DDMXXX.getString("XH");
//                    String newDDQQLSH = "";
                    //            处理流水号信息。凭借固定类型+流水号+序号
//                    if("市场费税编".equals(spbmname)){
//                        newDDQQLSH = "ZLSCKPPCH"+DDQQLSH+xh;
//                    }else if("物业费税编".equals(spbmname)){
//                        newDDQQLSH = "ZLWYKPPCH"+DDQQLSH+xh;
//                    }

                    newDDTXX.put("DDH",DDQQLSH);
                    newDDTXX.put("DDQQLSH",DDQQLSH);
                    newDDZXX.put("DDTXX",newDDTXX);
                    newDDZXXs.add(newDDZXX);
                    res.put("DDZXX",newDDZXXs);
                }
            }
        }
        return res.toString();
    }


//    /**
//     * 市场费、物业费开票参数处理
//     * @param formDataVo
//     * @param request
//     * @param spbmname
//     * @return
//     * @throws NoSuchFieldException
//     */
//    private String handlePropertyManagementFee(FormDataVo formDataVo,String request,String spbmname) throws NoSuchFieldException {
//        IHxinvoiceDao hxinvoiceDao = new HxinvoiceDaoImpl();
//        JSONObject jsonObject = JSONObject.parseObject(request);
//        JSONObject DDPCXX = jsonObject.getJSONObject("DDPCXX");
////        处理请求批次号
//        if("市场费税编".equals(spbmname)){
//            DDPCXX.put("DDQQPCH","SCQ"+DDPCXX.getString("DDQQPCH"));
//        }else if("物业费税编".equals(spbmname)){
//            DDPCXX.put("DDQQPCH","WYQ"+DDPCXX.getString("DDQQPCH"));
//        }
//        jsonObject.put("DDPCXX",DDPCXX);
//        JSONArray DDZXXs = jsonObject.getJSONArray("DDZXX");
//        JSONObject res = new JSONObject();
//        res.put("DDPCXX",jsonObject.getJSONObject("DDPCXX"));
//        if(DDZXXs!=null){
//            JSONObject DDZXX = DDZXXs.getJSONObject(0);
//            JSONArray DDMXXXs = DDZXX.getJSONArray("DDMXXX");
//            JSONObject DDTXX = DDZXX.getJSONObject("DDTXX");
//            JSONArray newDDZXXs = new JSONArray();
//            for(int i = 0 ; i < DDMXXXs.size();i++){
//                JSONObject DDMXXX = DDMXXXs.getJSONObject(i);
//                //            查询金额是否为空，不为空则记录订单参数
//                if(isNumberNull(DDMXXX.getString("JE"))){
//                    continue;
//                }
//                JSONObject newDDZXX = new JSONObject();
//
//                JSONArray newDDMXXXs = new JSONArray();
//                JSONObject toDDMXXX = new JSONObject(DDMXXX);
//                String DDMXXXstr = toDDMXXX.toString();
//                JSONObject newDDMXXX = JSONObject.parseObject(DDMXXXstr);
//                FieldDataVo SPBMvo = formDataVo.getFieldData(spbmname);
//                String SPBM = SPBMvo.getStringValue();
//                newDDMXXX.put("SPBM",SPBM);//获取税目编码
//                Map<String,Object> good = hxinvoiceDao.getGoodsByCode(SPBM);
//                newDDMXXX.put("SL",good.get("sl").toString());//根据税目编码查询税率
//                newDDMXXX.put("XH","1");//序号默认为1
//                newDDMXXXs.add(newDDMXXX);
//                newDDZXX.put("DDMXXX",newDDMXXXs);
//
//                JSONObject toDDTXX = new JSONObject(DDTXX);
//                String DDTXXstr = toDDTXX.toString();
//                JSONObject newDDTXX = JSONObject.parseObject(DDTXXstr);
//                newDDTXX.put("JSHJ",DDMXXX.getString("JE"));//每行创建一条开票哦信息价税合计为明细表单个金额
//                String DDQQLSH = DDTXX.getString("DDQQLSH");
//                String xh = DDMXXX.getString("XH");
//                String newDDQQLSH = "";
//                //            处理流水号信息。凭借固定类型+流水号+序号
//                if("市场费税编".equals(spbmname)){
//                    newDDQQLSH = "ZLSCKPPCH"+DDQQLSH+xh;
//                }else if("物业费税编".equals(spbmname)){
//                    newDDQQLSH = "ZLWYKPPCH"+DDQQLSH+xh;
//                }
//
//                newDDTXX.put("DDH",newDDQQLSH);
//                newDDTXX.put("DDQQLSH",newDDQQLSH);
//                newDDZXX.put("DDTXX",newDDTXX);
//                newDDZXXs.add(newDDZXX);
//            }
//            res.put("DDZXX",newDDZXXs);
//        }
//        return res.toString();
//    }


    public String toDecimalFormat2(String decimalFormat){
        String formattedNum = decimalFormat;
        if(StringUtil.isNotEmpty(decimalFormat) && !"0".equals(formattedNum)){
            double num = Double.parseDouble(decimalFormat);
            DecimalFormat df = new DecimalFormat("0.00");
            formattedNum = df.format(num);
        }
        return formattedNum;
    }
    public String toDecimalFormat4(String decimalFormat){
        String formattedNum = decimalFormat;
        if(StringUtil.isNotEmpty(decimalFormat) && !"0".equals(formattedNum)){
            double num = Double.parseDouble(decimalFormat);
            DecimalFormat df = new DecimalFormat("0.0000");
            formattedNum = df.format(num);
        }
        return formattedNum;
    }
    public String toDecimalFormat6(String decimalFormat){
        String formattedNum = decimalFormat;
        if(StringUtil.isNotEmpty(decimalFormat) && !"0".equals(formattedNum)){
            double num = Double.parseDouble(decimalFormat);
            DecimalFormat df = new DecimalFormat("0.000000");
            formattedNum = df.format(num);
        }
        return formattedNum;
    }

    /**
     * 判断文本是否为空或者为0
     * @param num
     * @return
     */
    public boolean isNumberNull(String num){
        boolean is = false;
        if("0".equals(num) || StringUtil.isEmpty(num)){
            is = true;
        }
        return is;
    }

}
