package com.steering.pss.plnsaleord.order;

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

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

import com.steering.comm.db.DBCommon;


/**
 * 监制信息
 * @author xiaohai
 *
 */
public class CoreOrdLinePrdcrLst extends DBCommon {
    /**
     * 获取监制工序
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     */
    public CoreReturnObject queryOrdPrdcrproPk(String ordLnDlyPk,
            String plnDivideId){
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrdLinePrdcrLst.getOrdPrdcrNoMscPline");
            psta = conn.prepareStatement(sql);
            psta.setObject(1, ordLnDlyPk);
            psta.setObject(2, plnDivideId);
            ResultSet rsSet = psta.executeQuery();
            if (rsSet.next()) {
                if (rsSet.getString("PRDCR_NO") != null
                        && rsSet.getString("INDEX_SEQ") != null) {
                    String prdcrNo = rsSet.getString("PRDCR_NO");
                    String mscPline = rsSet.getString("INDEX_SEQ");
                    String prdcrpro = getOrdPrdcrpro(conn,prdcrNo,mscPline);
                    cro.setResult(prdcrpro);
                }
            }
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }
    
    /**
     * 获取监制工序
     * @param prdcrNo
     * @param mscPline
     * @return
     */
    public CoreReturnObject queryOrdPrdcrpro(String prdcrNo,
            String mscPline){
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            
            String prdcrpro = getOrdPrdcrpro(conn,prdcrNo,mscPline);
            cro.setResult(prdcrpro);
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }
    
    /**
     * 获取监制工序
     * @param prdcrNo
     * @param mscPline
     * @throws SQLException
     */
    public static String getOrdPrdcrpro(Connection conn, String prdcrNo,
            String mscPline) throws SQLException {
        String prdcrpro = "";
        PreparedStatement ps = null;
        String sqlGt = CoreXmlSqlParsersFactory
                .getSql("CoreOrdLinePrdcrLst.getOrdPrdcrproGt");
        String sqlJg = CoreXmlSqlParsersFactory
                .getSql("CoreOrdLinePrdcrLst.getOrdPrdcrproJg");
        String sqlBl = CoreXmlSqlParsersFactory
                .getSql("CoreOrdLinePrdcrLst.getOrdPrdcrproBl");
        ps = conn.prepareStatement(sqlGt);
        ps.setObject(1, prdcrNo);
        ps.setObject(2, mscPline);
        ps.setObject(3, prdcrNo);
        ps.setObject(4, mscPline);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("PRDCRPRO") != null
                    && !rs.getString("PRDCRPRO").equals("")) {
                prdcrpro = "管体(" + rs.getString("PRDCR_DESC") + ":"
                        + rs.getString("PRDCRPRO") + ")";
            }
        }
        rs.close();
        ps.close();

        ps = conn.prepareStatement(sqlJg);
        ps.setObject(1, prdcrNo);
        ps.setObject(2, mscPline);
        ps.setObject(3, prdcrNo);
        ps.setObject(4, mscPline);
        rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("PRDCRPRO") != null
                    && !rs.getString("PRDCRPRO").equals("")) {
                prdcrpro += ";接箍(" + rs.getString("PRDCR_DESC") + ":"
                        + rs.getString("PRDCRPRO") + ")";
            }
        }
        rs.close();
        ps.close();

        ps = conn.prepareStatement(sqlBl);
        ps.setObject(1, prdcrNo);
        ps.setObject(2, mscPline);
        ps.setObject(3, prdcrNo);
        ps.setObject(4, mscPline);
        rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("PRDCRPRO") != null
                    && !rs.getString("PRDCRPRO").equals("")) {
                prdcrpro += ";备料(" + rs.getString("PRDCR_DESC") + ":"
                        + rs.getString("PRDCRPRO") + ")";
            }
        }
        rs.close();
        ps.close();
        return prdcrpro;
    }
    
    /**
     * 获取监制工序
     * @param ordLnDlyPk
     * @param plnDivideId
     * @throws SQLException
     */
    public static String getOrdPrdcrproPk(Connection conn, String ordLnDlyPk,
            String plnDivideId) throws SQLException {
        String prdcrpro = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdLinePrdcrLst.getOrdPrdcrNoMscPline");
        PreparedStatement psta = conn.prepareStatement(sql);
        psta.setObject(1, ordLnDlyPk);
        psta.setObject(2, plnDivideId);
        ResultSet rsSet = psta.executeQuery();
        if (rsSet.next()) {
            if (rsSet.getString("PRDCR_NO") != null
                    && rsSet.getString("INDEX_SEQ") != null) {
                String prdcrNo = rsSet.getString("PRDCR_NO");
                String mscPline = rsSet.getString("INDEX_SEQ");
                prdcrpro = getOrdPrdcrpro(conn, prdcrNo, mscPline);
            }
        }
        psta.close();
        rsSet.close();
        return prdcrpro;
    }

    /**
     * 更新监制工序
     * @param ordLnDlyPk
     * @param plnDivideId
     * @throws SQLException
     */
    public static boolean updateOrdPrdcrproPk(Connection conn, String ordLnDlyPk,
            String plnDivideId) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdLinePrdcrLst.updateOrdPrdcrPro");
        String prdcrpro = getOrdPrdcrproPk(conn, ordLnDlyPk, plnDivideId);
        PreparedStatement psta = conn.prepareStatement(sql);
        psta.setObject(1, prdcrpro);
        psta.setObject(2, ordLnDlyPk);
        psta.setObject(3, plnDivideId);
        psta.executeUpdate();
        psta.close();
        return true;
    }
    
    /**
     * 合同产品生产监制要求监制从表信息单位绑定
     * @param order 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderProducerPdeptBase() {
        return this
                .getDataBySqlID("CoreOrdLinePrdcrLst.CoreOrderProducerPdeptBase");
    }

    /**
     * 合同产品生产监制要求监制从表信息监制类型绑定
     * @param order 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderProducerPrcBase() {
        return this
                .getDataBySqlID("CoreOrdLinePrdcrLst.CoreOrderProducerPrcBase");
    }

    /**
     * 获取监制主表信息
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     */
    public CoreReturnObject getOrderLinePrdcr(String ordLnDlyPk,
            String plnDivideId) {
        Object[] obj = new Object[] { ordLnDlyPk, plnDivideId };
        return this.getDataBySqlID("CoreOrdLinePrdcrLst.SelectOrderLinePrdcr",
                obj);
    }

    /**
     * 获取监制从表信息
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     */
    public CoreReturnObject getOrderLinePrdcrLst(String ordLnDlyPk,
            String plnDivideId) {
        Object[] obj = new Object[] { ordLnDlyPk, plnDivideId };
        return this.getDataBySqlID(
                "CoreOrdLinePrdcrLst.SelectOrderLinePrdcrLst", obj);
    }
    
    /**
     * 获取监制主表信息
     * @param prdcrNo
     * @param plnDivideId
     * @return
     */
    public CoreReturnObject getGxJgMPrdcr(String prdcrNo, String codeJg) {
        Object[] obj = new Object[] { prdcrNo, codeJg };
        return this.getDataBySqlID("CoreOrdLinePrdcrLst.getGxJgMPrdcr", obj);
    }

    /**
     * 获取监制从表信息
     * @param prdcrNo
     * @param codeJg
     * @return
     */
    public CoreReturnObject getGxJgMPrdcrLst(String prdcrNo, String codeJg) {
        Object[] obj = new Object[] { prdcrNo, codeJg };
        return this.getDataBySqlID("CoreOrdLinePrdcrLst.getGxJgMPrdcrLst", obj);
    }
}