package com.steering.pss.plnsaleord.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
     * @param checkDbk
     * @param checkZgPline
     * @return
     */
    public CoreReturnObject getOrdDesignBom(ArrayList<String> list,
            String checkDbk, String checkZgPline) {
        CoreReturnObject cro = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdDesginBom.getOrdDesignBom");
        if (!checkDbk.equals("")) {
            sql = sql + " AND T.E_IN_MATERIAL_NO = '" + checkDbk + "'";
        }
        if (!checkZgPline.equals("")) {
            sql = sql
                    + " AND '"
                    + checkZgPline
                    + "' IN (SELECT B.PLINE_CODE FROM SLM_BASE_MATERIAL_D_PL B "
                    + "WHERE B.IN_MATERIAL_NO = T.D_IN_MATERIAL_NO AND B.OUT_MATERIAL_NO = T.D_OUT_MATERIAL_NO "
                    + "AND (B.DISABLE_FL!='1' OR B.DISABLE_FL IS NULL))";
        }
        cro = this.getData(sql, list.toArray());
        return cro;
    }

    /**
     * 合同行Bom
     * @param designKey
     * @param checkDbk
     * @param checkZgPline
     * @return
     */
    public CoreReturnObject getOrdLineDesignBom(String designKey,
            String checkDbk, String checkZgPline) {
        CoreReturnObject cro = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdDesginBom.getOrdLineDesignBom");
        if (!checkDbk.equals("")) {
            sql = sql + " AND T.E_IN_MATERIAL_NO = '" + checkDbk + "'";
        }
        if (!checkZgPline.equals("")) {
            sql = sql
                    + " AND '"
                    + checkZgPline
                    + "' IN (SELECT B.PLINE_CODE FROM SLM_BASE_MATERIAL_D_PL B "
                    + "WHERE B.IN_MATERIAL_NO = T.D_IN_MATERIAL_NO AND B.OUT_MATERIAL_NO = T.D_OUT_MATERIAL_NO "
                    + "AND (B.DISABLE_FL!='1' OR B.DISABLE_FL IS NULL))";
        }
        cro = this.getData(sql, new Object[] { designKey });
        return cro;
    }

    /**
     * 验证规格是否存在
     * @param list
     * @return object
     */
    public CoreReturnObject getBaseSpec(String diameter, String wallthick) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrdDesginBom.getBaseSpec", new Object[] {
                diameter, wallthick });
        return cro;
    }

    /**
     * 查询bom主表
     * @param list
     * @return object
     */
    public CoreReturnObject getOrdMscBom(ArrayList<String> list) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrdDesginBom.getOrdMscBom", list
                .toArray());
        return cro;
    }

    /**
     * 查询bom主表
     * @param desginKey
     * @return object
     */
    public CoreReturnObject getOrdLineMscBom(
            String desginKey, String strMsc, String strMscPline) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrdDesginBom.getOrdLineMscBom",
                new Object[] { desginKey, strMsc, strMscPline });
        return cro;
    }
    /**
     * 查询默认钢种
     */
    public CoreReturnObject getGradeCode(
            String gradeCode, String outsBillet) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrdDesginBom.getGradeCode",
                new Object[] {gradeCode, outsBillet });
        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("CoreOrdDesginBom.getCustomRpline");
            ps = conn.prepareStatement(sql);
            ps.setString(1, list.get(4).toString());
            ps.setString(2, list.get(5).toString());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                cro = this.getDataBySqlID("CoreOrdDesginBom.getBomCustomRDpl",
                        list.toArray());
            }
            else {
                Object[] param = new Object[] { list.get(0), list.get(1),
                        list.get(2), list.get(3) };
                cro = this.getDataBySqlID("CoreOrdDesginBom.getBomMaterialDpl",
                        param);
            }
            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主表
     * @return object
     */
    public CoreReturnObject getDesignBomInf(String bom) {
        CoreReturnObject cro = null;
        cro = this.getDataBySqlID("CoreOrdDesginBom.getDesignBomInf",
                new Object[] { bom });
        return cro;
    }

    /**
     * 修改销售合同状态
     * @param list
     * @return object
     */
    public CoreReturnObject setModifySale(ArrayList<ArrayList<String>> list,
            ArrayList<ArrayList<String>> listLine) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        PreparedStatement psta = null;
        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            for (int i = 0; i < list.size(); i++) {
                String sqlUp = CoreXmlSqlParsersFactory
                        .getSql("CoreOrdDesginBom.setModifySale");
                String sqlUpPrc = CoreXmlSqlParsersFactory
                        .getSql("CoreOrdDesginBom.setModifySaleProc");
                String sqlDelivery = CoreXmlSqlParsersFactory
                        .getSql("CoreOrdDesginBom.setLineDeliveryStatus");
                String status = list.get(i).get(0);
                String ordLnDlyPk = list.get(i).get(1);
                String ordLnPk = "";// 和同行pk
                ordLnPk = ordLnDlyPk.substring(0, ordLnDlyPk.length() - 3);

                psta = conn.prepareStatement(sqlUp);
                psta.setObject(1, list.get(i).get(0));
                psta.setObject(2, ordLnDlyPk);
                psta.executeUpdate();
                psta.close();

                psta = conn.prepareStatement(sqlUpPrc);
                psta.setObject(1, list.get(i).get(0));
                psta.setObject(2, ordLnDlyPk);
                psta.executeUpdate();
                psta.close();

                psta = conn.prepareStatement(sqlDelivery);
                psta.setObject(1, listLine.get(i).get(0));
                psta.setObject(2, ordLnDlyPk);
                psta.executeUpdate();
                psta.close();

                if (status.trim().equals("4")) {
                    // 和同行生产状态设置
                    setOrdLineFinishSc(conn, ordLnPk);
                }
                if (status.trim().equals("5")) {
                    // 和同行缴库状态设置
                    setOrdLineFinishJk(conn, ordLnPk);
                }
            }
            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());
        }
        catch (Exception ex) {
            cro.setV_errCode(-1);
            cro.setV_errMsg(ex.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 设置合同行生产完成
     * @param ordLnPk 合同行Pk
     * @throws SQLException
     */
    public void setOrdLineFinishSc(Connection conn, String ordLnPk)
            throws SQLException {
        PreparedStatement psta = null;
        int count1 = 0;
        int count2 = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdDesginBom.getNumDeliveryByLine");
        String sqlUp = CoreXmlSqlParsersFactory
                .getSql("CoreOrdDesginBom.setOrdLineStatus");
        psta = conn.prepareStatement(sql);
        psta.setObject(1, ordLnPk);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            count1 = rs.getInt("DELIVERY_NUM");
        }
        rs.close();
        psta.close();

        sql = sql + " AND SUBSTR(T.DELIVERY_STATUS, 0, 6) = '120205'";
        psta = conn.prepareStatement(sql);
        psta.setObject(1, ordLnPk);
        rs = psta.executeQuery();
        if (rs.next()) {
            count2 = rs.getInt("DELIVERY_NUM");
        }
        rs.close();
        psta.close();

        if (count2 > 0 && count1 > 0 && count1 > count2) {
            // 已开始生产
            psta = conn.prepareStatement(sqlUp);
            psta.setObject(1, "12020501");
            psta.setObject(2, ordLnPk);
            psta.executeUpdate();
            psta.close();
        }
        if (count2 > 0 && count1 > 0 && count1 == count2) {
            // 生产完成
            psta = conn.prepareStatement(sqlUp);
            psta.setObject(1, "12020502");
            psta.setObject(2, ordLnPk);
            psta.executeUpdate();
            psta.close();
        }
    }

    /**
     * 设置合同行缴库完成
     * @param ordLnPk 合同行Pk
     * @throws SQLException
     */
    public void setOrdLineFinishJk(Connection conn, String ordLnPk)
            throws SQLException {
        PreparedStatement psta = null;
        int count1 = 0;
        int count2 = 0;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdDesginBom.getNumDeliveryByLine");
        String sqlUp = CoreXmlSqlParsersFactory
                .getSql("CoreOrdDesginBom.setOrdLineStatus");
        psta = conn.prepareStatement(sql);
        psta.setObject(1, ordLnPk);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            count1 = rs.getInt("DELIVERY_NUM");
        }
        rs.close();
        psta.close();

        sql = sql + " AND SUBSTR(T.DELIVERY_STATUS, 0, 6) = '120206'";
        psta = conn.prepareStatement(sql);
        psta.setObject(1, ordLnPk);
        rs = psta.executeQuery();
        if (rs.next()) {
            count2 = rs.getInt("DELIVERY_NUM");
        }
        rs.close();
        psta.close();

        if (count2 > 0 && count1 > 0 && count1 > count2) {
            // 已开始生产
            psta = conn.prepareStatement(sqlUp);
            psta.setObject(1, "12020601");
            psta.setObject(2, ordLnPk);
            psta.executeUpdate();
            psta.close();
        }
        if (count2 > 0 && count1 > 0 && count1 == count2) {
            // 生产完成
            psta = conn.prepareStatement(sqlUp);
            psta.setObject(1, "12020602");
            psta.setObject(2, ordLnPk);
            psta.executeUpdate();
            psta.close();
        }
    }

}