package com.steering.pss.ydm.pipemanage;

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.pss.judge.Bll.BllCreateZbsInfo;
import com.steering.pss.judge.Bll.BllZcColligateJudge;
import com.steering.pss.judge.model.CraftOrdDesignStdCicEntity;
import com.steering.pss.judge.model.JudgeCraftParam;
import com.steering.pss.judge.model.QcmJhyElementsEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.entity.*;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;

/**
 * @author TJG 2015-05-29
 */
public class FrmPipeMagement extends DBCommon {

    /**
     * 查询材料信息（成品）
     * @param
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryMatMent(ArrayList<String> params,
            String[] dataPurviews,String custom) {
        // String str = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryMatMent");
        String str = CoreConverter.toSqlConditionString("T1.BELONG_CODE",
                dataPurviews);
        if(!params.get(0).equals(""))
        {
        	sql += " AND T1.JUDGE_STOVE_NO = '"+params.get(0)+"'";
        }
        if(!params.get(1).equals(""))
        {
        	sql += " AND T2.STORAGE_NO = '"+params.get(1)+"'";
        }
        if(!params.get(2).equals(""))
        {
        	sql += " AND T1.SPEC_NAME LIKE '"+params.get(2)+"%'";
        }
        sql += " AND T1.PRODUCT_FLAG = '801402'";
        if(custom.equals("120501"))
        {
            sql += " AND T1.SUPPLY_UNIT_CODE = '120501'";
        }else if(custom.equals("120504"))
        {
            sql += " AND T1.SUPPLY_UNIT_CODE = '120504'";
        }
        sql += CoreConverter.toSqlConditionString("T1.BELONG_CODE",dataPurviews);
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,T1.PLINE_CODE,T1.PIPMEMO,T1.SUPPLY_UNIT_CODE,T1.SUPPLY_UNIT,"
                + "T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.FIX_NUM,T2.LOCATION_NO,"
                + "T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.LOCK_FLAG,T1.MNG_LOCK_FLAG,"
                + "T1.MEMO,T1.JUDGE_RESULT_CODE,T1.JUDGE_RESULT,T2.STORAGE_NO,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.ZBS_FLAG ";
        //ORDER BY T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MIN(T1.INSTOCK_TIME)
        return this.getData(sql);
    }

    /***
     * 查询商品物料
     * @param list
     * @param arr
     * @return
     */
    public CoreReturnObject queryMatMentS(ArrayList<String> list,
            String[] arr,String custom) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryMatMentS");
        if(!list.get(0).equals(""))
        {
        	sql += " AND T1.ORDER_NO || '&' LIKE  '"+list.get(0)+"' || '%'";
        }
        if(!list.get(1).equals(""))
        {
        	sql += " AND T1.JUDGE_STOVE_NO = '"+list.get(1)+"'";
        }
        if(!list.get(2).equals(""))
        {
        	sql += " AND T2.STORAGE_NO = '"+list.get(2)+"'";
        }
        if(!list.get(3).equals(""))
        {
        	sql += " AND T1.SPEC_NAME || '&' LIKE '%' || '"+list.get(3)+"' || '%'";
        }
        if(custom.equals("120501"))
        {
            sql += " AND T1.SUPPLY_UNIT_CODE = '120501' ";
        }else if(custom.equals("120504"))
        {
            sql += "  AND T1.SUPPLY_UNIT_CODE = '120504'";
        }
        //sql += CoreConverter.toSqlConditionString("T5.SALE_AREA_NO", arr);
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,T1.PIPMEMO,T2.LOCATION_NO,"
                + "T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.JUDGE_STD_NAME,T1.MNG_LOCK_FLAG,"
                + "T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.LOCK_FLAG,T1.PRO_NO,T1.LOCK_LIKU,T1.IN_MOLD,T1.FIX_NUM,"
                + "T1.MEMO,T1.JUDGE_RESULT_CODE,T1.JUDGE_RESULT,T2.STORAGE_NO,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC ORDER BY T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,"
                + "T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MIN(T1.INSTOCK_TIME)";
        return this.getData(sql);
    }

    /**
     * 查询历史信息
     * @param params
     * @return
     */
    public CoreReturnObject queryHistoryMatMent(ArrayList<String> params,
            String[] dataPurviews,String custom,String startTim,String endTim) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryHistoryMatMent");
        sql += CoreConverter.toSqlConditionString("T1.BELONG_CODE",
                dataPurviews);
        if(custom.equals("120501"))
        {
            sql += " AND T1.SUPPLY_UNIT_CODE = '120501' ";
        }else if(custom.equals("120504"))
        {
            sql += "  AND T1.SUPPLY_UNIT_CODE = '120504'";
        }
        if(!startTim.equals(""))
        {
            sql +=  " and t1.CREATE_TIME >= to_date('"+startTim+"', 'yyyy-MM-dd HH24:mi:ss') and t1.CREATE_TIME < to_date('"+endTim+"', 'yyyy-MM-dd HH24:mi:ss')";
        }
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.EVENT_ID,"
                + "T1.EVENT_DESC,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,"
                + "T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,"
                + "T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.IN_MOLD,T1.FINISHPRO_MOLD,"
                + "T1.CREATE_NAME,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.MEMO ORDER BY T1.JUDGE_STOVE_NO,"
                + "T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MAX(T1.CREATE_TIME)";
        return this.getData(sql, params.toArray());
    }

    /**
     * 查询脱单历史信息
     * @param params
     * @return
     */
    public CoreReturnObject queryHistoryMomner(ArrayList<String> params,
            String[] dataPurviews,String custom) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryHistoryMomner");
        sql += CoreConverter.toSqlConditionString("T1.BELONG_CODE",
                dataPurviews);
        if(custom.equals("120501"))
        {
            sql += " AND T1.SUPPLY_UNIT_CODE = '120501' ";
        }else if(custom.equals("120504"))
        {
            sql += "  AND T1.SUPPLY_UNIT_CODE = '120504'";
        }

        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.EVENT_ID,"
                + "T1.EVENT_DESC,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,"
                + "T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,"
                + "T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.FINISHPRO_MOLD,T1.IN_MOLD,"
                + "T1.CREATE_NAME,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.MEMO ORDER BY T1.JUDGE_STOVE_NO,"
                + "T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MAX(T1.CREATE_TIME)";
        return this.getData(sql, params.toArray());
    }

    /**
     * 查询材料类别信息
     * @return CoreReturnObject
     */
    public CoreReturnObject queryMaterialSTyle() {
        return this.getDataBySqlID("FrmPipeMagement.queryMaterialSTyle");
    }

    /**
     * 查询材料状态信息
     * @return CoreReturnObject
     */
    public CoreReturnObject queryMaterialStatus() {
        return this.getDataBySqlID("FrmPipeMagement.queryMaterialStatus");
    }

    /**
     * 查询材料状态信息
     * @return CoreReturnObject
     */
    public CoreReturnObject queryMaterialStatus(String flag) {
        return this.getDataBySqlID("FrmPipeMagement.queryMaterialStatusflag",
                new Object[] { flag });
    }

    /**
     * 查询交货行信息
     * @param
     * @return CoreReturnObject
     */
    public CoreReturnObject getDelivyInfo(MatZcMEntity matZcEntity,
            String orderNo, String[] dataPurviews) {
        QualityDesignTool.nullConverNullString(matZcEntity);
        Object[] params = new Object[] { orderNo, matZcEntity.getProduccode(),
                matZcEntity.getSteelcode(), matZcEntity.getStdCode(),
                matZcEntity.getStdStyle(), matZcEntity.getModelCode(),
                matZcEntity.getSpecCode() };
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.getDelivyInfo");
        //sql += CoreConverter.toSqlConditionString("T1.SALE_AREA_NO",
        //       dataPurviews);
        sql += "    ORDER BY T1.ORDER_NO,T2.ORDER_SEQ,T3.DELIVERY_NO";
        return this.getData(sql, params);
    }

    /**
     * 脱单
     * @param paramList
     * @param monomerExplain
     * @param isRolling
     * @param userName
     * @param deptId
     * @return
     */
    public CoreReturnObject Monomer(ArrayList<MatZcMEntity> paramList,
            String monomerExplain, String isRolling, String userName,
            String deptId, String[] validDataPurviewIds) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "";
        String processSql = "";
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);

            // 保存所有需要脱单的物料数据
            List<MatZcMInfoEntity> matZcMInfoEntityList = new ArrayList<MatZcMInfoEntity>();
            List<MatZcMInfoEntity> matZcMInfoEntitys = new ArrayList<MatZcMInfoEntity>();
            for (MatZcMEntity matZcMEntity : paramList) {
                QualityDesignTool.nullConverNullString(matZcMEntity);
                int countOrdNo = FrmPipeMagement.checkOrdNoHead(con,
                        matZcMEntity.getOrdPk(), validDataPurviewIds);
                if (countOrdNo == 0) {
                    core.setV_errMsg("没有权限对合同号:" + matZcMEntity.getOrderNo()
                            + "的库存信息脱单!");
                    return core;
                }
                // 处理标准类别为空
                if (matZcMEntity.getStdStyle().equals("")) {
                    matZcMEntity.setStdStyle("/");
                }
                matZcMInfoEntitys = FrmPipeMagement.getMageStatus(con,
                        matZcMEntity, "Monomer");
                if (matZcMInfoEntitys == null) {
                    core.setV_errMsg("炉号:" + matZcMEntity.getJudgeStoveNo()
                            + "批号:" + matZcMEntity.getBatchNo() + "组号:"
                            + matZcMEntity.getBatchGroudNo()
                            + "材料信息已改变,请重新获取材料信息!");
                    return core;
                }
                for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntitys) {
                    if (!matZcMInfoEntity.getProductFlag().equals("801403")
                            || !matZcMInfoEntity.getMatStatus()
                                    .equals("80150301")) {
                        core.setV_errMsg("炉号:"
                                + matZcMInfoEntity.getJudgeStoveNo() + "批号:"
                                + matZcMInfoEntity.getBatchNo() + "组号:"
                                + matZcMInfoEntity.getBatchGroudNo()
                                + "不是商品/材料可编计划的物料信息,不能脱单!");
                        return core;
                    }
                    matZcMInfoEntityList.add(matZcMInfoEntity);
                }
            }
            // 将脱单的物料数据写入钢管物料跟踪履历表
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.addMatZcTrack");
            int seq = 0;
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyyMMddHHmmss");
            DecimalFormat df = new DecimalFormat("0000000000");

            SimpleDateFormat dateFormat1 = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String systime = dateFormat.format(now);
            double wgt = 0;
            int count = 0;
            double len = 0;
            for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntityList) {
                seq += 1;
                String resumeSeq = dateFormat.format(now) + df.format(seq);
                MatZcTrackEntity matZcTrackEntity = FrmPipeMagement
                        .copyOfMatZcMInfoEntityByMonomer(matZcMInfoEntity,
                                userName, monomerExplain);
                matZcTrackEntity.setResumeSeq(resumeSeq);
                matZcTrackEntity.setCreateTime(systime);
                String sqlAdd = EntityTool.fillSqlByEntity(sql,
                        matZcTrackEntity);
                ps = con.prepareStatement(sqlAdd);
                ps.executeUpdate();
                ps.close();
            }
            // 将轧管、热处理、管加工等钢管生产物料主表的对应数据修改状态
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.updateMaterialStatus");
            for (MatZcMEntity matZcMEntity : paramList) {
                String sqlMonomer = EntityTool.fillSqlByEntity(sql,
                        matZcMEntity);
                // String[] arr = new String[matZcMInfoEntitys.size()];
                for (int i = 0; i < matZcMInfoEntitys.size(); i++) {
                    MatZcMInfoEntity matList = matZcMInfoEntitys.get(i);
                    ps = con.prepareStatement(sqlMonomer);
                    ps.setString(1, matList.getMatNo());
                    ps.executeUpdate();
                    ps.close();
                    wgt += Double
                            .parseDouble((matList.getActWeight().toString()));
                    count += Integer.parseInt(matList.getActCount());
                    if (!matList.getActLen().equals("")) {
                        len += Double
                                .parseDouble((matList.getActLen().toString()));
                    }
                    else {
                        len += 0;
                    }
                }
                processSql = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeMagement.UpdateProcessMonomer");// 写进程表
                ps = con.prepareStatement(processSql);
                ps.setString(1, String.valueOf(count));
                ps.setString(2, String.valueOf(wgt));
                ps.setString(3, String.valueOf(len));
                ps.setString(4, matZcMEntity.getOrdLnDlyPk());
                ps.executeUpdate();
                ps.close();
            }
            core.setV_errMsg("脱单成功!");
            con.commit();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return core;
    }

    /***
     * 脱单
     * @param parm
     * @param monomerExplain
     * @param isRolling
     * @param userName
     * @param deptId
     * @param validDataPurviewIds
     * @return
     */
    public CoreReturnObject monomer(ArrayList<String> parm,
            String monomerExplain, String isRolling, String userName,
            String deptId, String[] validDataPurviewIds,String finishedProduct) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        ResultSet rs = null;
        PreparedStatement psta = null;
        String sql = "";
        String acount = "";
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String monomerSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.monomer");
            PreparedStatement monomerPsta = con.prepareStatement(monomerSql);
            String updateDivelNoSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.updateDiveNo");// 修改交货行脱单操作单号
            PreparedStatement updateDiveNoPsta = con
                    .prepareStatement(updateDivelNoSql);
            for (int i = 0; i < parm.size(); i++) {
                MatZcMInfoEntity matZcMEntity = EntityTool.getEntityByJSONStr(
                        parm.get(i), MatZcMInfoEntity.class);
                if (!matZcMEntity.getMatStatus().equals("材料可编提单")) {
                    core.setV_errMsg("材料状态不为材料可编提单,不允许脱单!");
                    return core;
                }
                else {
                    matZcMEntity.setMatStatus("80150301");
                }
                QualityDesignTool.nullConverNullString(matZcMEntity);
                // 处理标准类别为空
                if (matZcMEntity.getStdStyle().equals("")) {
                    matZcMEntity.setStdStyle("/");
                }
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeMagement.selectMatZc");
                String selectMatsql = EntityTool.fillSqlByEntity(sql,
                        matZcMEntity);
                psta = con.prepareStatement(selectMatsql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    acount = rs.getString("ACOUNT");
                }
                rs.close();
                psta.close();
                if (!acount.equals(matZcMEntity.getActCount())) {
                    core.setV_errMsg("材料状态发生了变化,请刷新页面!");
                    return core;
                }
                // 判断交货行是否能够脱单
                DelivyEntity delivyInfo = FrmPipeMagement
                        .getDelivyInfoByKey(con, matZcMEntity.getOrdLnDlyPk());

                monomerPsta.setString(1, matZcMEntity.getJudgeStoveNo());
                monomerPsta.setString(2, matZcMEntity.getBatchNo());
                monomerPsta.setString(3, matZcMEntity.getBatchGroudNo());
                monomerPsta.setString(4, matZcMEntity.getProduccode());
                monomerPsta.setString(5, matZcMEntity.getSteelcode());
                monomerPsta.setString(6, matZcMEntity.getSpecCode());
                monomerPsta.setString(7, matZcMEntity.getModelCode());
                monomerPsta.setString(8, matZcMEntity.getStdStyle());
                monomerPsta.setString(9, matZcMEntity.getMatStatus());
                monomerPsta.setString(10, matZcMEntity.getStorageNo());
                monomerPsta.setString(11, matZcMEntity.getOrdPk());
                monomerPsta.setString(12, matZcMEntity.getOrdLnPk());
                monomerPsta.setString(13, matZcMEntity.getOrdLnDlyPk());
                monomerPsta.setString(14, matZcMEntity.getProNo());
                monomerPsta.setString(15, matZcMEntity.getInMold());
                monomerPsta.setString(16, matZcMEntity.getActLenMin());
                monomerPsta.setString(17, matZcMEntity.getActLenMax());
                monomerPsta.setString(18,matZcMEntity.getPipmemo());
                monomerPsta.setString(19,matZcMEntity.getLocationNo());

                monomerPsta.setInt(20,
                        Integer.parseInt(matZcMEntity.getTdactCount()) + 1);
                monomerPsta.setString(21, matZcMEntity.getJudgeStoveNo());
                monomerPsta.setString(22, matZcMEntity.getBatchNo());
                monomerPsta.setString(23, matZcMEntity.getBatchGroudNo());
                monomerPsta.setString(24, matZcMEntity.getProduccode());
                monomerPsta.setString(25, matZcMEntity.getSteelcode());
                monomerPsta.setString(26, matZcMEntity.getSpecCode());
                monomerPsta.setString(27, matZcMEntity.getModelCode());
                monomerPsta.setString(28, matZcMEntity.getStdStyle());
                monomerPsta.setString(29, matZcMEntity.getMatStatus());
                monomerPsta.setString(30, matZcMEntity.getStorageNo());
                monomerPsta.setString(31, matZcMEntity.getOrdPk());
                monomerPsta.setString(32, matZcMEntity.getOrdLnPk());
                monomerPsta.setString(33, matZcMEntity.getOrdLnDlyPk());
                monomerPsta.setString(34, matZcMEntity.getProNo());
                monomerPsta.setString(35, matZcMEntity.getInMold());
                monomerPsta.setString(36, matZcMEntity.getActLenMin());
                monomerPsta.setString(37, matZcMEntity.getActLenMax());
                monomerPsta.setString(38, matZcMEntity.getFixNum());
                monomerPsta.setString(39,matZcMEntity.getPipmemo());
                monomerPsta.setString(40,matZcMEntity.getLocationNo());
                monomerPsta.setInt(41,
                        Integer.parseInt(matZcMEntity.getTdactCount()) + 1);
                monomerPsta.setString(42, monomerExplain);
                monomerPsta.setString(43, userName);
                monomerPsta.setString(44, finishedProduct);
                monomerPsta.setString(45, finishedProduct);
                monomerPsta.setString(46, matZcMEntity.getOrdLnDlyPk());
                monomerPsta.addBatch();
                if (matZcMEntity.getActCount()
                        .equals(matZcMEntity.getTdactCount())) {
                    updateDiveNoPsta.setString(1, matZcMEntity.getProNo());
                    updateDiveNoPsta.setString(2, matZcMEntity.getOrdLnDlyPk());
                    updateDiveNoPsta.addBatch();
                }
            }

            monomerPsta.executeBatch();
            updateDiveNoPsta.executeBatch();
            core.setV_errMsg("脱单成功!");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    core.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
    /***
     * 按支脱单
     * @return
     */
    public CoreReturnObject seqMoner(ArrayList<String> parm,
            String monomerExplain, String isRolling, String userName,
            String deptId, String[] validDataPurviewIds,String finishedProduct,String _acount)
    {
    	CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        ResultSet rs = null;
        PreparedStatement psta = null;
        String sql = "";
        int acount = 0;
        double wgt = 0.0;
        double len = 0.0;
        try {
        	con.setAutoCommit(false);
            ArrayList<String> sqllist = new ArrayList<String>();
            for(String str : parm)
            {
            	MatZcMEntity matZcEntity = EntityTool.getEntityByJSONStr(str, MatZcMEntity.class);
            	
            	MatZcTrackEntity matZcTrackEntity = new MatZcTrackEntity();
            	matZcTrackEntity.setMatNo(matZcEntity.getMatNo());
            	matZcTrackEntity.setCreateName(userName);
            	matZcTrackEntity.setMemo(monomerExplain);
            	matZcTrackEntity.setFinishproMold(finishedProduct);
            	String insertMatZcTrackSql = CoreXmlSqlParsersFactory.getSql("seqMoner.insertMatZcTarck");
            	insertMatZcTrackSql = EntityTool.fillSqlByEntity(insertMatZcTrackSql, matZcTrackEntity);
            	sqllist.add(insertMatZcTrackSql);
            	
            	/*matZcEntity.setFinishproMold(finishedProduct);
            	String updateMatZcSql = CoreXmlSqlParsersFactory.getSql("seqMoner.UpdateMatZcSeq");
            	updateMatZcSql = EntityTool.fillSqlByEntity(updateMatZcSql, matZcEntity);
            	sqllist.add(updateMatZcSql);*/
            	
            	acount = acount + 1;
            	wgt += matZcEntity.getActWeight();
            	len += matZcEntity.getActLen();
            }
            if(acount - Integer.parseInt(_acount) == 0)
            {
            	String updateOrdPkSql = CoreXmlSqlParsersFactory.getSql("seqMoner.UpdateOrdPk");
            	updateOrdPkSql = updateOrdPkSql.replace("#PRO_NO#", EntityTool.getEntityByJSONStr(parm.get(0), MatZcMEntity.class).getProNo());
            	updateOrdPkSql = EntityTool.fillSqlByEntity(updateOrdPkSql, EntityTool.getEntityByJSONStr(parm.get(0), MatZcMEntity.class).getOrdLnDlyPk());
            	sqllist.add(updateOrdPkSql);
            }
            String updateProcessSql = CoreXmlSqlParsersFactory.getSql("seqMoner.UpdateProcess");
            psta = con.prepareStatement(updateProcessSql);
            psta.setString(1, String.valueOf(acount));
            psta.setString(2, String.valueOf(wgt));
            psta.setString(3, String.valueOf(len));
            psta.setString(4, EntityTool.getEntityByJSONStr(parm.get(0), MatZcMEntity.class).getOrdLnDlyPk());
            psta.executeUpdate();
            
            core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
            if(core.getV_errMsg().equals(""))
            {
            	core.setV_errMsg("脱单成功!");
            }
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    core.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /***
     * 补量新增交货行
     * @return
     */
    public CoreReturnObject insertOrderSeqNo(ArrayList<String> parm,
            String userName, String departId, String wgt) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        ResultSet rs = null;
        PreparedStatement psta = null;
        String sql = "";
        String flag = "";
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String insertSql = CoreXmlSqlParsersFactory
                    .getSql("insertOrderSeqNo.insertOrderSeqDivleyNo");// 新增交货行
            PreparedStatement insertPsta = con.prepareStatement(insertSql);
            String updateTrackSql = CoreXmlSqlParsersFactory
                    .getSql("insertOrderSeqNo.updateFlag");
            PreparedStatement updateTrackPsta = con
                    .prepareStatement(updateTrackSql);
            for (int k = 0; k < parm.size(); k++) {
                MatZcTrackEntity matZcTrack2 = EntityTool.getEntityByJSONStr(
                        parm.get(k), MatZcTrackEntity.class);
                sql = CoreXmlSqlParsersFactory
                        .getSql("insertOrderSeqNo.selectMatZcTrackO");
                psta = con.prepareStatement(sql);
                psta.setString(1, matZcTrack2.getOrdPk());
                psta.setString(2, matZcTrack2.getOrdLnPk());
                psta.setString(3, matZcTrack2.getOrdLnDlyPk());
                rs = psta.executeQuery();
                if (rs.next()) {
                    flag = rs.getString("FILL_QUANTITY_FLAG");
                }
                rs.close();
                psta.close();
                if (flag == "1") {
                    core.setV_errMsg("此交货行已进行过补量了,请核实!");
                    return core;
                }
            }
            MatZcTrackEntity matZcTrack = EntityTool
                    .getEntityByJSONStr(parm.get(0), MatZcTrackEntity.class);
            insertPsta.setString(1, matZcTrack.getOrdPk());
            insertPsta.setString(2, matZcTrack.getOrdLnPk());
            insertPsta.setString(3, matZcTrack.getOrdPk());
            insertPsta.setString(4, matZcTrack.getOrdLnPk());
            insertPsta.setString(5, matZcTrack.getOrdLnDlyPk());
            insertPsta.setString(6, wgt);
            insertPsta.setString(7, wgt);
            insertPsta.setString(8, userName);
            insertPsta.addBatch();
            for (int i = 0; i < parm.size(); i++) {
                MatZcTrackEntity matZcTrack1 = EntityTool.getEntityByJSONStr(
                        parm.get(i), MatZcTrackEntity.class);
                updateTrackPsta.setString(1, matZcTrack1.getJudgeStoveNo());
                updateTrackPsta.setString(2, matZcTrack1.getBatchNo());
                updateTrackPsta.setString(3, matZcTrack1.getBatchGroudNo());
                updateTrackPsta.addBatch();
            }
            insertPsta.executeBatch();
            updateTrackPsta.executeBatch();
            core.setV_errMsg("补量成功!");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    core.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 查询物料主表信息
     * @param con
     * @param matzcMEntity 查询条件
     * @return
     * @throws SQLException
     */
    public static List<MatZcMInfoEntity> getMageStatus(Connection con,
                                                       MatZcMEntity matzcMEntity, String flag) throws SQLException {
        return getMageStatus(con,matzcMEntity,flag,null);
    }
    public static List<MatZcMInfoEntity> getMageStatus(Connection con,
            MatZcMEntity matzcMEntity, String flag,ArrayList<String> matNoList) throws SQLException {
        String sql = "";
        PreparedStatement ps = null;
        if (!flag.equals("Liku")) { // 脱单
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.getMageStatusByMononer");
            sql = EntityTool.fillSqlByEntity(sql, matzcMEntity);
            double e = matzcMEntity.getTdactCount();
            int t = (int) e;
            ps = con.prepareStatement(sql);
            if (matzcMEntity.getProductFlag().equals("商品")) {
                ps.setString(1, "801403");
            }
            else {
                ps.setString(1, "");
            }
            ps.setInt(2, t + 1);
        }
        else { // 利库
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.getMageStatusByLiku");
            double d = matzcMEntity.getLkactCount();
            int s = (int) d;
            if("连铸圆坯".equals(matzcMEntity.getProducname())){
                sql = StringTool.ReplaceAll(sql, "#conditions#", " AND T1.FIX_NUM = #FIX_NUM#");
            }else{
                if (matNoList != null && matNoList.size() > 0) {
                    sql = sql.replaceAll("#conditions#", Matcher.quoteReplacement(CoreConverter.toSqlConditionString("T1.mat_no", matNoList.toArray(new String[matNoList.size()]))));
                } else {
                    sql = StringTool.ReplaceAll(sql, "#conditions#", " ");
                }
            }

            sql = EntityTool.fillSqlByEntity(sql, matzcMEntity);
            ps = con.prepareStatement(sql);
            if (matzcMEntity.getProductFlag().equals("801402")) {
                ps.setString(1, "801402");
            }
            else {
                ps.setString(1, "");
            }
            ps.setString(2, matzcMEntity.getMatStatus());
            ps.setInt(3, s + 1);
        }
        ResultSet rs = ps.executeQuery();
        ArrayList<MatZcMInfoEntity> list = EntityTool.getEntitysByRs(rs,
                MatZcMInfoEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            for (MatZcMInfoEntity matZcMEntity2 : list) {
                QualityDesignTool.nullConverNullString(matZcMEntity2);
            }
            return list;
        }
        else {
            return null;
        }
    }
    /***
     * 按支查询状态信息
     * @return
     * @throws SQLException 
     */
    public static MatZcMInfoEntity getMatNo(Connection con,
            MatZcMEntity matzcMEntity) throws SQLException
    {
    	String sql = "";
        PreparedStatement ps = null;
        sql = CoreXmlSqlParsersFactory
        .getSql("FrmPipeMagement.selectMatNoStaus");
        sql = EntityTool.fillSqlByEntity(sql, matzcMEntity);
        ps = con.prepareStatement(sql);
        ps.setString(1, matzcMEntity.getMatNo());
        ResultSet rs = ps.executeQuery();
        MatZcMInfoEntity list = EntityTool.getEntityByRs(rs,
                MatZcMInfoEntity.class);
        rs.close();
        ps.close();
        if (list != null) {
            
            return list;
        }
        else {
            return null;
        }
    }

    
    /**
     * 赋值钢管物料跟踪履历表(利库)
     * @param matZcMInfoEntity
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static MatZcTrackEntity copyOfMatZcMInfoEntityByMonomer(
            MatZcMInfoEntity matZcMInfoEntity, String userName,
            String monomerExplain)
                    throws IllegalAccessException, InvocationTargetException {
        MatZcTrackEntity matZcTrackEntity = new MatZcTrackEntity();
        BeanUtils.copyProperties(matZcTrackEntity, matZcMInfoEntity);
        matZcTrackEntity.setEventId("900101");
        matZcTrackEntity.setEventDesc("脱单");
        matZcTrackEntity
                .setFormName("com.steering.pss.ydm.pipemanage.FrmPipeMagement");
        matZcTrackEntity.setCallFuncCoding("Monomer");
        matZcTrackEntity.setKeyvalue1("ArrayList<MatZcMEntity> paramList");
        matZcTrackEntity.setKeyvalue1Desc("物料主表关键字(group)实体类集合");
        matZcTrackEntity.setKeyvalue2("String monomerExplain");
        matZcTrackEntity.setKeyvalue2Desc2("脱单说明");
        matZcTrackEntity.setKeyvalue3("String isRolling");
        matZcTrackEntity.setKeyvalue3Desc3("是否补轧");
        matZcTrackEntity.setKeyvalue4("String userName");
        matZcTrackEntity.setKeyvalue4Desc4("操作人");
        matZcTrackEntity.setKeyvalue5("String deptId");
        matZcTrackEntity.setKeyvalue5Desc5("操作人部门ID");
        matZcTrackEntity.setCreateName(userName);
        matZcTrackEntity.setMemo(monomerExplain);
        return matZcTrackEntity;
    }

    /**
     * 赋值钢管物料跟踪履历表(利库)
     * @param matZcMInfoEntity
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static MatZcTrackEntity copyOfMatZcMInfoEntityByLiku(
            MatZcMInfoEntity matZcMInfoEntity, String userName)
                    throws IllegalAccessException, InvocationTargetException {
        MatZcTrackEntity matZcTrackEntity = new MatZcTrackEntity();
        BeanUtils.copyProperties(matZcTrackEntity, matZcMInfoEntity);
        matZcTrackEntity.setEventId("900102");
        matZcTrackEntity.setEventDesc("利库");
        matZcTrackEntity
                .setFormName("com.steering.pss.ydm.pipemanage.FrmPipeMagement");
        matZcTrackEntity.setCallFuncCoding("Liku");
        matZcTrackEntity.setKeyvalue1("DelivyEntity delivyEntity");
        matZcTrackEntity.setKeyvalue1Desc("交货行信息");
        matZcTrackEntity.setKeyvalue2("ArrayList<MatZcMEntity> matZcMEntitys");
        matZcTrackEntity.setKeyvalue2Desc2("物料主表关键字(group)实体类集合");
        matZcTrackEntity.setKeyvalue3("String userName");
        matZcTrackEntity.setKeyvalue3Desc3("操作人");
        matZcTrackEntity.setCreateName(userName);
        return matZcTrackEntity;
    }

    /**
     * 赋值钢管物料跟踪履历表(强制利库)
     * @param matZcMInfoEntity
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static MatZcTrackEntity doForceCopyOfMatZcMInfoEntityByLiku(
            MatZcMInfoEntity matZcMInfoEntity, String userName)
                    throws IllegalAccessException, InvocationTargetException {
        MatZcTrackEntity matZcTrackEntity = new MatZcTrackEntity();
        BeanUtils.copyProperties(matZcTrackEntity, matZcMInfoEntity);
        matZcTrackEntity.setEventId("900103");
        matZcTrackEntity.setEventDesc("强制利库");
        matZcTrackEntity
                .setFormName("com.steering.pss.ydm.pipemanage.FrmPipeMagement");
        matZcTrackEntity.setCallFuncCoding("doForceLiku");
        matZcTrackEntity.setKeyvalue1("DelivyEntity delivyEntity");
        matZcTrackEntity.setKeyvalue1Desc("交货行信息");
        matZcTrackEntity.setKeyvalue2("ArrayList<MatZcMEntity> matZcMEntitys");
        matZcTrackEntity.setKeyvalue2Desc2("物料主表关键字(group)实体类集合");
        matZcTrackEntity.setKeyvalue3("String userName");
        matZcTrackEntity.setKeyvalue3Desc3("操作人");
        matZcTrackEntity.setCreateName(userName);
        return matZcTrackEntity;
    }

    /***
     * 按支利库
     * @param matZcTity
     * @param ordLnDlyPk
     * @param userName
     * @return
     */
    public CoreReturnObject seqLiku(ArrayList<String> matZcTity,
            String ordLnDlyPk, String userName,String custom)
    {
    	CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "";
        String processSql = "";
        String proNo = "";
        ResultSet rs = null;
        String processCode = "";//工序代码
        String matStaus = "";
        String inDoc = "";
        String outDoc = "";
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String bathYear = df.format(now);
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            ArrayList<String> sqllist = new ArrayList<String>();
            
            MatZcMInfoEntity matZcMInfoEntitys = null;
            // 判断交货行是否能够利库
            DelivyEntity delivyInfo = FrmPipeMagement.getDelivyInfoByKey(con,
                    ordLnDlyPk);
            String judgeId = getJudgeId(con);
            
            int seq = 0;

            SimpleDateFormat dateFormat1 = new SimpleDateFormat(
                    "yyyyMMddHHmmss");
            DecimalFormat df1 = new DecimalFormat("0000000000");
            double wgt = 0;
            int count = 0;
            double len = 0;
            proNo = doProNo(con, "900103", delivyInfo.getSaleOrg());
            
            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectProcessCode");//查询工序代码
        	sql = EntityTool.fillSqlByEntity(sql, EntityTool.getEntityByJSONStr(matZcTity.get(0), MatZcMEntity.class));
        	ps = con.prepareStatement(sql);
        	rs = ps.executeQuery();
        	if(rs.next())
        	{
        		processCode = rs.getString("PROCESS_CODE");
        	}
        	rs.close();
        	ps.close();

            inDoc = getInListDoc(con, "800712");
            outDoc = getOutListDoc(con, "800812");
            for(String str : matZcTity)
            {
            	MatZcMEntity matZcEntity = EntityTool.getEntityByJSONStr(str, MatZcMEntity.class);
                if(!matZcEntity.getBelongCode().equals(delivyInfo.getSaleOrg())){
                    core.setV_errMsg("物料不在您所在的销售组织，请所属权转移!");
                    return core;
                }
            	QualityDesignTool.nullConverNullString(matZcEntity);
                // 处理标准类别为空
                if (matZcEntity.getStdStyle().equals("")) {
                	matZcEntity.setStdStyle("/");
                }
                
                matZcMInfoEntitys = FrmPipeMagement.getMatNo(con, matZcEntity);
                if((!matZcMInfoEntitys.getMatStatus().equals("80150203"))
                        && (!matZcMInfoEntitys.getMatStatus()
                                .equals("80150204")
                                && (!matZcMInfoEntitys.getMatStatus()
                                        .equals("80150202"))))
                {
                	core.setV_errMsg("材料号:"
                            + matZcEntity.getMatNo()
                            + "材料状态不是 材料为余材(已综合判定) 或者 材料脱单余材(销售组织脱单),不能利库!");
                    return core;
                }               
                if(delivyInfo == null)
                {
                	core.setV_errMsg("现货合同/预排合同/研发合同不允许进行强制利库!");
                	return core;
                }
                String lock = getOrderLineLock(con, delivyInfo.getOrdLnPk());
                String valflag = getOrderLineCraft(con, delivyInfo.getOrdLnPk());
                
                if (lock.equals("1")) {
                    core.setV_errMsg("您选择交货行所在合同行处于变更中,不允许强制利库!");
                    return core;
                }
                if ((!delivyInfo.getDeliveryStatus().equals("12020301"))
                        && (!delivyInfo.getDeliveryStatus().equals("12020502"))
                        && (!delivyInfo.getDeliveryStatus()
                                .equals("12020602"))&&(!delivyInfo.getDeliveryStatus().equals("12020401"))&&(!delivyInfo.getDeliveryStatus().equals("12020402"))&&(!delivyInfo.getDeliveryStatus().equals("12020403"))&&(!delivyInfo.getDeliveryStatus().equals("12020701"))) {
                    core.setV_errMsg("交货行只有生效或者生产完毕/缴库完毕状态的,才能利库!");
                    return core;
                }
                if(matZcEntity.getJudgeResult().equals("缺项合格"))
                {
                	if(!matZcEntity.getProOrderNo().equals(delivyInfo.getOrderNo()+"/"+delivyInfo.getOrderSeq()))
                    {
//                    	core.setV_errMsg("此物料是缺项合格产品,不允许利库到不是原合同上!");
//                    	return core;
                    }
                }



                //当合同所属权不等于库存所属权写入出交易
                if(!matZcEntity.getBelongCode().equals(delivyInfo.getSaleOrg()))
                {
                    YdmZcInlistEntity ydmEntity = new YdmZcInlistEntity();
                    ydmEntity.setInstockDoc(inDoc);
                    ydmEntity.setOutstockDoc(outDoc);
                    ydmEntity.setMatNo(matZcEntity.getMatNo());
                    ydmEntity.setInstockSqe(inDoc + StringTool.formatDecimal(seq+1, "0000"));
                    ydmEntity.setOutstockSqe(outDoc + StringTool.formatDecimal(seq+1, "0000"));
                    ydmEntity.setNewbelongCode(delivyInfo.getSaleOrg());
                    ydmEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                    ydmEntity.setCreateName(userName);
                    ydmEntity.setCreateTime(systime);
                    ydmEntity.setBalYearMonth(bathYear);
//                    String addInList = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.insertInListOut");
//                    addInList = EntityTool.fillSqlByEntity(addInList,ydmEntity);
//                    ps = con.prepareStatement(addInList);
//                    ps.executeUpdate();
//                    ps.close();
                }




                 sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.addMatZcTrack");
				matZcMInfoEntitys.setOrderNo(delivyInfo.getOrderNo());
				matZcMInfoEntitys.setOrderSeq(delivyInfo.getOrderSeq());
				matZcMInfoEntitys.setOrderSeqNo(delivyInfo.getDeliveryNo());
				matZcMInfoEntitys.setOrdPk(delivyInfo.getOrdPk());
				matZcMInfoEntitys.setOrdLnPk(delivyInfo.getOrdLnPk());
				matZcMInfoEntitys.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
				matZcMInfoEntitys.setBelongCode(delivyInfo.getSaleOrg());// 销售组织代码
				matZcMInfoEntitys.setBelongName(delivyInfo.getSaleOrgDesc());
                matZcMInfoEntitys.setSupplyUnitCode(custom);
                if(custom.equals("120501"))
                {
                    matZcMInfoEntitys.setSupplyUnit("天津钢管制造有限公司");
                }else if(custom.equals("120504"))
                {
                    matZcMInfoEntitys.setSupplyUnit("江苏天淮钢管有限公司");
                }
				seq += 1;
				String resumeSeq = dateFormat1.format(now) + df1.format(seq);
				MatZcTrackEntity matZcTrackEntity = FrmPipeMagement
						.doForceCopyOfMatZcMInfoEntityByLiku(matZcMInfoEntitys,
								userName);
				matZcTrackEntity.setResumeSeq(resumeSeq);
                matZcTrackEntity.setCreateTime(systime);
				String sqlAdd = EntityTool.fillSqlByEntity(sql,
						matZcTrackEntity);
				sqlAdd = sqlAdd.replace("#PRO_NO#", proNo);
				ps = con.prepareStatement(sqlAdd);
				ps.executeUpdate();
				ps.close();
				
                matZcEntity.setProNo(proNo);
                matZcEntity.setLockLiku("0");
                if((delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108")) && matZcEntity.getZbsFlag().equals("1"))
                {
                	if(!(delivyInfo.getOrderNo() + "/" + delivyInfo
							.getOrderSeq()).equals(matZcEntity
									.getProOrderNo()))
                	{
                		matZcEntity.setLockFlag("2");
    					matZcEntity.setLockId(judgeId);
                	}else
                	{
                		matZcEntity.setLockFlag("0");
    					matZcEntity.setLockId("");
                	}
                }else
                {
                	matZcEntity.setLockFlag("0");
					matZcEntity.setLockId("");
                }
				
				
				matZcEntity.setOrderNo(delivyInfo.getOrderNo());
				matZcEntity.setOrderSeq(delivyInfo.getOrderSeq());
				matZcEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
				matZcEntity.setOrdPk(delivyInfo.getOrdPk());
				matZcEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
				matZcEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
				matZcEntity.setBelongCode(delivyInfo.getSaleOrg());// 销售组织代码
				matZcEntity.setBelongName(delivyInfo.getSaleOrgDesc());
                
				String updateMatZcSql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.LikuSeq");
				updateMatZcSql = EntityTool.fillSqlByEntity(updateMatZcSql, matZcEntity);
				ps = con.prepareStatement(updateMatZcSql);
				ps.executeUpdate();
				ps.close();




				wgt += matZcEntity.getActWeight();
				count += Integer.parseInt(matZcEntity.getActCount());
                if (matZcEntity.getActLen()> 0) {
                    len += matZcEntity.getActLen();
                }
                else {
                    len += 0;
                }
                
            }
            
            MatZcMEntity matZcEntityZ = EntityTool.getEntityByJSONStr(matZcTity.get(0), MatZcMEntity.class);
            if(matZcEntityZ.getProducname() != null)
            {
                if(!matZcEntityZ.getProducname().contains("坯"))
                {
                    if(matZcEntityZ.getZbsFlag().equals("1"))
                    {
                        //现货利库
                        if(delivyInfo.getOrderTyp().equals("120102"))
                        {
                            BllCreateZbsInfo.saveSpotInfo(matZcEntityZ.getJudgeStoveNo(),matZcEntityZ.getBatchNo(),matZcEntityZ.getBatchGroudNo(),processCode,delivyInfo.getOrdLnDlyPk(),con);
                        }
                        //期货利库
                        else if(delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108"))
                        {
                            if(!(delivyInfo.getOrderNo() + "/" + delivyInfo.getOrderSeq()).equals(matZcEntityZ.getProOrderNo()))
                            {
                                Boolean bol = compareBaclogDesc(matZcEntityZ,delivyInfo,con);
                                if(bol)
                                {
                                    String craftFileNo = createJudgeApply(matZcEntityZ,delivyInfo,userName,judgeId,con,custom);
                                    if(craftFileNo.equals(""))
                                    {
                                        core.setV_errMsg("此利库合同无生效工艺文件,不允许利库");
                                        return core;
                                    }
                                }
                                else
                                {
                                    core.setV_errMsg("生产合同与利库合同制程不一致,不允许利库!");
                                    return core;
                                }
                            }
                        }
                    }
                }
            }

			processSql = CoreXmlSqlParsersFactory
					.getSql("FrmPipeMagement.UpdateProcessLiKu");
			ps = con.prepareStatement(processSql);
			ps.setString(1, String.valueOf(count));
			ps.setString(2, String.valueOf(wgt));
			ps.setString(3, String.valueOf(len));
			ps.setString(4, delivyInfo.getOrdLnDlyPk());
			ps.setString(5, proNo);
			ps.setString(6, delivyInfo.getOrdLnDlyPk());
			ps.executeUpdate();
			ps.close();
            
			
			core.setV_errMsg("利库成功!");
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }
    
    /***
     * 强制利库
     * @param matZcMEntitys
     * @param ordLnDlyPk
     * @param userName
     * @return
     */
    public CoreReturnObject doForceLiku(ArrayList<MatZcMEntity> matZcMEntitys,
                                        String ordLnDlyPk, String userName,String formFrom,String custom) {
        return doForceLiku(matZcMEntitys,ordLnDlyPk,userName,formFrom,custom,null);
    }
    public CoreReturnObject doForceLiku(ArrayList<MatZcMEntity> matZcMEntitys,
            String ordLnDlyPk, String userName,String formFrom,String custom,ArrayList<String> matNoList) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "";
        String processSql = "";
        String proNo = "";
        ResultSet rs = null;
        String processCode = "";//工序代码
        String inDoc = "";
        String outDoc = "";
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String bathYear = df.format(now);
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            // 保存所有需要利库的物料数据
            List<MatZcMInfoEntity> matZcMInfoEntityList = new ArrayList<MatZcMInfoEntity>();
            List<MatZcMInfoEntity> matZcMInfoEntitys = new ArrayList<MatZcMInfoEntity>();
            double countWeight = 0;
            // 判断交货行是否能够利库
            DelivyEntity delivyInfo = FrmPipeMagement.getDelivyInfoByKey(con,
                    ordLnDlyPk);
            String judgeId = getJudgeId(con);
            inDoc = getInListDoc(con, "800712");
            outDoc = getOutListDoc(con, "800812");
            for (MatZcMEntity matZcMEntity : matZcMEntitys) {
                QualityDesignTool.nullConverNullString(matZcMEntity);
                if(!matZcMEntity.getBelongCode().equals(delivyInfo.getSaleOrg())){
                    core.setV_errMsg("物料不在您所在的销售组织，请所属权转移!");
                    return core;
                }
                // 处理标准类别为空
                if (matZcMEntity.getStdStyle().equals("")) {
                    matZcMEntity.setStdStyle("/");
                }
                matZcMInfoEntitys = FrmPipeMagement.getMageStatus(con,
                        matZcMEntity, "Liku",matNoList);
                if (matZcMInfoEntitys == null) {
                    core.setV_errMsg("炉号:" + matZcMEntity.getJudgeStoveNo()
                            + "批号:" + matZcMEntity.getBatchNo() + "组号:"
                            + matZcMEntity.getBatchGroudNo()
                            + "材料信息已改变,请重新获取材料信息!");
                    return core;
                }
                if(delivyInfo == null)
                {
                	core.setV_errMsg("现货合同/预排合同/研发合同不允许进行强制利库!");
                	return core;
                }
                String lock = getOrderLineLock(con, delivyInfo.getOrdLnPk());
                String valflag = getOrderLineCraft(con, delivyInfo.getOrdLnPk());
                
                if (lock.equals("1")) {
                    core.setV_errMsg("您选择交货行所在合同行处于变更中,不允许强制利库!");
                    return core;
                }
                // && (!delivyInfo.getDeliveryStatus().equals("12020302"))
                if ((!delivyInfo.getDeliveryStatus().equals("12020301")) && (!delivyInfo.getDeliveryStatus().equals("12020501"))
                        && (!delivyInfo.getDeliveryStatus().equals("12020502"))
                        && (!delivyInfo.getDeliveryStatus()
                                .equals("12020602"))&&(!delivyInfo.getDeliveryStatus().equals("12020401"))&&(!delivyInfo.getDeliveryStatus().equals("12020402"))&&(!delivyInfo.getDeliveryStatus().equals("12020403"))&&(!delivyInfo.getDeliveryStatus().equals("12020701"))) {
                    core.setV_errMsg("交货行只有生效或者生产完毕/缴库完毕状态的,才能利库!");
                    return core;
                }
                if(matZcMEntity.getJudgeResult().equals("缺项合格"))
                {
                	if(!matZcMEntity.getProOrderNo().equals(delivyInfo.getOrderNo()+"/"+delivyInfo.getOrderSeq()))
                    {
//                    	core.setV_errMsg("此物料是缺项合格产品,不允许利库到不是原合同上!");
//                    	return core;
                    }
                }
                // 判断物料是否能够利库
                for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntitys) {
                    if ((!matZcMInfoEntity.getMatStatus().equals("80150203"))
                            && (!matZcMInfoEntity.getMatStatus()
                                    .equals("80150204")
                                    && (!matZcMInfoEntity.getMatStatus()
                                            .equals("80150202")))) {
                        core.setV_errMsg("炉号:"
                                + matZcMInfoEntity.getJudgeStoveNo() + "批号:"
                                + matZcMInfoEntity.getBatchNo() + "组号:"
                                + matZcMInfoEntity.getBatchGroudNo()
                                + "材料状态不是 材料为余材(已综合判定) 或者 材料脱单余材(销售组织脱单),不能利库!");
                        return core;
                    }
                    countWeight += Double.parseDouble(
                            matZcMInfoEntity.getActWeight().toString());
                    matZcMInfoEntityList.add(matZcMInfoEntity);
                }
            }
            double orderWgtEstmt = Double.parseDouble(delivyInfo.getWgtEstmt())
                    * (1 + Double.parseDouble(delivyInfo.getDelvryRangeMax())
                            / 100);
            double likuWeight = FrmPipeMagement.queryWeightByLiku(con,
                    ordLnDlyPk);

            proNo = doProNo(con, "900103", delivyInfo.getSaleOrg());
            // 将利库的物料数据写入钢管物料跟踪履历表
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.addMatZcTrack");
            int seq = 0;

            SimpleDateFormat dateFormat1 = new SimpleDateFormat(
                    "yyyyMMddHHmmss");
            DecimalFormat df1 = new DecimalFormat("0000000000");
            double wgt = 0;
            int count = 0;
            double len = 0;
            for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntityList) {
                matZcMInfoEntity.setOrderNo(delivyInfo.getOrderNo());
                matZcMInfoEntity.setOrderSeq(delivyInfo.getOrderSeq());
                matZcMInfoEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
                matZcMInfoEntity.setOrdPk(delivyInfo.getOrdPk());
                matZcMInfoEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
                matZcMInfoEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
                matZcMInfoEntity.setBelongCode(delivyInfo.getSaleOrg());// 销售组织代码
                matZcMInfoEntity.setBelongName(delivyInfo.getSaleOrgDesc());
                if (StringUtils.isEmpty(custom)) {
                    custom = delivyInfo.getSupplyUnit();
                }
                matZcMInfoEntity.setSupplyUnitCode(custom);
                if(custom.equals("120501"))
                {
                    matZcMInfoEntity.setSupplyUnit("天津钢管制造有限公司");
                }else if(custom.equals("120504"))
                {
                    matZcMInfoEntity.setSupplyUnit("江苏天淮钢管有限公司");
                }
                seq += 1;
                String resumeSeq = dateFormat1.format(now) + df1.format(seq);
                MatZcTrackEntity matZcTrackEntity = FrmPipeMagement
                        .doForceCopyOfMatZcMInfoEntityByLiku(matZcMInfoEntity,
                                userName);
                matZcTrackEntity.setResumeSeq(resumeSeq);
                matZcTrackEntity.setCreateTime(systime);
                String sqlAdd = EntityTool.fillSqlByEntity(sql,
                        matZcTrackEntity);
                sqlAdd = sqlAdd.replace("#PRO_NO#", proNo);
                ps = con.prepareStatement(sqlAdd);
                ps.executeUpdate();
                ps.close();
            }
            // 轧管、热处理、管加工等钢管生产物料主表的对应数据修改状态
            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.Liku");
            for (MatZcMEntity matZcMEntity : matZcMEntitys) {
                if("连铸圆坯".equals(matZcMEntity.getProducname())){
                    matZcMInfoEntityList.clear();
                    matZcMInfoEntitys = FrmPipeMagement.getMageStatus(con,
                            matZcMEntity, "Liku");
                    for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntitys) {
                        matZcMInfoEntityList.add(matZcMInfoEntity);
                    }
                }
                matZcMEntity.setOrderNo(delivyInfo.getOrderNo());
                matZcMEntity.setOrderSeq(delivyInfo.getOrderSeq());
                matZcMEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
                matZcMEntity.setOrdPk(delivyInfo.getOrdPk());
                matZcMEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
                matZcMEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
                matZcMEntity.setNewbelongCode(delivyInfo.getSaleOrg());// 销售组织代码
                matZcMEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                matZcMEntity.setStdCode(delivyInfo.getStdCode());
                matZcMEntity.setStdName(delivyInfo.getStdName());
                String sqlLiku = EntityTool.fillSqlByEntity(sql, matZcMEntity);
                String[] arr = new String[matZcMInfoEntitys.size()];

                for (int i = 0; i < matZcMInfoEntitys.size(); i++) {


                    MatZcMInfoEntity matList = matZcMInfoEntitys.get(i);
                    arr[i] = matList.getMatNo();
                    //当合同所属权不等于库存所属权写入出交易
                    if(!matZcMEntity.getBelongCode().equals(delivyInfo.getSaleOrg()))
                    {
                        YdmZcInlistEntity ydmEntity = new YdmZcInlistEntity();
                        ydmEntity.setInstockDoc(inDoc);
                        ydmEntity.setOutstockDoc(outDoc);
                        ydmEntity.setMatNo(arr[i].toString());
                        ydmEntity.setInstockSqe(inDoc + StringTool.formatDecimal(i+1, "0000"));
                        ydmEntity.setOutstockSqe(outDoc + StringTool.formatDecimal(i+1, "0000"));
                        ydmEntity.setNewbelongCode(delivyInfo.getSaleOrg());
                        ydmEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                        ydmEntity.setCreateName(userName);
                        ydmEntity.setCreateTime(systime);
                        ydmEntity.setBalYearMonth(bathYear);
//                        String addInList = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.insertInListOut");
//                        addInList = EntityTool.fillSqlByEntity(addInList,ydmEntity);
//                        ps = con.prepareStatement(addInList);
//                        ps.executeUpdate();
//                        ps.close();
                    }



                    ps = con.prepareStatement(sqlLiku);
                    ps.setString(1, proNo);
                    if(formFrom.equals("1"))
                    {
                    	ps.setString(2, "0");
                    }else
                    {
                    	ps.setString(2, "1");
                    }
                    if((delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108")) && matZcMInfoEntitys.get(0).getZbsFlag().equals("1"))
                    {
                    	if(!(delivyInfo.getOrderNo() + "/" + delivyInfo
    							.getOrderSeq()).equals(matZcMEntity
    									.getProOrderNo()))
                    	{
                    		ps.setString(3, "2");
                        	ps.setString(4, judgeId);
                    	}else
                    	{
                    		ps.setString(3, "0");
                        	ps.setString(4, "");
                    	}
                    }else
                    {
                    	ps.setString(3, "0");
                    	ps.setString(4, "");
                    }
                    ps.setString(5, arr[i].toString());
                    ps.executeUpdate();
                    ps.close();
                    wgt += Double
                            .parseDouble((matList.getActWeight().toString()));
                    count += Integer.parseInt(matList.getActCount());
                    if (!matList.getActLen().equals("")) {
                        len += Double
                                .parseDouble((matList.getActLen().toString()));
                    }
                    else {
                        len += 0;
                    }
                }
                if(matZcMInfoEntitys.get(0).getProducname() != null)
                {
                    if(!matZcMInfoEntitys.get(0).getProducname().contains("坯"))
                    {
                        if(matZcMInfoEntitys.get(0).getZbsFlag().equals("1"))
                        {
                            //现货利库
                            if(delivyInfo.getOrderTyp().equals("120102"))
                            {
                                BllCreateZbsInfo.saveSpotInfo(matZcMEntity.getJudgeStoveNo(),matZcMEntity.getBatchNo(),matZcMEntity.getBatchGroudNo(),processCode,delivyInfo.getOrdLnDlyPk(),con);
                            }
                            //期货利库
                            else if(delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108"))
                            {
                                if(!(delivyInfo.getOrderNo() + "/" + delivyInfo.getOrderSeq()).equals(matZcMEntity.getProOrderNo()))
                                {
                                    Boolean bol = compareBaclogDesc(matZcMEntity,delivyInfo,con);
                                    if(bol)
                                    {
                                        String craftFileNo = createJudgeApply(matZcMEntity,delivyInfo,userName,judgeId,con,custom);
                                        if(craftFileNo.equals(""))
                                        {
                                            core.setV_errMsg("此利库合同无生效工艺文件,不允许强制利库");
                                            return core;
                                        }
                                    }
                                    else
                                    {
                                        core.setV_errMsg("生产合同与利库合同制程不一致,不允许强制利库!");
                                        return core;
                                    }
                                }
                            }
                        }
                    }
                }

            }
            processSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.UpdateProcessLiKu");
            ps = con.prepareStatement(processSql);
            ps.setString(1, String.valueOf(count));
            ps.setString(2, String.valueOf(wgt));
            ps.setString(3, String.valueOf(len));
            ps.setString(4, delivyInfo.getOrdLnDlyPk());
            ps.setString(5, proNo);
            ps.setString(6, delivyInfo.getOrdLnDlyPk());
            ps.executeUpdate();
            ps.close();

            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectProcessCode");//查询工序代码
        	sql = EntityTool.fillSqlByEntity(sql, matZcMEntitys.get(0));
        	ps = con.prepareStatement(sql);
        	rs = ps.executeQuery();
        	if(rs.next())
        	{
        		processCode = rs.getString("PROCESS_CODE");
        	}
        	rs.close();
        	ps.close();
        	
            
            core.setV_errMsg("强制利库成功!");
            con.commit();
//            con.rollback();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /***
     * 比较全程途径码
     * @return
     * @throws SQLException 
     */
    public Boolean compareBaclogDesc(MatZcMEntity matZcEntity,DelivyEntity delivyEntity,Connection con) throws SQLException
    {
    	String sql = "";
    	PreparedStatement psta = null;
    	ResultSet rs = null;
    	String proOrderNo = "";//原生产合同号
    	String oldBacklogDesc = "";//原制程
    	String backLogDesc = "";//新合同制程
    	
    	sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectMatZcProOrderNo");
    	sql = EntityTool.fillSqlByEntity(sql, matZcEntity);
    	psta = con.prepareStatement(sql);
    	rs = psta.executeQuery();
    	if(rs.next())
    	{
    		proOrderNo = rs.getString("PRO_ORDER_NO");
    	}
    	rs.close();
    	psta.close();
    	
    	
    	//查询原合同的全程途径码
    	PlnOrderZgSEntity plnEntity = new PlnOrderZgSEntity();
    	plnEntity.setOrderNo(proOrderNo);
    	sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectPlnOrd");
    	sql = EntityTool.fillSqlByEntity(sql, plnEntity);
    	psta = con.prepareStatement(sql);
    	rs = psta.executeQuery();
    	if(rs.next())
    	{
    		oldBacklogDesc = rs.getString("WHOLE_BACKLOG");
    	}
    	rs.close();
    	psta.close();
    	
    	//查询新合同的全程途径码
    	PlnOrderZgSEntity plnNewEntity = new PlnOrderZgSEntity();
    	plnNewEntity.setOrderNo(delivyEntity.getOrderNo());
    	plnNewEntity.setOrderSeq(delivyEntity.getOrderSeq());
    	plnNewEntity.setOrdLnDlyPk(delivyEntity.getOrdLnDlyPk());
    	sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectNewPlnOrd");
    	sql = EntityTool.fillSqlByEntity(sql, plnNewEntity);
    	psta = con.prepareStatement(sql);
    	rs = psta.executeQuery();
    	if(rs.next())
    	{
    		backLogDesc = rs.getString("WHOLE_BACKLOG");
    	}
    	rs.close();
    	psta.close();
    	
    	if(!oldBacklogDesc.equals("") && !backLogDesc.equals("")) {
    	    String prefix = "A-B-";//兼容新流程 去掉A-B-
    	    
    	    String processedOld = oldBacklogDesc.startsWith(prefix) ? 
    	        oldBacklogDesc.substring(prefix.length()) : oldBacklogDesc;
    	    
    	    String processedNew = backLogDesc.startsWith(prefix) ? 
    	        backLogDesc.substring(prefix.length()) : backLogDesc;
    	    
    	    if(processedOld.equals(processedNew)) {
    	        return true;
    	    } else {
    	        return false;
    	    }
    	}
    	
    	return true;
    }
    /**
     * 改判
     * @param
     * @param matZcMEntitys 物料信息集合
     * @return CoreReturnObject
     */
    public CoreReturnObject GaiPan(ArrayList<MatZcMEntity> matZcMEntitys,
            String ordLnDlyPk, String userName,String formFrom,String custom) {
        return GaiPan( matZcMEntitys, ordLnDlyPk,  userName, formFrom, custom,null);
    }
    public CoreReturnObject GaiPan(ArrayList<MatZcMEntity> matZcMEntitys,
            String ordLnDlyPk, String userName,String formFrom,String custom,ArrayList<String> matNoList) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        SqlBuilder b = null;
        String sql = "";
        String processSql = "";
        String proNo = "";
        ResultSet rs = null;
        String processCode = "";//工序代码
        String inDoc = "";//入库单号
        String outDoc = "";//出库单号
        try {
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
            String systime = dateFormat.format(now);
            String bathYear = df.format(now);
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            // 保存所有需要改判的物料数据
            List<MatZcMInfoEntity> matZcMInfoEntityList = new ArrayList<MatZcMInfoEntity>();
            List<MatZcMInfoEntity> matZcMInfoEntitys = new ArrayList<MatZcMInfoEntity>();
            double countWeight = 0;
            // 判断交货行是否能够改判
            DelivyEntity delivyInfo = FrmPipeMagement.getDelivyInfoByKey(con,
                    ordLnDlyPk);
            String judgeId = getJudgeId(con);
            inDoc = getInListDoc(con, "800712");
            outDoc = getOutListDoc(con, "800812");
            for (MatZcMEntity matZcMEntity : matZcMEntitys) {
                QualityDesignTool.nullConverNullString(matZcMEntity);
                if(!matZcMEntity.getBelongCode().equals(delivyInfo.getSaleOrg())){
                    core.setV_errMsg("物料不在您所在的销售组织，请所属权转移!");
                    return core;
                }
                // 处理标准类别为空
                if (matZcMEntity.getStdStyle().equals("")) {
                    matZcMEntity.setStdStyle("/");
                }
                matZcMInfoEntitys = FrmPipeMagement.getMageStatus(con,
                        matZcMEntity, "Liku",matNoList);
                if (matZcMInfoEntitys == null) {
                    core.setV_errMsg("炉号:" + matZcMEntity.getJudgeStoveNo()
                            + "批号:" + matZcMEntity.getBatchNo() + "组号:"
                            + matZcMEntity.getBatchGroudNo()
                            + "材料信息已改变,请重新获取材料信息!");
                    return core;
                }
                if(delivyInfo == null)
                {
                	core.setV_errMsg("现货合同/预排合同/研发合同不允许进行改判!");
                	return core;
                }
                String lock = getOrderLineLock(con, delivyInfo.getOrdLnPk());
                String valflag = getOrderLineCraft(con, delivyInfo.getOrdLnPk());
                if (lock.equals("1")) {
                    core.setV_errMsg("您选择的交货行所在合同行处在变更中,不允许改判!");
                    return core;
                }
                // && (!delivyInfo.getDeliveryStatus().equals("12020302"))
                if ((!delivyInfo.getDeliveryStatus().equals("12020301"))
                        && (!delivyInfo.getDeliveryStatus().equals("12020502"))
                        ) {
                	//&& (!delivyInfo.getDeliveryStatus().equals("12020602"))/缴库完毕
                    core.setV_errMsg("交货行只有生效或者生产完毕状态的,才能改判!");
                    return core;
                }
                if(matZcMEntity.getJudgeResult().equals("缺项合格"))
                {
                	if(!matZcMEntity.getProOrderNo().equals(delivyInfo.getOrderNo()+"/"+delivyInfo.getOrderSeq()))
                    {
//                    	core.setV_errMsg("此物料是缺项合格产品,不允许利库到不是原合同上!");
//                    	return core;
                    }
                }
                
                // 判断物料是否能够利库
                for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntitys) {
                    if ((!matZcMInfoEntity.getMatStatus().equals("80150203"))
                            && (!matZcMInfoEntity.getMatStatus()
                                    .equals("80150204")
                                    && (!matZcMInfoEntity.getMatStatus()
                                            .equals("80150202")))) {
                        core.setV_errMsg("炉号:"
                                + matZcMInfoEntity.getJudgeStoveNo() + "批号:"
                                + matZcMInfoEntity.getBatchNo() + "组号:"
                                + matZcMInfoEntity.getBatchGroudNo()
                                + "材料状态不是 材料为余材(已综合判定) 或者 材料脱单余材(销售组织脱单),不能改判!");
                        return core;
                    }
                    countWeight += Double.parseDouble(
                            matZcMInfoEntity.getActWeight().toString());
                    matZcMInfoEntityList.add(matZcMInfoEntity);
                }
              //成分判定
                QcmJhyElementsEntity jhyChem = BllZcColligateJudge.getLgJhyChem(matZcMEntity.getStoveNo(), b, con);
                Object[] chemResults = null;
                if (jhyChem != null) {
                	b = new SqlBuilder("CraftOrdDesignStdCic.getCraftJfChemByOrdLnPk1");
                    b.Fill(new String[]{delivyInfo.getOrdLnPk()});
                    ArrayList<CraftOrdDesignStdCicEntity> jfChems = EntityTool.getEntitysBySqlNoClsCon(
                            b.toString(), con, CraftOrdDesignStdCicEntity.class);
                    chemResults = BllZcColligateJudge.chemJudge(jhyChem, jfChems, jfChems, "HX");
                    if(chemResults[1].toString().equals("0")){
                    	core.setV_errMsg("成分实绩不满足交付标准,不允许改判!");
                        return core;
                    }
                } else {
                	core.setV_errMsg("找不到熔炼成分实绩,不允许改判!");
                    return core;
                }
            }
            double orderWgtEstmt = Double.parseDouble(delivyInfo.getWgtEstmt())
                    * (1 + Double.parseDouble(delivyInfo.getDelvryRangeMax())
                            / 100);
            double likuWeight = FrmPipeMagement.queryWeightByLiku(con,
                    ordLnDlyPk);
            proNo = doProNo(con, "900102", delivyInfo.getSaleOrg());
            // 将利库的物料数据写入钢管物料跟踪履历表
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.addMatZcTrack");
            int seq = 0;
            double wgt = 0;
            int count = 0;
            double len = 0;
            SimpleDateFormat dateFormat1 = new SimpleDateFormat(
                    "yyyyMMddHHmmss");
            DecimalFormat df1 = new DecimalFormat("0000000000");
            for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntityList) {
                matZcMInfoEntity.setOrderNo(delivyInfo.getOrderNo());
                matZcMInfoEntity.setOrderSeq(delivyInfo.getOrderSeq());
                matZcMInfoEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
                matZcMInfoEntity.setOrdPk(delivyInfo.getOrdPk());
                matZcMInfoEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
                matZcMInfoEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
                matZcMInfoEntity.setBelongCode(delivyInfo.getSaleOrg());// 销售组织代码
                matZcMInfoEntity.setBelongName(delivyInfo.getSaleOrgDesc());
                matZcMInfoEntity.setSupplyUnitCode(custom);
                if(custom.equals("120501"))
                {
                    matZcMInfoEntity.setSupplyUnit("天津钢管制造有限公司");
                }else if(custom.equals("120504"))
                {
                    matZcMInfoEntity.setSupplyUnit("江苏天淮钢管有限公司");
                }
                seq += 1;
                String resumeSeq = dateFormat1.format(now) + df1.format(seq);
                MatZcTrackEntity matZcTrackEntity = FrmPipeMagement
                        .copyOfMatZcMInfoEntityByLiku(matZcMInfoEntity,
                                userName);
                matZcTrackEntity.setResumeSeq(resumeSeq);
                matZcTrackEntity.setCreateTime(systime);
                String sqlAdd = EntityTool.fillSqlByEntity(sql,
                        matZcTrackEntity);
                sqlAdd = sqlAdd.replace("#PRO_NO#", proNo);// 新加的2016-03-02
                ps = con.prepareStatement(sqlAdd);
                ps.executeUpdate();
                ps.close();

            }

            // 轧管、热处理、管加工等钢管生产物料主表的对应数据修改状态
            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.Liku");
            for (MatZcMEntity matZcMEntity : matZcMEntitys) {
                matZcMEntity.setOrderNo(delivyInfo.getOrderNo());
                matZcMEntity.setOrderSeq(delivyInfo.getOrderSeq());
                matZcMEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
                matZcMEntity.setOrdPk(delivyInfo.getOrdPk());
                matZcMEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
                matZcMEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
                matZcMEntity.setNewbelongCode(delivyInfo.getSaleOrg());// 销售组织代码
                matZcMEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                matZcMEntity.setStdCode(delivyInfo.getStdCode());
                matZcMEntity.setStdName(delivyInfo.getStdName());
                String sqlLiku = EntityTool.fillSqlByEntity(sql, matZcMEntity);
                String[] arr = new String[matZcMInfoEntitys.size()];







                for (int i = 0; i < matZcMInfoEntitys.size(); i++) {
                    MatZcMInfoEntity matList = matZcMInfoEntitys.get(i);
                    arr[i] = matList.getMatNo();
                    //当合同所属权不等于库存所属权写入出交易
                    if(!matZcMEntity.getBelongCode().equals(delivyInfo.getSaleOrg()))
                    {
                        YdmZcInlistEntity ydmEntity = new YdmZcInlistEntity();
                        ydmEntity.setInstockDoc(inDoc);
                        ydmEntity.setOutstockDoc(outDoc);
                        ydmEntity.setMatNo(arr[i].toString());
                        ydmEntity.setInstockSqe(inDoc + StringTool.formatDecimal(i+1, "0000"));
                        ydmEntity.setOutstockSqe(outDoc + StringTool.formatDecimal(i+1, "0000"));
                        ydmEntity.setNewbelongCode(delivyInfo.getSaleOrg());
                        ydmEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                        ydmEntity.setCreateName(userName);
                        ydmEntity.setCreateTime(systime);
                        ydmEntity.setBalYearMonth(bathYear);
//                        String addInList = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.insertInListOut");
//                        addInList = EntityTool.fillSqlByEntity(addInList,ydmEntity);
//                        ps = con.prepareStatement(addInList);
//                        ps.executeUpdate();
//                        ps.close();
                    }





                    ps = con.prepareStatement(sqlLiku);
                    ps.setString(1, proNo);// 新增加2016-03-02
                    if(formFrom.equals("1"))
                    {
                    	ps.setString(2, "0");
                    }else
                    {
                    	ps.setString(2, "1");
                    }
                    if((delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108")) && matZcMEntity.getZbsFlag().equals("1"))
                    {
                    	if(!(delivyInfo.getOrderNo() + "/" + delivyInfo
    							.getOrderSeq()).equals(matZcMEntity
    									.getProOrderNo()))
                    	{
                    		ps.setString(3, "2");
                        	ps.setString(4, judgeId);
                    	}else
                    	{
                    		ps.setString(3, "0");
                        	ps.setString(4, "");
                    	}
                    }else
                    {
                    	ps.setString(3, "0");
                    	ps.setString(4, "");
                    }
                    ps.setString(5, arr[i].toString());
                    ps.executeUpdate();
                    ps.close();
                    wgt += Double
                            .parseDouble((matList.getActWeight().toString()));
                    count += Integer.parseInt(matList.getActCount());
                    if (!matList.getActLen().equals("")) {
                        len += Double
                                .parseDouble((matList.getActLen().toString()));
                    }
                    else {
                        len += 0;
                    }



                }
                if(matZcMInfoEntitys.get(0).getProducname() != null) {
                    if (!matZcMInfoEntitys.get(0).getProducname().contains("坯")) {
                        if(matZcMInfoEntitys.get(0).getZbsFlag().equals("1"))
                        {
                            //现货利库
                            if(delivyInfo.getOrderTyp().equals("120102"))
                            {
                                BllCreateZbsInfo.saveSpotInfo(matZcMEntity.getJudgeStoveNo(),matZcMEntity.getBatchNo(),matZcMEntity.getBatchGroudNo(),processCode,delivyInfo.getOrdLnDlyPk(),con);
                            }
                            //期货利库
                            else if(delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108"))
                            {
                                if(!(delivyInfo.getOrderNo() + "/" + delivyInfo.getOrderSeq()).equals(matZcMEntity.getProOrderNo()))
                                {
                                    Boolean bol = compareBaclogDesc(matZcMEntity,delivyInfo,con);
                                    if(bol)
                                    {
                                        String craftFileNo = createJudgeApply(matZcMEntity,delivyInfo,userName,judgeId,con,custom);
                                        if(craftFileNo.equals(""))
                                        {
                                            core.setV_errMsg("此利库合同无生效工艺文件,不允许利库");
                                            return core;
                                        }
                                    }
                                    else
                                    {
                                        core.setV_errMsg("生产合同与利库合同制程不一致,不允许利库!");
                                        return core;
                                    }
                                }
                            }
                        }
                    }
                }

                
            }
            processSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.UpdateProcessLiKu");
            ps = con.prepareStatement(processSql);
            ps.setString(1, String.valueOf(count));
            ps.setString(2, String.valueOf(wgt));
            ps.setString(3, String.valueOf(len));
            ps.setString(4, delivyInfo.getOrdLnDlyPk());
            ps.setString(5, proNo);
            ps.setString(6, delivyInfo.getOrdLnDlyPk());
            ps.executeUpdate();
            ps.close();
            
//            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectProcessCode");//查询工序代码
//        	sql = EntityTool.fillSqlByEntity(sql, matZcMEntitys.get(0));
//        	ps = con.prepareStatement(sql);
//        	rs = ps.executeQuery();
//        	if(rs.next())
//        	{
//        		processCode = rs.getString("PROCESS_CODE");
//        	}
//        	rs.close();
//        	ps.close();
            core.setV_errMsg("利库成功!");
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }
    
    /**
     * 利库
     * @param
     * @param matZcMEntitys 物料信息集合
     * @return CoreReturnObject
     */
    public CoreReturnObject Liku(ArrayList<MatZcMEntity> matZcMEntitys,
                                 String ordLnDlyPk, String userName,String formFrom,String custom) {
        return Liku( matZcMEntitys, ordLnDlyPk,  userName, formFrom, custom,null);
    }
    public CoreReturnObject Liku(ArrayList<MatZcMEntity> matZcMEntitys,
            String ordLnDlyPk, String userName,String formFrom,String custom,ArrayList<String> matNoList) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "";
        String processSql = "";
        String proNo = "";
        ResultSet rs = null;
        String processCode = "";//工序代码
        String inDoc = "";//入库单号
        String outDoc = "";//出库单号
        try {
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
            String systime = dateFormat.format(now);
            String bathYear = df.format(now);
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            // 保存所有需要利库的物料数据
            List<MatZcMInfoEntity> matZcMInfoEntityList = new ArrayList<MatZcMInfoEntity>();
            List<MatZcMInfoEntity> matZcMInfoEntitys = new ArrayList<MatZcMInfoEntity>();
            double countWeight = 0;
            // 判断交货行是否能够利库
            DelivyEntity delivyInfo = FrmPipeMagement.getDelivyInfoByKey(con,
                    ordLnDlyPk);
            String judgeId = getJudgeId(con);
            inDoc = getInListDoc(con, "800712");
            outDoc = getOutListDoc(con, "800812");
            for (MatZcMEntity matZcMEntity : matZcMEntitys) {
                QualityDesignTool.nullConverNullString(matZcMEntity);
                if(!matZcMEntity.getBelongCode().equals(delivyInfo.getSaleOrg())){
                    core.setV_errMsg("物料不在您所在的销售组织，请所属权转移!");
                    return core;
                }
                // 处理标准类别为空
                if (matZcMEntity.getStdStyle().equals("")) {
                    matZcMEntity.setStdStyle("/");
                }
                matZcMInfoEntitys = FrmPipeMagement.getMageStatus(con,
                        matZcMEntity, "Liku",matNoList);
                if (matZcMInfoEntitys == null) {
                    core.setV_errMsg("炉号:" + matZcMEntity.getJudgeStoveNo()
                            + "批号:" + matZcMEntity.getBatchNo() + "组号:"
                            + matZcMEntity.getBatchGroudNo()
                            + "材料信息已改变,请重新获取材料信息!");
                    return core;
                }
                if(delivyInfo == null)
                {
                	core.setV_errMsg("现货合同/预排合同/研发合同不允许进行利库!");
                	return core;
                }
                String lock = getOrderLineLock(con, delivyInfo.getOrdLnPk());
                String valflag = getOrderLineCraft(con, delivyInfo.getOrdLnPk());
                if (lock.equals("1")) {
                    core.setV_errMsg("您选择的交货行所在合同行处在变更中,不允许利库!");
                    return core;
                }
                // && (!delivyInfo.getDeliveryStatus().equals("12020302"))
                if ((!delivyInfo.getDeliveryStatus().equals("12020301"))
                        && (!delivyInfo.getDeliveryStatus().equals("12020502"))
                        ) {
                	//&& (!delivyInfo.getDeliveryStatus().equals("12020602"))/缴库完毕
                    core.setV_errMsg("交货行只有生效或者生产完毕状态的,才能利库!");
                    return core;
                }
                if(matZcMEntity.getJudgeResult().equals("缺项合格"))
                {
                	if(!matZcMEntity.getProOrderNo().equals(delivyInfo.getOrderNo()+"/"+delivyInfo.getOrderSeq()))
                    {
//                    	core.setV_errMsg("此物料是缺项合格产品,不允许利库到不是原合同上!");
//                    	return core;
                    }
                }
                
                // 判断物料是否能够利库
                for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntitys) {
                    if ((!matZcMInfoEntity.getMatStatus().equals("80150203"))
                            && (!matZcMInfoEntity.getMatStatus()
                                    .equals("80150204")
                                    && (!matZcMInfoEntity.getMatStatus()
                                            .equals("80150202")))) {
                        core.setV_errMsg("炉号:"
                                + matZcMInfoEntity.getJudgeStoveNo() + "批号:"
                                + matZcMInfoEntity.getBatchNo() + "组号:"
                                + matZcMInfoEntity.getBatchGroudNo()
                                + "材料状态不是 材料为余材(已综合判定) 或者 材料脱单余材(销售组织脱单),不能利库!");
                        return core;
                    }
                    countWeight += Double.parseDouble(
                            matZcMInfoEntity.getActWeight().toString());
                    matZcMInfoEntityList.add(matZcMInfoEntity);
                }
            }
            double orderWgtEstmt = Double.parseDouble(delivyInfo.getWgtEstmt())
                    * (1 + Double.parseDouble(delivyInfo.getDelvryRangeMax())
                            / 100);
            double likuWeight = FrmPipeMagement.queryWeightByLiku(con,
                    ordLnDlyPk);
            proNo = doProNo(con, "900102", delivyInfo.getSaleOrg());
            // 将利库的物料数据写入钢管物料跟踪履历表
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.addMatZcTrack");
            int seq = 0;
            double wgt = 0;
            int count = 0;
            double len = 0;
            SimpleDateFormat dateFormat1 = new SimpleDateFormat(
                    "yyyyMMddHHmmss");
            DecimalFormat df1 = new DecimalFormat("0000000000");
            for (MatZcMInfoEntity matZcMInfoEntity : matZcMInfoEntityList) {
                matZcMInfoEntity.setOrderNo(delivyInfo.getOrderNo());
                matZcMInfoEntity.setOrderSeq(delivyInfo.getOrderSeq());
                matZcMInfoEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
                matZcMInfoEntity.setOrdPk(delivyInfo.getOrdPk());
                matZcMInfoEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
                matZcMInfoEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
                matZcMInfoEntity.setBelongCode(delivyInfo.getSaleOrg());// 销售组织代码
                matZcMInfoEntity.setBelongName(delivyInfo.getSaleOrgDesc());
                matZcMInfoEntity.setSupplyUnitCode(custom);
                if(custom.equals("120501"))
                {
                    matZcMInfoEntity.setSupplyUnit("天津钢管制造有限公司");
                }else if(custom.equals("120504"))
                {
                    matZcMInfoEntity.setSupplyUnit("江苏天淮钢管有限公司");
                }
                seq += 1;
                String resumeSeq = dateFormat1.format(now) + df1.format(seq);
                MatZcTrackEntity matZcTrackEntity = FrmPipeMagement
                        .copyOfMatZcMInfoEntityByLiku(matZcMInfoEntity,
                                userName);
                matZcTrackEntity.setResumeSeq(resumeSeq);
                matZcTrackEntity.setCreateTime(systime);
                String sqlAdd = EntityTool.fillSqlByEntity(sql,
                        matZcTrackEntity);
                sqlAdd = sqlAdd.replace("#PRO_NO#", proNo);// 新加的2016-03-02
                ps = con.prepareStatement(sqlAdd);
                ps.executeUpdate();
                ps.close();

            }

            // 轧管、热处理、管加工等钢管生产物料主表的对应数据修改状态
            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.Liku");
            for (MatZcMEntity matZcMEntity : matZcMEntitys) {
                matZcMEntity.setOrderNo(delivyInfo.getOrderNo());
                matZcMEntity.setOrderSeq(delivyInfo.getOrderSeq());
                matZcMEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
                matZcMEntity.setOrdPk(delivyInfo.getOrdPk());
                matZcMEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
                matZcMEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
                matZcMEntity.setNewbelongCode(delivyInfo.getSaleOrg());// 销售组织代码
                matZcMEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                matZcMEntity.setStdCode(delivyInfo.getStdCode());
                matZcMEntity.setStdName(delivyInfo.getStdName());
                String sqlLiku = EntityTool.fillSqlByEntity(sql, matZcMEntity);
                String[] arr = new String[matZcMInfoEntitys.size()];







                for (int i = 0; i < matZcMInfoEntitys.size(); i++) {
                    MatZcMInfoEntity matList = matZcMInfoEntitys.get(i);
                    arr[i] = matList.getMatNo();
                    //当合同所属权不等于库存所属权写入出交易
                    if(!matZcMEntity.getBelongCode().equals(delivyInfo.getSaleOrg()))
                    {
                        YdmZcInlistEntity ydmEntity = new YdmZcInlistEntity();
                        ydmEntity.setInstockDoc(inDoc);
                        ydmEntity.setOutstockDoc(outDoc);
                        ydmEntity.setMatNo(arr[i].toString());
                        ydmEntity.setInstockSqe(inDoc + StringTool.formatDecimal(i+1, "0000"));
                        ydmEntity.setOutstockSqe(outDoc + StringTool.formatDecimal(i+1, "0000"));
                        ydmEntity.setNewbelongCode(delivyInfo.getSaleOrg());
                        ydmEntity.setNewbelongName(delivyInfo.getSaleOrgDesc());
                        ydmEntity.setCreateName(userName);
                        ydmEntity.setCreateTime(systime);
                        ydmEntity.setBalYearMonth(bathYear);
//                        String addInList = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.insertInListOut");
//                        addInList = EntityTool.fillSqlByEntity(addInList,ydmEntity);
//                        ps = con.prepareStatement(addInList);
//                        ps.executeUpdate();
//                        ps.close();
                    }





                    ps = con.prepareStatement(sqlLiku);
                    ps.setString(1, proNo);// 新增加2016-03-02
                    if(formFrom.equals("1"))
                    {
                    	ps.setString(2, "0");
                    }else
                    {
                    	ps.setString(2, "1");
                    }
                    if((delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108")) && matZcMEntity.getZbsFlag().equals("1"))
                    {
                    	if(!(delivyInfo.getOrderNo() + "/" + delivyInfo
    							.getOrderSeq()).equals(matZcMEntity
    									.getProOrderNo()))
                    	{
                    		ps.setString(3, "2");
                        	ps.setString(4, judgeId);
                    	}else
                    	{
                    		ps.setString(3, "0");
                        	ps.setString(4, "");
                    	}
                    }else
                    {
                    	ps.setString(3, "0");
                    	ps.setString(4, "");
                    }
                    ps.setString(5, arr[i].toString());
                    ps.executeUpdate();
                    ps.close();
                    wgt += Double
                            .parseDouble((matList.getActWeight().toString()));
                    count += Integer.parseInt(matList.getActCount());
                    if (!matList.getActLen().equals("")) {
                        len += Double
                                .parseDouble((matList.getActLen().toString()));
                    }
                    else {
                        len += 0;
                    }



                }
                if(matZcMInfoEntitys.get(0).getProducname() != null) {
                    if (!matZcMInfoEntitys.get(0).getProducname().contains("坯")) {
                        if(matZcMInfoEntitys.get(0).getZbsFlag().equals("1"))
                        {
                            //现货利库
                            if(delivyInfo.getOrderTyp().equals("120102"))
                            {
                                BllCreateZbsInfo.saveSpotInfo(matZcMEntity.getJudgeStoveNo(),matZcMEntity.getBatchNo(),matZcMEntity.getBatchGroudNo(),processCode,delivyInfo.getOrdLnDlyPk(),con);
                            }
                            //期货利库
                            else if(delivyInfo.getOrderTyp().equals("120101")||delivyInfo.getOrderTyp().equals("120108"))
                            {
                                if(!(delivyInfo.getOrderNo() + "/" + delivyInfo.getOrderSeq()).equals(matZcMEntity.getProOrderNo()))
                                {
                                    Boolean bol = compareBaclogDesc(matZcMEntity,delivyInfo,con);
                                    if(bol)
                                    {
                                        String craftFileNo = createJudgeApply(matZcMEntity,delivyInfo,userName,judgeId,con,custom);
                                        if(craftFileNo.equals(""))
                                        {
                                            core.setV_errMsg("此利库合同无生效工艺文件,不允许利库");
                                            return core;
                                        }
                                    }
                                    else
                                    {
                                        core.setV_errMsg("生产合同与利库合同制程不一致,不允许利库!");
                                        return core;
                                    }
                                }
                            }
                        }
                    }
                }

                
            }
            processSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.UpdateProcessLiKu");
            ps = con.prepareStatement(processSql);
            ps.setString(1, String.valueOf(count));
            ps.setString(2, String.valueOf(wgt));
            ps.setString(3, String.valueOf(len));
            ps.setString(4, delivyInfo.getOrdLnDlyPk());
            ps.setString(5, proNo);
            ps.setString(6, delivyInfo.getOrdLnDlyPk());
            ps.executeUpdate();
            ps.close();
            
//            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectProcessCode");//查询工序代码
//        	sql = EntityTool.fillSqlByEntity(sql, matZcMEntitys.get(0));
//        	ps = con.prepareStatement(sql);
//        	rs = ps.executeQuery();
//        	if(rs.next())
//        	{
//        		processCode = rs.getString("PROCESS_CODE");
//        	}
//        	rs.close();
//        	ps.close();
            core.setV_errMsg("利库成功!");
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }

    /***
     * 获取入库单号
     *
     * @param conn
     * @param strInbound
     * @return
     * @throws SQLException
     */
    public String getInListDoc(Connection conn, String strInbound)
            throws SQLException {
        String inListDoc = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmFinishAfrim.getInListDoc");
        PreparedStatement psta = conn.prepareStatement(sql);
        psta.setString(1, strInbound);
        psta.setString(2, strInbound);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            inListDoc = rs.getString("INSTOCK_DOC");
        }
        rs.close();
        psta.close();
        return inListDoc;
    }
    /***
     * 获取出库单号
     *
     * @param conn
     * @param
     * @return
     * @throws SQLException
     */
    public String getOutListDoc(Connection conn, String strOutbound)
            throws SQLException {
        String outListDoc = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmFinishAfrim.getOutListDoc");
        PreparedStatement psta = conn.prepareStatement(sql);
        psta.setString(1, strOutbound);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            outListDoc = rs.getString("OUTSTOCK_DOC");
        }
        rs.close();
        psta.close();
        return outListDoc;
    }
    /***
     * 生成利库、脱单的操作单号
     * @param con
     * @param beglogCode
     * @return
     */
    public String doProNo(Connection con, String eventId, String beglogCode) {
        String proNo = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        String sqlloading = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.doQueryMaxProNo");// 查询最大实绩编号

        try {
            psta = con.prepareStatement(sqlloading);
            psta.setObject(1, eventId);
            psta.setObject(2, beglogCode);
            psta.setObject(3, eventId);
            psta.setObject(4, beglogCode);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                proNo = rs1.getString("PRO_NO");
            }
            rs1.close();
            psta.close();
        }
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return proNo;
    }

    /**
     * 通过交货行主键查询交货行信息
     * @param con
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public static DelivyEntity getDelivyInfoByKey(Connection con,
            String ordLnDlyPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.getDelivyInfoByKey");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        List<DelivyEntity> delivyEntitys = EntityTool.getEntitysByRs(rs,
                DelivyEntity.class);
        rs.close();
        ps.close();
        if (delivyEntitys != null && delivyEntitys.size() != 0) {
            for (DelivyEntity delivyEntity : delivyEntitys) {
                QualityDesignTool.nullConverNullString(delivyEntity);
            }
            return delivyEntitys.get(0);
        }
        return null;
    }

    /**
     * 查询交货行已经利库的量
     * @param con
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public static double queryWeightByLiku(Connection con, String ordLnDlyPk)
            throws SQLException {
        double likuWeight = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryWeightByLiku");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            likuWeight = rs.getDouble("ACT_WEIGHT");
        }
        rs.close();
        ps.close();
        return likuWeight;
    }

    /**
     * 验证脱单的数据权限
     * @param con
     * @param ordPk
     * @param validDataPurviewIds
     * @return
     * @throws SQLException
     */
    public static int checkOrdNoHead(Connection con, String ordPk,
            String[] validDataPurviewIds) throws SQLException {
        int count = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.checkOrdNoHead");
        sql += CoreConverter.toSqlConditionString("T.SALE_AREA_NO",
                validDataPurviewIds);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            count = rs.getInt("COUNT");
        }
        rs.close();
        ps.close();
        return count;
    }

    /***
     * 撤销脱单
     * @param list
     * @return
     */
    public CoreReturnObject cancelMonomer(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        String staus = "";
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            // List<String[]> getStatus =
            // getMatStatus(con,list.get(2),list.get(3),list.get(4));
            // String[] arr = getStatus.get(0);
            // if(arr[0]!=null && !arr[0].equals("0"))
            // {
            // cro.setV_errMsg("此炉【"+list.get(2)+"】材料状态已改变,不能撤销脱单!");
            // return cro;
            // }
            sql = CoreXmlSqlParsersFactory.getSql("CorePipeInTemp.selectTrack");// 获取履历表中数据
            ps = con.prepareStatement(sql);
            ps.setString(1, list.get(0));
            ps.setString(2, list.get(5));
            ps.setString(3, list.get(6));
            ps.setString(4, list.get(2));
            ps.setString(5, list.get(3));
            ps.setString(6, list.get(4));
            rs = ps.executeQuery();
            List<MatZcTrackEntity> matTrackList = EntityTool.getEntitysByRs(rs,
                    MatZcTrackEntity.class);
            for (MatZcTrackEntity trackEntity : matTrackList) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePipeInTemp.selectStaus");
                ps = con.prepareStatement(sql);
                ps.setString(1, trackEntity.getMatNo());
                rs = ps.executeQuery();
                if (rs.next()) {
                    staus = rs.getString("PRODUCT_FLAG");
                }
                rs.close();
                ps.close();
                if (staus.equals("801403") || staus.equals("")) {
                    cro.setV_errMsg("材料状态已改变,不允许撤销!");
                    return cro;
                }
                sql = CoreXmlSqlParsersFactory
                        .getSql("cancelMonomer.updateMat");// 修改物料表状态
                ps = con.prepareStatement(sql);
                ps.setString(1, trackEntity.getMatNo());
                ps.executeUpdate();
                ps.close();

                sql = CoreXmlSqlParsersFactory
                        .getSql("cancelMonomer.deleteMatTrack");// 删除物料跟踪履历表
                ps = con.prepareStatement(sql);
                ps.setString(1, trackEntity.getMatNo());
                ps.executeUpdate();
                ps.close();
            }
            List<String[]> strList = queryMatTrackCount(con, list.get(0));
            String[] arrayStr = strList.get(0);
            sql = CoreXmlSqlParsersFactory
                    .getSql("cancelMonomer.updateProcess");// 还原进程表的脱单量
            ps = con.prepareStatement(sql);
            ps.setString(1, arrayStr[0]);
            ps.setString(2, arrayStr[1]);
            ps.setString(3, arrayStr[2]);
            ps.setString(4, list.get(0));
            ps.executeUpdate();
            ps.close();
            cro.setV_errMsg("撤销成功!");
            cro.setResult(1);
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /***
     * 查询跟踪履历表数据
     * @param con
     * @param order_diyPk
     * @return
     */
    public static List<String[]> queryMatTrackCount(Connection con,
            String order_diyPk) {
        int count = 0;
        double wgt = 0.0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.queryMatTrackCount");
        PreparedStatement psta;
        List<String[]> list = new ArrayList<String[]>();
        try {
            psta = con.prepareStatement(sql);
            psta.setString(1, order_diyPk);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                String[] arr = new String[3];
                arr[0] = rs.getString("ACT_COUNT");
                arr[1] = rs.getString("ACT_WEIGHT");
                arr[2] = rs.getString("ACT_LEN");
                list.add(arr);
            }
            rs.close();
            psta.close();
        }
        catch (SQLException e) {

            e.printStackTrace();
        }

        return list;
    }

    /***
     * 查询脱单后物料是否被改变状态
     * @param con
     * @param heatNo
     * @param bacthNo
     * @param groupNo
     * @return
     */
    public List<String[]> getMatStatus(Connection con, String heatNo,
            String bacthNo, String groupNo) {
        int count = 0;
        double wgt = 0.0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.getMatStatus");
        PreparedStatement psta;
        List<String[]> list = new ArrayList<String[]>();
        try {
            psta = con.prepareStatement(sql);
            psta.setString(1, heatNo);
            psta.setString(2, bacthNo);
            psta.setString(3, groupNo);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                String[] arr = new String[1];
                arr[0] = rs.getString("ACOUNT");
                list.add(arr);
            }
            rs.close();
            psta.close();
        }
        catch (SQLException e) {

            e.printStackTrace();
        }

        return list;
    }

    /***
     * 查询封锁状态
     * @param matZcMEntitys
     * @return
     */
    public CoreReturnObject getCount(MatZcMEntity matZcMEntitys) {
        // String sql = "";
        // PreparedStatement ps = null;
        // CoreReturnObject cro = null;
        // Connection con = this.getDao("TgDao").getConnection();
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.getCount");
        sql = EntityTool.fillSqlByEntity(sql, matZcMEntitys);
        return this.getData(sql);
    }

    /***
     * 查询封锁状态
     * @param
     * @return
     */
    public String getOrderLineLock(Connection con, String orderLinePk)
            throws SQLException {
        String lockStus = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.getOrderLineLock");
        ;
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, orderLinePk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            lockStus = rs.getString(1);
        }
        rs.close();
        ps.close();
        return lockStus;
    }

    /***
     * 查询是否存在有效工艺文件
     * @param
     * @return
     */
    public String getOrderLineCraft(Connection con, String orderLinePk)
            throws SQLException {
        String lockStus = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.getOrderLineCraft");
        ;
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, orderLinePk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            lockStus = rs.getString(1);
        }
        rs.close();
        ps.close();
        return lockStus;
    }

    
    /**
     * 查询合同相关库存信息
     * @param parm
     * @param saleOrg 销售组织
     * @return
     */
    public CoreReturnObject queryMatInfo(ArrayList<Object> parm,
            String saleOrg) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryMatInfo");
        String str = "";
        if (saleOrg.equals("100101") || saleOrg.equals("100103")) {
            str = " AND T1.BELONG_CODE IN ('100101','100103')";
        }
        else if (saleOrg.equals("100102")) {
            str = " AND T1.BELONG_CODE  = '100102'";
        }

        sql += str;
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,"
                + "T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.JUDGE_STD_NAME,"
                + "T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.LOCK_FLAG,T1.MNG_LOCK_FLAG,"
                + "T1.MEMO,t1.PIPMEMO,T1.JUDGE_RESULT_CODE,T1.JUDGE_RESULT,T2.STORAGE_NO,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T2.LOCATION_NO "
                + "ORDER BY T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MIN(T1.INSTOCK_TIME)";
        return this.getData(sql, parm.toArray());
    }

    public CoreReturnObject queryMatInfo120504(ArrayList<Object> parm,
                                         String saleOrg) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryMatInfo120504");
        String str = "";
        if (saleOrg.equals("100101") || saleOrg.equals("100103")) {
            str = " AND T1.BELONG_CODE IN ('100101','100103')";
        }
        else if (saleOrg.equals("100102")) {
            str = " AND T1.BELONG_CODE  = '100102'";
        }

        sql += str;
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,"
                + "T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.JUDGE_STD_NAME,"
                + "T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.LOCK_FLAG,T1.MNG_LOCK_FLAG,"
                + "T1.MEMO,T1.JUDGE_RESULT_CODE,T1.JUDGE_RESULT,T2.STORAGE_NO,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T2.LOCATION_NO "
                + "ORDER BY T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MIN(T1.INSTOCK_TIME)";
        return this.getData(sql, parm.toArray());
    }

    public CoreReturnObject queryMatInfo1(ArrayList<Object> parm,
                                         String saleOrg) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryMatInfo1");
        String str = "";
        if (saleOrg.equals("100101") || saleOrg.equals("100103")) {
            str = " AND T1.BELONG_CODE IN ('100101','100103')";
        }
        else if (saleOrg.equals("100102")) {
            str = " AND T1.BELONG_CODE  = '100102'";
        }
        parm.remove(5);
        parm.remove(4);
        parm.remove(3);
        parm.remove(0);
        sql += str;
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,"
                + "T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.JUDGE_STD_NAME,"
                + "T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.LOCK_FLAG,T1.MNG_LOCK_FLAG,"
                + "T1.MEMO,T1.JUDGE_RESULT_CODE,T1.JUDGE_RESULT,T2.STORAGE_NO,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T2.LOCATION_NO "
                + "ORDER BY T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MIN(T1.INSTOCK_TIME)";
        return this.getData(sql, parm.toArray());
    }

    /**
     * 查询交货行信息 -销售管理-合同提报管理界面
     * @param ordLnDlyPk
     * @param dataPurviews
     * @return
     */
    public CoreReturnObject getDelivyInfoByOrdLnDlyPk(String ordLnDlyPk,
                                                      String[] dataPurviews) {
        return getDelivyInfoByOrdLnDlyPk(ordLnDlyPk,dataPurviews,"");
    }
    public CoreReturnObject getDelivyInfoByOrdLnDlyPk(String ordLnDlyPk,
            String[] dataPurviews,String form) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.getDelivyInfoByOrdLnDlyPk");
        Object[] obj = new Object[] { ordLnDlyPk };
        if(StringUtils.isEmpty(form) && !"2".equals(form)){
            sql += CoreConverter.toSqlConditionString("T1.SALE_AREA_NO",
                    dataPurviews);
        }
        sql += "    ORDER BY T1.ORDER_NO,T2.ORDER_SEQ,T3.DELIVERY_NO";
        return this.getData(sql, obj);
    }

    /***
     * 审核通过
     * @param parm
     * @return
     */
    public CoreReturnObject doCheckMat(ArrayList<String> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        String staus = "";
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            String updateSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.doCheckMat1");
            PreparedStatement updatePsta = con.prepareStatement(updateSql);
            for (int i = 0; i < parm.size(); i++) {
                MatZcMInfoEntity matZcMEntity = EntityTool.getEntityByJSONStr(
                        parm.get(i), MatZcMInfoEntity.class);
                if (matZcMEntity.getLockLiku().equals("审核通过")) {
                    cro.setV_errMsg("此记录已审核通过,不允许再审核!");
                    return cro;
                }
                updatePsta.setString(1, matZcMEntity.getProNo());
                updatePsta.addBatch();
            }
            updatePsta.executeBatch();
            cro.setV_errMsg("审核成功!");
            con.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /***
     * 审核不通过
     * @param parm
     * @return
     */
    public CoreReturnObject doNoCheckMat(ArrayList<String> parm,
            String userName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        String staus = "";
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            String updateSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.doNoCheckMat");
            PreparedStatement updatePsta = con.prepareStatement(updateSql);
            for (int i = 0; i < parm.size(); i++) {
                MatZcMInfoEntity matZcMEntity = EntityTool.getEntityByJSONStr(
                        parm.get(i), MatZcMInfoEntity.class);
                if (matZcMEntity.getLockLiku().equals("审核通过")) {
                    cro.setV_errMsg("此记录已审核通过,不允许再审核!");
                    return cro;
                }
                updatePsta.setString(1, matZcMEntity.getProNo());
                updatePsta.addBatch();
            }
            CoreReturnObject core = monomer(parm, userName);
            if (!core.getV_errMsg().equals("")) {
                return core;
            }
            updatePsta.executeBatch();
            cro.setV_errMsg("审核成功!");
            con.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /***
     * 审核不通过自动脱单
     * @param parm
     * @param
     * @param
     * @param userName
     * @param
     * @param
     * @return
     */
    public CoreReturnObject monomer(ArrayList<String> parm, String userName) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        ResultSet rs = null;
        PreparedStatement psta = null;
        String sql = "";
        String acount = "";
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String monomerSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.monomer");
            PreparedStatement monomerPsta = con.prepareStatement(monomerSql);
            String updateDivelNoSql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeMagement.updateDiveNo");// 修改交货行脱单操作单号
            PreparedStatement updateDiveNoPsta = con
                    .prepareStatement(updateDivelNoSql);
            for (int i = 0; i < parm.size(); i++) {
                MatZcMInfoEntity matZcMEntity = EntityTool.getEntityByJSONStr(
                        parm.get(i), MatZcMInfoEntity.class);
                if (!matZcMEntity.getMatStatus().equals("材料可编提单")) {
                    core.setV_errMsg("材料状态不为材料可编提单,不允许脱单!");
                    return core;
                }
                else {
                    matZcMEntity.setMatStatus("80150301");
                }
                QualityDesignTool.nullConverNullString(matZcMEntity);

                // 处理标准类别为空
                if (matZcMEntity.getStdStyle().equals("")) {
                    matZcMEntity.setStdStyle("/");
                }
                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeMagement.selectMatZc");
                String selectMatsql = EntityTool.fillSqlByEntity(sql,
                        matZcMEntity);
                psta = con.prepareStatement(selectMatsql);
                rs = psta.executeQuery();
                if (rs.next()) {
                    acount = rs.getString("ACOUNT");
                }
                rs.close();
                psta.close();
                if (!acount.equals(matZcMEntity.getActCount())) {
                    core.setV_errMsg("材料状态发生了变化,请刷新页面!");
                    return core;
                }
                // 判断交货行是否能够脱单
                DelivyEntity delivyInfo = FrmPipeMagement
                        .getDelivyInfoByKey(con, matZcMEntity.getOrdLnDlyPk());
                if (delivyInfo == null) {
                    core.setV_errMsg("现货/研发/预排合同不允许脱单!");
                    return core;
                }

                monomerPsta.setString(1, matZcMEntity.getJudgeStoveNo());
                monomerPsta.setString(2, matZcMEntity.getBatchNo());
                monomerPsta.setString(3, matZcMEntity.getBatchGroudNo());
                monomerPsta.setString(4, matZcMEntity.getProduccode());
                monomerPsta.setString(5, matZcMEntity.getSteelcode());
                monomerPsta.setString(6, matZcMEntity.getSpecCode());
                monomerPsta.setString(7, matZcMEntity.getModelCode());
                monomerPsta.setString(8, matZcMEntity.getStdStyle());
                monomerPsta.setString(9, matZcMEntity.getMatStatus());
                monomerPsta.setString(10, matZcMEntity.getStorageNo());
                monomerPsta.setString(11, matZcMEntity.getOrdPk());
                monomerPsta.setString(12, matZcMEntity.getOrdLnPk());
                monomerPsta.setString(13, matZcMEntity.getOrdLnDlyPk());
                monomerPsta.setString(14, matZcMEntity.getProNo());
                monomerPsta.setString(15, matZcMEntity.getInMold());
                monomerPsta.setString(16, matZcMEntity.getActLenMin());
                monomerPsta.setString(17, matZcMEntity.getActLenMax());
                monomerPsta.setString(18,matZcMEntity.getPipmemo());
                monomerPsta.setString(19,matZcMEntity.getLocationNo());

                monomerPsta.setInt(20,
                        Integer.parseInt(matZcMEntity.getTdactCount()) + 1);
                monomerPsta.setString(21, matZcMEntity.getJudgeStoveNo());
                monomerPsta.setString(22, matZcMEntity.getBatchNo());
                monomerPsta.setString(23, matZcMEntity.getBatchGroudNo());
                monomerPsta.setString(24, matZcMEntity.getProduccode());
                monomerPsta.setString(25, matZcMEntity.getSteelcode());
                monomerPsta.setString(26, matZcMEntity.getSpecCode());
                monomerPsta.setString(27, matZcMEntity.getModelCode());
                monomerPsta.setString(28, matZcMEntity.getStdStyle());
                monomerPsta.setString(29, matZcMEntity.getMatStatus());
                monomerPsta.setString(30, matZcMEntity.getStorageNo());
                monomerPsta.setString(31, matZcMEntity.getOrdPk());
                monomerPsta.setString(32, matZcMEntity.getOrdLnPk());
                monomerPsta.setString(33, matZcMEntity.getOrdLnDlyPk());
                monomerPsta.setString(34, matZcMEntity.getProNo());
                monomerPsta.setString(35, matZcMEntity.getInMold());
                monomerPsta.setString(36, matZcMEntity.getActLenMin());
                monomerPsta.setString(37, matZcMEntity.getActLenMax());
                monomerPsta.setString(38, matZcMEntity.getFixNum());
                monomerPsta.setString(39,matZcMEntity.getPipmemo());
                monomerPsta.setString(40,matZcMEntity.getLocationNo());
                monomerPsta.setInt(41,
                        Integer.parseInt(matZcMEntity.getTdactCount()) + 1);
                monomerPsta.setString(42, "");
                monomerPsta.setString(43, userName);
                monomerPsta.setString(44, matZcMEntity.getFinishproMold());
                monomerPsta.setString(45, matZcMEntity.getFinishproMold());
                monomerPsta.setString(46, matZcMEntity.getOrdLnDlyPk());
                monomerPsta.addBatch();
                if (matZcMEntity.getActCount()
                        .equals(matZcMEntity.getTdactCount())) {
                    updateDiveNoPsta.setString(1, matZcMEntity.getProNo());
                    updateDiveNoPsta.setString(2, matZcMEntity.getOrdLnDlyPk());
                    updateDiveNoPsta.addBatch();
                }
            }

            monomerPsta.executeBatch();
            updateDiveNoPsta.executeBatch();
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    core.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 查询商品信息
     * @param ordLnDLyPk
     * @param arr
     * @return
     */
    public CoreReturnObject queryMatMentSByOrdLnDlyPk(String[] arr,
            String ordLnDLyPk) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeMagement.queryMatMentSByOrdLnDlyPk");
        sql += CoreConverter.toSqlConditionString("T5.SALE_AREA_NO", arr);
        sql += " GROUP BY T1.BELONG_CODE,T1.BELONG_NAME,T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.PRODUCT_FLAG,T1.MAT_STATUS,T1.SPEC_CODE,T1.SPEC_NAME,"
                + "T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.PRODUCCODE,T1.PRODUCNAME,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.STEELCODE,T1.STEELNAME,T1.JUDGE_STD_NAME,T1.FIX_NUM,"
                + "T1.STD_CODE,T1.STD_NAME,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,T1.ORD_PK,T1.ORD_LN_PK,T1.ORD_LN_DLY_PK,T1.PRO_ORDER_NO,T1.PRO_ORDER_ID,T1.LOCK_FLAG,T1.PRO_NO,T1.LOCK_LIKU,"
                + "T1.MEMO,T1.JUDGE_RESULT_CODE,T1.JUDGE_RESULT,T2.STORAGE_NO,T1.MODEL_CODE,T1.MODEL_DESC,T1.STD_STYLE,T1.STD_STYLE_DESC,T2.LOCATION_NO,T1.IN_MOLD,T1.PIPMEMO "
                + "ORDER BY T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.ORDER_NO,T1.ORDER_SEQ,T1.ORDER_SEQ_NO,MIN(T1.INSTOCK_TIME)";
        return this.getData(sql, new Object[] { ordLnDLyPk });
    }
    //利库时生成审批单
    public String createJudgeApply(MatZcMEntity matZcTity,DelivyEntity delivyInfo,String userName,String judgeId,Connection con,String custom) throws SQLException
    {
    	String isVd = "0";
    	int acount = 0;
    	String manageNo = "";
    	String manageMent = "";//新合同工艺科室
    	String craftFileNo = "";//工艺文件号
    	String craftSeq = "";//工艺序号
    	String craftPath = "";//工艺文件路径
    	String oldCic = "";//原CIC
    	
    	matZcTity.setOrdPk(delivyInfo.getOrdPk());
    	matZcTity.setOrdLnPk(delivyInfo.getOrdLnPk());
    	matZcTity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
    	matZcTity.setLockId(judgeId);
    	List<MatZcMEntity1> matZcEnTity = null;
    	if(matZcTity.getNewbelongCode().equals(""))
        {
            matZcTity.setNewbelongCode(matZcTity.getBelongCode());
        }
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectMatZcLiku1");//查询物料信息
    	sql = EntityTool.fillSqlByEntity(sql, matZcTity);
    	PreparedStatement psta = con.prepareStatement(sql);
    	ResultSet rs = psta.executeQuery();
    	
    	matZcEnTity = EntityTool.getEntitysByRs(rs, MatZcMEntity1.class);
    	rs.close();
    	psta.close();
    	
    	sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectStoveVd");
    	sql = EntityTool.fillSqlByEntity(sql, matZcTity);
    	psta = con.prepareStatement(sql);
    	rs = psta.executeQuery();
    	if(rs.next())
    	{
    		acount = Integer.parseInt(rs.getString("ACOUNT"));
    	}
    	rs.close();
    	psta.close();
    	if(acount > 0)
    	{
    		isVd = "1";
    	}
    	//查询工艺科室
    	sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectManageN");
    	sql = EntityTool.fillSqlByEntity(sql, matZcTity);
    	psta = con.prepareStatement(sql);
    	rs = psta.executeQuery();
    	if(rs.next())
    	{
    		manageNo = rs.getString("UNIT_CODE_PROCESS");
    		manageMent = rs.getString("UNIT_DESC_PROCESS");
    		craftFileNo = rs.getString("CRAFT_NO");
    		craftSeq = rs.getString("CRAFT_SEQ");
    		craftPath = rs.getString("CRAFT_PATH");
    	}
    	rs.close();
    	psta.close();
    	
    	if(manageNo.equals("")){
    		sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectManageNByNew");
        	sql = EntityTool.fillSqlByEntity(sql, matZcTity);
        	psta = con.prepareStatement(sql);
        	rs = psta.executeQuery();
        	if(rs.next())
        	{
        		manageNo = rs.getString("UNIT_CODE_PROCESS");
        		manageMent = rs.getString("UNIT_DESC_PROCESS");
        		craftFileNo = rs.getString("CRAFT_NO");
        		craftSeq = rs.getString("CRAFT_SEQ");
        		craftPath = rs.getString("CRAFT_PATH");
        	}
        	rs.close();
        	psta.close();
    	}
    	
    	
    	//查询原CIC
    	sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.selectOldCic");
    	sql = EntityTool.fillSqlByEntity(sql, matZcTity);
    	psta = con.prepareStatement(sql);
    	rs = psta.executeQuery();
    	if(rs.next())
    	{
    		oldCic = rs.getString("CIC");
    	}
    	rs.close();
    	psta.close();
    	
    	QcmLkJudgeChangeEntity qcmLkEntity = new QcmLkJudgeChangeEntity();

    	// 计算 ACT_LEN, ACT_COUNT, ACT_WEIGHT 的总和
    	double totalActLen = 0;
    	int totalActCount = 0;
    	double totalActWeight = 0;

    	for (int i = 0; i < matZcEnTity.size(); i++) {
    	    totalActLen += Double.valueOf(matZcEnTity.get(i).getActLen());
    	    totalActCount += Integer.valueOf(matZcEnTity.get(i).getActCount());
    	    totalActWeight += Double.valueOf(matZcEnTity.get(i).getActWeight());
    	}

    	// 使用第一个元素的值设置其他字段
    	qcmLkEntity.setJudgeId(judgeId);
    	qcmLkEntity.setJudgeStoveNo(matZcEnTity.get(0).getJudgeStoveNo());
    	qcmLkEntity.setBatchNo(matZcEnTity.get(0).getBatchNo());
    	qcmLkEntity.setBatchGroudNo(matZcEnTity.get(0).getBatchGroudNo());
    	qcmLkEntity.setMaterialNo(matZcEnTity.get(0).getMaterialNo());
    	qcmLkEntity.setMaterialName(matZcEnTity.get(0).getMaterialName());
    	qcmLkEntity.setProduccode(matZcEnTity.get(0).getProduccode());
    	qcmLkEntity.setProducname(matZcEnTity.get(0).getProducname());
    	qcmLkEntity.setSteelcode(matZcEnTity.get(0).getSteelcode());
    	qcmLkEntity.setSteelname(matZcEnTity.get(0).getSteelname());
    	qcmLkEntity.setSpecCode(matZcEnTity.get(0).getSpecCode());
    	qcmLkEntity.setSpecName(matZcEnTity.get(0).getSpecName());
    	qcmLkEntity.setModelCode(matZcEnTity.get(0).getModelCode());
    	qcmLkEntity.setModelDesc(matZcEnTity.get(0).getModelDesc());
    	qcmLkEntity.setStdStyle(matZcEnTity.get(0).getStdStyle());
    	qcmLkEntity.setStdStyleDesc(matZcEnTity.get(0).getStdStyleDesc());
    	qcmLkEntity.setStdCode(matZcEnTity.get(0).getStdCode());
    	qcmLkEntity.setStdName(matZcEnTity.get(0).getStdName());
    	qcmLkEntity.setGradecode(matZcEnTity.get(0).getGradecode());
    	qcmLkEntity.setGradename(matZcEnTity.get(0).getGradename());
    	qcmLkEntity.setJudgeStdName(matZcEnTity.get(0).getJudgeStdName());
    	qcmLkEntity.setActDimater(matZcEnTity.get(0).getActDimater());
    	qcmLkEntity.setActHeight(matZcEnTity.get(0).getActHeight());
    	qcmLkEntity.setActLenMin(matZcEnTity.get(0).getActLenMin());
    	qcmLkEntity.setActLenMax(matZcEnTity.get(0).getActLenMax());
    	qcmLkEntity.setActLen(String.valueOf(totalActLen)); // 使用总和
    	qcmLkEntity.setStoveNo(matZcEnTity.get(0).getStoveNo());
    	qcmLkEntity.setLotNo(matZcEnTity.get(0).getLotNo());
    	qcmLkEntity.setPlineCode(matZcEnTity.get(0).getPlineCode());
    	qcmLkEntity.setPlineName(matZcEnTity.get(0).getPlineName());
    	qcmLkEntity.setProOrderNo(matZcEnTity.get(0).getProOrderNo());
    	qcmLkEntity.setActCount(String.valueOf(totalActCount)); // 使用总和
    	qcmLkEntity.setActWeight(String.valueOf(totalActWeight)); // 使用总和

    	qcmLkEntity.setOrderNo(delivyInfo.getOrderNo());
    	qcmLkEntity.setOrderSeq(delivyInfo.getOrderSeq());
    	qcmLkEntity.setOrderSeqNo(delivyInfo.getDeliveryNo());
    	qcmLkEntity.setOrdPk(delivyInfo.getOrdPk());
    	qcmLkEntity.setOrdLnPk(delivyInfo.getOrdLnPk());
    	qcmLkEntity.setOrdLnDlyPk(delivyInfo.getOrdLnDlyPk());
    	qcmLkEntity.setCreateName(userName);
    	qcmLkEntity.setIsVd(isVd);//是否经过VD炉
    	qcmLkEntity.setManageNo(manageNo);
    	qcmLkEntity.setManageMent(manageMent);//新合同工艺科室
    	qcmLkEntity.setCraftNo(craftFileNo);
    	qcmLkEntity.setCraftSeq(craftSeq);
    	qcmLkEntity.setCraftPath(craftPath);
    	qcmLkEntity.setOldCic(oldCic);
    	qcmLkEntity.setSupplyUnitCode(custom);
    	if(custom.equals("120501")) {
    	    qcmLkEntity.setSupplyUnit("天津钢管制造有限公司");
    	} else if(custom.equals("120504")) {
    	    qcmLkEntity.setSupplyUnit("江苏天淮钢管有限公司");
    	}

    	String insertQcmLkSql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.insertQcmLk");
    	insertQcmLkSql = EntityTool.fillSqlByEntity(insertQcmLkSql, qcmLkEntity);
    	Statement st = con.createStatement();
    	st.executeUpdate(insertQcmLkSql);
    	st.close();
    	
    	return craftFileNo;
    }

    //生成利库单号
	private String getJudgeId(Connection con) throws SQLException {
		String judgeId = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.getJudgeId");
		psta = con.prepareStatement(sql);
		rs = psta.executeQuery();
		if(rs.next())
		{
			judgeId = rs.getString("JUDGE_ID");
		}
		rs.close();
		psta.close();
		return judgeId;
	}
	
	/***
	 * 按支查询成品
	 * @param matZc
	 * @return
	 */
	//查询成品支
	public CoreReturnObject doQuery(String matZc)
	{
		MatZcMEntity matEntity = EntityTool.getEntityByJSONStr(matZc, MatZcMEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.doQueryProduct");
		sql = EntityTool.fillSqlByEntity(sql, matEntity);
		return this.getData(sql);
	}
	/***
	 * 按支查询商品
	 * @param matZc
	 * @return
	 */
	public CoreReturnObject doQueryGoods(String matZc)
	{
		MatZcMEntity matEntity = EntityTool.getEntityByJSONStr(matZc, MatZcMEntity.class);
		String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.doQueryGoods");
		sql = EntityTool.fillSqlByEntity(sql, matEntity);
		return this.getData(sql);
	}

    public CoreReturnObject queryMatTH(String orderNo){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.queryMatTH");
        if (!StringUtils.isEmpty(orderNo)) {
            sql += " AND T1.ORDER_NO LIKE '%" + orderNo + "%'";
        }
        sql += " AND T1.PRODUCT_FLAG in ('801402','801403')";
        sql += " AND T1.SUPPLY_UNIT_CODE = '120504'";
        return this.getData(sql);
    }

    public CoreReturnObject queryMatTHLog(String orderNo,String startTime, String endTime){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.queryMatTHLog");
        if (!StringUtils.isEmpty(orderNo)) {
            sql += " AND T.NEW_ORDER_NO LIKE '%" + orderNo + "%'";
        }
        if(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
            sql += " AND T.CREATE_TIME BETWEEN to_date('" + startTime + "','yyyy-MM-dd HH24:MI:SS') AND to_date('" + endTime + "','yyyy-MM-dd HH24:MI:SS')";

        }
        return this.getData(sql);
    }


    public CoreReturnObject changeMatTH(ArrayList<String> list,String createName){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            for (String json : list) {
                MatZcMEntity entity = EntityTool.getEntityByJSONStr(json, MatZcMEntity.class);
                ArrayList<String> sqllist = new ArrayList<String>();
                ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
                SqlBuilder b = new SqlBuilder("FrmFinishAfrim.getCustmOrderNoByOrderNo");
                b.Fill(new String[]{entity.getOrderNo(),entity.getOrderSeq(),entity.getOrderSeqNo()});
                YdmZcInlistEntity ydmEntity =EntityTool.getEntityBySqlNoClsCon(b.toString(), connection,  YdmZcInlistEntity.class);

                if(ydmEntity == null) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("没有找到对应的本部合同交货行");
                    return cro;
                }
                Date now = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String systime = dateFormat.format(now);
                String bathYear = ComBaseInfo.GetBalYear_2416(now);
                String inDoc = getInListDoc(connection, "800712");
                String outDoc = getOutListDoc(connection, "800812");

                Object[] objects = new Object[]{entity.getOrderNo(),entity.getOrderSeq(),entity.getOrderSeqNo(),entity.getMatNo()};
                MatZcMEntity matZcMEntity = new MatZcMEntity();
                matZcMEntity.setOrderNo(ydmEntity.getOrderNo());
                matZcMEntity.setOrderSeq(ydmEntity.getOrderSeq());
                matZcMEntity.setOrderSeqNo(ydmEntity.getDeliveryNo());
                matZcMEntity.setOrdPk(ydmEntity.getOrdPk());
                matZcMEntity.setOrdLnPk(ydmEntity.getOrdLnPk());
                matZcMEntity.setOrdLnDlyPk(ydmEntity.getOrdLnDlyPk());
                matZcMEntity.setBelongCode(ydmEntity.getBelongCode());
                matZcMEntity.setBelongName(ydmEntity.getBelongName());
                matZcMEntity.setInstockDoc(inDoc);
                matZcMEntity.setOutstockDoc(outDoc);
                matZcMEntity.setBalYearMonth(bathYear);
                matZcMEntity.setCreateTime(systime);
                matZcMEntity.setCreateName(createName);
                String changeMatTH = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.changeMatTH");
                changeMatTH = EntityTool.fillSqlByEntity(changeMatTH,matZcMEntity);
                sqllist.add(changeMatTH);
                parmlist.add(objects);

                String changeMatThLog = CoreXmlSqlParsersFactory.getSql("FrmPipeMagement.changeMatThLog");
                MatThChangLogEntity matThChangLogEntity = new MatThChangLogEntity();
                matThChangLogEntity.setOldOrderNo(entity.getOrderNo());
                matThChangLogEntity.setOldOrderSeq(entity.getOrderSeq());
                matThChangLogEntity.setOldDeliveryNo(entity.getOrderSeqNo());
                matThChangLogEntity.setNewOrderNo(matZcMEntity.getOrderNo());
                matThChangLogEntity.setNewOrderSeq(matZcMEntity.getOrderSeq());
                matThChangLogEntity.setNewDeliveryNo(matZcMEntity.getOrderSeqNo());
                matThChangLogEntity.setMatNo(entity.getMatNo());
                matThChangLogEntity.setJudgeStoveNo(entity.getJudgeStoveNo());
                matThChangLogEntity.setBatchNo(entity.getBatchNo());
                matThChangLogEntity.setBatchGroudNo(entity.getBatchGroudNo());
                matThChangLogEntity.setCreateName(createName);
                changeMatThLog = EntityTool.fillSqlByEntity(changeMatThLog,matThChangLogEntity);
                sqllist.add(changeMatThLog);
                parmlist.add(null);
                cro = this.setData(sqllist,parmlist);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return cro;
    }
}
