package com.engine.htsc.payment.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.payment.service.SaveIntoFinancePaymentService;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.payment.SaveIntoFinanceAction;
import weaver.interfaces.htsc.payment.WorkFlowNameConvertor;
import weaver.interfaces.workflow.action.Action;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import static weaver.hrm.common.ReflectUtil.writeLog;

public class SaveIntoFinancePaymentServiceImpl extends Service implements SaveIntoFinancePaymentService {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    @Override
    public String saveIntoFinance(List<String> requestidList) throws Exception {
        String result = "";
        //根据requestid写入资金系统
        for(String requestId : requestidList){
            JAVATools.writelog("requestId: "+requestId+" 根据requestId写入资金系统 saveIntoFinance");
            String tableName = "";
            String code = "";
            String lcbh = "";

            //获取流程类型获取其主表名
            String sql = "select t1.tablename, t2.ECOLOGY_PINYIN_SEARCH as code,t2.WORKFLOWNAME\n" +
                    "  from workflow_bill t1, workflow_base t2, workflow_requestbase t3\n" +
                    " where t1.id = t2.formid\n" +
                    "   and t2.id = t3.workflowid\n" +
                    "   and t3.requestid = " + requestId;
            RecordSet rs = new RecordSet();
            JAVATools.writelog("requestId:" + requestId + "根据requestId获取流程类型和主表名Sql: " + sql);
            rs.executeQuery(sql);
            if (rs.next()){
                tableName = Util.null2String(rs.getString("tableName"));
                code = Util.null2String(rs.getString("code"));
            }

            //判断是否已经写入资金系统，有则不再进行写入
            if(!"".equals(tableName)) {
                String sqlMain = "select lcbh from " + tableName + " where requestId = "+requestId;
                rs.execute(sqlMain);
                if (rs.next()){
                    lcbh = Util.null2String(rs.getString("lcbh"));
                }
                RecordSetDataSource rsdM = new RecordSetDataSource("FinanceM");
                String sqlF = "select OAFKZJB_ZDDW,OAFKZJB_SQSJ,OAFKZJB_OABH,OAFKZJB_OAID from OAFKZJB where OAFKZJB_OAID = '"+lcbh+"'";
                rsdM.execute(sqlF);
                if (rsdM.next()){
                    result = lcbh+ " 该流程已写入资金系统";
                }else {
                    if("zbghfk".equals(code)||"yfkfyqr".equals(code)||"zbitfk".equals(code)||"zbtyfk".equals(code)||"fzjgtyfk".equals(code)){
                        //通用付款
                        result = tygkSaveIntoFinance(requestId,tableName);
                    }else if("zbtybx".equals(code)||"fzjgtybx".equals(code)){
                        //通用报销
                        result = tybxSaveIntoFinance(requestId,tableName);
                    }else if("zbclbx".equals(code)||"fzjgclbx".equals(code)){
                        //差旅报销
                        result = clbxSaveIntoFinance(requestId,tableName);
                    }else if("zbsljsfk".equals(code)||"fzjgsljsfk".equals(code)){
                        SaveIntoFinanceAction saveIntoFinanceAction = new SaveIntoFinanceAction();
                        RequestInfo requestInfo=new RequestInfo();
                        requestInfo.setRequestid(requestId);
                        RequestManager requestManager=new RequestManager();
                        requestManager.setBilltablename(tableName);
                        requestInfo.setRequestManager(requestManager);
                        result = saveIntoFinanceAction.execute(requestInfo);
                    }
                }
            }
        }
        return result;
    }

    private String clbxSaveIntoFinance(String requestId, String mainTableName) {
        try {
            //拼接获取明细表名
            String detailTableName = mainTableName + "_dt10";
            if(isFZF(requestId, mainTableName)) {
                JAVATools.writelog("requestId:" + requestId + " 全是非自付费用 不写资金表");
                return "SUCCESS";
            }
            JAVATools.writelog("requestId:" + requestId + " 开始写入资金系统表,差旅报销名:" + mainTableName);
            Map<String, Object> map = getOAMainData(requestId, mainTableName);
            innserttable(map, "OAFKZJB");
            JAVATools.writelog("requestId:" + requestId + " 写入资金中间表主表完成");
            List<List<Object>> insertList = getOADetailData(requestId, mainTableName, detailTableName);
            String sqlTable = "OAFKZJBMX_OABH,OAFKZJBMX_DFDWMC,OAFKZJBMX_DFYHZH,OAFKZJBMX_DFHM,OAFKZJBMX_BXRGH," +
                    "OAFKZJBMX_DFKHH,OAFKZJBMX_DFSF,OAFKZJBMX_DFCS,OAFKZJBMX_JE,OAFKZJBMX_JSZT";
            String sqlTableE = "?,?,?,?,?,?,?,?,?,?";
            insertList(sqlTable, sqlTableE, insertList, "FinanceM", "OAFKZJBMX");
            JAVATools.writelog("requestId:" + requestId + " 写入资金中间表明细表完成");
        }catch(Exception e) {
            e.printStackTrace();
            writeLog(e);
            return "FAIL";
        }
        return "SUCCESS";
    }

    private String tybxSaveIntoFinance(String requestId, String mainTableName) {
        try {
            //拼接获取明细表名
            String detailTableName = mainTableName + "_dt11";
            writeLog("requestId:" + requestId + " 开始写入资金系统表,通用报销主表名:" + mainTableName);
            Map<String, Object> map = getOAMainData(requestId, mainTableName);
            innserttable(map, "OAFKZJB");
            writeLog("requestId:" + requestId + " 写入资金中间表主表完成");

            List<List<Object>> insertList = getOATYBXDetailData(requestId, mainTableName, detailTableName);
            String sqlTable = "OAFKZJBMX_OABH,OAFKZJBMX_DFDWMC,OAFKZJBMX_DFYHZH,OAFKZJBMX_DFHM,OAFKZJBMX_BXRGH," +
                    "OAFKZJBMX_DFKHH,OAFKZJBMX_DFSF,OAFKZJBMX_DFCS,OAFKZJBMX_JE,OAFKZJBMX_JSZT";
            String sqlTableE = "?,?,?,?,?,?,?,?,?,?";
            insertList(sqlTable, sqlTableE, insertList, "FinanceM", "OAFKZJBMX");
            writeLog("requestId:" + requestId + " 写入资金中间表明细表完成");
        }catch(Exception e) {
            e.printStackTrace();
            writeLog(e);
            return "FAIL";
        }
        return "SUCCESS";
    }

    private String tygkSaveIntoFinance(String requestId, String tableName) {
        String lcbh ="";//业务流程编号
        //拼接获取明细表名
        String tableNameDt = tableName + "_dt11";
        //工会付款时存在dt13表
        String tableNameDt13 = tableName + "_dt13";

        JAVATools.SetCmdwritelog("写入资金系统表","主表为"+tableName);
        RecordSet rs = new RecordSet();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        try {
            String sqlMain = "select r.id ,r.requestid,r.lcbh,r.requestId,r.szbm,r.bz,r.ngsj,r.ngr ,d.DEPARTMENTNAME,d.DEPARTMENTCODE,s.LASTNAME,s.workcode " +
                    "from " + tableName +" r,hrmdepartment d,hrmresource s where r.szbm = d.id and s.id = r.ngr  and REQUESTID = "+requestId+"";
            rs.execute(sqlMain);
            JAVATools.SetCmdwritelog("查询" + tableName + "语句:" + sqlMain);
            if(rs.next()){
                Map<String,Object> map = new HashMap<>();
                String mainId = Util.null2String(rs.getString("id"));
                map.put("OAFKZJB_OAID", Util.null2String(rs.getString("lcbh")));
                lcbh = Util.null2String(rs.getString("lcbh"));
                map.put("OAFKZJB_OABH", WorkFlowNameConvertor.getEN_Lcbh(Util.null2String(rs.getString("lcbh"))));
                map.put("OAFKZJB_ZDDW", Util.null2String(rs.getString("DEPARTMENTCODE")));
                map.put("OAFKZJB_DWBH", Util.null2String(rs.getString("DEPARTMENTCODE")));
                map.put("OAFKZJB_FKBMMC",Util.null2String(rs.getString("DEPARTMENTNAME")));
                map.put("OAFKZJB_BZ",Util.null2String(rs.getString("bz")));
                String timeStr = Util.null2String(rs.getString("ngsj"));
                timeStr = timeStr + ":00";
                String[] timeStrs = timeStr.split(" ");
                String[] dataStrs = timeStrs[0].split("-");
                map.put("OAFKZJB_SQRQ",dataStrs[0]+dataStrs[1]+dataStrs[2]);
                map.put("OAFKZJB_SQSJ",timeStrs[1]);
                map.put("OAFKZJB_JSZT","0");
                //拟稿人
                map.put("OAFKZJB_SQR",Util.null2String(rs.getString("LASTNAME")));
                //拟稿人工号
                map.put("OAFKZJB_SQRGH",Util.null2String(rs.getString("workcode")));
                String date = sdf.format(new Date());
                String[] newDates = date.split(" ");
                map.put("OAFKZJB_BJRQ",newDates[0]);
                map.put("OAFKZJB_BJSJ",newDates[1]);
                innserttable(map,"OAFKZJB");
                String sqlDetail = "select d.skdw,d.yxzh,d.khx,d.ss,d.dfkje,d.khxmc,d.bz,d.yxzfje from "+tableNameDt+" d where mainid = "+mainId+" and d.yxzh is not null and d.yxzfje !=0 and d.yxzfje !=0.00";
                JAVATools.SetCmdwritelog("查询" + tableNameDt + "语句:" + sqlDetail);
                rs.execute(sqlDetail);
                List<List<Object>> insertList = new ArrayList<>();
                while (rs.next()){
                    List<Object> list = new ArrayList<>();
                    HashMap detailMap = new HashMap();
                    list.add(WorkFlowNameConvertor.getEN_Lcbh(lcbh));
                    //对skdw 进行格式化处理去除<br>
                    String skdw = Util.null2String(rs.getString("skdw")).replace("<br>","");
                    skdw = skdw.replace("&nbsp;","");
                    list.add(skdw);
                    String regEx="[^(0-9)]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(Util.null2String(rs.getString("yxzh")));
                    list.add(m.replaceAll("").trim());
                    list.add(skdw);
                    list.add("");
                    list.add(Util.null2String(rs.getString("khxmc")));
                    String area = Util.null2String(rs.getString("ss"));
                    List<String> areaArray = cityCodeToName(area);
                    if (null!=areaArray&&areaArray.size()>0){
                        list.add(areaArray.get(0));
                        if (areaArray.size()>1){
                            list.add(areaArray.get(1));
                        }else {
                            list.add("");
                        }
                    }else {
                        list.add("");
                        list.add("");
                    }
                    list.add(Util.null2String(rs.getString("yxzfje")));
                    list.add("0");
                    list.add("0");
                    list.add(Util.null2String(rs.getString("bz")));
                    insertList.add(list);
                }

                String sqlIsGH = "select * from workflow_base t1 where t1.formid in (" +
                        " select t2.id from workflow_bill t2 where tablename = '" + tableName.toLowerCase(Locale.ROOT) + "' ) " +
                        " and t1.unnumber = 'zbghfk'" ;
                rs.execute(sqlIsGH);
                if(rs.next()) {
                    String sqlDetail13 = "select s.lastname,d.yxzh,d.khx,d.ss,d.yxzfje,d.khxmc,d.bz from " + tableNameDt13 + " d ," +
                            " hrmresource s where d.ygxm=s.id and d.mainid = " + mainId + " and d.yxzfje !=0 ";
                    JAVATools.SetCmdwritelog("查询" + tableNameDt13 + "语句:" + sqlDetail13);
                    rs.execute(sqlDetail13);
                    while (rs.next()) {
                        List<Object> list = new ArrayList<>();
                        HashMap detailMap = new HashMap();
                        list.add(WorkFlowNameConvertor.getEN_Lcbh(lcbh));
                        list.add(Util.null2String(rs.getString("lastname")));
                        String regEx="[^(0-9)]";
                        Pattern p = Pattern.compile(regEx);
                        Matcher m = p.matcher(Util.null2String(rs.getString("yxzh")));
                        list.add(m.replaceAll("").trim());
                        list.add(Util.null2String(rs.getString("lastname")));
                        list.add("");
                        list.add(Util.null2String(rs.getString("khxmc")));
                        String area = Util.null2String(rs.getString("ss"));
                        List<String> areaArray = cityCodeToName(area);
                        if (null != areaArray && areaArray.size() > 0) {
                            list.add(areaArray.get(0));
                            if (areaArray.size() > 1) {
                                list.add(areaArray.get(1));
                            } else {
                                list.add("");
                            }
                        } else {
                            list.add("");
                            list.add("");
                        }
                        list.add(Util.null2String(rs.getString("yxzfje")));
                        list.add("0");
                        list.add("0");
                        list.add(Util.null2String(rs.getString("bz")));
                        insertList.add(list);
                    }
                }
                String sqlTable = "OAFKZJBMX_OABH,OAFKZJBMX_DFDWMC,OAFKZJBMX_DFYHZH,OAFKZJBMX_DFHM,OAFKZJBMX_BXRGH," +
                        "OAFKZJBMX_DFKHH,OAFKZJBMX_DFSF,OAFKZJBMX_DFCS,OAFKZJBMX_JE,OAFKZJBMX_DJZT,OAFKZJBMX_JSZT,OAFKZJBMX_YHBZ";
                String sqlTableE = "?,?,?,?,?,?,?,?,?,?,?,?";
                insertList(sqlTable,sqlTableE,insertList,"FinanceM","OAFKZJBMX");

            }

        } catch (Exception e) {
            e.printStackTrace();
            return "FAIL";
        }
        return "SUCCESS";
    }

    //批量插入表单，切换数据源
    public boolean insertList(String sqlTableS,String sqlTableE,List<List<Object>> insertList,String sqlData,String tablename){
        boolean flag = true;
        try {
            RecordSet rs = new RecordSet();
            JAVATools.SetCmdwritelog("新表插入:", "表名" + tablename + "数据源为" + sqlData);
            JAVATools.SetCmdwritelog("sql:" + String.format("insert into %s (" + sqlTableS + ") values(" + sqlTableE + ") ", tablename));
            JAVATools.SetCmdwritelog("数据:" + insertList);
            //批量插入并切换数据源
            flag = rs.executeBatchSql(String.format("insert into %s (" + sqlTableS + ") values(" + sqlTableE + ") ", tablename), insertList, sqlData);
        }catch (Exception exo){
            flag=false;
            exo.printStackTrace();
            JAVATools.writelog("插入明细表失败:"+exo.getMessage());
        }
        return flag;
    }

    //插入表
    public static boolean innserttable(Map<String,Object> mapvalue, String tablename){
        boolean flag=true;
        String dttablename="";
        String dttablenvalue="";
        List<String> list=new ArrayList<>();
        JAVATools.SetCmdwritelog("新表插入:","表名"+tablename);
        for (Map.Entry<String, Object> entry : mapvalue.entrySet())
        {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            JAVATools.SetCmdwritelog(" key: "+fieldname+" value:"+fieldvalue);
            dttablename += dttablename == "" ? fieldname : "," + fieldname;
            dttablenvalue += dttablenvalue == "" ? "'"+Lengthmax(fieldvalue.replace("\r\n",""),150)+"'" : ",'"+Lengthmax(fieldvalue.replace("\r\n",""),150)+"'";
        }
        String sql = "insert into "+tablename+"("+dttablename+") values("+dttablenvalue+")";
        JAVATools.SetCmdwritelog(" sql: "+sql);
        JAVATools.SetCmdwritelog(" sql: "+list.toString());
        try {
            RecordSetDataSource rsdM = new RecordSetDataSource("FinanceM");
            flag = rsdM.execute(sql);
        }catch (Exception exo){
            flag=false;
            exo.printStackTrace();
            new BaseBean().writeLog("插入明细表失败:"+exo.getMessage());
        }
        return flag;
    }

    public static String Lengthmax(String value,Integer sum){
        if("".equals(value)) return "";
        if(value.length()>sum){
            value=value.substring(0,sum-1);
        }
        return  value;
    }

    public static List<String> cityCodeToName(String code){
        RecordSet rs = new RecordSet();
        List<String> resultList = new ArrayList<>();
        if (StringUtils.isBlank(code)){
            return resultList;
        }
        String[] desInfo = code.split(",");
        String ids = Arrays.stream(desInfo).map(s -> "\'" + s +"\'").collect(Collectors.joining(","));
        String sql = "select name from uf_city where code in (" + ids + ") order by instr('" + code + "',code)";
        rs.executeQuery(sql);
        while (rs.next()){
            resultList.add(rs.getString("name"));
        }
        return resultList;
    }

    /**
     * 获取资金表主表数据
     * @param requestId
     * @param mainTableName
     * @return
     */
    Map<String,Object> getOAMainData(String requestId, String mainTableName) {
        RecordSet rs = new RecordSet();
        Map<String,Object> map = new HashMap<>();
        String sqlMain = " select r.id ,r.requestid,r.lcbh,r.requestId,r.szbm,r.bz,r.ngsj,r.ngr ,d.DEPARTMENTNAME,d.DEPARTMENTCODE,s.LASTNAME,s.workcode " +
                " from "+mainTableName+" r,hrmdepartment d,hrmresource s where r.szbm = d.id and s.id = r.ngr  and REQUESTID = '"+requestId+"'";
        JAVATools.writelog("requestId:"+requestId+" 关联查询查询主表sql:"+sqlMain);
        rs.execute(sqlMain);
        if(rs.next()){
            String mainId = Util.null2String(rs.getString("id"));
            map.put("OAFKZJB_OAID", Util.null2String(rs.getString("lcbh")));
            map.put("OAFKZJB_OABH", WorkFlowNameConvertor.getEN_Lcbh(Util.null2String(rs.getString("lcbh"))));
            map.put("OAFKZJB_ZDDW", Util.null2String(rs.getString("DEPARTMENTCODE")));
            map.put("OAFKZJB_DWBH", Util.null2String(rs.getString("DEPARTMENTCODE")));
            map.put("OAFKZJB_FKBMMC",Util.null2String(rs.getString("DEPARTMENTNAME")));
            map.put("OAFKZJB_BZ",Util.null2String(rs.getString("bz")));
            String timeStr = Util.null2String(rs.getString("ngsj"));
            timeStr = timeStr + ":00";
            String[] timeStrs = timeStr.split(" ");
            String[] dataStrs = timeStrs[0].split("-");
            map.put("OAFKZJB_SQRQ",dataStrs[0]+dataStrs[1]+dataStrs[2]);
            map.put("OAFKZJB_SQSJ",timeStrs[1]);
            map.put("OAFKZJB_JSZT","0");
            map.put("OAFKZJB_SQR",Util.null2String(rs.getString("LASTNAME")));
            map.put("OAFKZJB_SQRGH",Util.null2String(rs.getString("workcode")));
            String date = sdf.format(new Date());
            String[] newDates = date.split(" ");
            map.put("OAFKZJB_BJRQ",newDates[0]);
            map.put("OAFKZJB_BJSJ",newDates[1]);
        }
        return map;
    }

    /**
     * 获取资金表明细表数据
     * @param requestId
     * @param mainTableName
     * @param dtTableName
     * @return
     */
    List<List<Object>> getOADetailData(String requestId, String mainTableName, String dtTableName) {
        RecordSet rs = new RecordSet();
        List<List<Object>> insertList = new ArrayList<>();
        String sqlDetail = "  select t0.requestid,t1.lastname,d.yxzh,t1.lastname,d.khxmc,d.ss,t1.workcode, d.yxzfje,d.xjzfje,t0.lcbh " +
                "  from "+dtTableName + " d,"+ mainTableName +" t0,hrmresource t1 where t0.id = d.mainid and d.bxr = t1.id and t0.requestid = '"+requestId+"'";
        JAVATools.writelog("requestId:"+requestId+" 关联查询查询明细表sql:"+sqlDetail);
        rs.execute(sqlDetail);
        while(rs.next()) {
            List<Object> list = new ArrayList<>();
            list.add(WorkFlowNameConvertor.getEN_Lcbh(Util.null2String(rs.getString("lcbh"))));
            list.add(Util.null2String(rs.getString("lastname")));
            String regEx="[^(0-9)]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(Util.null2String(rs.getString("yxzh")));
            list.add(m.replaceAll("").trim());
            list.add(Util.null2String(rs.getString("lastname")));
            list.add(Util.null2String(rs.getString("workcode")));
            list.add(Util.null2String(rs.getString("khxmc")));
            //list.add(Util.null2String(rs.getString("shzxs")));// 省或直辖市
            //list.add(Util.null2String(rs.getString("cs")));// 城市
            String area = Util.null2String(rs.getString("ss"));
            List<String> areaArray = cityCodeToName(area);
            if (null!=areaArray&&areaArray.size()>0){
                list.add(areaArray.get(0));
                if (areaArray.size()>1){
                    list.add(areaArray.get(1));
                }else {
                    list.add("");
                }
            }else {
                list.add("");
                list.add("");
            }
            BigDecimal b =BigDecimal.valueOf(StringUtils.isEmpty(rs.getString("yxzfje")) ? 0.00 :Double.parseDouble(rs.getString("yxzfje")));
            //b.add(BigDecimal.valueOf(StringUtils.isEmpty(rs.getString("xjzfje")) ? 0.00 :Double.parseDouble(rs.getString("xjzfje")))); //周伟说现金支付不写明细表
            list.add(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            list.add("0");
            //金额为0的情况不写入资金明细表 且 银行账户不为空
            if(b.compareTo(BigDecimal.ZERO)>0&&null!=rs.getString("yxzh")&&org.apache.commons.lang.StringUtils.isNotEmpty(rs.getString("yxzh"))) {
                insertList.add(list);
            }
        }
        return insertList;
    }



    List<List<Object>> getOATYBXDetailData(String requestId, String mainTableName, String dtTableName) {
        RecordSet rs = new RecordSet();
        List<List<Object>> insertList = new ArrayList<>();
        String sqlDetail = " select t0.requestid,t1.lastname,d.yxzh,t1.lastname,d.khxmc,d.ss,t1.workcode, "+
                " d.yxzfje,d.xjzfje,t0.lcbh "+
                " from "+dtTableName+" d, "+ mainTableName +" t0,hrmresource t1 where t0.id = d.mainid and d.ygxm = t1.id and t0.requestid = '"+requestId+"'";
        writeLog("requestId:"+requestId+" 关联查询查询明细表sql:"+sqlDetail);
        rs.execute(sqlDetail);
        while(rs.next()) {
            List<Object> list = new ArrayList<>();
            list.add(WorkFlowNameConvertor.getEN_Lcbh(Util.null2String(rs.getString("lcbh"))));
            list.add(Util.null2String(rs.getString("lastname")));
//            list.add(Util.null2String(rs.getString("yxzh")));
            String regEx="[^(0-9)]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(Util.null2String(rs.getString("yxzh")));
            list.add(m.replaceAll("").trim());
            list.add(Util.null2String(rs.getString("lastname")));
            list.add(Util.null2String(rs.getString("workcode")));
            list.add(Util.null2String(rs.getString("khxmc")));
            String area = Util.null2String(rs.getString("ss"));
            List<String> areaArray = cityCodeToName(area);
            if (null!=areaArray&&areaArray.size()>0){
                list.add(areaArray.get(0));
                if (areaArray.size()>1){
                    list.add(areaArray.get(1));
                }else {
                    list.add("");
                }
            }else {
                list.add("");
                list.add("");
            }
            BigDecimal b =BigDecimal.valueOf(StringUtils.isEmpty(rs.getString("yxzfje")) ? 0.00 :Double.parseDouble(rs.getString("yxzfje")));
            //b.add(BigDecimal.valueOf(StringUtils.isEmpty(rs.getString("xjzfje")) ? 0.00 :Double.parseDouble(rs.getString("xjzfje"))));  //周伟说现金支付不写明细表
            list.add(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            list.add("0");
            //金额为0的情况不写入资金明细表 且 银行账户不为空
            if(b.compareTo(BigDecimal.ZERO)>0&&null!=rs.getString("yxzh")&&org.apache.commons.lang.StringUtils.isNotEmpty(rs.getString("yxzh"))) {
                insertList.add(list);
            }
        }
        return insertList;
    }


    /**
     * 判断是是否是非自付（非自付不写资金表）
     * @param requestId
     * @param mainTableName
     */
    public boolean isFZF (String requestId, String mainTableName) {
        RecordSet rs = new RecordSet();
        String querySql = " select t.xj from "+mainTableName+"_dt12 t where t.mainid in ( select id from "+mainTableName+"  t where t.requestid = '"+requestId+"' )";
        JAVATools.writelog("requestId:"+requestId+" 查询是否为全部非自付SQL:"+querySql);
        BigDecimal totalZFB = new BigDecimal("0.00");
        rs.executeQuery(querySql);
        while(rs.next()) {
            BigDecimal tempB = (new BigDecimal(StringUtils.isEmpty(rs.getString("xj")) ? 0.00 :Double.parseDouble(rs.getString("xj")))).setScale(2, BigDecimal.ROUND_HALF_UP);
            JAVATools.writelog("requestId:"+requestId+" 查询是否为全部非自付小计金额tempB:"+tempB);
            totalZFB = (totalZFB.add(tempB).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        JAVATools.writelog("requestId:"+requestId+" 自付费用汇总金额 totalZFB:"+totalZFB);
        if(totalZFB.compareTo(BigDecimal.ZERO)==0) {
            return true;//没有自付费用
        }
        return false;
    }
}
