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.*;
import core.stlmes.server.sale.util.Convert;

/**
 * 炼钢工艺卡
 * @author tjg 2017.08.022
 */
public class FrmSteelProcessCard extends DBCommon {

    /**
     * 查询工艺卡主信息
     */
    public CoreReturnObject queryLgM(String cic, String carftNo,
            String plineCodeA, String gradCode,String planRoute) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgM");
        if (!cic.equals("")) {
            sql += " AND  T.CIC  LIKE '%'||'" + cic + "'||'%'";
        }
        // sql += " AND  T.CRAFT_NO = '" + carftNo + "'";
        sql += " AND  T.PLINE_CODE = '" + plineCodeA + "'";
        sql += " AND  T.GRADECODE = '" + gradCode + "'";
        sql += " AND  T.CRAFT_ROUTE = '" + planRoute + "'";
        sql += " ORDER BY T.MARK_DATE ASC ";
        return this.getData(sql);
    }

    /**
     * 查询工艺卡主信息
     */
    public CoreReturnObject queryLgM(String[] cic, String carftNo,
                                     String plineCodeA, String gradCode,String planRoute,Boolean SingleCIC) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgM");
//        if (!cic.equals("")) {
//            if(SingleCIC) {
//                sql += " AND  T.CIC  = '" + cic + "'";
//            }
//            else{
//                sql += " AND  T.CIC  LIKE '%'||'" + cic + "'||'%'";
//            }
//        }

        sql += CoreConverter.toSqlConditionStringOrLike("T.CIC",cic);

        // sql += " AND  T.CRAFT_NO = '" + carftNo + "'";
        if (!plineCodeA.equals("")) {
            sql += " AND  T.PLINE_CODE = '" + plineCodeA + "'";
        }

        if (!gradCode.equals("")) {
            sql += " AND  T.GRADECODE = '" + gradCode + "'";
        }

        if (!planRoute.equals("")) {
            sql += " AND  T.CRAFT_ROUTE = '" + planRoute + "'";
        }
        sql += " ORDER BY T.MARK_DATE ASC ";
        return this.getData(sql);
    }

    /**
     * 查询工艺卡主信息
     */
    public CoreReturnObject queryLgMCondition(String carftNo, String cic, String gradCode) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgM");
        if (!cic.equals("")) {
            sql += " AND  T.CIC  LIKE '%'||'" + cic + "'||'%'";
        }
        if (!carftNo.equals("")) {
            sql += " AND  T.CRAFT_NO = '" + carftNo + "'";
        }

        if(!gradCode.equals("")){
            sql += " AND  T.GRADECODE = '" + gradCode + "'";
        }

        // sql += " AND  T.CRAFT_NO = '" + carftNo + "'";
        sql += " ORDER BY T.MARK_DATE ASC ";
        return this.getData(sql);
    }


    /**
     * 查询工艺卡CIC信息
     */
    public CoreReturnObject queryLgCic(String cicGid) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgCic");
        return this.getData(sql, new Object[] { cicGid });
    }

    /**
     * 查询工艺卡目标CIC信息
     */
    public CoreReturnObject queryLgTarCic(String tarCicGid) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgTarCic");
        return this.getData(sql, new Object[] { tarCicGid });
    }

    /**
     * 查询炉次订单对应品名
     */
    public CoreReturnObject queryProdu(String furnaceNo,String flag) {
        String sql;
        if("1".equals(flag)) {
            sql= CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryProdu");
        }else
        {
            sql= CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryProduByProPlanID");
        }
        return this.getData(sql, new Object[]{furnaceNo});
    }

    /**
     * 查询工艺卡CCM信息
     */
    public CoreReturnObject queryLgCcm(String ccmGid) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgCcm");
        return this.getData(sql, new Object[] { ccmGid });
    }

    /**
     * 查询工艺温度信息
     */
    public CoreReturnObject queryLgtTem(String tepGid) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgtTem");
        return this.getData(sql, new Object[] { tepGid });
    }

    /**
     * 查询合金配置信息
     */
    public CoreReturnObject queryLgtHj(String alloyGid) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgtHj");
        return this.getData(sql, new Object[] { alloyGid });
    }

    /**
     * 根据钢种查询合金配置信息
     */
    public CoreReturnObject queryLgtHjGrae(String gradecode, String itemCode) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgtHjGrae");
        if (!itemCode.equals("")) {
            sql += " AND T.ITEM_CODE='" + itemCode + "' ";
        }
        return this.getData(sql, new Object[] { gradecode });
    }

    /**
     * 查询工艺号
     */
    public CoreReturnObject queryCraftNo() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryCraftNo");
        return this.getData(sql);
    }

    /**
     * 查询合金物料信息
     */
    public CoreReturnObject queryItemCode(String itemCode) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryItemCode");
        return this.getData(sql);
    }

    /**
     * 查询工艺评审CIC
     */
    public CoreReturnObject queryOrdDesigStdCic(String cic, String furnacNo,String flag) {
        String sql;
        if("1".equals(flag)) {
             sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigStdCic");
            return this.getData(sql, new Object[]{cic, furnacNo, furnacNo});
        }else
        {
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigStdCicByProPlnID");
            return this.getData(sql, new Object[]{cic, furnacNo, furnacNo});
        }

    }


    /**
     * 查询工艺评审CIC
     */
    public CoreReturnObject queryOrdDesigStdCic(String cic, String para1,String para2, String flag) {
        String sql;
        if("1".equals(flag)) {
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigStdCic");
            return this.getData(sql, new Object[]{cic, para1, para1});
        }else   if("0".equals(flag))
        {
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigStdCicByProPlnID");
            return this.getData(sql, new Object[]{cic, para1, para1});
        }
        else{
             sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigStdCicByZGProPlnID");
            return this.getData(sql, new Object[] { para1, para2, cic });
        }

    }

    /**
     * 查询CIC(工艺评审，交集)
     */
    public CoreReturnObject queryOrdDesigCic(String cic,  String para1,String para2,String flag) {

        if("1".equals(flag)) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigCic");
            return this.getData(sql, new Object[] { cic, para1, para1, cic });
        }else   if("0".equals(flag))
        {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigCicByProPlanID");
            return this.getData(sql, new Object[]{cic, para1, para1});
        }
        else{
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigCicByZGProPlanID");
            return this.getData(sql, new Object[] { para1, para2, cic });
        }
    }

    /**
     * 查询CIC(工艺评审，交集)
     */
    public CoreReturnObject queryOrdDesigCic(String cic, String furnacNo,String flag) {

        if("1".equals(flag)) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigCic");
            return this.getData(sql, new Object[] { cic, furnacNo, furnacNo, cic });
        }else
        {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmSteelProcessCard.queryOrdDesigCicByProPlanID");
            return this.getData(sql, new Object[] { cic, furnacNo, furnacNo });
        }
    }


    /**
     * 查询基础数据CIC
     */
    public CoreReturnObject queryComMscChemCode(String[] cic) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryComMscChemCode");

//        if (cic.split(",").length == 1) {
//            sql += " AND T.CIC='" + cic + "'";
//        }
        sql += CoreConverter.toSqlConditionStringOrLike("T.CIC",cic);
        sql += " ORDER BY T.CHEM_CODE ASC";
        return this.getData(sql);
    }

    /**
     * 查询交集CIC
     */
    public CoreReturnObject queryOrdDesigJJCic(String cic, String furnacNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryOrdDesigJJCic");
        return this.getData(sql, new Object[] { cic, furnacNo });
    }

    /**
     * 查询元素
     */
    public CoreReturnObject queryChenName(String chemCode, String chemType) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryChenName");
        if (!chemCode.equals("")) {
            sql += " AND T.CHEM_CODE ='" + chemCode + "'";
        }
        if (!chemType.equals("")) {
            sql += " AND T.CHEM_TYPE ='" + chemType + "'";
        }
        sql += "  ORDER BY T.CHEM_SEQ ASC,T.CHEM_CODE ASC";
        return this.getData(sql);
    }

    /**
     * 查询最大工艺卡号
     * @throws SQLException
     */
    public String queryMaxCraftNo(String gradCode, Connection con)
            throws SQLException {
        String craftNo = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxCraftNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, gradCode);
        ps.setObject(2, gradCode);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            craftNo = rs.getString("CRAFT_NO");
        }
        rs.close();
        ps.close();
        return craftNo;
    }

    /**
     * 查询最大成分Id
     * @throws SQLException
     */
    public String queryMaxCicNo(String craftNo, Connection con)
            throws SQLException {
        String cicNo = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxCicNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, craftNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            cicNo = rs.getString("CIC_GID");
        }
        rs.close();
        ps.close();
        return cicNo;
    }

    /**
     * 查询最大目标成分Id
     * @throws SQLException
     */
    public String queryMaxTarCicNo(String craftNo, Connection con)
            throws SQLException {
        String cicNo = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxTarCicNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, craftNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            cicNo = rs.getString("TAR_CIC_GID");
        }
        rs.close();
        ps.close();
        return cicNo;
    }

    /**
     * 查询最大目标成分Id序号
     * @throws SQLException
     */
    public int queryMaxTarCicXh(String tarCicNo, Connection con)
            throws SQLException {
        int xh = 1;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxTarCicXH");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, tarCicNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            xh = rs.getInt("XH") + 1;
        }
        rs.close();
        ps.close();
        return xh;
    }

    /**
     * 查询最大目CCM序号
     * @throws SQLException
     */
    public int queryMaxCcmXH(String ccmNo, Connection con) throws SQLException {
        int xh = 1;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxCcmXH");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ccmNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            xh = rs.getInt("XH") + 1;
        }
        rs.close();
        ps.close();
        return xh;
    }

    /**
     * 查询最大CCMId
     * @throws SQLException
     */
    public String queryMaxCcmNo(String craftNo, Connection con)
            throws SQLException {
        String cicNo = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxCcmNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, craftNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            cicNo = rs.getString("CCM_GID");
        }
        rs.close();
        ps.close();
        return cicNo;
    }

    /**
     * 查询最大目温度序号
     * @throws SQLException
     */
    public int queryMaxTepXH(String tepNo, Connection con) throws SQLException {
        int xh = 1;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxTepXH");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, tepNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            xh = rs.getInt("XH") + 1;
        }
        rs.close();
        ps.close();
        return xh;
    }

    /**
     * 查询最大目合金配置序号
     * @throws SQLException
     */
    public int queryMaxHjXH(String hjNo, Connection con) throws SQLException {
        int xh = 1;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxHjXH");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, hjNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            xh = rs.getInt("XH") + 1;
        }
        rs.close();
        ps.close();
        return xh;
    }

    /**
     * 查询最大温度Id
     * @throws SQLException
     */
    public String queryMaxTepNo(String craftNo, Connection con)
            throws SQLException {
        String cicNo = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMaxTepNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, craftNo);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            cicNo = rs.getString("TEP_GID");
        }
        rs.close();
        ps.close();
        return cicNo;
    }

    /**
     * 查询工艺版本号
     * @throws SQLException
     */
    public String queryLgMversionNo(String cic, String gradCode,
            String plineCode, Connection con) throws SQLException {
        String versionNo = "1";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryLgMversionNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, cic);
        ps.setObject(2, gradCode);
        ps.setObject(3, plineCode);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            versionNo = String.valueOf(rs.getInt("VERSION_NO") + 1);
        }
        rs.close();
        ps.close();
        return versionNo;
    }

    /**
     * 获取品名
     */
    public CoreReturnObject queryProduName(String produCode) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryProduName");
        return this.getData(sql);
    }

    /**
     * 查询内控标准
     */
    public CoreReturnObject queryStdCic(String cic) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryStdCic");
        String[] stringArr = cic.split(",");
        sql += CoreConverter.toSqlConditionString("C.CIC", stringArr);
        return this.getData(sql);
    }

    /**
     * 获取水表
     */
    public CoreReturnObject queryComBaseCcm(String plineCode, String gradeCode,
            String cic) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryComBaseCcm");
        return this.getData(sql, new Object[] { plineCode, gradeCode, cic });
    }

    /**
     * 获取温度上下偏差
     */
    public CoreReturnObject queryComBaseTep(String plineCode,
            String craftRoute, String gradeCode, String proPoint) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryComBaseTep");
        return this.getData(sql,
                new Object[] { plineCode, craftRoute, proPoint });
    }

    /***
     * 保存炼钢工艺卡
     * @param jsonListM 主信息
     * @param jsonListCic 成分信息
     * @param jsonListTar 目标成分信息
     * @param jsonListCcm ccm信息
     * @param jsonListTem 温度信息
     * @param username 操作人
     * @param flag 0:新增，1：修改
     * @return
     */
    public CoreReturnObject doSaveUpdate(ArrayList<String> jsonListM,
            ArrayList<String> jsonListCic, ArrayList<String> jsonListTar,
            ArrayList<String> jsonListCcm, ArrayList<String> jsonListTem,
            String username, String flag, String gradeCode,
            ArrayList<String> jsonListHj) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        // SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
        // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 获取数据库当前时间
            // String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
            // json转化为实体类
            List<CraftLg2MEntity> entityM = SteelHelper.jsonsToEntityList(
                    CraftLg2MEntity.class, jsonListM);
            List<CraftLg2CicEntity> entityCic = SteelHelper.jsonsToEntityList(
                    CraftLg2CicEntity.class, jsonListCic);
            List<CraftLg2TarCicEntity> entityTar = SteelHelper
                    .jsonsToEntityList(CraftLg2TarCicEntity.class, jsonListTar);
            List<CraftLg2CcmEntity> entityCcm = SteelHelper.jsonsToEntityList(
                    CraftLg2CcmEntity.class, jsonListCcm);
            List<CraftLg2TemperatureEntity> entityTem = SteelHelper
                    .jsonsToEntityList(CraftLg2TemperatureEntity.class,
                            jsonListTem);
            List<CraftLg2AlloyEntity> entityHj = SteelHelper.jsonsToEntityList(
                    CraftLg2AlloyEntity.class, jsonListHj);

            String cicNO = "";
            String tarCicNo = "";
            String ccmNo = "";
            String temNo = "";
            String craftNo = "";
            String alloyGid = "";
            if (flag.equals("0")) {// 新增
                craftNo = queryMaxCraftNo(gradeCode, con);
                cicNO = "CIC" + craftNo;
                // queryMaxCicNo(craftNo, con);
                tarCicNo = "ICIC" + craftNo;
                // queryMaxTarCicNo(craftNo, con);
                ccmNo = "CCM" + craftNo;
                // queryMaxCcmNo(craftNo, con);
                temNo = "TEP" + craftNo;
                alloyGid = "AOY" + craftNo;
                // queryMaxTepNo(craftNo, con);


                String DeleteLgCicSql = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgCic"),
                        entityCic.get(0));
                ps = con.prepareStatement(DeleteLgCicSql);
                ps.executeUpdate();
                ps.close();
                for (CraftLg2CicEntity entLgCic : entityCic) {
                    entLgCic.setCicGid(cicNO);
                    String sqlAddCic = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.addLgCic"),
                            entLgCic);
                    ps = con.prepareStatement(sqlAddCic);
                    ps.executeUpdate();
                    ps.close();

                }
                int xhTar = 1;
                for (CraftLg2TarCicEntity entLgTar : entityTar) {

                    if (xhTar == 0) {
                        // 查询最大序号
                        xhTar = queryMaxTarCicXh(ccmNo, con);
                    }
                    entLgTar.setTarCicGid(tarCicNo);
                    entLgTar.setXh(String.valueOf(xhTar));
                    String sqlAddTar = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.addLgTarCic"),
                            entLgTar);
                    ps = con.prepareStatement(sqlAddTar);
                    ps.executeUpdate();
                    ps.close();
                    xhTar++;

                }
                int xhCcm = 1;
                if (entityCcm != null) {
                    for (CraftLg2CcmEntity entLgCcm : entityCcm) {

                        if (xhCcm == 0) {
                            // 查询最大序号
                            xhCcm = queryMaxCcmXH(ccmNo, con);
                        }
                        entLgCcm.setCcmGid(ccmNo);
                        entLgCcm.setXh(String.valueOf(xhCcm));
                        String sqlAddCcm = EntityTool
                                .fillSqlByEntity(
                                        CoreXmlSqlParsersFactory
                                                .getSql("FrmSteelProcessCard.addLgCcm"),
                                        entLgCcm);
                        ps = con.prepareStatement(sqlAddCcm);
                        ps.executeUpdate();
                        ps.close();
                        xhCcm++;

                    }
                }
                int xhTep = 1;
                for (CraftLg2TemperatureEntity entLgTep : entityTem) {
                    if (xhTep == 0) {
                        // 查询最大序号
                        xhTep = queryMaxTepXH(temNo, con);
                    }
                    entLgTep.setTepGid(temNo);
                    entLgTep.setXh(String.valueOf(xhTep));
                    String sqlAddTep = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.addLgTep"),
                            entLgTep);
                    ps = con.prepareStatement(sqlAddTep);
                    ps.executeUpdate();
                    ps.close();
                    xhTep++;

                }
                int xhHj = 1;
                for (CraftLg2AlloyEntity entLgHj : entityHj) {
                    if (xhHj == 0) {
                        // 查询最大序号
                        xhHj = queryMaxHjXH(alloyGid, con);
                    }
                    entLgHj.setAlloyGid(alloyGid);
                    entLgHj.setXh(String.valueOf(xhHj));
                    String sqlAddAll = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.addLgHj"),
                            entLgHj);
                    ps = con.prepareStatement(sqlAddAll);
                    ps.executeUpdate();
                    ps.close();
                    xhHj++;
                }
                String versionNo = "";
                for (CraftLg2MEntity entLgM : entityM) {
                    entLgM.setCraftNo(craftNo);
                    entLgM.setTepGid(temNo);
                    entLgM.setCcmGid(ccmNo);
                    entLgM.setTarCicGid(tarCicNo);
                    entLgM.setCicGid(cicNO);
                    entLgM.setMarkMan(username);
                    entLgM.setAppMan(username);
                    entLgM.setAlloyGid(alloyGid);
                    if (versionNo.equals("")) {
                        versionNo = queryLgMversionNo(entLgM.getCic(), entLgM
                                .getGradecode(), entLgM.getPlineCode(), con);
                    }
                    entLgM.setVersionNo(versionNo);
                    String sqlAddM = EntityTool
                            .fillSqlByEntity(
                                    CoreXmlSqlParsersFactory
                                            .getSql("FrmSteelProcessCard.addLgCraftNoM"),
                                    entLgM);
                    ps = con.prepareStatement(sqlAddM);
                    ps.executeUpdate();
                    ps.close();
                }

            }
            else {
                String DeleteLgCicSql = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgCic"),
                        entityCic.get(0));
                ps = con.prepareStatement(DeleteLgCicSql);
                ps.executeUpdate();
                ps.close();
                for (CraftLg2CicEntity entLgCic : entityCic) {
/*

                    String sqlupdareCic = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.updateLgCic"),
                            entLgCic);
                    ps = con.prepareStatement(sqlupdareCic);
                    ps.executeUpdate();
                    ps.close();
*/
                    String sqlAddCic = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.addLgCic"),
                            entLgCic);
                    ps = con.prepareStatement(sqlAddCic);
                    ps.executeUpdate();
                    ps.close();
                }
                for (CraftLg2TarCicEntity entLgTar : entityTar) {

                    String sqlupdareTar = EntityTool
                            .fillSqlByEntity(
                                    CoreXmlSqlParsersFactory
                                            .getSql("FrmSteelProcessCard.updateLgTarCic"),
                                    entLgTar);
                    ps = con.prepareStatement(sqlupdareTar);
                    ps.executeUpdate();
                    ps.close();

                }
                if (entityCcm != null) {
                    for (CraftLg2CcmEntity entLgCcm : entityCcm) {

                        String sqlupdareCcm = EntityTool
                                .fillSqlByEntity(
                                        CoreXmlSqlParsersFactory
                                                .getSql("FrmSteelProcessCard.updateLgCcm"),
                                        entLgCcm);
                        ps = con.prepareStatement(sqlupdareCcm);
                        ps.executeUpdate();
                        ps.close();

                    }
                }
                for (CraftLg2TemperatureEntity entLgTep : entityTem) {

                    String sqlupdareTep = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.updateLgTem"),
                            entLgTep);
                    ps = con.prepareStatement(sqlupdareTep);
                    ps.executeUpdate();
                    ps.close();

                }
                for (CraftLg2AlloyEntity entLgHj : entityHj) {
                    String sqlAddAll = EntityTool.fillSqlByEntity(
                            CoreXmlSqlParsersFactory
                                    .getSql("FrmSteelProcessCard.UpdateLgHj"),
                            entLgHj);
                    ps = con.prepareStatement(sqlAddAll);
                    ps.executeUpdate();
                    ps.close();
                }
                for (CraftLg2MEntity entLgM : entityM) {
                    String sql = "SELECT COUNT(*) COUNT FROM PLN_STEELFORFURNACE T WHERE  T.CRAFT_NO='"
                            + entLgM.getCraftNo() + "' AND T.IF_CONFIRM='1'";
                    ps = con.prepareStatement(sql);
                    ResultSet rs = ps.executeQuery();
                    if (rs.next()) {
                        if (rs.getInt("COUNT") != 0) {
                            core.setV_errMsg("当前工艺文件号有炉次引用并确认,不能修改!");
                            return core;
                        }
                    }
                    rs.close();
                    ps.close();

                    String sqlUPDATEM = EntityTool
                            .fillSqlByEntity(
                                    CoreXmlSqlParsersFactory
                                            .getSql("FrmSteelProcessCard.updateLgCraftNoM"),
                                    entLgM);
                    ps = con.prepareStatement(sqlUPDATEM);
                    ps.executeUpdate();
                    ps.close();

                }
            }

            if (flag.equals("0")) {
                core.setV_errMsg("新增成功!");
            }
            else {
                core.setV_errMsg("修改成功!");
            }
            con.commit();
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 删除
     * @param jsonListM
     * @return
     */
    public CoreReturnObject doDelete(ArrayList<String> jsonListM,
            String proPlanId, String gxPlanNo, String flag) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 获取数据库当前时间
            // String dateTime = FrmSteelMakingPlanChange.getSystemTime(con);
            // json转化为实体类
            List<CraftLg2MEntity> entityM = SteelHelper.jsonsToEntityList(
                    CraftLg2MEntity.class, jsonListM);

            for (CraftLg2MEntity entLgM : entityM) {
                // 删除成分
                String sqldeleteCic = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgCic"),
                        entLgM);
                ps = con.prepareStatement(sqldeleteCic);
                ps.executeUpdate();
                ps.close();
                // 删除目标成分
                String sqlDeleteTar = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgTar"),
                        entLgM);
                ps = con.prepareStatement(sqlDeleteTar);
                ps.executeUpdate();
                ps.close();
                // 删除CCM
                String sqlDeleteCcm = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgCcm"),
                        entLgM);
                ps = con.prepareStatement(sqlDeleteCcm);
                ps.executeUpdate();
                ps.close();
                // 删除温度
                String sqlDeleteTep = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgTep"),
                        entLgM);
                ps = con.prepareStatement(sqlDeleteTep);
                ps.executeUpdate();
                ps.close();
                // 删除合金配置
                String sqlDeleteHj = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmSteelProcessCard.DeleteLgHj"),
                        entLgM);
                ps = con.prepareStatement(sqlDeleteHj);
                ps.executeUpdate();
                ps.close();
                // 删除工艺主表信息
                String sqlDeleteM = EntityTool
                        .fillSqlByEntity(
                                CoreXmlSqlParsersFactory
                                        .getSql("FrmSteelProcessCard.DeleteLgCraftNoM"),
                                entLgM);
                ps = con.prepareStatement(sqlDeleteM);
                ps.executeUpdate();
                ps.close();
                if (flag.equals("1")) {
                    String sql = "SELECT COUNT(*) COUNT FROM PLN_STEELFORFURNACE T WHere T.CRAFT_NO='"
                            + entLgM.getCraftNo() + "' AND T.IF_CONFIRM='1'";
                    ps = con.prepareStatement(sql);
                    ResultSet rs = ps.executeQuery();
                    if (rs.next()) {
                        if (rs.getInt("COUNT") != 0) {
                            core.setV_errMsg("当前工艺文件号有炉次引用并确认,不能删除!");
                            return core;
                        }
                    }
                    rs.close();
                    ps.close();
                    String sqlUpdate = CoreXmlSqlParsersFactory
                            .getSql("FrmSteelProcessCard.UpdateFurnNo");
                    ps = con.prepareStatement(sqlUpdate);
                    ps.setObject(1, "");
                    ps.setObject(2, entLgM.getCraftNo());
                    ps.executeUpdate();
                    ps.close();

                }
                else {

                    // 修改铸造订单工艺号
                    String sqlUpdate = CoreXmlSqlParsersFactory
                            .getSql("FrmSteelProcessCard.UpdatePlnOrderCastCraftNo");
                    ps = con.prepareStatement(sqlUpdate);
                    ps.setObject(1, proPlanId);
                    ps.setObject(2, gxPlanNo);
                    ps.setObject(3, entLgM.getCraftNo());
                    ps.executeUpdate();
                    ps.close();
                }
            }

            core.setV_errMsg("删除成功!");
            con.commit();
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     *  设置默认工艺卡
     * @return
     */
    public CoreReturnObject doSetDefault(String cic, String carftNo,
                                      String plineCodeA, String gradCode, String Craft_Route) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sqlUpdate = "";


                sqlUpdate ="UPDATE CRAFT_LG2_M SET DEFAULTCRAFT = '0' WHERE CIC=? AND GRADECODE=? AND PLINE_CODE=? AND CRAFT_ROUTE=?";
                ps = con.prepareStatement(sqlUpdate);
                ps.setObject(1, cic);
                ps.setObject(2, gradCode);
                ps.setObject(3, plineCodeA);
                ps.setObject(4, Craft_Route);
                ps.executeUpdate();
                ps.close();

                 sqlUpdate ="UPDATE CRAFT_LG2_M SET DEFAULTCRAFT = 1 WHERE CRAFT_NO=?";
                ps = con.prepareStatement(sqlUpdate);
                ps.setObject(1, carftNo);
                ps.executeUpdate();
                ps.close();


            core.setV_errMsg("设置成功!");
            con.commit();
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /**
     * 确认
     * @param craftNo
     * @param furnNo
     * @return
     */
    public CoreReturnObject doConfirm(String craftNo, String furnNo,
            String falg, String gxPlanNo) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sqlUpdate = "";
            if (falg.equals("1")) {
                sqlUpdate ="UPDATE PLN_STEELFORFURNACE SET CRAFT_NO=? WHERE FURNACENO=?";
                ps = con.prepareStatement(sqlUpdate);
                ps.setObject(1, craftNo);
                ps.setObject(2, furnNo);
                ps.executeUpdate();
                ps.close();
            }
            else {
                sqlUpdate = CoreXmlSqlParsersFactory
                        .getSql("FrmSteelProcessCard.UpdateFurnNoZz");
                ps = con.prepareStatement(sqlUpdate);
                ps.setObject(1, craftNo);
                ps.setObject(2, furnNo);
                ps.setObject(3, gxPlanNo);
                ps.executeUpdate();
                ps.close();
            }

            core.setV_errMsg("确认成功!");
            con.commit();
        }
        catch (Exception e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(e.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }


    /**
     * 查询基础钢种
     */
    public CoreReturnObject queryAllGrade() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryAllGrade");
        return this.getData(sql);
    }

    /**
     * 查询内控标准
     */
    public CoreReturnObject queryMscStd() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMscStd");
        return this.getData(sql);
    }

    /**
     * 查询内控标准成分
     */
    public CoreReturnObject queryMscStdChem(String[] CICs) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryMscStdChem");
        sql +=  CoreConverter.toSqlConditionString("CIC",CICs );

        return this.getData(sql);
    }



    /**
     * 查询内控标准成分
     */
    public CoreReturnObject queryAllMscStdChem() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryAllMscStdChem");

        return this.getData(sql);
    }

    /**
     * 查询基础元素表
     */
    public CoreReturnObject queryBaseChem() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSteelProcessCard.queryBaseChem");
        return this.getData(sql);
    }

}
