package com.steering.pss.plnsaleord.processOrder;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import com.steering.comm.db.DBCommon;
import com.steering.pss.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.processOrder.base.ProducHelperBean;

/**
 * 工序生产完毕
 * @author xiaohai
 */
public class CoreProcessFinish extends DBCommon {

    /**
     * 交货行工序生产完毕
     * @param conn
     * @param ordLnDlyPk
     * @throws SQLException
     * @throws ParseException
     */
    public static void setOrdLnDlyProc(Connection conn, String ordLnDlyPk,
            String status) throws SQLException, ParseException {
        List<String[]> arrList = PlnHelper.getArrays(conn,
                "CoreProcessFinish.selectPlnProcOrd",
                new String[] { ordLnDlyPk }, new String[] { "PLN_DIVIDE_ID",
                        "JGX_ORD", "RCL_ORD", "DBK_ORD", "ZG_ORD" ,"JG_ORD"});
        if (arrList != null && arrList.size() > 0) {
            for (String[] arr : arrList) {
              if (arr[5] != null && !arr[5].equals("")) {
                      processJgFinish(conn, ordLnDlyPk, arr[0], status, arr[5]);
                }
                if (arr[1] != null && !arr[1].equals("")) {
                    processJgxFinish(conn, ordLnDlyPk, arr[0], status);
                }
                if (arr[2] != null && !arr[2].equals("")) {
                    processRclFinish(conn, ordLnDlyPk, arr[0], status);
                }
                if (arr[3] != null && !arr[3].equals("")) {
                    processDbkFinish(conn, ordLnDlyPk, arr[0], status);
                }
                if (arr[4] != null && !arr[4].equals("")) {
                    processZgFinish(conn, ordLnDlyPk, arr[0], status, arr[4]);
                }
              
            }
        }
    }
    /**
     * 接箍生产完毕
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @param status
     * @throws SQLException
     * @throws ParseException
     */
    public static void processJgFinish(Connection conn, String ordLnDlyPk,
            String plnDivideId, String status,String proPlanId) throws SQLException,
            ParseException {
        if (!status.equals("4")) {
            return;
        }
        // 订单状态
        String exeStatus = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectJgProcMId", new String[] {
                        ordLnDlyPk, plnDivideId }, "EXE_STATUS");
        if (exeStatus == null) {
            return;
        }
        else if (exeStatus.equals("")) {
            return;
        }
        int numPro = Integer.parseInt(exeStatus);
        if (numPro <= 0) {
            // 查询加工线排产订单
            List<String[]> arrList = PlnHelper.getArrays(conn,
                    "CoreProcessFinish.selectOrderJgS",
                    new String[] { proPlanId }, new String[] { "EXE_STATUS",
                            "PLINE_CODE", "GX_PRO_SEQ", "GX_PLAN_NO" });
            if (arrList == null || arrList.size() == 0) {
                return;
            }
            ProducHelperBean helperBean = new ProducHelperBean();
            int i = 0;
            for (String[] arr : arrList) {
                String exestatus = arr[0];
                String plineCode = arr[1];
                String gxProSeq = arr[2];
                String gxPlanNo = arr[3];
                // 如果已经关闭
                if (exestatus.equals("3")) {
                    continue;
                }
                // 修改顺序序号
                helperBean.seqIncrease(conn, plineCode, Integer
                        .parseInt(gxProSeq)
                        - i, -1, -1, "H");
                i++;
                PlnHelper.deleteOrUpdate(conn,
                        "CoreProcessFinish.updateJgMSGxStatus", new String[] {
                                proPlanId, gxPlanNo });
                // 产线中没有生产中的记录，设置顺序号最小的为生产中
                String plineGx = PlnHelper.getParam(conn,
                        "CoreProcessFinish.selectOrdJgSPline",
                        " AND T.EXE_STATUS = '1'", new String[] { plineCode },
                        "GX_PRO_SEQ");
                if (plineGx.equals("")) {
                    String[] minArr = PlnHelper
                            .getArray(
                                    conn,
                                    "CoreProcessFinish.selectOrdJgSPline",
                                    "AND T.EXE_STATUS = '0' ORDER BY to_number(T.GX_PRO_SEQ)",
                                    new String[] { plineCode }, new String[] {
                                            "PRO_PLAN_ID", "GX_PLAN_NO" });
                    if (minArr != null && minArr.length > 0) {
                        PlnHelper.deleteOrUpdate(conn,
                                "CoreProcessFinish.updateOrdJgSStatus",
                                new String[] { "1", minArr[0], minArr[1] });
                    }
                }
            }
        }

    }
    /**
     * 加工工序生产完毕
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @param status
     * @throws SQLException
     * @throws ParseException
     */
    public static void processJgxFinish(Connection conn, String ordLnDlyPk,
            String plnDivideId, String status) throws SQLException,
            ParseException {
        if (!status.equals("4")) {
            return;
        }
        // 订单编号
        String proPlanId = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectJgxProcMId", new String[] {
                        ordLnDlyPk, plnDivideId }, "PRO_PLAN_ID");
        // if (proPlanId.equals("")) {
        // return;
        // }

        // 工序合同关闭
        PlnHelper.deleteOrUpdate(conn, "CoreProcessFinish.processJgxFinish",
                new Object[] { "4", ordLnDlyPk, plnDivideId });

        if (proPlanId == null) {
            return;
        }
        else if (proPlanId.equals("")) {
            return;
        }

        String numOrd = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectJgxMProcessNum",
                new String[] { proPlanId }, "NUM_ORD");
        if (numOrd == null) {
            return;
        }
        else if (numOrd.equals("")) {
            return;
        }
        int numPro = Integer.parseInt(numOrd);
        if (numPro <= 0) {
            // 关闭状态
            PlnHelper.deleteOrUpdate(conn,
                    "CoreProcessFinish.updateJgxMStatus",
                    new Object[] { proPlanId });

            // 查询加工线排产订单
            List<String[]> arrList = PlnHelper.getArrays(conn,
                    "CoreProcessFinish.selectOrderJgxS",
                    new String[] { proPlanId }, new String[] { "EXE_STATUS",
                            "PLINE_CODE", "GX_PRO_SEQ", "GX_PLAN_NO" });
            if (arrList == null || arrList.size() == 0) {
                return;
            }
            ProducHelperBean helperBean = new ProducHelperBean();
            int i = 0;
            for (String[] arr : arrList) {
                String exestatus = arr[0];
                String plineCode = arr[1];
                String gxProSeq = arr[2];
                String gxPlanNo = arr[3];
                // 如果已经关闭
                if (exestatus.equals("3")) {
                    continue;
                }
                // 修改顺序序号
                helperBean.seqIncrease(conn, plineCode, Integer
                        .parseInt(gxProSeq)
                        - i, -1, -1, "G");
                i++;
                PlnHelper.deleteOrUpdate(conn,
                        "CoreProcessFinish.updateJgxMSGxStatus", new String[] {
                                proPlanId, gxPlanNo });
                // 产线中没有生产中的记录，设置顺序号最小的为生产中
                String plineGx = PlnHelper.getParam(conn,
                        "CoreProcessFinish.selectOrdJgxSPline",
                        " AND T.EXE_STATUS = '1'", new String[] { plineCode },
                        "GX_PRO_SEQ");
                if (plineGx.equals("")) {
                    String[] minArr = PlnHelper
                            .getArray(
                                    conn,
                                    "CoreProcessFinish.selectOrdJgxSPline",
                                    "AND T.EXE_STATUS = '0' ORDER BY to_number(T.GX_PRO_SEQ)",
                                    new String[] { plineCode }, new String[] {
                                            "PRO_PLAN_ID", "GX_PLAN_NO" });
                    if (minArr != null && minArr.length > 0) {
                        PlnHelper.deleteOrUpdate(conn,
                                "CoreProcessFinish.updateOrdJgxSStatus",
                                new String[] { "1", minArr[0], minArr[1] });
                    }
                }
            }
        }

    }

    /**
     * 热处理工序生产完毕
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @param status
     * @throws SQLException
     * @throws ParseException
     */
    public static void processRclFinish(Connection conn, String ordLnDlyPk,
            String plnDivideId, String status) throws SQLException,
            ParseException {
        if (!status.equals("4")) {
            return;
        }
        // 订单编号
        String proPlanId = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectRclProcMId", new String[] {
                        ordLnDlyPk, plnDivideId }, "PRO_PLAN_ID");
        // if (proPlanId.equals("")) {
        // return;
        // }
        // 工序合同关闭
        PlnHelper.deleteOrUpdate(conn, "CoreProcessFinish.processRclFinish",
                new Object[] { "4", ordLnDlyPk, plnDivideId });

        if (proPlanId == null) {
            return;
        }
        else if (proPlanId.equals("")) {
            return;
        }

        String numOrd = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectRclMProcessNum",
                new String[] { proPlanId }, "NUM_ORD");
        if (numOrd == null) {
            return;
        }
        else if (numOrd.equals("")) {
            return;
        }
        int numPro = Integer.parseInt(numOrd);
        if (numPro <= 0) {
            // 关闭状态
            PlnHelper.deleteOrUpdate(conn,
                    "CoreProcessFinish.updateRclMStatus",
                    new Object[] { proPlanId });

            // 查询热处理排产订单
            List<String[]> arrList = PlnHelper.getArrays(conn,
                    "CoreProcessFinish.selectOrderRclS",
                    new String[] { proPlanId }, new String[] { "EXE_STATUS",
                            "PLINE_CODE", "GX_PRO_SEQ", "GX_PLAN_NO" });
            if (arrList == null || arrList.size() == 0) {
                return;
            }
            ProducHelperBean helperBean = new ProducHelperBean();
            int i = 0;
            for (String[] arr : arrList) {
                String exestatus = arr[0];
                String plineCode = arr[1];
                String gxProSeq = arr[2];
                String gxPlanNo = arr[3];
                // 如果已经关闭
                if (exestatus.equals("3")) {
                    continue;
                }
                // 修改顺序序号
                helperBean.seqIncrease(conn, plineCode, Integer
                        .parseInt(gxProSeq)
                        - i, -1, -1, "F");
                i++;
                PlnHelper.deleteOrUpdate(conn,
                        "CoreProcessFinish.updateRclMSGxStatus", new String[] {
                                proPlanId, gxPlanNo });
                // 产线中没有生产中的记录，设置顺序号最小的为生产中
                String plineGx = PlnHelper.getParam(conn,
                        "CoreProcessFinish.selectOrdRclsPline",
                        " AND T.EXE_STATUS = '1'", new String[] { plineCode },
                        "GX_PRO_SEQ");
                if (plineGx.equals("")) {
                    String[] minArr = PlnHelper
                            .getArray(
                                    conn,
                                    "CoreProcessFinish.selectOrdRclsPline",
                                    "AND T.EXE_STATUS = '0' ORDER BY to_number(T.GX_PRO_SEQ)",
                                    new String[] { plineCode }, new String[] {
                                            "PRO_PLAN_ID", "GX_PLAN_NO" });
                    if (minArr != null && minArr.length > 0) {
                        PlnHelper.deleteOrUpdate(conn,
                                "CoreProcessFinish.updateOrdRclsStatus",
                                new String[] { "1", minArr[0], minArr[1] });
                    }
                }
            }
        }
    }

    /**
     * 镦拔扩工序生产完毕
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @param status
     * @throws SQLException
     * @throws ParseException
     */
    public static void processDbkFinish(Connection conn, String ordLnDlyPk,
            String plnDivideId, String status) throws SQLException,
            ParseException {
        if (!status.equals("4")) {
            return;
        }
        // 订单编号
        String proPlanId = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectDbkProcMId", new String[] {
                        ordLnDlyPk, plnDivideId }, "PRO_PLAN_ID");
        // if (proPlanId.equals("")) {
        // return;
        // }

        // 工序合同关闭
        PlnHelper.deleteOrUpdate(conn, "CoreProcessFinish.processDbkFinish",
                new Object[] { "4", ordLnDlyPk, plnDivideId });

        if (proPlanId == null) {
            return;
        }
        else if (proPlanId.equals("")) {
            return;
        }

        String numOrd = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectDbkMProcessNum",
                new String[] { proPlanId }, "NUM_ORD");
        if (numOrd == null) {
            return;
        }
        else if (numOrd.equals("")) {
            return;
        }
        int numPro = Integer.parseInt(numOrd);
        if (numPro <= 0) {
            // 关闭状态
            PlnHelper.deleteOrUpdate(conn,
                    "CoreProcessFinish.updateDbkMStatus",
                    new Object[] { proPlanId });

            // 查询热处理排产订单
            List<String[]> arrList = PlnHelper.getArrays(conn,
                    "CoreProcessFinish.selectOrderDbkS",
                    new String[] { proPlanId }, new String[] { "EXE_STATUS",
                            "PLINE_CODE", "GX_PRO_SEQ", "GX_PLAN_NO" });
            if (arrList == null || arrList.size() == 0) {
                return;
            }
            ProducHelperBean helperBean = new ProducHelperBean();
            int i = 0;
            for (String[] arr : arrList) {
                String exestatus = arr[0];
                String plineCode = arr[1];
                String gxProSeq = arr[2];
                String gxPlanNo = arr[3];
                // 如果已经关闭
                if (exestatus.equals("3")) {
                    continue;
                }
                // 修改顺序序号
                helperBean.seqIncrease(conn, plineCode, Integer
                        .parseInt(gxProSeq)
                        - i, -1, -1, "E");
                i++;
                PlnHelper.deleteOrUpdate(conn,
                        "CoreProcessFinish.updateDbkMSGxStatus", new String[] {
                                proPlanId, gxPlanNo });
                // 产线中没有生产中的记录，设置顺序号最小的为生产中
                String plineGx = PlnHelper.getParam(conn,
                        "CoreProcessFinish.selectOrdDbksPline",
                        " AND T.EXE_STATUS = '1'", new String[] { plineCode },
                        "GX_PRO_SEQ");
                if (plineGx.equals("")) {
                    String[] minArr = PlnHelper
                            .getArray(
                                    conn,
                                    "CoreProcessFinish.selectOrdDbksPline",
                                    "AND T.EXE_STATUS = '0' ORDER BY to_number(T.GX_PRO_SEQ)",
                                    new String[] { plineCode }, new String[] {
                                            "PRO_PLAN_ID", "GX_PLAN_NO" });
                    if (minArr != null && minArr.length > 0) {
                        PlnHelper.deleteOrUpdate(conn,
                                "CoreProcessFinish.updateOrdDbksStatus",
                                new String[] { "1", minArr[0], minArr[1] });
                    }
                }
            }
        }
    }

    /**
     * 轧管工序生产完毕
     * @param conn
     * @param ordLnDlyPk
     * @param plnDivideId
     * @param status
     * @throws SQLException
     * @throws ParseException
     */
    public static void processZgFinish(Connection conn, String ordLnDlyPk,
            String plnDivideId, String status, String proOrderNo)
            throws SQLException, ParseException {
        if (!status.equals("4")) {
            return;
        }
        // 订单编号
        String proPlanId = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectZgProcMId", new String[] { ordLnDlyPk,
                        plnDivideId, proOrderNo }, "PRO_PLAN_ID");
        // if (proPlanId.equals("")) {
        // return;
        // }

        // 工序合同关闭
        PlnHelper.deleteOrUpdate(conn, "CoreProcessFinish.processZgFinish",
                new Object[] { "4", ordLnDlyPk, plnDivideId });

        if (proPlanId == null) {
            return;
        }
        else if (proPlanId.equals("")) {
            return;
        }

        String numOrd = PlnHelper.getParam(conn,
                "CoreProcessFinish.selectZgMProcessNum",
                new String[] { proPlanId }, "NUM_ORD");
        if (numOrd == null) {
            return;
        }
        else if (numOrd.equals("")) {
            return;
        }
        int numPro = Integer.parseInt(numOrd);
        if (numPro <= 0) {
            // 关闭状态
            PlnHelper.deleteOrUpdate(conn, "CoreProcessFinish.updateZgMStatus",
                    new Object[] { proPlanId });

            // 查询轧管排产订单
            List<String[]> arrList = PlnHelper.getArrays(conn,
                    "CoreProcessFinish.selectOrderZgS",
                    new String[] { proPlanId }, new String[] { "EXE_STATUS",
                            "PLINE_CODE", "GX_PRO_SEQ", "GX_PLAN_NO",
                            "ZY_BATCH_ID", "IF_STOVE_ISSUED" });
            if (arrList == null || arrList.size() == 0) {
                return;
            }
            ProducHelperBean helperBean = new ProducHelperBean();
            int i = 0;
            for (String[] arr : arrList) {
                String exestatus = arr[0];
                String plineCode = arr[1];
                String gxProSeq = arr[2];
                String gxPlanNo = arr[3];
                String zyBatchId = arr[4];
                String ifStoveIssued = arr[5];
                // 如果已经关闭
                if (exestatus.equals("3")) {
                    continue;
                }
                if (!exestatus.equals("1") && ifStoveIssued.equals("0")) {
                    zyBatchId = "";
                }

                // 如果强制生产完成，并轧管排产是计划状态，则轧批号置空
                // if (exestatus.equals("0")) {
                // PlnHelper.deleteOrUpdate(conn,
                // "CoreProcessFinish.updateZgZyBatch",
                // new String[] { zyBatchId,proPlanId, gxPlanNo });
                // }
                // 修改顺序序号
                helperBean.seqIncrease(conn, plineCode, Integer
                        .parseInt(gxProSeq)
                        - i, -1, -1, "D");
                i++;
                PlnHelper.deleteOrUpdate(conn,
                        "CoreProcessFinish.updateZgMSGxStatus", new String[] {
                                zyBatchId, proPlanId, gxPlanNo });
                // 产线中没有生产中的记录，设置顺序号最小的为生产中
                String plineGx = PlnHelper.getParam(conn,
                        "CoreProcessFinish.selectOrdZgsPline",
                        " AND T.EXE_STATUS = '1'", new String[] { plineCode },
                        "GX_PRO_SEQ");
                if (plineGx.equals("")) {
                    String[] minArr = PlnHelper
                            .getArray(
                                    conn,
                                    "CoreProcessFinish.selectOrdZgsPline",
                                    "AND T.EXE_STATUS = '0' ORDER BY to_number(T.GX_PRO_SEQ)",
                                    new String[] { plineCode }, new String[] {
                                            "PRO_PLAN_ID", "GX_PLAN_NO" });
                    if (minArr != null && minArr.length > 0) {
                        PlnHelper.deleteOrUpdate(conn,
                                "CoreProcessFinish.updateOrdZgsStatus",
                                new String[] { "1", minArr[0], minArr[1] });
                    }
                }
            }
        }

    }
}