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.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;

/**
 * 合同质量设计bom信息查询
 * @author 杨天海    date 2015-09-08
 */
public class CoreOrderDesignBom extends DBCommon{
    /**
     * 查询bom主表
     * @param list 
     * @return object
     */
    public CoreReturnObject getOrdDesignBom(ArrayList<String> list){
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrderDesignBom.getOrdDesignBom", list.toArray());
        return cro;
    }
    
    /**
     * 验证规格是否存在
     * @param list 
     * @return object
     */
    public CoreReturnObject getBaseSpec(String diameter, String wallthick){
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrderDesignBom.getBaseSpec", new Object[]{ diameter, wallthick });
        return cro;
    }
    
    /**
     * 查询bom主表
     * @param list 
     * @return object
     */
    public CoreReturnObject getOrdMscBom(ArrayList<String> list){
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrderDesignBom.getOrdMscBom", list.toArray());
        return cro;
    }
    
    /**
     * 查询轧管小时能力表
     * @return object
     */
    public CoreReturnObject getBomMaterialDpl(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();

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

            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderDesignBom.getCustomRpline");
            ps = conn.prepareStatement(sql);
            ps.setString(1, list.get(0).toString());
            ps.setString(2, list.get(1).toString());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                cro = this.getDataBySqlID(
                        "CoreOrderDesignBom.getBomCustomRDpl", list.toArray());
            }
            else {
                Object[] param = new Object[] { list.get(2), list.get(3),
                        list.get(4), list.get(5) };
                cro = this.getDataBySqlID(
                        "CoreOrderDesignBom.getBomMaterialDpl", param);
            }
            rs.close();
            ps.close();
        }
        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();
            }
        }
        return cro;
    }
    
    /**
     * 查询bom主表
     * @return object
     */
    public CoreReturnObject getDesignBomInf(String bom){
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrderDesignBom.getDesignBomInf", new Object[]{ bom } );
        return cro;
    }
    
    /**
     * 保存选取bom后的信息
     * @param list 
     * @return object
     */
    public CoreReturnObject setSaleOrderBom(ArrayList<String> list){
        CoreReturnObject cro = null;
        cro = this.setDataBySqlID("CoreOrderDesignBom.setSaleOrderBom", list.toArray());
        return cro;
    }
    
    /**
     * 保存更改制程后的信息
     * @param list 
     * @return object
     */
    public CoreReturnObject setBomChanged(ArrayList<String> list){
        CoreReturnObject cro = null;
        cro = this.setDataBySqlID("CoreOrderDesignBom.setBomChanged", list.toArray());
        return cro;
    }
    
    /**
     * 修改销售合同状态
     * @param list 
     * @return object
     */
    public CoreReturnObject setModifySale(ArrayList<ArrayList<String>> list,
            ArrayList<ArrayList<String>> listLine){
        CoreReturnObject cro = null;
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CoreOrderDesignBom.setModifySale");
            parmlist.add(list.get(i).toArray());
            
            sqllist.add("CoreOrderDesignBom.setLineStatus");
            parmlist.add(listLine.get(i).toArray());
        }
        cro = this.setDataById(sqllist, parmlist);
        return cro;
    }
    
}