package com.steering.pss.plnsaleord.order.baseMethod;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

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

public class CoreDefGourp extends DBCommon{
    /**
     * 查询二级工序分类
     * @return
     */
    public CoreReturnObject queryProcessStyle(String str){
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreDefGourp.queryProcessStyle", new Object[]{ str });
        return cro;
    }
    
    /**
     * 查询扣型分类
     * @return
     */
    public CoreReturnObject queryModelStyle(String str){
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreDefGourp.queryModelStyle", new Object[]{ str });
        return cro;
    }

    /**
     * 查询工序评审产线分组
     * @param desginKey
     * @param mscPline
     * @param processCode
     * @return
     */
    public CoreReturnObject queryProcessPlineGroup(String desginKey,
            String mscPline, String processCode) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreDefGourp.queryProcessPlineGroup",
                new Object[] { desginKey, mscPline, processCode });
        return cro;
    }
    
    
    
    /**
     * 获取加工优先分组
     * @param conn
     * @param desginKey
     * @param mscPline
     * @param bom
     * @return
     * @throws SQLException
     */
    public static String getJgxDefGroup(Connection conn,String desginKey,
            String mscPline, String bom) throws SQLException{
        PreparedStatement ps = null;
        String sqlProc = CoreXmlSqlParsersFactory
                .getSql("CoreDefGourp.queryDesignMscplprc");
        String sqlQ = CoreXmlSqlParsersFactory
                .getSql("CoreDefGourp.queryDesginBomI");
        String sqlDef = CoreXmlSqlParsersFactory
                .getSql("CoreDefGourp.selectJgxDefGroup");
        String groupJgx = "/";

        ps = conn.prepareStatement(sqlProc);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, "G");
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            groupJgx = "200407";
            PreparedStatement pst = conn.prepareStatement(sqlQ);
            pst.setObject(1, bom);
            ResultSet rst = pst.executeQuery();
            if (rst.next()) {
                if (rst.getString("IN_MATERIAL_NO") != null
                        && rst.getString("OUT_MATERIAL_NO") != null) {
                    String inMaterialNo = rst.getString("IN_MATERIAL_NO");
                    String outMaterialNo = rst.getString("OUT_MATERIAL_NO");
                    PreparedStatement psta = conn.prepareStatement(sqlDef);
                    psta.setObject(1, inMaterialNo);
                    psta.setObject(2, outMaterialNo);
                    psta.setObject(3, desginKey);
                    psta.setObject(4, mscPline);
                    ResultSet rsta = psta.executeQuery();
                    if (rsta.next()) {
                        if (rsta.getString("GROUP_PLAN") != null) {
                            groupJgx = rsta.getString("GROUP_PLAN");
                        }
                    }
                    rsta.close();
                    psta.close();
                }
            }
            rst.close();
            pst.close();
        }
        rs.close();
        ps.close();
        return groupJgx;
    }
    
    
    /**
     * 获取热处理优先分组
     * @param conn
     * @param desginKey
     * @param mscPline
     * @param bom
     * @return
     * @throws SQLException
     */
    public static String getRclDefGroup(Connection conn,String desginKey,
                                        String mscPline, String bom) throws SQLException {
        return getRclDefGroup(conn,desginKey,mscPline,bom,"");
    }
    public static String getRclDefGroup(Connection conn,String desginKey,
            String mscPline, String bom, String supp) throws SQLException{
        PreparedStatement ps = null;
        String sqlProc = CoreXmlSqlParsersFactory
                .getSql("CoreDefGourp.queryDesignMscplprc");
        String sqlQ = CoreXmlSqlParsersFactory
                .getSql("CoreDefGourp.queryDesginBomF");
        String sqlDef = CoreXmlSqlParsersFactory
                .getSql("CoreDefGourp.selectRclDefGroup");
        if (StringUtils.isEmpty(supp)) {
            sqlDef = StringTool.ReplaceAll(sqlDef, "#SUPPLY_UNIT_CODE#", "");
        } else {
            sqlDef = StringTool.ReplaceAll(sqlDef, "#SUPPLY_UNIT_CODE#", " AND P.SUPPLY_UNIT_CODE = '" + supp + "'" );
        }
        String groupRcl = "/";

        ps = conn.prepareStatement(sqlProc);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, "F");
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            groupRcl = "200501";
            PreparedStatement pst = conn.prepareStatement(sqlQ);
            pst.setObject(1, bom);
            ResultSet rst = pst.executeQuery();
            if (rst.next()) {
                if (rst.getString("F_IN_MATERIAL_NO") != null
                        && rst.getString("F_OUT_MATERIAL_NO") != null) {
                    String inMaterialNo = rst.getString("F_IN_MATERIAL_NO");
                    String outMaterialNo = rst.getString("F_OUT_MATERIAL_NO");
                    PreparedStatement psta = conn.prepareStatement(sqlDef);
                    psta.setObject(1, inMaterialNo);
                    psta.setObject(2, outMaterialNo);
                    psta.setObject(3, desginKey);
                    psta.setObject(4, mscPline);

                    ResultSet rsta = psta.executeQuery();
                    if (rsta.next()) {
                        if (rsta.getString("GROUP_PLAN") != null) {
                            groupRcl = rsta.getString("GROUP_PLAN");
                        }
                    }
                    rsta.close();
                    psta.close();
                }
            }
            rst.close();
            pst.close();
            if(StringUtils.isEmpty(supp) && "120504".equals(supp) && !"200506".equals(groupRcl)){
                groupRcl = "200506";
            }
        }
        rs.close();
        ps.close();
        return groupRcl;
    }
    
    
    /**
     * 获取加工优先分组
     * @param desginKey
     * @param mscPline
     * @param bom
     * @return
     */
    public CoreReturnObject selectJgxDefGroup(String desginKey,
            String mscPline, String bom) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            cro.setResult(getJgxDefGroup(conn, desginKey, mscPline, bom));
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }
    
    /**
     * 获取热处理优先分组
     * @param desginKey
     * @param mscPline
     * @param bom
     * @return
     */
    public CoreReturnObject selectRclDefGroup(String desginKey,
            String mscPline, String bom) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            cro.setResult(getRclDefGroup(conn, desginKey, mscPline, bom));
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }
    
    
    
    
    
    
    
}