package com.steering.pss.plan.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plan.base.CorePlnComm;
import com.steering.pss.plan.order.model.PlnOrderJgEntity;

/**
 * 销售合同接箍短节处理类
 * @author 杨天海
 * @version 1.2
 */
public class CorePlnOrderJgDj extends DBCommon {
    /**
     * 查询基础维护数据
     * @return
     */
    public CoreReturnObject queryBaseinfoAll() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBaseinfoAll");
        return cro;
    }

    /**
     * 查询钢种
     * @return
     */
    public CoreReturnObject queryBaseGrade() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBaseGrade");
        return cro;
    }

    /**
     * 查询备料轧管产线
     * @return
     */
    public CoreReturnObject queryBlZgPline() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBlZgPline");
        return cro;
    }

    /**
     * 查询销售合同状态
     * @param list 合同主键
     * @return object
     */
    public CoreReturnObject querySaleStatus(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.querySaleStatus", list
                .toArray());
        return cro;
    }

    /**
     * 查询产品码
     * @param list 查询参数
     * @return object
     */
    public CoreReturnObject queryblPsc(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this
                .getDataBySqlID("CorePlnOrderJgDj.queryblPsc", list.toArray());
        return cro;
    }

    /**
     * 根据产品码查找对应冶金规范码
     * @param str 查询参数
     * @return object
     */
    public CoreReturnObject queryPscForMsc(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryPscForMsc",
                new Object[] { str });
        return cro;
    }

    /**
     * 根据备料冶金规范查找制程号
     * @param list 冶金规范码
     * @return object
     */
    public CoreReturnObject queryMscIndex(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryMscIndex", list
                .toArray());
        return cro;
    }

    /**
     * 备料需求查询工序
     * @param list 冶金规范码和全程产线号
     * @return object
     */
    public CoreReturnObject queryBlProcess(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBlProcess", list
                .toArray());
        return cro;
    }

    /**
     * 根据DESGIN_KEY查询接箍码
     * @param str 查询参数
     * @return object
     */
    public CoreReturnObject queryJgCode(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryJgCode",
                new Object[] { str });
        return cro;
    }

    /**
     * 根据DESGIN_KEY查询短节备料码
     * @param str 查询参数
     * @return object
     */
    public CoreReturnObject queryBlCode(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBlCode",
                new Object[] { str });
        return cro;
    }

    /**
     * 根据DESGIN_KEY与备料组查询短节备料码
     * @param list 查询参数
     * @return object
     */
    public CoreReturnObject queryBlCodeByGp(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBlCodeByGp", list
                .toArray());
        return cro;
    }

    /**
     * 查询投料原则，投料率
     * @param list 年月
     */
    public CoreReturnObject queryPutRate(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryPutRate", list
                .toArray());
        return cro;
    }

    /**
     * 根据接箍码查询接箍基础信息
     * @param str 查询参数
     * @return object
     */
    public CoreReturnObject queryJgBase(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryJgbase",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询备料清单表
     * @param str 查询参数
     * @return object
     */
    public CoreReturnObject queryBlList(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBlList",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询接箍合同信息
     * @param str 接箍合同号
     * @return object
     */
    public CoreReturnObject queryJgbyOrderno(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryJgbyOrderno",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询备料需求信息
     * @param str 备料需求编号
     * @return object
     */
    public CoreReturnObject queryBLbyReqBlId(String str) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryBLbyReqBlId",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询销售合同
     * @param list 查询参数
     * @return object
     */
    public CoreReturnObject querySaleOrder(ArrayList<Object> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.querySaleOrder", list
                .toArray());
        return cro;
    }

    /**
     * 查询接箍合同
     * @return object
     */
    public CoreReturnObject queryJgOrder(ArrayList<Object> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryJgOrder", list
                .toArray());
        return cro;
    }

    /**
     * 查询短节备料需求信息
     * @return object
     */
    public CoreReturnObject queryDjBlNeed(ArrayList<Object> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryDjBlNeed", list
                .toArray());
        return cro;
    }

    /**
     * 查询接箍及备料关系
     * @param str 查询参数
     * @return object
     */
    public CoreReturnObject queryRelation(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryRelation",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询接箍加工监制
     * @param str
     * @return
     */
    public CoreReturnObject queryPrdcrLst(String str){
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryPrdcrLst", new Object[]{ str });
        return cro;
    }
    
    /**
     * 根据状态查询所有接箍合同
     * @return object
     */
    public CoreReturnObject queryJgDeal() {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.QueryJgDeal");
        return cro;
    }

    /**
     * 根据状态查询所有接箍料编辑信息
     * @return object
     */
    public CoreReturnObject queryJgDealedit() {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryJgDealedit");
        return cro;
    }

    /**
     * 查询接箍备料需求信息
     * @return object
     */
    public CoreReturnObject queryJgBlNeed() {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryJgBlNeed");
        return cro;
    }

    /**
     * 查询镦拔扩与轧管规格关联
     * @param list
     * @return object
     */
    public CoreReturnObject queryRollRupset(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CorePlnOrderJgDj.queryRollRupset", list
                .toArray());
        return cro;
    }

    /**
     * 备料轧管确认
     * @param list
     * @return object
     */
    public CoreReturnObject confirmBlZg(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.confirmBlZg");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        // 更新需求是否可以下发
        for (int i = 0; i < list.size(); i++) {
            String blReqid = list.get(i).get(list.get(i).size() - 1);
            isBliussed(blReqid);
        }
        return cro;
    }

    /**
     * 备料镦拔扩确认
     * @param list
     * @return object
     */
    public CoreReturnObject confirmBlBj(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.confirmBlBj");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        // 更新需求是否可以下发
        for (int i = 0; i < list.size(); i++) {
            String blReqid = list.get(i).get(list.get(i).size() - 1);
            isBliussed(blReqid);
        }
        return cro;
    }

    /**
     * 备料热处理确认
     * @param list
     * @return object
     */
    public CoreReturnObject confirmBlRcl(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.confirmBlRcl");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        // 更新需求是否可以下发
        for (int i = 0; i < list.size(); i++) {
            String blReqid = list.get(i).get(list.get(i).size() - 1);
            isBliussed(blReqid);
        }
        return cro;
    }

    /**
     * 保存短节备料需求利库信息
     * @param list 更新列表
     * @return object
     */
    public CoreReturnObject updateDjblRcl(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.UpdateDjblRcl");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        // 更新需求是否可以下发
        for (int i = 0; i < list.size(); i++) {
            String blReqid = list.get(i).get(list.get(i).size() - 1);
            isBliussed(blReqid);
        }
        return cro;
    }

    /**
     * 接箍信息处理
     * @param listJG 接箍信息
     * @param listUpdate 合同更新信息
     * @return object
     */
    public CoreReturnObject tranSactionJg(ArrayList<PlnOrderJgEntity> listJG,
            ArrayList<ArrayList<String>> listUpdate) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < listJG.size(); i++) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.SavePretreatJG");
            String sqlUp = EntityTool.fillSqlByEntity(sql, listJG.get(i));
            sqllist.add(sqlUp);
            parmlist.add(new Object[] {});
        }

        for (int i = 0; i < listUpdate.size(); i++) {
            sqllist.add(CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.updateJgSaleorder"));
            parmlist.add(listUpdate.get(i).toArray());
        }
        cro = this.setData(sqllist, parmlist);

         //判断是否可以下发
        for (int i = 0; i < listUpdate.size(); i++) {
            ArrayList<String> param = listUpdate.get(i);
            isPrepIssued(param.get(param.size() - 1));
        }
        return cro;
    }

    /**
     * 取消接箍合同
     * @param list
     * @return
     */
    public CoreReturnObject doCancelJg(ArrayList<ArrayList<Object>> list) {
        CoreReturnObject cro = null;
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.querySaleStatus");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            for (int i = 0; i < list.size(); i++) {
                psta = conn.prepareStatement(sql);
                psta.setString(1, list.get(i).get(1).toString());
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    // 查询接箍合同接箍量
                    String sqlJg = CoreXmlSqlParsersFactory
                            .getSql("CorePlnOrderJgDj.haveJgOrder");
                    PreparedStatement ps = null;
                    ps = conn.prepareStatement(sqlJg);
                    ps.setString(1, rs.getString("ORDER_NO"));
                    ps.setString(2, rs.getString("ORDER_SEQ"));
                    ps.setString(3, rs.getString("DELIVERY_NO"));
                    ResultSet reSet = ps.executeQuery();
                    while (reSet.next()) {
                        // 更新接箍料需求量
                        String sqlJgBl = CoreXmlSqlParsersFactory
                                .getSql("CorePlnOrderJgDj.queryRelbyJg");
                        PreparedStatement psJgl = conn
                                .prepareStatement(sqlJgBl);
                        psJgl.setString(1, reSet.getString("JG_ORDERNO"));
                        ResultSet rsJgl = psJgl.executeQuery();
                        while (rsJgl.next()) {
                            Date dt = new Date();// 如果不需要格式,可直接用dt,dt就是当前系统时间
                            DateFormat df = new SimpleDateFormat(
                                    "yyyy/MM/dd HH:mm:ss");// 设置显示格式
                            String nowTime = df.format(dt);// 用DateFormat的format()方法在dt中获取并以yyyy/MM/dd
                            // HH:mm:ss格式显示
                            double jgblReqWeight = rsJgl
                                    .getDouble("JGBL_REQ_WEIGHT");
                            int jgblReqNum = rsJgl.getInt("JGBL_REQ_NUM");

                            Object[] parm = new Object[] { -jgblReqWeight,
                                    -jgblReqNum, list.get(i).get(0).toString(),
                                    nowTime, rsJgl.getString("REQ_BL_ID") };
                            sqllist.add("CorePlnOrderJgDj.updateJglneed");
                            parmlist.add(parm);
                        }
                        psJgl.close();
                        rsJgl.close();

                        // 删除接箍合同形成的接箍料。
                        sqllist.add("CorePlnOrderJgDj.DeleteJgReq");
                        parmlist.add(new Object[] { reSet
                                .getString("JG_ORDERNO") });
                    }
                    ps.close();
                    reSet.close();

                    // 更新销售合同计划量
                    sqllist.add("CorePlnOrderJgDj.updateJgSaleorder");
                    Object[] param = new Object[] { "0", "0", "0",
                            list.get(i).get(1).toString() };
                    parmlist.add(param);

                    // 删除接箍合同
                    sqllist.add("CorePlnOrderJgDj.RollbackJg");
                    param = new Object[] { rs.getString("ORDER_NO"),
                            rs.getString("ORDER_SEQ"),
                            rs.getString("DELIVERY_NO") };
                    parmlist.add(param);
                }
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        cro = this.setDataById(sqllist, parmlist);

        // 判断是否可以下发
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Object> param = list.get(i);
            isPrepIssued(param.get(param.size() - 1).toString());
        }
        return cro;
    }

    /**
     * 更新短节备料组
     * @param list 短节备料组
     * @return Object
     */
    public CoreReturnObject updateDjBlGroup(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CorePlnOrderJgDj.updateDjBlGroup", list
                .toArray());
        return cro;
    }

    /**
     * 短节备料处理
     * @param listBL 短节备料信息
     * @param param 合同更新信息
     * @return object
     */
    public CoreReturnObject tranSactionDj(ArrayList<ArrayList<String>> listBL,
            ArrayList<ArrayList<String>> param) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < listBL.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.SavePretreatBL");
            parmlist.add(listBL.get(i).toArray());
        }
        for (int i = 0; i < param.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.updateDjSaleorder");
            parmlist.add(param.get(i).toArray());
        }

        cro = this.setDataById(sqllist, parmlist);

        // 判断是否可以下发
        for (int i = 0; i < param.size(); i++) {
            ArrayList<String> list = param.get(i);
            isPrepIssued(list.get(list.size() - 1));
        }
        return cro;
    }

    /**
     * 撤消短节合同
     * @param list
     * @return
     */
    public CoreReturnObject doCancelDj(ArrayList<ArrayList<Object>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.querySaleStatus");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            for (int i = 0; i < list.size(); i++) {
                psta = conn.prepareStatement(sql);
                psta.setString(1, list.get(i).get(0).toString());
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    Object[] parm = new Object[] { "0",
                            list.get(i).get(0).toString() };
                    Object[] param = new Object[] { rs.getString("ORDER_NO"),
                            rs.getString("ORDER_SEQ"),
                            rs.getString("DELIVERY_NO"), };
                    sqllist.add("CorePlnOrderJgDj.updateDjSaleorder");
                    parmlist.add(parm);

                    sqllist.add("CorePlnOrderJgDj.RollbackDj");
                    parmlist.add(param);
                }
                rs.close();
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        cro = this.setDataById(sqllist, parmlist);
        // 判断是否可以下发
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Object> param = list.get(i);
            isPrepIssued(param.get(param.size() - 1).toString());
        }
        return cro;
    }

    /**
     * 判断备料是否可以下发
     * @param blReqId
     */
    private void isBliussed(String blReqId) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.queryBLbyReqBlId");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            psta = conn.prepareStatement(sql);
            psta.setString(1, blReqId);
            ResultSet rs = psta.executeQuery();
            while (rs.next()) {
                if (rs.getString("D_IS_OK") != null
                        && !rs.getString("D_IS_OK").equals("0")
                        && rs.getString("E_IS_OK") != null
                        && !rs.getString("E_IS_OK").equals("0")
                        && rs.getString("F_IS_OK") != null
                        && !rs.getString("F_IS_OK").equals("0")
                        && rs.getString("GRADECODE") != null
                        && rs.getString("JGBL_REQ_WEIGHT") != null
                        && !rs.getString("JGBL_REQ_WEIGHT").equals("0")
                        && rs.getString("JGBL_REQ_NUM") != null
                        && !rs.getString("JGBL_REQ_NUM").equals("0")) {
                    this.setDataBySqlID("CorePlnOrderJgDj.IfAllowBlIssue",
                            new Object[] { "1", blReqId });
                }
                else {
                    this.setDataBySqlID("CorePlnOrderJgDj.IfAllowBlIssue",
                            new Object[] { "0", blReqId });
                }
            }
            psta.close();
            rs.close();
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新接箍合同利库量和外购量
     * @param list 利库外购信息
     * @return object
     */
    public CoreReturnObject updateLkwgWeight(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.UpdateLkwgWeight");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);

        // 更新接箍合同对应销售合同接箍料编制状态
        for (int i = 0; i < list.size(); i++) {
            isFinishedJgl(list.get(i).get(5).toString());
        }
        return cro;
    }

    /**
     * 是否完成接箍料编制
     * @param jgOrderNum 接箍合同号
     */
    public void isFinishedJgl(String jgOrderNum) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            boolean isFinished = true;
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.queryJgbyOrderno");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            psta = conn.prepareStatement(sql);
            psta.setString(1, jgOrderNum);
            ResultSet result = psta.executeQuery();
            while (result.next()) {
                if (result.getString("ORDER_NO") != null
                        && result.getString("ORDER_SEQ") != null
                        && result.getString("DELIVERY_NO") != null) {
                    String order_no = result.getString("ORDER_NO");
                    String order_seq = result.getString("ORDER_SEQ");
                    String delivery_no = result.getString("DELIVERY_NO");

                    // 查询接箍合同信息,判断是否完成接箍料编制
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CorePlnOrderJgDj.haveJgOrder");
                    PreparedStatement psta1 = conn.prepareStatement(sql);
                    psta1.setString(1, order_no);
                    psta1.setString(2, order_seq);
                    psta1.setString(3, delivery_no);
                    ResultSet rs = psta1.executeQuery();
                    while (rs.next()) {
                        if (rs.getString("IFBUYPLAN") != null
                                && !rs.getString("IFBUYPLAN").equals("1")) {
                            if (rs.getInt("ORDERNUM") > rs.getInt("JGWT_BLREQ")
                                    + rs.getInt("MATCHNUM")) {
                                isFinished = false;
                            }
                        }
                    }
                    rs.close();
                    psta1.close();

                    // 如果完成，更新销售合同接箍料需求是否制订
                    ArrayList<String> sqllist = new ArrayList<String>();
                    ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
                    if (isFinished) {
                        sqllist.add("CorePlnOrderJgDj.isFinishedJgOrder");
                        parmlist.add(new Object[] { "1", jgOrderNum });

                        sqllist.add("CorePlnOrderJgDj.isFinishedJgl");
                        parmlist.add(new Object[] { "1", order_no, order_seq,
                                delivery_no });
                        this.setDataById(sqllist, parmlist);
                    }
                    else {
                        sqllist.add("CorePlnOrderJgDj.isFinishedJgOrder");
                        parmlist.add(new Object[] { "0", jgOrderNum });

                        sqllist.add("CorePlnOrderJgDj.isFinishedJgl");
                        parmlist.add(new Object[] { "0", order_no, order_seq,
                                delivery_no });
                        this.setDataById(sqllist, parmlist);
                    }

                    // 判断是否可以下发
                    // 查询接箍合同信息,判断是否完成接箍料编制
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CorePlnOrderJgDj.querySaleByOrdNO");
                    psta1 = conn.prepareStatement(sql);
                    psta1.setString(1, order_no);
                    psta1.setString(2, order_seq);
                    psta1.setString(3, delivery_no);
                    rs = psta1.executeQuery();
                    while (rs.next()) {
                        String ord_ln_dly_pk = rs.getString("ORD_LN_DLY_PK");
                        isPrepIssued(ord_ln_dly_pk);
                    }
                    rs.close();
                    psta1.close();
                }
            }
            result.close();
            psta.close();
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 保存接箍料需求信息，并保存接箍与备料关系
     * @param list 需求信息
     * @param listrlation 对应关系
     * @param listupdate 更新接箍料信息
     * @return object
     */
    public CoreReturnObject saveJglNeed(ArrayList<ArrayList<String>> list,
            ArrayList<ArrayList<String>> listrlation,
            ArrayList<ArrayList<String>> listupdate) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.queryJgReqblid");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            psta = conn.prepareStatement(sql);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                if (rs.getString("REQ_BL_ID") != null
                        && !rs.getString("REQ_BL_ID").equals("")) {
                    String sequences = rs.getString("REQ_BL_ID");

                    for (int i = 0; i < list.size(); i++) {
                        String num = String.valueOf(Integer.parseInt(sequences
                                .substring(13, 16))
                                + i);
                        num = sequences.substring(0, 13)
                                + CorePlnComm.padding(num, '0', 3);
                        for (int j = 0; j < listrlation.size(); j++) {
                            if (listrlation.get(j).get(1).equals(
                                    list.get(i).get(0))) {
                                listrlation.get(j).set(1, num);
                                sqllist.add("CorePlnOrderJgDj.saveRelation");
                                parmlist.add(listrlation.get(j).toArray());
                            }
                        }

                        list.get(i).set(0, num);
                        sqllist.add("CorePlnOrderJgDj.saveJglneed");
                        parmlist.add(list.get(i).toArray());
                    }

                    for (int i = 0; i < listupdate.size(); i++) {
                        sqllist.add("CorePlnOrderJgDj.updateBlreq");
                        parmlist.add(listupdate.get(i).toArray());
                    }
                }
            }
            rs.close();
            psta.close();
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        cro = this.setDataById(sqllist, parmlist);

        // 更新接箍合同对应销售合同接箍料编制状态
        for (int i = 0; i < listupdate.size(); i++) {
            isFinishedJgl(listupdate.get(i).get(1).toString());
        }
        return cro;
    }

    /**
     * 更新接箍料需求信息，并更新接箍与备料关系
     * @param list 备料需求信息
     * @param listUpdateRel 更新对应关系
     * @param listRelation 新建对应关系
     * @param listupdate 更新接箍料信息
     * @return object
     */
    public CoreReturnObject updateJglNeed(ArrayList<String> list,
            ArrayList<ArrayList<String>> listUpdateRel,
            ArrayList<ArrayList<String>> listRelation,
            ArrayList<ArrayList<String>> listupdate) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        sqllist.add("CorePlnOrderJgDj.updateJglneed");
        parmlist.add(list.toArray());
        for (int i = 0; i < listUpdateRel.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.updateRelation");
            parmlist.add(listUpdateRel.get(i).toArray());
        }
        for (int i = 0; i < listRelation.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.saveRelation");
            parmlist.add(listRelation.get(i).toArray());
        }
        for (int i = 0; i < listupdate.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.updateBlreq");
            parmlist.add(listupdate.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);

        // 更新接箍合同对应销售合同接箍料编制状态
        for (int i = 0; i < listupdate.size(); i++) {
            isFinishedJgl(listupdate.get(i).get(1).toString());
        }
        return cro;
    }

    /**
     * 删除备料需求信息
     * @param listRel 接箍与备料对应关系信息
     * @param list 备料信息
     * @return object
     */
    public CoreReturnObject deleteBlNeed(ArrayList<ArrayList<String>> listRel,
            ArrayList<ArrayList<String>> listJgl,
            ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < listRel.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.deleteRelation");
            parmlist.add(listRel.get(i).toArray());
        }
        for (int i = 0; i < listJgl.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.deleteBlneed");
            parmlist.add(listJgl.get(i).toArray());
        }
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.updateBlreq");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);

        // 更新接箍合同对应销售合同接箍料编制状态
        for (int i = 0; i < list.size(); i++) {
            isFinishedJgl(list.get(i).get(1).toString());
        }
        return cro;
    }

    /**
     * 删除接箍与备料关系
     * @param list_Rel 接箍与接箍料关系信息
     * @param list_Jgl 接箍料需求更新信息
     * @return object
     */
    public CoreReturnObject deleteJglOne(ArrayList<ArrayList<String>> list_Rel,
            ArrayList<ArrayList<String>> list_Jgl,
            ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list_Rel.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.deleteRelation");
            parmlist.add(list_Rel.get(i).toArray());
        }
        for (int i = 0; i < list_Jgl.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.updateJglneed");
            parmlist.add(list_Jgl.get(i).toArray());
        }
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.updateBlreq");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);

        // 更新接箍合同对应销售合同接箍料编制状态
        for (int i = 0; i < list.size(); i++) {
            isFinishedJgl(list.get(i).get(1).toString());
        }
        return cro;
    }

    /**
     * 下发接箍合同
     * @param list 接箍合同信息
     * @return object
     */
    public CoreReturnObject issuedJgOrder(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.issuedJgOrder");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 取消下发接箍合同
     * @param list
     * @return object
     */
    public CoreReturnObject cancelJgIssued(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.cancelJgIssued");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 下发备料需求
     * @param list 需求信息
     * @param listPro 轧管、热处理等工序列
     * @return object
     */
    public CoreReturnObject issuedJglNeed(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            // 备料下发工序
//            CoreBlPrcOrder prcOrder = new CoreBlPrcOrder("");
//            prcOrder.creatProcess(list.get(i).get(0));
//            sqllist.addAll(prcOrder.getSqllist());
//            parmlist.addAll(prcOrder.getParmlist());

            sqllist.add("CorePlnOrderJgDj.issuedJglNeed");
            parmlist.add(list.get(i).toArray());

            // 下发接箍与接箍料对应关系表
            sqllist.add("CorePlnOrderJgDj.issuedRelation");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }

    /**
     * 取消下发备料
     * @param list
     * @return
     */
    public CoreReturnObject cancelBlIssued(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CorePlnOrderJgDj.cancelBlIssued");
            parmlist.add(list.get(i).toArray());

            // 删除轧管工序合同
            sqllist.add("CorePrcOrder.DelBlProZg");
            parmlist.add(list.get(i).toArray());

            // 删除镦拔扩工序合同
            sqllist.add("CorePrcOrder.DelBlProBj");
            parmlist.add(list.get(i).toArray());

            // 删除热处理工序合同
            sqllist.add("CorePrcOrder.DelBlProRcl");
            parmlist.add(list.get(i).toArray());

            // 更新接箍料与接箍合同关系表
            sqllist.add("CorePlnOrderJgDj.cancelBlrelIssued");
            parmlist.add(list.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }
    
    /**
     * 判断是否可以下发
     * @param ord_ln_dly_pk
     */
    public void isPrepIssued(String ordLnDlyPk) {
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnOrderJgDj.querySaleStatus");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            psta = conn.prepareStatement(sql);
            psta.setString(1, ordLnDlyPk);
            ResultSet rs = psta.executeQuery();
            while (rs.next()) {
                // 合同状态为已接收
                if (rs.getString("PORDERSTATUS") != null
                        && rs.getString("PORDERSTATUS").equals("1")) {
                    if (rs.getString("JGORD_IS_OK") != null
                            && rs.getString("DJBL_REQ_IS_OK") != null
                            && !rs.getString("JGORD_IS_OK").equals("0")
                            && !rs.getString("DJBL_REQ_IS_OK").equals("0")) {
                        // 接箍、接箍料需求、短节备料需求、钢种是否确认、
                        // 轧管、管加工都经过确认
                        this.setDataBySqlID("CorePlnOrderJgDj.IfAllowIssue",
                                new Object[] { "1", ordLnDlyPk });
                    }
                    else {
                        this.setDataBySqlID("CorePlnOrderJgDj.IfAllowIssue",
                                new Object[] { "0", ordLnDlyPk });
                    }
                }
            }
            rs.close();
            psta.close();
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}