package com.steering.pss.plan.order;

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

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

import com.steering.comm.db.DBCommon;
import com.steering.pss.plan.order.model.PlnOrderZgEntity;

/**
 * 备料下发工序合同
 * @author 杨天海
 * @version 1.1
 */
public class CoreBlPrcOrder extends DBCommon{
    private ArrayList<String> sqllist = new ArrayList<String>();
    private ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
    private String createName = "";
    
    /**
     * 构造
     * @param name
     */
    public CoreBlPrcOrder(String name){
        createName = name;
    }
    
    /**
     * sqlID集合
     * @return the sqllist
     */
    public ArrayList<String> getSqllist() {
        return sqllist;
    }
    /**
     * sqlID集合
     * @param sqllist the sqllist to set
     */
    public void setSqllist(ArrayList<String> sqllist) {
        this.sqllist = sqllist;
    }
    /**
     * 参数集合
     * @return the parmlist
     */
    public ArrayList<Object[]> getParmlist() {
        return parmlist;
    }
    /**
     * 参数集合
     * @param parmlist the parmlist to set
     */
    public void setParmlist(ArrayList<Object[]> parmlist) {
        this.parmlist = parmlist;
    }
    
    /**
     * 备料下发创建工序合同
     * @param reqBlId
     */
    public void creatProcess(String reqBlId){
        Connection conn = null;
        PreparedStatement psta = null;
        try{
            String sql = CoreXmlSqlParsersFactory.getSql("CorePrcOrder.queryBLbyReqBlId");
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            
            psta = conn.prepareStatement(sql);
            psta.setString(1, reqBlId);
            ResultSet result = psta.executeQuery();
            while(result.next()){
                double planWeight = result.getDouble("JGBL_REQ_WEIGHT");//排产量（t）
                PlnOrderZgEntity plnOrderZg = new PlnOrderZgEntity();
                plnOrderZg.setOrderSource("备料需求");
                plnOrderZg.setOrderNo(result.getString("ORDER_NO"));
                plnOrderZg.setOrderSeq(result.getString("ORDER_SEQ"));
                plnOrderZg.setDeliveryNo(result.getString("DELIVERY_NO"));
                plnOrderZg.setReqBlId(result.getString("REQ_BL_ID"));
                plnOrderZg.setProcessSeq("1");
                plnOrderZg.setSteelcode(result.getString("STEELCODE"));
                plnOrderZg.setSteelname(result.getString("STEELNAME"));
                plnOrderZg.setOutdiameter(result.getString("OUTDIAMETER"));
                plnOrderZg.setWallthick(result.getString("WALLTHICK"));
                plnOrderZg.setLengthmin(result.getString("LENGTHMIN"));
                plnOrderZg.setLengthmax(result.getString("LENGTHMAX"));
                plnOrderZg.setGetrate("1");
                plnOrderZg.setOrderweight(Double.toString(planWeight));
                plnOrderZg.setOrdernum(result.getString("JGBL_REQ_NUM"));
                plnOrderZg.setOrderstatus("0");
                plnOrderZg.setCreateName(createName);
                
                Object[] param = new Object[]{
                        plnOrderZg.getOrderSource(),
                        plnOrderZg.getOrderNo(),
                        plnOrderZg.getOrderSeq(),
                        plnOrderZg.getDeliveryNo(),
                        plnOrderZg.getReqBlId(),
                        plnOrderZg.getProcessSeq(),
                        plnOrderZg.getSteelcode(),
                        plnOrderZg.getSteelname(),
                        plnOrderZg.getOutdiameter(),
                        plnOrderZg.getWallthick(),
                        plnOrderZg.getLengthmax(),
                        plnOrderZg.getLengthmin(),
                        plnOrderZg.getOutwlId(),
                        plnOrderZg.getInwlGp(),
                        plnOrderZg.getGetrate(),
                        plnOrderZg.getOrderweight(),
                        plnOrderZg.getOrdernum(),
                        plnOrderZg.getProdEndDate(),
                        plnOrderZg.getOrderstatus(),
                        plnOrderZg.getIndexSeq(),
                        plnOrderZg.getIsendgx(),
                        plnOrderZg.getNextGx(),
                        plnOrderZg.getIfMonitorZg(),
                        plnOrderZg.getPrdcrNo(),
                        plnOrderZg.getCreateName()
                };
                sqllist.add("CorePrcOrder.addSlaeZgPrc");
                parmlist.add(param);
            }
        }
        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();
            }
        }
    }
}