package com.steering.pss.plan.jgtransa;

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

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.jgtransa.model.PlnGxJgDEntity;
import com.steering.pss.plan.jgtransa.model.PlnGxJgMEntity;

public class CoreOrderJGGXPln extends DBCommon {
    /**
     * 查询待排产接箍合同数据
     * @return
     */
    public CoreReturnObject queryOrdering() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryOrdering");
        return cro;
    }

    /**
     * 查询备料数据
     * @return
     */
    public CoreReturnObject queryBLSJ() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryBLSJ");
        return cro;
    }

    /**
     * 查询接箍订单对应的接箍料明细
     * @return
     */
    public CoreReturnObject queryJGDDMForD(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDMForD",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询接箍订单
     * @return
     */
    public CoreReturnObject queryJGDD() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDD");
        return cro;
    }

    /**
     * 查询接箍订单从表
     * @return
     */
    public CoreReturnObject queryJGDDCB() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDCB");
        return cro;
    }

    /**
     * 查询接箍产线对应小时能力
     * @param list
     * @return
     */
    public CoreReturnObject queryJgPlHrCapcty(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJgPlHrCapcty", list
                .toArray());
        return cro;
    }

    /**
     * 查询接箍对应产线及小时能力
     * @return
     */
    public CoreReturnObject queryOrderbase(String inMaterialNo,
            String outMaterialNo) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryOrderbase",
                new Object[] { inMaterialNo, outMaterialNo });
        return cro;
    }

    /**
     * 查询接箍生产订单
     * @param str
     * @return
     */
    public CoreReturnObject queryJGDDById(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDById",
                new Object[] { str });
        return cro;
    }

    /**
     * 新增接箍生产订单
     * @return
     */
    public CoreReturnObject addJgDd(ArrayList<PlnGxJgDEntity> list,
            PlnGxJgMEntity jgMentity) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            // 获取订单ID
            String sqlId = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderJGGXPln.creatJgDdNum");
            psta = conn.prepareStatement(sqlId);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                if (rs.getString("PRO_PLAN_ID") != null) {
                    jgMentity.setProPlanId(rs.getString("PRO_PLAN_ID"));
                }
            }
            rs.close();
            psta.close();
            conn.commit();

            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderJGGXPln.insertGxJgM");
            String sqlUp = EntityTool.fillSqlByEntity(sql, jgMentity);
            sqllist.add(sqlUp);
            parmlist.add(new Object[] {});

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setProPlanId(jgMentity.getProPlanId());
                String sqlc = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.insertGxJgD");
                String sqlUpc = EntityTool.fillSqlByEntity(sqlc, list.get(i));
                sqllist.add(sqlUpc);
                parmlist.add(new Object[] {});

                // 回写接箍备料关系表接箍计划已排个数
                String sqlUpdate = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.updateJgAndBlnum");
                sqllist.add(sqlUpdate);
                parmlist.add(new Object[] { list.get(i).getPlannum(),
                        list.get(i).getJgOrderno(), list.get(i).getReqBlId() });
            }
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sqllist.clear();
            parmlist.clear();
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
            return cro;
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        cro = this.setData(sqllist, parmlist);
        return cro;
    }

    /**
     * 加入生产订单
     * @param list
     * @param jgMentity
     * @return
     */
    public CoreReturnObject inCreaseJgDd(ArrayList<PlnGxJgDEntity> list,
            PlnGxJgMEntity jgMentity) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String sqlUpM = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderJGGXPln.updateGxJgM");
            String sqlUpMEn = EntityTool.fillSqlByEntity(sqlUpM, jgMentity);
            sqllist.add(sqlUpMEn);
            parmlist.add(new Object[] {});

            for (int i = 0; i < list.size(); i++) {
                boolean fl = false;
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.queryPlnGxjgD");
                psta = conn.prepareStatement(sql);
                psta.setString(1, list.get(i).getProPlanId());
                psta.setString(2, list.get(i).getJgOrderno());
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    fl = true;
                }
                rs.close();
                psta.close();

                String sqlAdd = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.insertGxJgD");
                String sqlUp = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.updateGxJgD");
                String sqlWb = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.updateJgAndBlnum");
                if (fl) {
                    // 有记录、修改接箍个数
                    String sqlEn = EntityTool.fillSqlByEntity(sqlUp, list
                            .get(i));
                    sqllist.add(sqlEn);
                    parmlist.add(new Object[] {});
                }
                else {
                    // 无记录、新增记录
                    String sqlEn = EntityTool.fillSqlByEntity(sqlAdd, list
                            .get(i));
                    sqllist.add(sqlEn);
                    parmlist.add(new Object[] {});
                }

                // 回写接箍备料关系表接箍计划已排个数
                sqllist.add(sqlWb);
                parmlist.add(new Object[] { list.get(i).getPlannum(),
                        list.get(i).getJgOrderno(), list.get(i).getReqBlId() });
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sqllist.clear();
            parmlist.clear();
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
            return cro;
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        cro = this.setData(sqllist, parmlist);
        return cro;
    }

    /**
     * 修改接箍订单
     * @param list
     * @param jgMentity
     * @return
     */
    public CoreReturnObject doModifyJGDD(ArrayList<PlnGxJgDEntity> list,
            PlnGxJgMEntity jgMentity) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String sqlUpM = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderJGGXPln.updateJGDD");
            String sqlUpMEn = EntityTool.fillSqlByEntity(sqlUpM, jgMentity);
            sqllist.add(sqlUpMEn);
            parmlist.add(new Object[] {});

            for (int i = 0; i < list.size(); i++) {
                String sqlQeury = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.queryJGDDCBById");
                String sqlUp = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.updateJGDDCB");
                String sqlWb = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderJGGXPln.updateJgAndBlnum");

                psta = conn.prepareStatement(sqlQeury);
                psta.setString(1, list.get(i).getProPlanId());
                psta.setString(2, list.get(i).getJgOrderno());
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    int plnNum = Integer.parseInt(list.get(i).getPlannum())
                            - rs.getInt("PLANNUM");
                    // 修改接箍个数
                    String sqlEn = EntityTool.fillSqlByEntity(sqlUp, list
                            .get(i));
                    sqllist.add(sqlEn);
                    parmlist.add(new Object[] {});

                    // 回写接箍备料关系表接箍计划已排个数
                    sqllist.add(sqlWb);
                    parmlist.add(new Object[] { plnNum,
                            list.get(i).getJgOrderno(),
                            list.get(i).getReqBlId() });
                }
                else {
                    cro.setV_errMsg("未找到订单" + list.get(i).getProPlanId()
                            + "下的明细项" + list.get(i).getJgOrderno() + "。");
                    cro.setV_errCode(-1);
                    return cro;
                }
                rs.close();
                psta.close();
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sqllist.clear();
            parmlist.clear();
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
            return cro;
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        cro = this.setData(sqllist, parmlist);
        return cro;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    /**
     * 查询接箍码基础表数据
     * @return
     */
    public CoreReturnObject queryBLJCB(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryBLJCB",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询接箍订单数
     * @return
     */
    public CoreReturnObject queryJGDDS() {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDS");
        return cro;
    }

    /**
     * 查询单个订单信息
     */
    public CoreReturnObject queryDGDD(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryDGDD",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询接箍订单从表A
     * @return
     */
    public CoreReturnObject queryJGDDCBA(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDCBA",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询备料关系表明细
     * @return
     */
    public CoreReturnObject queryJGDDBLMX(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDBLMX", list
                .toArray());
        return cro;
    }

    /**
     * 查询接箍合同
     * @return
     */
    public CoreReturnObject queryOrderingh(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryOrderingh",
                new Object[] { str });
        return cro;
    }

    /**
     * 查询生产耗时率
     * @return
     */
    public CoreReturnObject queryJGDDHSL(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderJGGXPln.queryJGDDHSL", list
                .toArray());
        return cro;
    }

    /**
     * 制作接箍订单从表
     * @return
     */
    public CoreReturnObject addTGJGDDCB(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.addTGJGDDCB", list
                .toArray());
        return cro;

    }

    /**
     * 制作接箍订单
     * @return
     */
    public CoreReturnObject addTGJGDD(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.addTGJGDD", list.toArray());
        return cro;

    }

    // /**
    // * 删除接箍订单
    // * @return
    // */
    // public CoreReturnObject detTGJGDD(String str ){
    // CoreReturnObject cro = new CoreReturnObject();
    // cro = this.setDataBySqlID("CoreOrderJGGXPln.detTGJGDD", new
    // Object[]{str});
    // return cro;
    //		
    // }
    /**
     * 删除接箍订单从表
     * @return
     */
    public CoreReturnObject detTGJGDDCB(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.detTGJGDDCB",
                new Object[] { str });
        return cro;

    }

    /**
     * 删除接箍订单从表明细
     * @return
     */
    public CoreReturnObject detTGJGDDCBMX(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.detTGJGDDCBMX", list
                .toArray());
        return cro;

    }

    /**
     * 删除订单
     */
    public CoreReturnObject detTGJGDD(ArrayList<ArrayList<String>> list,
            ArrayList<ArrayList<String>> ary) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> listid = new ArrayList<String>();
        ArrayList<Object[]> str = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            listid.add("CoreOrderJGGXPln.detTGJGDD");
            str.add(list.get(i).toArray());
            listid.add("CoreOrderJGGXPln.detTGJGDDCB");
            str.add(list.get(i).toArray());
        }
        for (int i = 0; i < ary.size(); i++) {
            listid.add("CoreOrderJGGXPln.UPTGJGBL");
            str.add(ary.get(i).toArray());
        }
        cro = this.setDataById(listid, str);
        return cro;
    }

    /**
     * 下发接箍订单
     * @return
     */
    public CoreReturnObject UPTGJGDD(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.UPTGJGDD",
                new Object[] { str });
        return cro;

    }

    /**
     * 更新备料关系表状态
     * @return
     */
    public CoreReturnObject UPTGJGBL(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> str1 = new ArrayList<String>();
        ArrayList<Object[]> str = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            str1.add("CoreOrderJGGXPln.UPTGJGBL");
            str.add(list.get(i).toArray());

        }

        cro = this.setDataById(str1, str);

        return cro;

    }

    /**
     * 更新备料关系表状态A
     * @return
     */
    public CoreReturnObject UPTGJGBLA(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.UPTGJGBLA", list.toArray());
        return cro;

    }

    /**
     *加入接箍订单
     * @return
     */
    public CoreReturnObject addJGDDThe(ArrayList<ArrayList<String>> list1,
            ArrayList<ArrayList<String>> list,
            ArrayList<ArrayList<String>> list2) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> str1 = new ArrayList<String>();
        ArrayList<Object[]> str = new ArrayList<Object[]>();
        str1.add("CoreOrderJGGXPln.addJGDDToo");
        str.add(list1.get(0).toArray());
        for (int i = 0; i < list.size(); i++) {
            str1.add("CoreOrderJGGXPln.UPTGJGBL");
            str.add(list.get(i).toArray());

        }
        for (int i = 0; i < list2.size(); i++) {
            str1.add("CoreOrderJGGXPln.addTGJGDDCB");
            str.add(list2.get(i).toArray());
        }

        cro = this.setDataById(str1, str);
        return cro;

    }

    /**
     *删除接箍订单明细B
     * @return
     */
    public CoreReturnObject deletmx(ArrayList<ArrayList<String>> list,
            ArrayList<ArrayList<String>> list1) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> str1 = new ArrayList<String>();
        ArrayList<Object[]> str = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            str1.add("CoreOrderJGGXPln.addJGDDToo");
            str.add(list.get(i).toArray());
        }
        for (int i = 0; i < list1.size(); i++) {
            str1.add("CoreOrderJGGXPln.detTGJGDDCBMX");
            str1.add("CoreOrderJGGXPln.UPTGJGBLA");
            str.add(list1.get(i).toArray());
            str.add(list1.get(i).toArray());

        }
        cro = this.setDataById(str1, str);
        return cro;

    }

    /**
     *修改接箍订单
     * @return
     */
    public CoreReturnObject UPJGDD(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.UPJGDD", list.toArray());
        return cro;

    }

    /**
     *修改生产时间
     * @return
     */
    public CoreReturnObject UPDDTime(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.UPDDTime", list.toArray());
        return cro;

    }

    /**
     *生产排序
     * @return
     */
    public CoreReturnObject DDSCPX(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.DDSCPX", list.toArray());
        return cro;

    }

    /**
     * 取消接箍订单下发
     * @return
     */
    public CoreReturnObject qxTGJGDD(String str) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.setDataBySqlID("CoreOrderJGGXPln.qxTGJGDD",
                new Object[] { str });
        return cro;

    }

    /**
     * 测试订单制定
     * @return
     */
    public CoreReturnObject ddtest(ArrayList<ArrayList<String>> listBL,
            ArrayList<ArrayList<String>> param,
            ArrayList<ArrayList<String>> lista) {
        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("CoreOrderJGGXPln.addTGJGDD");
            parmlist.add(listBL.get(i).toArray());
        }
        for (int i = 0; i < param.size(); i++) {
            sqllist.add("CoreOrderJGGXPln.addTGJGDDCB");
            parmlist.add(param.get(i).toArray());
        }
        for (int i = 0; i < lista.size(); i++) {
            sqllist.add("CoreOrderJGGXPln.UPTGJGBL");
            parmlist.add(lista.get(i).toArray());

        }

        cro = this.setDataById(sqllist, parmlist);

        return cro;
    }
}
