package com.steering.pss.plnsaleord.steelMarkingPlan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.CraftLg2CicEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.EntityChemCode;

/**
 * 炉次钢种索引码交集
 * @author TJG 2017-08-11
 */
public class FrmCicIntersection extends DBCommon {

    /**
     * 根据传入的两个CIC，判断是否有交集，并保存交集数据
     * vLH_1:第一个虚拟炉号，vLH_2：第二个虚拟炉号，vRet：返回的错误信息，iRet:返回的结果值(1:代表执行OK，0：代表执行失败）
     * @param
     * @return
     */
    public CoreReturnObject saveIntersectionCic(String vLH_1, String vLH_2) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);

            // 判断的第一个虚拟炉号是否存在CIC 成分元素
            String cic1[] = getQueryPlnZg(con, vLH_1);
            int icount1 = Integer.parseInt(cic1[1]);
            String craftNoErr1 = cic1[0];// 是否有工艺文件
            String ordLnPk1 = cic1[2];// 合同行Pk
            String craftSeq1 = cic1[3];// 工艺文件序号
            String cicF1 = cic1[4];
            String indexSeq1 = cic1[5];
            if (icount1 == 0) {
                core.setV_errMsg("虚拟炉号(" + vLH_1 + ")对应的成分索引号(" + cicF1
                        + ")在工艺文件中未找到具体成分标准值!");
                return core;
            }
            if (!craftNoErr1.equals("")) {
                core.setV_errMsg(craftNoErr1);
                return core;
            }
            String craftNoErr2 = "";
            String ordLnPk2 = "";
            String craftSeq2 = "";
            String indexSeq2 = "";
            String cicF2 = "";
            if (!vLH_2.equals("")) {
                // 判断的第二个虚拟炉号是否存在CIC 成分元素
                String cic2[] = getQueryPlnZg(con, vLH_2);
                int icount2 = Integer.parseInt(cic2[1]);
                craftNoErr2 = cic2[0];// 是否有工艺文件
                ordLnPk2 = cic2[2];// 合同行Pk
                craftSeq2 = cic2[3];// 工艺文件序号
                cicF2 = cic2[4];
                indexSeq2 = cic2[5];
                if (icount2 == 0) {
                    core.setV_errMsg("虚拟炉号(" + vLH_2 + ")对应的成分索引号(" + cicF2
                            + ")在工艺文件中未找到具体成分标准值!");
                    return core;
                }
                if (!craftNoErr2.equals("")) {
                    core.setV_errMsg(craftNoErr2);
                    return core;
                }
            }
            // 查询Cic交集
            String cicALL = getFurnCicALL(con, vLH_1, vLH_2);

            // 查询两个虚拟炉号对应的元素成分
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmCicIntersection.getQueryCicChemCode");
            ps = con.prepareStatement(sql);
            ps.setObject(1, vLH_1);
            ps.setObject(2, ordLnPk1);
            ps.setObject(3, craftSeq1);
            ps.setObject(4, indexSeq1);
            ps.setObject(5, vLH_2);
            ps.setObject(6, ordLnPk2);
            ps.setObject(7, craftSeq2);
            ps.setObject(8, indexSeq2);
            rs = ps.executeQuery();
            List<EntityChemCode> listchem1 = EntityTool.getEntitysByRs(rs,
                    EntityChemCode.class);
            rs.close();
            ps.close();
            String vChemCode = "";
            int iXh = 0;
            String vChemName = "";
            String vChem_type = "";
            String vChem_formula = "";
            String vStdMin_Sign = "";
            String vStdMin = "";
            String vStdMax_Sign = "";
            String vStdMax = "";
            String vStdtarget = "";// 标准目标值
            int m = 0;

            int count = 0;
            // 查询两个虚拟炉号是否有对应的交集元素成分
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmCicIntersection.getSelectSteeCic");
            ps = con.prepareStatement(sql);
            ps.setObject(1, vLH_1 + vLH_2);
            ps.setObject(2, vLH_2 + vLH_1);
            rs = ps.executeQuery();
            if (rs.next()) {
                count = rs.getInt("COUNT");
            }
            rs.close();
            ps.close();
            if (count == 0) {
                // 不存在交集时比较
                for (EntityChemCode chemCode : listchem1) {
                    m++;
                    if (vChemCode.equals("")
                            || !chemCode.getChemCode().equals(vChemCode)) {
                        if (!vChemCode.equals("")) {
                            String sqladd = CoreXmlSqlParsersFactory
                                    .getSql("FrmCicIntersection.getAddSteeCic");
                            PreparedStatement padd = con
                                    .prepareStatement(sqladd);
                            padd.setObject(1, vLH_1 + vLH_2);
                            padd.setObject(2, iXh);
                            padd.setObject(3, vChemCode);
                            padd.setObject(4, vChemName);
                            padd.setObject(5, vChem_type);
                            padd.setObject(6, vChem_formula);
                            padd.setObject(7, vStdMin_Sign);
                            padd.setObject(8, vStdMin);
                            padd.setObject(9, vStdMax_Sign);
                            padd.setObject(10, vStdMax);
                            padd.setObject(11, vStdtarget);
                            padd.setObject(12, cicALL);
                            padd.executeUpdate();
                            padd.close();
                            iXh++;
                        }
                        else {
                            String sqlDelete = "delete PLN_STEELFORFURNACE_CIC t where t.FURNACENO_ALL = '"
                                    + vLH_1
                                    + vLH_2
                                    + "' or  t.FURNACENO_ALL ='"
                                    + vLH_2
                                    + vLH_1 + "'";
                            PreparedStatement pDelete = con
                                    .prepareStatement(sqlDelete);
                            pDelete.executeUpdate();
                            pDelete.close();
                        }
                        vChemCode = chemCode.getChemCode();
                        vChemName = chemCode.getChemName();
                        vChem_type = chemCode.getChemType();
                        vChem_formula = chemCode.getChemFormula();
                        vStdMin_Sign = chemCode.getStdminSign();
                        vStdMin = chemCode.getStdmin();
                        vStdMax_Sign = chemCode.getStdmaxSign();
                        vStdMax = chemCode.getStdmax();
                        vStdtarget = chemCode.getStdtarget();
                    }
                    else {
                        String[] comper = getIntersectionCompare(vStdMin_Sign,
                                vStdMin, vStdMax_Sign, vStdMax, chemCode
                                        .getStdminSign(), chemCode.getStdmin(),
                                chemCode.getStdmaxSign(), chemCode.getStdmax());
                        if (!comper[4].equals("")) {
                            core.setV_errMsg("元素项:[" + chemCode.getChemName()
                                    + "]" + comper[4] + "!");
                            return core;
                        }
                        else {
                            vStdMin_Sign = comper[0];
                            vStdMin = comper[1];
                            vStdMax_Sign = comper[2];
                            vStdMax = comper[3];
                        }
                    }
                    if (m == listchem1.size()) {
                        String sqladd = CoreXmlSqlParsersFactory
                                .getSql("FrmCicIntersection.getAddSteeCic");
                        PreparedStatement padd = con.prepareStatement(sqladd);
                        padd.setObject(1, vLH_1 + vLH_2);
                        padd.setObject(2, iXh);
                        padd.setObject(3, vChemCode);
                        padd.setObject(4, vChemName);
                        padd.setObject(5, vChem_type);
                        padd.setObject(6, vChem_formula);
                        padd.setObject(7, vStdMin_Sign);
                        padd.setObject(8, vStdMin);
                        padd.setObject(9, vStdMax_Sign);
                        padd.setObject(10, vStdMax);
                        padd.setObject(11, vStdtarget);
                        padd.setObject(12, cicALL);
                        padd.executeUpdate();
                        padd.close();
                    }
                }
            }
            core.setV_errMsg("存在交集!");
            con.commit();
            core.setResult(true);
            return core;
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {

                e1.printStackTrace();
            }
            e.printStackTrace();
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 取2组值的交集 传入2组值 输出5个值（比较后的下限符号，下限值，上限符号，上限值，以及返回错误信息）
     * @param vStdMin_Sign1下限符号1
     * @param vStdMin1 下限值1
     * @param vStdMax_Sign1 上限符号1
     * @param vStdMax1 上限值1
     * @param vStdMin_Sign2 下限符号2
     * @param vStdMin2 下限值2
     * @param vStdMax_Sign2上限符号2
     * @param vStdMax2 上限值2
     * @return
     */
    public String[] getIntersectionCompare(String vStdMin_Sign1,
            String vStdMin1, String vStdMax_Sign1, String vStdMax1,
            String vStdMin_Sign2, String vStdMin2, String vStdMax_Sign2,
            String vStdMax2) {
        String[] compare = new String[] { "", "", "", "", "" };
        String vMin_Sign = "";
        String vMin = "";
        String vMax_Sign = "";
        String vMax = "";
        String error = "";
        // 比较获取下限符号
        if (!vStdMin_Sign1.equals("")) {// 下限符号1不为空
            if (!vStdMin_Sign2.equals("")) {// 下限符号2不为空
                if (vStdMin_Sign1.equals(">") && vStdMin_Sign2.equals(">")) {
                    vMin_Sign = ">";
                }
                else if (vStdMin_Sign1.equals(">")
                        && vStdMin_Sign2.equals(">=")) {
                    if (Double.parseDouble(vStdMin1) < Double
                            .parseDouble(vStdMin2)) {
                        vMin_Sign = ">=";
                    }
                    else {
                        vMin_Sign = ">";
                    }
                }
                else if (vStdMin_Sign1.equals(">=")
                        && vStdMin_Sign2.equals(">")) {
                    if (Double.parseDouble(vStdMin1) <= Double
                            .parseDouble(vStdMin2)) {
                        vMin_Sign = ">";
                    }
                    else {
                        vMin_Sign = ">=";
                    }
                }
                else if (vStdMin_Sign1.equals(">=")
                        && vStdMin_Sign2.equals(">=")) {
                    vMin_Sign = ">=";
                }
                else {
                    error = "标准值下限不存在交集！";
                }
            }
            else {// 下限符号2为空去下限符号1
                vMin_Sign = vStdMin_Sign1;
            }
        }
        else {// 下限符号1为空去下限符号2
            vMin_Sign = vStdMin_Sign2;
        }
        // 判断比较后的下限符号不为空才继续进行比较
        if (!vMin_Sign.equals("")) {
            if (!vStdMin1.equals("")) {// 下限值1不为空
                if (!vStdMin2.equals("")) {// 下限值2不为空
                    if (Double.parseDouble(vStdMin1) > Double
                            .parseDouble(vStdMin2)) {
                        vMin = vStdMin1;
                    }
                    else {
                        vMin = vStdMin2;
                    }
                }
                else {
                    vMin = vStdMin1;
                }
            }
            else {
                vMin = vStdMin2;
            }
        }
        // 比较获取上限符号
        if (!vStdMax_Sign1.equals("")) {// 上限符号1不为空
            if (!vStdMax_Sign2.equals("")) {// 上限符号2不为空
                if (vStdMax_Sign1.equals("<") && vStdMax_Sign2.equals("<")) {
                    vMax_Sign = "<";
                }
                else if (vStdMax_Sign1.equals("<")
                        && vStdMax_Sign2.equals("<=")) {
                    if (Double.parseDouble(vStdMax1) < Double
                            .parseDouble(vStdMax2)) {
                        vMax_Sign = "<=";
                    }
                    else {
                        vMax_Sign = "<";
                    }
                }
                else if (vStdMax_Sign1.equals("<=")
                        && vStdMax_Sign2.equals("<")) {
                    if (Double.parseDouble(vStdMax1) <= Double
                            .parseDouble(vStdMax2)) {
                        vMax_Sign = "<";
                    }
                    else {
                        vMax_Sign = "<=";
                    }
                }
                else if (vStdMax_Sign1.equals("<=")
                        && vStdMax_Sign2.equals("<=")) {
                    vMax_Sign = "<=";
                }
                else {
                    error = "标准值上限不存在交集！";
                }
            }
            else {// 上限符号2为空去上限符号1
                vMax_Sign = vStdMax_Sign1;
            }
        }
        else {// 上限符号1为空去上限符号2
            vMax_Sign = vStdMax_Sign2;
        }
        // 判断比较后的上限符号不为空才继续进行比较
        if (!vMax_Sign.equals("")) {
            if (!vStdMax1.equals("")) {// 上限值1不为空
                if (!vStdMax2.equals("")) {// 上限值2不为空
                    if (Double.parseDouble(vStdMax1) > Double
                            .parseDouble(vStdMax2)) {
                        vMax = vStdMax2;
                    }
                    else {
                        vMax = vStdMax1;
                    }
                }
                else {
                    vMax = vStdMax1;
                }
            }
            else {
                vMax = vStdMax2;
            }
        }
        // 验证上下限合理性
        if (vMin_Sign.equals(">") && vMax_Sign.equals("<")) {
            if (Double.parseDouble(vMin) >= Double.parseDouble(vMax)) {
                error = "标准值上下限不存在交集！";
            }
        }
        else if (vMin_Sign.equals(">=") && vMax_Sign.equals("<")) {
            if (Double.parseDouble(vMin) >= Double.parseDouble(vMax)) {
                error = "标准值上下限不存在交集！";
            }
        }
        else if (vMin_Sign.equals(">") && vMax_Sign.equals("<=")) {
            if (Double.parseDouble(vMin) >= Double.parseDouble(vMax)) {
                error = "标准值上下限不存在交集！";
            }
        }
        else if (vMin_Sign.equals(">=") && vMax_Sign.equals("<=")) {
            if (Double.parseDouble(vMin) >= Double.parseDouble(vMax)) {
                error = "标准值上下限不存在交集！";
            }
        }
        compare[0] = vMin_Sign;
        compare[1] = vMin;
        compare[2] = vMax_Sign;
        compare[3] = vMax;
        compare[4] = error;
        return compare;
    }

    /**
     * 虚拟炉号对应的铸造记录是否存在轧管记录
     * @param con
     * @param time
     * @return
     * @throws SQLException
     */
    public String[] getQueryPlnZg(Connection con, String furnNo)
            throws SQLException {
        int sequenceno = 0;
        String[] paramArr = new String[] { "", "", "", "", "", "" };
        String ordLnPk = "";// 合同行PK
        String craftSeq = "";// 工艺文件序号
        String err = "";
        String indexSeq = "";
        // 查询合同行PK,工艺文件序号
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryPlnZg");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            ordLnPk = rs.getString("ORD_LN_PK");
            craftSeq = rs.getString("CRAFT_SEQ");
            indexSeq = rs.getString("INDEX_SEQ");
        }
        else {
            err = "虚拟炉号(" + furnNo + ")未找到工艺文件！";
        }
        rs.close();
        ps.close();

        // 通过工艺文件序号,合同行PK 查询钢种成分索引码
        sql = CoreXmlSqlParsersFactory.getSql("FrmCicIntersection.getQueryCIC");
        ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ps.setObject(2, ordLnPk);
        ps.setObject(3, craftSeq);
        ps.setObject(4, indexSeq);
        rs = ps.executeQuery();
        if (rs.next()) {
            sequenceno = rs.getInt("ICOUNT");
        }
        rs.close();
        ps.close();
        String cic = "";
        // 查询虚拟炉号对应的CIC
        sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryFurnCIC");
        ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        rs = ps.executeQuery();
        while (rs.next()) {
            cic += rs.getString("CIC") + ",";
        }
        rs.close();
        ps.close();
        cic = cic.substring(0, cic.length() - 1);
        paramArr[0] = err;
        paramArr[1] = String.valueOf(sequenceno);
        paramArr[2] = ordLnPk;
        paramArr[3] = craftSeq;
        paramArr[4] = cic;
        paramArr[5] = indexSeq;
        return paramArr;
    }

    /**
     * 获取cic集合
     * @param con
     * @param time
     * @return
     * @throws SQLException
     */
    public String getFurnCicALL(Connection con, String furnNo1, String furnNo2)
            throws SQLException {
        String cicAll = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getFurnCicALL");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo1);
        ps.setObject(2, furnNo2);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            cicAll = rs.getString("CIC");
        }
        rs.close();
        ps.close();
        return cicAll;
    }

    /**
     * 铸造订单层通过钢种，钢种索引码查询元素集合
     * @param plinecode
     * @return
     */
    public CoreReturnObject getQueryGraderCIC(String[] cic, String gradeCode,
            String[] desginKey, String[] indexSeq) {
        // 查询索引码对应元素
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryCicChemCodezZ");
        sql += CoreConverter.toSqlConditionString("V.CIC||'/'||V.DESGIN_KEY||'/'||V.MSC_PLINE", cic);
//        sql += CoreConverter.toSqlConditionString("V.DESGIN_KEY", desginKey);
//        sql += CoreConverter.toSqlConditionString("V.MSC_PLINE", indexSeq);
        sql += " UNION ALL   SELECT CIC,CHEM_NAME,STDMIN_SIGN,STDMAX_SIGN,STD_NAME_NK,STYTECHEM,CHEM_CODE,CHEM_SEQ FROM ( "
                + "SELECT V.CIC,V.CHEM_NAME,V.STDMIN_SIGN || V.STDMIN STDMIN_SIGN,"
                + "V.STDMAX_SIGN || V.STDMAX STDMAX_SIGN,T1.STD_NAME STD_NAME_NK,'基础数据' STYTECHEM,V.CHEM_CODE,RANK() OVER(ORDER BY V.CIC ASC) RK,  C.CHEM_SEQ FROM "
                + "COM_MSC_STD_CHEM V, COM_MSC_STD_CIC T, COM_BASE_STD T1,COM_BASE_CHEM C WHERE V.VALIDFLAG = '1'"
                + " AND T.CIC = V.CIC AND T.STDSTYLE = 'N' AND T.STD_CODE_NK = T1.STD_CODE(+) "
                + " AND T1.VALIDFLAG = '1' AND V.CHEM_CODE = C.CHEM_CODE AND T1.STD_CODE = 'STD0000860' AND T.GRADENAME = '"
                + gradeCode + "')  WHERE RK = 1) T";
        sql += " ORDER BY T.CIC ASC,T.CHEM_SEQ ASC";
        return this.getData(sql);
    }

    /**
     * 铸造订单层通过钢种，钢种索引码查询元素集合
     * @param plinecode
     * @return
     */
    public CoreReturnObject getQueryGraderCICDesgin(String proPlanId) {
        // 查询合同行PK,工艺文件序号
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryPlnZgZz");
        sql += " AND T.GPREQ_ID='" + proPlanId + "'";
        sql += "AND rownum = 1";
        return this.getData(sql);
    }

    /**
     * 查询交集CIC元素
     * @param plinecode
     * @return
     */
    public CoreReturnObject getQueryGraderAllCIC(String cic, String proPlanId) {
        // 查询索引码对应元素
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryGraderAllCIC");
        return this.getData(sql, new Object[] { cic, proPlanId });
    }

    /**
     * 铸造订单层cic合并
     * @param list
     * @return
     */
    public CoreReturnObject addMerging(ArrayList<ArrayList<String>> list) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String cicall = "";
            String proPlanIdall = "";
            List<CraftLg2CicEntity> listchem1 = null;
            String sql = "";
            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> lista = (ArrayList<String>) list.get(i);
                String cic = lista.get(0);
                String proPlanId = lista.get(1);
                cicall += cic + ",";
                proPlanIdall += proPlanId + ",";
                // 判断铸造订单是否存在CIC 成分元素
                String[] cic1 = getQueryPlnZgPro(con, proPlanId, cic);
                int icount1 = Integer.parseInt(cic1[1]);
                String craftNoErr1 = cic1[0];// 是否有工艺文件
                String ordLnPk1 = cic1[2];// 合同行Pk
                String craftSeq1 = cic1[3];// 工艺文件序号
                // String cicF1 = cic1[4];
                String indexSeq1 = cic1[5];
                if (icount1 == 0) {
                    core.setV_errMsg("铸造订单(" + proPlanId + ")对应的成分索引号(" + cic
                            + ")在工艺文件中未找到具体成分标准值!");
                    return core;
                }
                if (!craftNoErr1.equals("")) {
                    core.setV_errMsg(craftNoErr1);
                    return core;
                }

                // 查询多个cic对应的元素成分
                if (i == 0) {// 一个CIC
                    sql += "SELECT *  FROM (SELECT V.CHEM_CODE,V.CHEM_NAME,V.CHEM_TYPE,V.CHEM_FORMULA,"
                            + "V.STDMIN_SIGN,V.STDMIN,V.STDMAX_SIGN,V.STDMAX,V.STDTARGET,"
                            + "  V.ELE_CODE , V.ELE_NAME ,V.ELE_MIN_SIGN , V.ELE_MIN , V.ELE_MAX_SIGN ,V.ELE_MAX ,"
                            + " V.ISCHECK, V.ISJUDGE, V.ISQUOTE,V.REP_CODE, V.REP_NAME,V.REP_MIN_SIGN,   V.REP_MIN, V.REP_MAX_SIGN,V.REP_MAX,V.REP_VAL"
                            + " FROM CRAFT_ORD_DESIGN_STD_CIC V  WHERE V.CIC ='"
                            + cic
                            + "'  "
                            + "AND V.PROCESS_CODE = 'A' AND V.DESGIN_KEY = (SELECT T.DESGIN_KEY FROM "
                            + "CRAFT_ORD_DESIGN T WHERE T.ORD_LN_PK = '"
                            + ordLnPk1
                            + "' AND T.CRAFT_SEQ = '"
                            + craftSeq1
                            + "') AND V.MSC_PLINE='"
                            + indexSeq1
                            + "'  "
                            + "AND V.SPECL_FL='2' AND V.VALIDFLAG='1'";
                }
                else if (i + 1 == list.size()) {// 最后一个CIC
                    sql += "UNION ALL (SELECT V.CHEM_CODE,V.CHEM_NAME,V.CHEM_TYPE,V.CHEM_FORMULA,"
                            + "V.STDMIN_SIGN,V.STDMIN,V.STDMAX_SIGN,V.STDMAX,V.STDTARGET ,"
                            + "  V.ELE_CODE , V.ELE_NAME ,V.ELE_MIN_SIGN , V.ELE_MIN , V.ELE_MAX_SIGN ,V.ELE_MAX ,"
                            + " V.ISCHECK, V.ISJUDGE, V.ISQUOTE,V.REP_CODE, V.REP_NAME,V.REP_MIN_SIGN,   V.REP_MIN, V.REP_MAX_SIGN,V.REP_MAX,V.REP_VAL"
                            + " FROM CRAFT_ORD_DESIGN_STD_CIC V  WHERE V.CIC ='"
                            + cic
                            + "'  "
                            + "AND V.PROCESS_CODE = 'A' AND V.DESGIN_KEY = (SELECT T.DESGIN_KEY FROM "
                            + "CRAFT_ORD_DESIGN T WHERE T.ORD_LN_PK = '"
                            + ordLnPk1
                            + "' AND T.CRAFT_SEQ = '"
                            + craftSeq1
                            + "') AND V.MSC_PLINE='"
                            + indexSeq1
                            + "'  "
                            + "AND V.SPECL_FL='2' AND V.VALIDFLAG='1')";
                    sql += " ) ORDER BY CHEM_CODE";
                    ps = con.prepareStatement(sql);
                    rs = ps.executeQuery();
                    listchem1 = EntityTool.getEntitysByRs(rs,
                            CraftLg2CicEntity.class);
                    rs.close();
                    ps.close();
                }
                else {
                    sql += "UNION ALL (SELECT V.CHEM_CODE,V.CHEM_NAME,V.CHEM_TYPE,V.CHEM_FORMULA,"
                            + "V.STDMIN_SIGN,V.STDMIN,V.STDMAX_SIGN,V.STDMAX,V.STDTARGET, "
                            + "  V.ELE_CODE , V.ELE_NAME ,V.ELE_MIN_SIGN , V.ELE_MIN , V.ELE_MAX_SIGN ,V.ELE_MAX ,"
                            + " V.ISCHECK, V.ISJUDGE, V.ISQUOTE,V.REP_CODE, V.REP_NAME,V.REP_MIN_SIGN,   V.REP_MIN, V.REP_MAX_SIGN,V.REP_MAX,V.REP_VAL"
                            + " FROM CRAFT_ORD_DESIGN_STD_CIC V  WHERE V.CIC ='"
                            + cic
                            + "'  "
                            + "AND V.PROCESS_CODE = 'A' AND V.DESGIN_KEY = (SELECT T.DESGIN_KEY FROM "
                            + "CRAFT_ORD_DESIGN T WHERE T.ORD_LN_PK = '"
                            + ordLnPk1
                            + "' AND T.CRAFT_SEQ = '"
                            + craftSeq1
                            + "') AND V.MSC_PLINE='"
                            + indexSeq1
                            + "'  "
                            + "AND V.SPECL_FL='2' AND V.VALIDFLAG='1')";
                }
            }
            cicall = cicall.substring(0, cicall.length() - 1);
            proPlanIdall = proPlanIdall.substring(0, proPlanIdall.length() - 1);

            String vChemCode = "";
            int iXh = 0;
            String vChemName = "";
            String vChem_type = "";
            String vChem_formula = "";
            String vStdMin_Sign = "";
            String vStdMin = "";
            String vStdMax_Sign = "";
            String vStdMax = "";
            String vStdtarget = "";// 标准目标值

            String ISCHECK = "";
            String ISJUDGE = "";
            String ISQUOTE="";


            int m = 0;

            // int count = 0;
            // // 查询cic号是否有对应的交集元素成分
            // sql = CoreXmlSqlParsersFactory
            // .getSql("FrmCicIntersection.getSelectSteeCic");
            // ps = con.prepareStatement(sql);
            // ps.setObject(1, proPlanIdall);
            // ps.setObject(2, proPlanIdall);
            // rs = ps.executeQuery();
            // if (rs.next()) {
            // count = rs.getInt("COUNT");
            // }
            // rs.close();
            // ps.close();
            // if (count == 0) {
            // 不存在交集时比较
            for (CraftLg2CicEntity chemCode : listchem1) {
                m++;
                if (vChemCode.equals("")
                        || !chemCode.getChemCode().equals(vChemCode)) {
                    if (!vChemCode.equals("")) {
                        String sqladd = CoreXmlSqlParsersFactory
                                .getSql("FrmCicIntersection.getAddSteeCic");
                        PreparedStatement padd = con.prepareStatement(sqladd);
                        padd.setObject(1, proPlanIdall);
                        padd.setObject(2, iXh);
                        padd.setObject(3, vChemCode);
                        padd.setObject(4, vChemName);
                        padd.setObject(5, vChem_type);
                        padd.setObject(6, vChem_formula);
                        padd.setObject(7, vStdMin_Sign);
                        padd.setObject(8, vStdMin);
                        padd.setObject(9, vStdMax_Sign);
                        padd.setObject(10, vStdMax);
                        padd.setObject(11, vStdtarget);
                        padd.setObject(12, cicall);
                        padd.setObject(13, ISCHECK);
                        padd.setObject(14, ISJUDGE);
                        padd.setObject(15, ISQUOTE);
                        padd.executeUpdate();
                        padd.close();
                        iXh++;
                    }
                    else {
                        String sqlDelete = "delete PLN_STEELFORFURNACE_CIC t where t.CIC = '"
                                + cicall + "'";
                        PreparedStatement pDelete = con
                                .prepareStatement(sqlDelete);
                        pDelete.executeUpdate();
                        pDelete.close();
                    }
                    vChemCode = chemCode.getChemCode();
                    vChemName = chemCode.getChemName();
                    vChem_type = chemCode.getChemType();
                    vChem_formula = chemCode.getChemFormula();
                    vStdMin_Sign = chemCode.getStdminSign();
                    vStdMin = chemCode.getStdmin();
                    vStdMax_Sign = chemCode.getStdmaxSign();
                    vStdMax = chemCode.getStdmax();
                    vStdtarget = chemCode.getStdtarget();
                    ISCHECK = chemCode.getIscheck();
                    ISJUDGE = chemCode.getIsjudge();
                    ISQUOTE = chemCode.getIsquote();
                }
                else {
                    String[] comper = getIntersectionCompare(vStdMin_Sign,
                            vStdMin, vStdMax_Sign, vStdMax, chemCode
                                    .getStdminSign(), chemCode.getStdmin(),
                            chemCode.getStdmaxSign(), chemCode.getStdmax());
                    if (!comper[4].equals("")) {
                        core.setV_errMsg("元素项:[" + chemCode.getChemName() + "]"
                                + comper[4] + "!");
                        return core;
                    }
                    else {
                        vStdMin_Sign = comper[0];
                        vStdMin = comper[1];
                        vStdMax_Sign = comper[2];
                        vStdMax = comper[3];

                        ISCHECK = ISCHECK.equals("1") || chemCode.getIscheck().equals("1") ? "1" : "0";
                        ISJUDGE = ISJUDGE.equals("1") || chemCode.getIsjudge().equals("1") ? "1" : "0";
                        ISQUOTE = ISQUOTE.equals("1") || chemCode.getIsquote().equals("1") ? "1" : "0";
                    }
                }
                if (m == listchem1.size()) {
                    String sqladd = CoreXmlSqlParsersFactory
                            .getSql("FrmCicIntersection.getAddSteeCic");
                    PreparedStatement padd = con.prepareStatement(sqladd);
                    padd.setObject(1, proPlanIdall);
                    padd.setObject(2, iXh);
                    padd.setObject(3, vChemCode);
                    padd.setObject(4, vChemName);
                    padd.setObject(5, vChem_type);
                    padd.setObject(6, vChem_formula);
                    padd.setObject(7, vStdMin_Sign);
                    padd.setObject(8, vStdMin);
                    padd.setObject(9, vStdMax_Sign);
                    padd.setObject(10, vStdMax);
                    padd.setObject(11, vStdtarget);
                    padd.setObject(12, cicall);
                    padd.setObject(13, ISCHECK);
                    padd.setObject(14, ISJUDGE);
                    padd.setObject(15, ISQUOTE);
                    padd.executeUpdate();
                    padd.close();
                }
            }
            // }
            core.setV_errMsg("合并交集成功!");
            con.commit();
            core.setResult(true);
            return core;
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {

                e1.printStackTrace();
            }
            e.printStackTrace();
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 删除交集
     * @param list
     * @return
     */
    public CoreReturnObject doDelete(ArrayList<ArrayList<String>> list) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sqlDelete = CoreXmlSqlParsersFactory
                    .getSql("FrmCicIntersection.DeleteAllCIC");
            PreparedStatement psdelete = con.prepareStatement(sqlDelete);
            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> lista = (ArrayList<String>) list.get(i);
                String cic = lista.get(0);
                String proPlanId = lista.get(1);
                psdelete.setObject(1, proPlanId);
                psdelete.setObject(2, cic);
                psdelete.addBatch();
            }
            psdelete.executeBatch();
            core.setV_errMsg("删除交集成功!");
            con.commit();
            core.setResult(true);
            return core;
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {

                e1.printStackTrace();
            }
            e.printStackTrace();
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 铸造记录是否存在轧管记录
     * @param con
     * @param time
     * @return
     * @throws SQLException
     */
    public String[] getQueryPlnZgPro(Connection con, String furnNo, String cic)
            throws SQLException {
        int sequenceno = 0;
        String[] paramArr = new String[] { "", "", "", "", "", "" };
        String ordLnPk = "";// 合同行PK
        String craftSeq = "";// 工艺文件序号
        String err = "";
        String indexSeq = "";
        // 查询合同行PK,工艺文件序号
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryPlnZgPro");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            ordLnPk = rs.getString("ORD_LN_PK");
            craftSeq = rs.getString("CRAFT_SEQ");
            indexSeq = rs.getString("INDEX_SEQ");
        }
        else {
            err = "铸造订单(" + furnNo + ")未找到工艺文件！";
        }
        rs.close();
        ps.close();

        // 通过工艺文件序号,合同行PK 查询钢种成分索引码
        sql = CoreXmlSqlParsersFactory
                .getSql("FrmCicIntersection.getQueryCICPro");
        ps = con.prepareStatement(sql);
        ps.setObject(1, cic);
        ps.setObject(2, ordLnPk);
        ps.setObject(3, craftSeq);
        ps.setObject(4, indexSeq);
        rs = ps.executeQuery();
        if (rs.next()) {
            sequenceno = rs.getInt("ICOUNT");
        }
        rs.close();
        ps.close();
        paramArr[0] = err;
        paramArr[1] = String.valueOf(sequenceno);
        paramArr[2] = ordLnPk;
        paramArr[3] = craftSeq;
        paramArr[4] = cic;
        paramArr[5] = indexSeq;
        return paramArr;
    }

}
