package com.steering.pss.glbusiness;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.glModels.SlmGlOrderHeadEntity;
import com.steering.pss.glModels.SlmGlOrderLineEntity;
import com.steering.pss.judge.Bll.BllCreateZbsInfo;
import com.steering.pss.sale.order.CoreGenerateOrderNo;
import com.steering.pss.sale.order.CoreOrderInfoUpdate;
import com.steering.pss.sale.order.CoreOrderStatusCommFun;
import com.steering.pss.sale.order.model.CoreOrderStatus;
import com.steering.pss.sale.order.model.SlmOrderHeadEntity;
import com.steering.pss.sale.order.model.SlmOrderLineNewEntity;
import com.steering.pss.sale.order.model.SlmOrderPriceEntity;

public class GlOrderManage  extends DBCommon{
    
	/**
     * 合同头查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject doQuery(String orderNo, String orderStart,
            String orderEnd,String[] arr) {
    	Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.doQuery");
        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",arr);
        sql += condition;
        return this.getData(sql, params);
    }
    
	/**
     * 根据ordPk合同头查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryByOrdPk(String ordPk) {
    	Object[] params = new Object[] { ordPk};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryByOrdPk");
        return this.getData(sql, params);
    }
    
    public CoreReturnObject doQueryLine(String ordpk) {
    	Object[] params = new Object[] { ordpk };
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.doQueryLine");
        sql = sql + " ORDER BY ORDER_SEQ";
        return this.getData(sql, params);
    }

    public CoreReturnObject getFilePath(String CraftNo){
        Object[] params = new Object[] { CraftNo };
        String sql= CoreXmlSqlParsersFactory.getSql("GlOrderManage.getFilePath");
        return this.getData(sql, params);
    }
    
    public CoreReturnObject doQueryMould() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.doQueryMould");
        return this.getData(sql);
    }
    
    /**
     * 新增合同头
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.doAdd");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return cro;
    }
    
    /**
     * 修改合同头
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doUpdate(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.doUpdate");
        try {
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return cro;
    }
    
    /**
     * 加载合同分类
     * @return
     */
    public CoreReturnObject getBaseInfo(String sortcode) {
        String sql = "SELECT BASENAME,BASECODE FROM COM_BASE_INFO WHERE SORTCODE = '" + sortcode + "' AND VALIDFLAG = '1'";
        return this.getData(sql);
    }
    
	/**
     * 入库信息查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryInList(String judgeStoveNo, String proName,
            String specName,String orderStart,String orderEnd,String tradeType,
            String glOrderNo,String typeFlag) {
    	String flag1="";
    	String flag2="";
    	if("122301".equals(tradeType)){//销售
    		flag1="0";
    		flag2="1";
    	}else{//采购
    		flag1="1";
    		flag2="0";
    	}
    	Object[] params = new Object[] { judgeStoveNo, proName,specName,orderStart, orderEnd};
    	//Object[] params = new Object[] { judgeStoveNo, proName, steelName,specName,orderStart, orderEnd,flag1,flag2};
        //AND (select t1.seam_area from ydm_bs_storage t1 where t1.storage_no=T.STORAGE_NO)= ?
        //AND (select t1.seam_area from ydm_bs_storage t1 where t1.storage_no=T.SOURE_CODE)= ? 
    	String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryInList");
    	if("1".equals(typeFlag)){
    		sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.QueryGpList");
    	}else{
    		sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.QueryInList");
    	}
        return this.getData(sql, params);
    }
    
    /**
     * 入库信息查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryPlnOrderInfo(String tradeType,String orderNo) {
    	//Object[] params = new Object[] { orderStart, orderEnd,orderNo};
    	Object[] params = new Object[] { orderNo};
    	String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryPlnOrderInfoBuy");
    	if("122301".equals(tradeType)){//销售
    		sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.QueryPlnOrderInfoSale");	
    	}else{//采购
    		sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.QueryPlnOrderInfoBuy");
    		//params = new Object[] { orderStart, orderEnd,orderNo,orderStart, orderEnd,orderNo};
    		params = new Object[] { orderNo,orderNo};
    	}
    	return this.getData(sql, params);
    }
    
    /**
     * 新增现货合同+利库
     * @param
     * @param parm
     * @param orderNo
     * @param saleOrg
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject addGlOrderLine(ArrayList<Object> parm, String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String validflag = getGlOrderHeadStatus(con, ordPk); // 合同状态

            if ("10".equals(validflag)) { // 录入
                PreparedStatement ps = null;
                ResultSet rs = null;
                String sql = CoreXmlSqlParsersFactory
                        .getSql("GlOrderManage.GetGlOrderLinePk");
                for (int i = 0; i < parm.size(); i++) {
                	ArrayList<Object> list=(ArrayList<Object>)parm.get(i);
                    ps = con.prepareStatement(sql);
                    ps.setObject(1, ordPk);
                    ps.setObject(2, ordPk);
                    rs = ps.executeQuery();
                    String ordLnPk = "";
                    if (rs.next()) {
                        ordLnPk = rs.getString(1);
                    }
                    if (ordLnPk == null || ordLnPk.equals("")) {
                        cro.setV_errMsg("合同行主键生成失败！");
                        cro.setV_errCode(-1);
                        con.rollback();
                        return cro;
                    }
                    rs.close();
                    ps.close();
                    list.set(1, ordLnPk);
                    String sql1 = CoreXmlSqlParsersFactory
                            .getSql("GlOrderManage.doAddGlLine");
                    ps = con.prepareStatement(sql1);
                    for (int j = 0; j < list.size(); j++) {
                        ps.setObject(j + 1, list.get(j));
                    }
                    ps.executeUpdate();
                    ps.close();
                }
                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errMsg("只有在录入状态下允许新增合同行！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }

        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                    cro.setV_errMsg(e.getMessage());
                    cro.setV_errCode(-1);
                    e.printStackTrace();
                }
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }
    
    /**
     * 作废合同头
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject deleteHead(String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.deleteHead");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ps.executeUpdate();
            ps.close();
            sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.deleteLineByOrdPk");
            ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    /**
     * 作废合同行
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject deleteLine(String ordLnPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.deleteLine");
            PreparedStatement ps = con.prepareStatement(sql);
            /*String sql1 = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.deleteInLIst");
            PreparedStatement ps1 = con.prepareStatement(sql1);
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.deleteGpLIst");
            PreparedStatement ps2 = con.prepareStatement(sql2);
            ps1.setObject(1, ordLnPk);
            ps2.setObject(1, ordLnPk);
            ps1.executeUpdate();
            ps2.executeUpdate();
            ps1.close();   
            ps2.close(); */
            ps.setObject(1, ordLnPk);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    /**
     * 价格维护
     * @param parm 合同头参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doSavePrice(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
        	for (int i = 0; i < parm.size(); i++) {
                String sql = CoreXmlSqlParsersFactory
                        .getSql("GlOrderManage.doSavePrice");
                ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, list.get(0));
                ps.setObject(2, list.get(1));
                ps.setObject(3, list.get(2));
                ps.executeUpdate();
                ps.close();
            }
        	
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
  
    
    /**
     * 合同确认
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doConfirm(String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            if(!"10".equals(getGlOrderHeadStatus(con,ordPk))){
            	cro.setV_errMsg("合同状态非有效,不能确认！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            String lineCount=getGlOrderLineCount(con,ordPk);
            String priceCount=getGlOrderLinePrice(con,ordPk);
            if("0".equals(lineCount)){
            	cro.setV_errMsg("该合同向下无有效合同行，不能确认！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            if(!priceCount.equals(lineCount)){
            	cro.setV_errMsg("合同向下所有合同行需先进行价格维护！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.doConfirm");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    /**
     * 合同确认
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject cancelConfirm(String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            if(!"20".equals(getGlOrderHeadStatus(con,ordPk))){
            	cro.setV_errMsg("合同状态非生效,不能取消确认！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            if(!"00".equals(getGlApproveStatus(con,ordPk))){
            	cro.setV_errMsg("已评审，不允许取消！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.cancelConfirm");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordPk);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    /**
     * 合同评审
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doApprove(String ordPk,String flag,
    		String reviewMemo,String userID,String userName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            if(!"20".equals(getGlOrderHeadStatus(con,ordPk))){
            	cro.setV_errMsg("合同状态非生效不能审批！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            if(!"00".equals(getGlApproveStatus(con,ordPk))){
            	cro.setV_errMsg("评审状态非待审不能审批！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
            String time = date.toString().replaceAll("-", "");
            time=time.substring(0, 6);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.getApproveNo");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, time);
            ps.setObject(2, time);
            ResultSet rs = ps.executeQuery();
            String code = "";
            if (rs.next()) {
                code = rs.getString(1);
            }
            rs.close();
            ps.close();
            sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.doApprove");
            ps = con.prepareStatement(sql);
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.insertApprove");
            PreparedStatement ps2 = con.prepareStatement(sql2);
            ps.setObject(1, code);
            ps2.setObject(1, code);
            if("1".equals(flag)){
            	ps.setObject(2, "10");
            	ps.setObject(3, "20");
            	ps2.setObject(2, "同意");
            }
            else if("2".equals(flag)){
            	ps.setObject(2, "20");
            	ps.setObject(3, "10");
            	ps2.setObject(2, "退回");
            }
            else if("3".equals(flag)){
            	ps.setObject(2, "30");
            	ps.setObject(3, "20");
            	ps2.setObject(2, "拒绝");
            }
            ps.setObject(4, ordPk);
            ps2.setObject(3, reviewMemo);
            ps2.setObject(4, userID);
            ps2.setObject(5, userName);
            ps2.setObject(6, ordPk);
            ps.executeUpdate();
            ps.close();
            ps2.executeUpdate();
            ps2.close();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    /**
     * 合同评审
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doReturn(String ordPk) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            if(!"20".equals(getGlOrderHeadStatus(con,ordPk))){
            	cro.setV_errMsg("合同状态非生效不能审批！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            if("00".equals(getGlApproveStatus(con,ordPk))){
            	cro.setV_errMsg("未审批，不需要撤销审批！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.doReturn");
            PreparedStatement ps = con.prepareStatement(sql);
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.deleteApprove");
            PreparedStatement ps2 = con.prepareStatement(sql2);
            ps.setObject(1, ordPk);
            ps2.setObject(1, ordPk);
            ps.executeUpdate();
            ps2.executeUpdate();
            ps.close();
            ps2.close();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    
    /**
     * 取得合同头的状态
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String getGlOrderHeadStatus(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.getGlOrderHeadStatus");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        String validflag="";
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
        	validflag=rs.getString("VALIDFLAG");
            return validflag;
        }
        rs.close();
        ps.close();
        return validflag;
    }
    
    /**
     * 取得合同头的状态
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String getGlOrderLinePrice(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.getGlOrderLinePrice");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        String validflag="";
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
        	validflag=rs.getString(1);
            return validflag;
        }
        rs.close();
        ps.close();
        return validflag;
    }
    
    /**
     * 取得合同头的状态
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String getGlOrderLineCount(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.getGlOrderLineCount");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        String validflag="";
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
        	validflag=rs.getString(1);
            return validflag;
        }
        rs.close();
        ps.close();
        return validflag;
    }
    
    /**
     * 取得合同头评审状态
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String getGlApproveStatus(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.getGlApproveStatus");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        String approveValidflag="";
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
        	approveValidflag=rs.getString("APPROVALFLAG");
            return approveValidflag;
        }
        rs.close();
        ps.close();
        return approveValidflag;
    }
    
    /**
     * 保存模板
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doSaveModel(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.getMouldCount");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, parm.get(1));
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
            	String count=rs.getString(1);
            	if(!"0".equals(count)){
            		cro.setV_errMsg("模板名称已存在！");
                    cro.setV_errCode(-1);
                    con.rollback();
                    return cro;
            	}
            }
            
        	sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.getMouldNo");
            ps = con.prepareStatement(sql);

            rs = ps.executeQuery();
            if (rs.next()) {
            	String mouldNo=rs.getString("MOULD");
            	parm.set(0, mouldNo);
            }
            sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.doSaveModel");
            cro = this.getDao(CoreConstantData.sysbeanstr)
                    .ExcuteNonQuery(sql, parm.toArray());
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
	/**
     * 合同头查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryHeadAndLine(String orderNo, String orderStart,
            String orderEnd,String[] arr) {
    	Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryHeadAndLine");
        /*String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",arr);
        sql += condition;*/
        return this.getData(sql, params);
    }
    
	/**
     * 合同头查询
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryBcOutList(String planId) {
    	Object[] params = new Object[] { planId};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryBcOutList");
        return this.getData(sql, params);
    }
    
	  /**
     * 插入关联交易表
     * @param parm 合同头参数
     * @param flag 1 首次编单 2 末次编单
     * @return
     */
    public CoreReturnObject addGlPriceInfo(ArrayList<String> list,String ordLnPk,String proOrderId,String userId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String insertGlInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.insertGlInfoCarry"); // 新增关联交易表
			PreparedStatement pstaGlInfo = con.prepareStatement(insertGlInfo);
			
			String updateOutInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.updateOutInfo"); // 修改出库表信息
			PreparedStatement pstaOutInfo = con.prepareStatement(updateOutInfo);
			
			//获取合同信息
            List<SlmGlOrderHeadEntity> listOrderHead = new ArrayList<SlmGlOrderHeadEntity>();
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.QueryByOrdLnPk");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordLnPk);
            ResultSet rs = ps.executeQuery();
            listOrderHead = EntityTool
                    .getEntitysByRs(rs, SlmGlOrderHeadEntity.class);
            rs.close();
            ps.close();
            SlmGlOrderHeadEntity head=listOrderHead.get(0);
            
			SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
	        String yyMMdd = f.format(new Date());
	        String strInvId = "JS" + yyMMdd;
	        String balanceNo="";
	        sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.getMaxBalanceNo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, strInvId);
            rs = ps.executeQuery();
            if(rs.next()){
            	balanceNo=rs.getString(1);
            }
            rs.close();
            ps.close();
            
	        double weight=0.0;
            for(String outNo : list){
            	sql = CoreXmlSqlParsersFactory
                        .getSql("GlOrderManage.getOutWeight");
                ps = con.prepareStatement(sql);
                ps.setObject(1, outNo);
                ps.setObject(2, proOrderId);
                String sumWeight="";
                String balanceNoQuery="";
                rs = ps.executeQuery();
                if (rs.next()) {
                	sumWeight=rs.getString("ACT_WEIGHT");
                	balanceNoQuery=rs.getString("BALANCE_NO");
                }
                rs.close();
                ps.close();
                if(balanceNoQuery!=null){
            		cro.setV_errMsg("数据已结转，请刷新后重试！");
                    cro.setV_errCode(-1);
                    con.rollback();
                    return cro;
            	}
            	weight+=Double.parseDouble(sumWeight);
            	pstaOutInfo.setString(1,balanceNo);
            	pstaOutInfo.setString(2,outNo);
            	pstaOutInfo.setObject(3, proOrderId);
            	pstaOutInfo.executeUpdate();
            }
        	pstaGlInfo.setString(1,balanceNo);
        	pstaGlInfo.setString(2,head.getGlOrderNo());
        	pstaGlInfo.setString(3,head.getOrderTyp());
        	pstaGlInfo.setString(4,head.getOrderTypDesc());
        	pstaGlInfo.setString(5,"123401");
        	pstaGlInfo.setString(6,"应收");
        	pstaGlInfo.setString(7,"123301");
        	pstaGlInfo.setString(8,"货款");
        	pstaGlInfo.setString(9,"0");
        	pstaGlInfo.setString(10,head.getSupplyUnit());
        	pstaGlInfo.setString(11,head.getCustomerNo());
        	pstaGlInfo.setString(12,head.getCustomerNm());
        	pstaGlInfo.setString(13,head.getStlmtUnitNo());
        	pstaGlInfo.setString(14,head.getStlmtUnitNm());
        	pstaGlInfo.setString(15,head.getSignDate().toString());
        	pstaGlInfo.setString(16,head.getOrderYear());
        	pstaGlInfo.setString(17,userId);
        	pstaGlInfo.setString(18,(String.valueOf(Double.parseDouble(head.getBalncePrice())*weight)));
        	pstaGlInfo.setString(19,"");
        	pstaGlInfo.setString(20,"1");
        	pstaGlInfo.setString(21,ordLnPk);
        	pstaGlInfo.setString(22,head.getOrderNo());
			pstaGlInfo.executeUpdate();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    
	/**
     * 挂账信息查询
     * @param 
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryBalanceInfo(String balanceNo,String orderNo, String orderStart,
            String orderEnd,String isPrint) {
    	Object[] params = new Object[] { balanceNo,orderNo, orderStart, orderEnd};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryBalanceInfo");
        if("1".equals(isPrint)){
        	sql+= " AND T.PRINT_NO IS NULL";
        }else if("2".equals(isPrint)){
        	sql+= " AND T.PRINT_NO IS NOT NULL";
        } 
        return this.getData(sql, params);
    }
    
    /**
     * 保存模板
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doCancelCarry(String balanceNo) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String cancelCarry = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.doCancelCarry"); // 新增关联交易表
            String updateOutInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.updateOutInfoBalance"); // 修改出库表信息
			PreparedStatement pstaOutInfo = con.prepareStatement(updateOutInfo);
			pstaOutInfo.setString(1,balanceNo);
        	
			PreparedStatement pstaCancel = con.prepareStatement(cancelCarry);
			pstaCancel.setString(1,balanceNo);
			
			pstaOutInfo.executeUpdate();
        	pstaCancel.executeUpdate();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
    /**
     * 保存打印id
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doPrint(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            
            SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
	        String yyMMdd = f.format(new Date());
	        String strInvId = yyMMdd;
	        String printId="";
	        String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.getMaxPrintId");
	        PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, strInvId);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
            	printId=rs.getString(1);
            }
            rs.close();
            ps.close();
            String cancelCarry = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.updatePrint"); // 新增关联交易表
			PreparedStatement pstaPrintInfo = con.prepareStatement(cancelCarry);
			pstaPrintInfo.setString(1,printId);
            for(String balanceNo:list){
            	pstaPrintInfo.setString(2,balanceNo);
            	pstaPrintInfo.executeUpdate();
            }
            con.commit();
            cro.setV_errMsg(printId);
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
        
    }
    
	/**
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryHeadAndLineGp(String orderNo, String orderStart,
            String orderEnd,String[] arr) {
    	Object[] params = new Object[] { orderNo, orderStart, orderEnd};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryHeadAndLineGp");
        /*String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",arr);
        sql += condition;*/
        return this.getData(sql, params);
    }
    
	/**
     * @param String order_no,String orderSts,String orderStart,String orderEnd
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject QueryGpOutList(String gradeCode,String diamter) {
    	Object[] params = new Object[] { gradeCode,diamter};
        String sql = CoreXmlSqlParsersFactory
                .getSql("GlOrderManage.QueryGpOutList");
        return this.getData(sql, params);
    }
    
    
    /**
     * 插入关联交易表
     * @param parm 合同头参数
     * @param flag 1 首次编单 2 末次编单
     * @return
     */
    public CoreReturnObject addGpPriceInfo(ArrayList<String> list,String ordLnPk,String gradeCode,String dimater,String userId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String insertGlInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.insertGlInfoCarry"); // 新增关联交易表
			PreparedStatement pstaGlInfo = con.prepareStatement(insertGlInfo);
			
			String updateOutInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.updateGpOutInfo"); // 修改出库表信息
			PreparedStatement pstaOutInfo = con.prepareStatement(updateOutInfo);
			
			//获取合同信息
            List<SlmGlOrderHeadEntity> listOrderHead = new ArrayList<SlmGlOrderHeadEntity>();
            String sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.QueryByOrdLnPk");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordLnPk);
            ResultSet rs = ps.executeQuery();
            listOrderHead = EntityTool
                    .getEntitysByRs(rs, SlmGlOrderHeadEntity.class);
            rs.close();
            ps.close();
            SlmGlOrderHeadEntity head=listOrderHead.get(0);
            if(head.getBalncePrice().equals("")){
            	cro.setV_errMsg("请先维护结算价！");
                cro.setV_errCode(-1);
                con.rollback();
                return cro;
            }
			SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
	        String yyMMdd = f.format(new Date());
	        String strInvId = "JS" + yyMMdd;
	        String balanceNo="";
	        sql = CoreXmlSqlParsersFactory
                    .getSql("GlOrderManage.getMaxBalanceNo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, strInvId);
            rs = ps.executeQuery();
            if(rs.next()){
            	balanceNo=rs.getString(1);
            }
            rs.close();
            ps.close();
            
	        double weight=0.0;
            for(String outNo : list){
            	sql = CoreXmlSqlParsersFactory
                        .getSql("GlOrderManage.getGpOutWeight");
                ps = con.prepareStatement(sql);
                ps.setObject(1, outNo);
                ps.setObject(2, gradeCode);
                ps.setObject(3, dimater);
                String sumWeight="";
                String balanceNoQuery="";
                rs = ps.executeQuery();
                if (rs.next()) {
                	sumWeight=rs.getString("ACT_WEIGHT");
                	balanceNoQuery=rs.getString("BALANCE_NO");
                }
                rs.close();
                ps.close();
                if(balanceNoQuery!=null){
            		cro.setV_errMsg("数据已结转，请刷新后重试！");
                    cro.setV_errCode(-1);
                    con.rollback();
                    return cro;
            	}
            	weight+=Double.parseDouble(sumWeight);
            	pstaOutInfo.setString(1,balanceNo);
            	pstaOutInfo.setString(2,outNo);
            	pstaOutInfo.setString(3,gradeCode);
            	pstaOutInfo.setObject(4, dimater);
            	pstaOutInfo.executeUpdate();
            }
        	pstaGlInfo.setString(1,balanceNo);
        	pstaGlInfo.setString(2,head.getGlOrderNo());
        	pstaGlInfo.setString(3,head.getOrderTyp());
        	pstaGlInfo.setString(4,head.getOrderTypDesc());
        	pstaGlInfo.setString(5,"123401");
        	pstaGlInfo.setString(6,"应收");
        	pstaGlInfo.setString(7,"123301");
        	pstaGlInfo.setString(8,"货款");
        	pstaGlInfo.setString(9,"0");
        	pstaGlInfo.setString(10,head.getSupplyUnit());
        	pstaGlInfo.setString(11,head.getCustomerNo());
        	pstaGlInfo.setString(12,head.getCustomerNm());
        	pstaGlInfo.setString(13,head.getStlmtUnitNo());
        	pstaGlInfo.setString(14,head.getStlmtUnitNm());
        	pstaGlInfo.setString(15,head.getSignDate().toString());
        	pstaGlInfo.setString(16,head.getOrderYear());
        	pstaGlInfo.setString(17,userId);
        	pstaGlInfo.setString(18,(String.valueOf(Double.parseDouble(head.getBalncePrice())*weight)));
        	pstaGlInfo.setString(19,"");
        	pstaGlInfo.setString(20,"1");
        	pstaGlInfo.setString(21,ordLnPk);
        	pstaGlInfo.setString(22,head.getOrderNo());
			pstaGlInfo.executeUpdate();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
    		try {
    			con.rollback();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		cro.setV_errMsg("程序处理失败！" + ex.getMessage());
    		cro.setV_errCode(-1);
    		return cro;
    	}finally{
    		if(con != null){
    			try {
    				con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
    
    /**
     * 保存模板
     * @param parm 合同头参数
     * @return
     */
    public CoreReturnObject doCancelCarryGp(String balanceNo) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String cancelCarry = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.doCancelCarry"); // 新增关联交易表
            String updateOutInfo = CoreXmlSqlParsersFactory
					.getSql("GlOrderManage.updateOutInfoBalanceGp"); // 修改出库表信息
			PreparedStatement pstaOutInfo = con.prepareStatement(updateOutInfo);
			pstaOutInfo.setString(1,balanceNo);
        	
			PreparedStatement pstaCancel = con.prepareStatement(cancelCarry);
			pstaCancel.setString(1,balanceNo);
			
			pstaOutInfo.executeUpdate();
        	pstaCancel.executeUpdate();
            con.commit();
            cro.setV_errCode(0);
            return cro;
        }
        catch(Exception ex){
			try {
				con.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			cro.setV_errMsg("程序处理失败！" + ex.getMessage());
			cro.setV_errCode(-1);
			return cro;
		}finally{
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
    }
    
}
