package com.steering.jobface.jhy.Task;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.jobface.jhy.CollectionType.BaseCollection;
import com.steering.jobface.mch.CoreRtdMeasurePosDataTask;
import com.steering.model.qcm.PipeCheckconsignPhydetailEntity;
import com.steering.model.qcm.PipeCheckconsignPhyrelationEntity;
import org.apache.log4j.Logger;
import javax.script.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

/**
 * @ClassName: PhysicalCollectionToJhyDb
 * @Description: 物理采集数据自动同步至检化验系统, 再进行下一步审核
 * @author: 朱少波
 * @date: 2018年3月8日
 */
public class PhysicalCollectionToJhyDbTask extends TimerTask {
    private final Logger logger = Logger.getLogger(CoreRtdMeasurePosDataTask.class);

    private Object lock = new Object();

    private BaseCollection PhyCollection = null;

    private Map<String, Field> field = null;

    private Map<String, Field> GetFields() {
        if (field == null || field.size() <= 0) {
            field = new HashMap<String, Field>();
            Field[] fields = PipeCheckconsignPhydetailEntity.class.getDeclaredFields();

            for (Field tempfield : fields
                    ) {
                field.put(getSqlFieldName(tempfield.getName()), tempfield);
            }
        }
        return field;
    }

    private static String getSqlFieldName(String fieldName) {
        StringBuilder sb = new StringBuilder(fieldName);
        for (int i = 1; i < sb.length(); i++) {
            char ch = sb.charAt(i);
            if (Character.isUpperCase(ch)) {
                sb.insert(i, '_');
                i++;
            }
        }

        return sb.toString().toUpperCase();
    }

    public PhysicalCollectionToJhyDbTask(BaseCollection BaseCollection) {
        PhyCollection = BaseCollection;
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    public void run() {
        // 同步锁
        synchronized (lock) {

            long a = System.currentTimeMillis();
            DBCommon db = new DBCommon();
            Connection JhyCon = null;

            try {


                //从各个试验结果集找到未同步的数据
                List<Map<String, Object>> CollectDatas = PhyCollection.GetCollectionData();

                Map<String, PipeCheckconsignPhydetailEntity> NeedUpdateEntitys = new HashMap<String, PipeCheckconsignPhydetailEntity>();
                String PhyDetailsql = CoreXmlSqlParsersFactory
                        .getSql("PhysicalCollectionToJhyDbTask.getPipeCheckconsignPhyDetail");
                String PhyRelationsql = CoreXmlSqlParsersFactory
                        .getSql("PhysicalCollectionToJhyDbTask.getPipeCheckconsignPhyRelation");

                for (Map<String, Object> Data : CollectDatas
                        ) {
                    ArrayList<Object> parameter = new ArrayList<Object>();
                    String CheckNo = String.valueOf(Data.get("CHECK_NO"));
                    String SampleNo = String.valueOf(Data.get("SAMPLE_NO"));

                    parameter.add(CheckNo);
                    //parameter.add(SampleNo.lastIndexOf("-") ==SampleNo.length()-2?  SampleNo.split("-")[0]:SampleNo );
                    parameter.add(SampleNo );


                    //找到委托详细表  根据检验大项 找到所有检验编号和试样号下的 详细
                    PhyDetailsql = PhyDetailsql + CoreConverter.toSqlConditionString("PHY_CODE_MIN",
                            PhyCollection.GetPhyCodeMin());

                    JhyCon = db.getDao("JhyDao").getConnection();
                    List<PipeCheckconsignPhydetailEntity> Entitys = EntityTool.getEntitysBySqlParameter(
                            PhyDetailsql, JhyCon, parameter, PipeCheckconsignPhydetailEntity.class);


                    // 若在产销系统无检验详细,   认为老数据或者错误数据  标记  防止一直获取该数据
                    if (Entitys == null || Entitys.size() <= 0) {
                        PhyCollection.RemarkOldData(CheckNo, SampleNo);
                        continue;
                    }

                    for (PipeCheckconsignPhydetailEntity entity : Entitys
                            ) {

                        // 若当前状态不是录入状态   不允许同步或者修改
                        if (!"2".equals(entity.getFlag()) && !"3".equals(entity.getFlag())) {
                            PhyCollection.RemarkOldData(CheckNo, SampleNo);
                            continue;
                        }


                        entity.setSampleNo(SampleNo);


                        // 若一条数据结果对应多条 检验小项      找到之前写入的实体类
                        PipeCheckconsignPhydetailEntity NeedUpdateEntity = entity;
                        if(NeedUpdateEntitys.containsKey(entity.getPhydetailId())){
                            NeedUpdateEntity = NeedUpdateEntitys.get(entity.getPhydetailId());
                        }



                        //循环detail 实体类的属性
                        for (Map.Entry<String, Field> entry : GetFields().entrySet()) {
                            ArrayList<Object> parameter2 = new ArrayList<Object>();
                            parameter2.add(entity.getPhyCode());
                            parameter2.add(entity.getPhyCodeMin());
                            parameter2.add(entity.getPhyCodeMax());
                            parameter2.add(entry.getKey());

                            //找到实体类的属性  是否在关系对应表  存在和结果集对应的关系
                            JhyCon = db.getDao("JhyDao").getConnection();
                            List<PipeCheckconsignPhyrelationEntity> Relations = EntityTool.getEntitysBySqlParameter(
                                    PhyRelationsql, JhyCon, parameter2, PipeCheckconsignPhyrelationEntity.class);

                            if (Relations == null || Relations.size() <= 0) continue;

                            //存在对应关系   循环对应关系   获取计算后的值赋值给实体类
                            for (PipeCheckconsignPhyrelationEntity PipeCheckconsignPhyrelationEntity : Relations) {
                                try {
                                    //根据关系表中的  限制字段  判断是否同步
                                    if(!CanSetEntity(Data, PipeCheckconsignPhyrelationEntity)) continue;

                                    //根据公式 获取计算后的值赋值给实体类
                                    //如果获取到值  更新实体类
                                    if (SetEntityData(Data, NeedUpdateEntity, entry, PipeCheckconsignPhyrelationEntity)) {

                                        NeedUpdateEntity.setOrData(Data);
                                        NeedUpdateEntity.setPhyCodeDir(PipeCheckconsignPhyrelationEntity.getPhyCodeDir());
                                        NeedUpdateEntity.setPhyNameDir(PipeCheckconsignPhyrelationEntity.getPhyNameDir());
                                        NeedUpdateEntitys.put(NeedUpdateEntity.getPhydetailId(), NeedUpdateEntity);

                                    }

                                } catch (Exception ignored) {
                                }
                            }

                        }


                    }


                }


                if(CollectDatas.size()>0  &&  NeedUpdateEntitys.size()<=0){
                    for (Map<String, Object> Data : CollectDatas
                            ) {
                        ArrayList<Object> parameter = new ArrayList<Object>();
                        String CheckNo = String.valueOf(Data.get("CHECK_NO"));
                        String SampleNo = String.valueOf(Data.get("SAMPLE_NO"));
                        PhyCollection.RemarkFailData(CheckNo, SampleNo);
                    }
                }

                JhyCon = db.getDao("JhyDao").getConnection();

                for (Map.Entry<String, PipeCheckconsignPhydetailEntity> Entity : NeedUpdateEntitys.entrySet()) {
                    String sql = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory
                            .getSql("PhysicalCollectionToJhyDbTask.UpdateCheckConsignDetail"), Entity);
                    PreparedStatement ps = JhyCon.prepareStatement(sql);
                    Map<String, Object> OrData = Entity.getValue().getOrData();
                   // String Date = OrData.containsKey("试验日期") ? OrData.get("试验日期")==null?"": String.valueOf(OrData.get("试验日期")): (OrData.containsKey("日期") ? OrData.get("日期")==null?"": String.valueOf(OrData.get("日期")): "");



                    String Size = OrData.containsKey("拉伸试样尺寸") ? String.valueOf(OrData.get("拉伸试样尺寸")) :
                            (OrData.containsKey("长度") &&OrData.containsKey("厚度") )?
                                    String.valueOf(OrData.get("厚度")) +"mm×" +String.valueOf(OrData.get("长度")) +"mm"
                                    :  OrData.containsKey("SAMPLE_TYPE") ? String.valueOf(OrData.get("SAMPLE_TYPE"))
                                    :"";

                    String PhyDir =  OrData.containsKey("拉伸试样取向") ? String.valueOf(OrData.get("拉伸试样取向")) :
                            (OrData.containsKey("试样取向") ? String.valueOf(OrData.get("试样取向")) :
                                    OrData.containsKey("PHY_DIR") ? String.valueOf(OrData.get("PHY_DIR")) :
                                    "");


                    ps.setString(1, OrData.containsKey("试验温度") ? String.valueOf(OrData.get("试验温度"))
                            :  OrData.containsKey("温度") ? String.valueOf(OrData.get("温度"))
                            :  OrData.containsKey("CHECK_TEMP") ? String.valueOf(OrData.get("CHECK_TEMP"))
                            : "");
                    ps.setString(2, OrData.containsKey("使用设备") ? String.valueOf(OrData.get("使用设备")) : "");
                    ps.setString(3, "系统同步采集数据");
                    ps.setString(4, OrData.containsKey("班组") ? ChangeClass(String.valueOf(OrData.get("班组"))) : "");
                    ps.setString(5, OrData.containsKey("班次") ? ChangeGroup(String.valueOf(OrData.get("班次"))) : "");
                    ps.setString(6, OrData.containsKey("试验者") ? String.valueOf(OrData.get("试验者")) : (OrData.containsKey("报出") ? String.valueOf(OrData.get("报出")):""));
                    ps.setString(7, Size);
                    ps.setString(8, OrData.containsKey("试验标准") ? String.valueOf(OrData.get("试验标准"))
                            : (OrData.containsKey("检验标准") ? String.valueOf(OrData.get("检验标准"))
                            : (OrData.containsKey("FORMUAL_NAME") ? String.valueOf(OrData.get("FORMUAL_NAME"))
                            : "")));
                    ps.setString(9, "3");
                    ps.setString(10, OrData.containsKey("原始面积") ? String.valueOf(OrData.get("原始面积"))
                            : (OrData.containsKey("ORIGINAL_AREA") ? String.valueOf(OrData.get("ORIGINAL_AREA"))
                            : ""));
                    ps.setString(11, OrData.containsKey("硬度类型1") ? String.valueOf(OrData.get("硬度类型1")) : "");
                    ps.setString(12, OrData.containsKey("试样类型") ? String.valueOf(OrData.get("试样类型")) : "");
                    ps.setString(13, PhyDir);
                    ps.setString(14,  OrData.containsKey("标准值") ? String.valueOf(OrData.get("标准值")==null?"":OrData.get("标准值")) : "");
                    ps.setString(15,  OrData.containsKey("实测值") ? String.valueOf(OrData.get("实测值")==null?"":OrData.get("实测值")) : "");
                    ps.setString(16,  OrData.containsKey("标准块编号") ? String.valueOf(OrData.get("标准块编号")==null?"":OrData.get("标准块编号")) : "");
                    ps.setString(17, Entity.getValue().getPhyCodeMax());
                   // ps.setString(18,  Entity.getValue().getSampleNo( ).lastIndexOf("-") ==Entity.getValue().getSampleNo( ).length()-2?  Entity.getValue().getSampleNo( ).split("-")[0]:Entity.getValue().getSampleNo( ));
                    ps.setString(18,  Entity.getValue().getSampleNo( ));
                    ps.setString(19, Entity.getValue().getCheckNo());
                    ps.executeUpdate();
                    ps.close();

                    sql = EntityTool.fillSqlByEntity(CoreXmlSqlParsersFactory
                            .getSql("PhysicalCollectionToJhyDbTask.UpdateCheckConsignPhyDetail"), Entity.getValue());
                    PreparedStatement ps1 = JhyCon.prepareStatement(sql);
                    ps1.executeUpdate();
                    ps1.close();

                    PhyCollection.RemarkOkData(Entity.getValue().getCheckNo(), Entity.getValue().getSampleNo());

                }
            } catch (Exception e) {

                e.printStackTrace();
                logger.error(e);
                try {
                    if (JhyCon != null && !JhyCon.isClosed()) {
                        JhyCon.rollback();
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }


            } finally {


                try {
                    if (JhyCon != null && !JhyCon.isClosed()) {
                        JhyCon.close();
                    }
                } catch (SQLException e) {
                    logger.error(e);
                    e.printStackTrace();
                }

            }

            System.out.println("\r<br>执行耗时 : " + ( System.currentTimeMillis() - a ) / 1000f + " 秒   --- ");
        }
    }

    public String ChangeClass(String Name){
        if (Name.equals("甲")){
            return "1";
        }else  if (Name.equals("乙")){
            return "2";
        }else  if (Name.equals("丙")){
            return "3";
        }else  if (Name.equals("丁")){
            return "4";
        }else{
            return "";
        }


    }


    public String ChangeGroup(String Name){
        if (Name.equals("常")){
            return "0";
        }else  if (Name.equals("早")){
            return "1";
        }else  if (Name.equals("中")){
            return "2";
        }else  if (Name.equals("晚")){
            return "3";
        }else{
            return "";
        }


    }

    /**
     *  根据关系表, 判断当前关系 是否满足条件去回写
     * @param Data  试验结果集
     * @param PipeCheckconsignPhyrelationEntity  关系对应表的实体类
     * @return
     */
    private Boolean CanSetEntity(Map<String, Object> Data, PipeCheckconsignPhyrelationEntity PipeCheckconsignPhyrelationEntity) {

        Boolean canSys =true;
        if (!"".endsWith(PipeCheckconsignPhyrelationEntity.getDbPara1())) {
            if(!Data.containsKey(PipeCheckconsignPhyrelationEntity.getDbPara1())){
                canSys = false;
            }
            else {
                String CheckValue = PipeCheckconsignPhyrelationEntity.getDbParaValue1().trim().replace("\r","").replace("\n","");
                String DataValue = String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbPara1())).trim().replace("\r","").replace("\n","");

                if(!DataValue.equals(CheckValue)){
                    if(StringTool.isNumeric(DataValue) && StringTool.isNumeric(CheckValue) ){
                        canSys =Math.abs ((Double.parseDouble(DataValue)  - Double.parseDouble(CheckValue)))<0.0001;
                    }
                    else {
                        canSys = false;
                    }
                }

            }

        }

        if(!canSys) return canSys;


        if (!"".endsWith(PipeCheckconsignPhyrelationEntity.getDbPara2())) {
            if(!Data.containsKey(PipeCheckconsignPhyrelationEntity.getDbPara2())){
                canSys = false;
            }
            else {
                String CheckValue = PipeCheckconsignPhyrelationEntity.getDbParaValue2().trim().replace("\r","").replace("\n","");
                String DataValue =  String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbPara2())).trim().replace("\r","").replace("\n","");

                if(!DataValue.equals(CheckValue)){
                    if(StringTool.isNumeric(DataValue) && StringTool.isNumeric(CheckValue) ){
                        canSys = (Double.parseDouble(DataValue)  - Double.parseDouble(CheckValue))<0.0001;
                    }
                    else {
                        canSys = false;
                    }
                }
            }

        }
        if(!canSys) return canSys;

        if (!"".endsWith(PipeCheckconsignPhyrelationEntity.getDbPara3())) {
            if(!Data.containsKey(PipeCheckconsignPhyrelationEntity.getDbPara3())){
                canSys = false;
            }
            else {
                String CheckValue = PipeCheckconsignPhyrelationEntity.getDbParaValue3().trim().replace("\r","").replace("\n","");
                String DataValue = String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbPara3())).trim().replace("\r","").replace("\n","");

                if(!DataValue.equals(CheckValue)){
                    if(StringTool.isNumeric(DataValue) && StringTool.isNumeric(CheckValue) ){
                        canSys = (Double.parseDouble(DataValue)  - Double.parseDouble(CheckValue))<0.0001;
                    }
                    else {
                        canSys = false;
                    }
                }
            }

        }
        return canSys;
    }

    /**
     *  根据关系表里面的公式 计算出试验值  写入实体类中
     * @param Data 试验结果集
     * @param entity  物理试验详细的实体类
     * @param entry  实体类中的属性
     * @param PipeCheckconsignPhyrelationEntity  关系对应表的实体类
     * @return 是否成功写入实体类
     */
    private Boolean SetEntityData(Map<String, Object> Data, PipeCheckconsignPhydetailEntity entity, Map.Entry<String, Field> entry, PipeCheckconsignPhyrelationEntity PipeCheckconsignPhyrelationEntity) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        Compilable compilable = (Compilable) engine;

        Bindings bindings = engine.createBindings(); //Local级别的Binding
        CompiledScript JSFunction; //解析编译脚本函数

        Boolean GetValue = false;
        String TempData ="";
        if (!"".endsWith(PipeCheckconsignPhyrelationEntity.getFormula1())) {
            TempData ="";
            try {

                TempData = PipeCheckconsignPhyrelationEntity.getFormula1()
                        .replace("{1}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn1())))
                        .replace("{2}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn2())))
                        .replace("{3}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn3())))
                        .replace("{4}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn4())))
                        .replace("{5}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn5())))
                        .replace("{6}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn6())));


                if(PipeCheckconsignPhyrelationEntity.getFormula1().equals("{1}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula1().equals("{2}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula1().equals("{3}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula1().equals("{4}") )
                {
                    if (TempData != null && !TempData.equals("") &&!TempData.equals("null")  ) {
                        GetValue = SetData(entity, entry, TempData);
                    }
                }
                else {
                    JSFunction = compilable.compile(TempData
                    );
                    Object result = JSFunction.eval(bindings);
                    if (result != null && !String.valueOf(result).equals("")&& !String.valueOf(result).equals("null") && !String.valueOf(result).equals("NaN")) {
                        GetValue = SetData(entity, entry, result.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();

            }
        }

        if (!GetValue && !"".equals(PipeCheckconsignPhyrelationEntity.getFormula2())) {
            TempData ="";
            try {

                TempData=PipeCheckconsignPhyrelationEntity.getFormula2()
                        .replace("{1}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn1())))
                        .replace("{2}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn2())))
                        .replace("{3}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn3())))
                        .replace("{4}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn4())))
                        .replace("{5}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn5())))
                        .replace("{6}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn6())));

                if(PipeCheckconsignPhyrelationEntity.getFormula2().equals("{1}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula2().equals("{2}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula2().equals("{3}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula2().equals("{4}") )
                {
                    if (TempData != null && !TempData.equals("") &&!TempData.equals("null")  ) {
                        GetValue = SetData(entity, entry, TempData);
                    }
                }
                else {
                    JSFunction = compilable.compile(TempData);
                    Object result = JSFunction.eval(bindings);
                    if (result != null && !String.valueOf(result).equals("")&& !String.valueOf(result).equals("null") && !String.valueOf(result).equals("NaN")) {
                        GetValue = SetData(entity, entry, result.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        if (!GetValue && !"".equals(PipeCheckconsignPhyrelationEntity.getFormula3())) {
            TempData ="";
            try {
                TempData=PipeCheckconsignPhyrelationEntity.getFormula3()
                        .replace("{1}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn1())))
                        .replace("{2}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn2())))
                        .replace("{3}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn3())))
                        .replace("{4}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn4())))
                        .replace("{5}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn5())))
                        .replace("{6}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn6())));

                if(PipeCheckconsignPhyrelationEntity.getFormula3().equals("{1}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula3().equals("{2}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula3().equals("{3}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula3().equals("{4}") )
                {
                    if (TempData != null && !TempData.equals("") &&!TempData.equals("null")  ) {
                        GetValue = SetData(entity, entry, TempData);
                    }
                }
                else {

                    JSFunction = compilable.compile(TempData);
                    Object result = JSFunction.eval(bindings);
                    if (result != null && !String.valueOf(result).equals("")&& !String.valueOf(result).equals("null") && !String.valueOf(result).equals("NaN")) {
                        GetValue = SetData(entity, entry, result.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        if (!GetValue && !"".equals(PipeCheckconsignPhyrelationEntity.getFormula4())) {
            TempData ="";
            try {
                TempData =PipeCheckconsignPhyrelationEntity.getFormula4()
                        .replace("{1}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn1())))
                        .replace("{2}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn2())))
                        .replace("{3}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn3())))
                        .replace("{4}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn4())))
                        .replace("{5}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn5())))
                        .replace("{6}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn6())));

                if(PipeCheckconsignPhyrelationEntity.getFormula4().equals("{1}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula4().equals("{2}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula4().equals("{3}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula4().equals("{4}") )
                {
                    if (TempData != null && !TempData.equals("") &&!TempData.equals("null")  ) {
                        GetValue = SetData(entity, entry, TempData);
                    }
                }
                else {

                    JSFunction = compilable.compile(TempData);
                    Object result = JSFunction.eval(bindings);
                    if (result != null && !String.valueOf(result).equals("")&& !String.valueOf(result).equals("null") && !String.valueOf(result).equals("NaN")) {
                        GetValue = SetData(entity, entry, result.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        if (!GetValue && !"".equals(PipeCheckconsignPhyrelationEntity.getFormula5())) {
            TempData ="";
            try {
                TempData= PipeCheckconsignPhyrelationEntity.getFormula5()
                        .replace("{1}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn1())))
                        .replace("{2}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn2())))
                        .replace("{3}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn3())))
                        .replace("{4}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn4())))
                        .replace("{5}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn5())))
                        .replace("{6}", String.valueOf(Data.get(PipeCheckconsignPhyrelationEntity.getDbColumn6())))
                ;

                if(PipeCheckconsignPhyrelationEntity.getFormula5().equals("{1}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula5().equals("{2}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula5().equals("{3}") ||
                        PipeCheckconsignPhyrelationEntity.getFormula5().equals("{4}") )
                {
                    if (TempData != null && !TempData.equals("") &&!TempData.equals("null")  ) {
                        GetValue = SetData(entity, entry, TempData);
                    }
                }
                else {
                    JSFunction = compilable.compile(TempData);
                    Object result = JSFunction.eval(bindings);
                    if (result != null && !String.valueOf(result).equals("")&& !String.valueOf(result).equals("null") && !String.valueOf(result).equals("NaN")) {
                        GetValue = SetData(entity, entry, result.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        //若是特殊字符串组成结果
/*
        if(!"".equals(TempData) && !GetValue && !"null".equals(TempData)    ) {
            GetValue = SetData(entity, entry, TempData);
        }
*/


        return GetValue;
    }

    private Boolean SetData(PipeCheckconsignPhydetailEntity entity, Map.Entry<String, Field> entry, String tempData) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Boolean GetValue;
        Class<?> clazz = entity.getClass();
        String methodName = "set";
        for (String tempStr : entry.getValue().getName().split("_")
                ) {
            methodName = methodName + tempStr.substring(0, 1).toUpperCase() + tempStr.substring(1);
        }
        // 调用Person类中的run方法
        Method method = clazz.getMethod(methodName
                , String.class);
        method.invoke(entity, tempData);
        GetValue = true;
        return GetValue;
    }
}
