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;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plan.order.model.PlnSaleordProcEntity;

/**
 * 销售合同接收下发
 * @author xiaohai
 */
public class CoreSaleOrderReceive extends DBCommon {
    /**
     * 查询
     * @param list
     * @return
     */
    public CoreReturnObject querySaleOrder(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreSaleOrderReceive.querySaleOrder", list
                .toArray());
        return cro;
    }
    
    /**
     * 查询BYID
     * @param list
     * @return
     */
    public CoreReturnObject querySaleOrder(String ordLnDlyPk, String plnDivideId) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID(
                "CoreSaleOrderReceive.querySaleOrderProcById", new Object[] {
                        ordLnDlyPk, plnDivideId });
        return cro;
    }

    /**
     * 销售合同查询制程
     * @param desginKey
     * @return
     */
    public CoreReturnObject queryMscPline(String desginKey) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreSaleOrderReceive.queryMscPline",
                new Object[] { desginKey });
        return cro;
    }

    /**
     * 销售合同查询制程适用钢种
     * @param list
     * @return object
     */
    public CoreReturnObject queryMscPlGz(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreSaleOrderReceive.queryMscPlGz", list
                .toArray());
        return cro;
    }

    /**
     * 销售合同查询制程工序点路径
     * @param list
     * @return object
     */
    public CoreReturnObject queryPlStation(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreSaleOrderReceive.queryPlStation", list
                .toArray());
        return cro;
    }

    /**
     * 查询拆分合同
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     */
    public CoreReturnObject querySaleProcStatus(String ordLnDlyPk,
            String plnDivideId) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreSaleOrderReceive.querySaleProcStatus",
                new Object[] { ordLnDlyPk, plnDivideId });
        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("CoreSaleOrderReceive.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(
                        "CoreSaleOrderReceive.getBomCustomRDpl", list.toArray());
            }
            else {
                list.remove(1);
                list.remove(0);
                cro = this.getDataBySqlID(
                        "CoreSaleOrderReceive.getBomMaterialDpl", list.toArray());
            }
            rs.close();
            ps.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();
            }
        }
        return cro;
    }

    /**
     * 更改Bom和轧管产线
     * @param ordProcEntity
     * @return
     */
    public CoreReturnObject updateSaleordProcBomDp(PlnSaleordProcEntity ordProcEntity) {
        CoreReturnObject cro = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreSaleOrderReceive.updateSaleordProcBomDp");
        String sqlUp = EntityTool.fillSqlByEntity(sql, ordProcEntity);
        cro = this.setData(sqlUp);
        return cro;
    }

}