package com.steering.pss.plnsaleord.processOrder;

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 CorePlnProcOrdFinish extends DBCommon {

    /**
     * 获取排产工序是否已经生产
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     */
    public CoreReturnObject getOrdFistProFinishWt(String ordLnDlyPk,
            String plnDivideId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            String fistProcCode = "";
            double finishWt = 0;
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CorePlnProcOrdFinish.getPinOrdProcessCode");
            ps = conn.prepareStatement(sql);
            ps.setObject(1, ordLnDlyPk);
            ps.setObject(2, plnDivideId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                if (rs.getString("PROCESS_CODE") != null) {
                    fistProcCode = rs.getString("PROCESS_CODE");
                    
                    if (fistProcCode.trim().equals("D")) {
                        finishWt = CorePlnProcOrdFinish.getZgOrdFinishWt(conn,
                                ordLnDlyPk, plnDivideId);
                        if (finishWt > 0) {
                            break;
                        }
                    }
                    else if (fistProcCode.trim().equals("E")) {
                        finishWt = CorePlnProcOrdFinish.getDbkOrdFinishWt(conn,
                                ordLnDlyPk, plnDivideId);
                        if (finishWt > 0) {
                            break;
                        }
                    }
                    else if (fistProcCode.trim().equals("F")) {
                        finishWt = CorePlnProcOrdFinish.getRclOrdFinishWt(conn,
                                ordLnDlyPk, plnDivideId);
                        if (finishWt > 0) {
                            break;
                        }
                    }
                    else if (fistProcCode.trim().equals("G")) {
                        finishWt = CorePlnProcOrdFinish.getJgxOrdFinishWt(conn,
                                ordLnDlyPk, plnDivideId);
                        if (finishWt > 0) {
                            break;
                        }
                    }
                }
            }
            rs.close();
            ps.close();
            if (finishWt > 0) {
                cro.setResult(true);
            }
            else {
                cro.setResult(false);
            }
            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 conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     * @throws SQLException
     */
    public static double getZgOrdFinishWt(Connection conn, String ordLnDlyPk,
            String plnDivideId) throws SQLException {
        PreparedStatement ps = null;
        double finishWt = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnProcOrdFinish.getZgOrdFinishWt");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.setObject(2, plnDivideId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            finishWt = rs.getDouble("FINISHWEIGHT");
        }
        rs.close();
        ps.close();
        return finishWt;
    }

    /**
     * 镦拔扩工序完成量
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     * @throws SQLException
     */
    public static double getDbkOrdFinishWt(Connection conn, String ordLnDlyPk,
            String plnDivideId) throws SQLException {
        PreparedStatement ps = null;
        double finishWt = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnProcOrdFinish.getDbkOrdFinishWt");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.setObject(2, plnDivideId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            finishWt = rs.getDouble("FINISHWEIGHT");
        }
        rs.close();
        ps.close();
        return finishWt;
    }

    /**
     * 热处理工序完成量
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     * @throws SQLException
     */
    public static double getRclOrdFinishWt(Connection conn, String ordLnDlyPk,
            String plnDivideId) throws SQLException {
        PreparedStatement ps = null;
        double finishWt = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnProcOrdFinish.getRclOrdFinishWt");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.setObject(2, plnDivideId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            finishWt = rs.getDouble("FINISHWEIGHT");
        }
        rs.close();
        ps.close();
        return finishWt;
    }

    /**
     * 加工工序完成量
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @return
     * @throws SQLException
     */
    public static double getJgxOrdFinishWt(Connection conn, String ordLnDlyPk,
            String plnDivideId) throws SQLException {
        PreparedStatement ps = null;
        double finishWt = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CorePlnProcOrdFinish.getJgxOrdFinishWt");
        ps = conn.prepareStatement(sql);
        ps.setObject(1, ordLnDlyPk);
        ps.setObject(2, plnDivideId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            finishWt = rs.getDouble("FINISHWEIGHT");
        }
        rs.close();
        ps.close();
        return finishWt;
    }

}