package com.steering.pss.sale.order;

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

import com.steering.comm.db.DBCommon;

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

public class CoreOrderCarryOver extends DBCommon {

    public CoreReturnObject query(String orderNo, String timeStart,
            String timeEnd, String isCO) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();

            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.Query");
            // 本来下面SQL放在一起的后来为了查找问题 单独分开了
            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.insertPlan");
            String sql2 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.insertSend");
            String sql3 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.insertBill");
            String sql4 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.insertPay");
            String sql5 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.insertMat");
            String sql6 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.insertOut");

            if (!timeStart.equals("") && !timeEnd.equals("")) {
                String str = " AND A.CREATE_TIME BETWEEN TO_DATE('" + timeStart
                        + "','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + timeEnd
                        + "','YYYY-MM-DD HH24:MI:SS')";
                sql = sql.replaceAll("#TIMECON#", str);
                sql1 = sql1.replaceAll("#TIMECON#", str);
                sql2 = sql2.replaceAll("#TIMECON#", str);
                sql3 = sql3.replaceAll("#TIMECON#", str);
                sql4 = sql4.replaceAll("#TIMECON#", str);
                sql5 = sql5.replaceAll("#TIMECON#", str);
                sql6 = sql6.replaceAll("#TIMECON#", str);
            }
            else {
                sql = sql.replaceAll("#TIMECON#", "");
                sql1 = sql1.replaceAll("#TIMECON#", "");
                sql2 = sql2.replaceAll("#TIMECON#", "");
                sql3 = sql3.replaceAll("#TIMECON#", "");
                sql4 = sql4.replaceAll("#TIMECON#", "");
                sql5 = sql5.replaceAll("#TIMECON#", "");
                sql6 = sql6.replaceAll("#TIMECON#", "");
            }

            if (orderNo.equals("")) {
                sql1 = sql1.replaceAll("#ORDER_NO#", "");
                sql2 = sql2.replaceAll("#ORDER_NO#", "");
                sql3 = sql3.replaceAll("#ORDER_NO#", "");
                sql4 = sql4.replaceAll("#ORDER_NO#", "");
                sql5 = sql5.replaceAll("#ORDER_NO#", "");
                sql6 = sql6.replaceAll("#ORDER_NO#", "");
            }
            else {
                sql1 = sql1.replaceAll("#ORDER_NO#", orderNo);
                sql2 = sql2.replaceAll("#ORDER_NO#", orderNo);
                sql3 = sql3.replaceAll("#ORDER_NO#", orderNo);
                sql4 = sql4.replaceAll("#ORDER_NO#", orderNo);
                sql5 = sql5.replaceAll("#ORDER_NO#", orderNo);
                sql6 = sql6.replaceAll("#ORDER_NO#", orderNo);
            }

            // MAT_WEIGHT BILL_WEIGHT OUT_WEIGHT
            if (isCO.equals("0")) {
                sql += " AND  (CASE  ORDER_UNIT "
                        + "WHEN '支' THEN  (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_COUNT THEN '0' "
                        + "   ELSE '1'   END)"
                        + " WHEN '吨' THEN (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_WEIGHT THEN '0' "
                        + "   ELSE '1'  END )"
                        + "   ELSE (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_LEN THEN '0' "
                        + "   ELSE '1'   END)   END ) = '0' "
                        + "AND NVL(R.BILL_COUNT,'0')='0' "
                        + " AND NVL(MAT_COUNT,'0')='0' "
                        + " AND NVL(OUT_COUNT,'0')= '0' "
                        + " AND NVL(SEND_COUNT,'0') <>'0'";
            }
            else if (isCO.equals("1")) {
                sql += " AND  ((CASE  ORDER_UNIT "
                        + "WHEN '支' THEN  (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_COUNT THEN  '0'  "
                        + " ELSE  '1'   END)"
                        + " WHEN '吨' THEN (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_WEIGHT THEN '0' "
                        + "   ELSE '1'  END )"
                        + " ELSE (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <=  R.SEND_LEN THEN  '0'  "
                        + " ELSE  '1'   END)   END ) <>  '0'  "
                        + " OR  NVL(R.BILL_COUNT,'0') <> '0' "
                        + " OR  NVL(MAT_COUNT,'0') <> '0' "
                        + " OR  NVL(OUT_COUNT,'0') <> '0' "
                        + " OR  NVL(SEND_COUNT, '0') = '0' )";

            }

            String sqlD = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCarryOver.MoveData");
            PreparedStatement ps = con.prepareStatement(sqlD);
            ps.executeUpdate();
            ps.close();
            con.commit();

            ps = con.prepareStatement(sql1);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(sql2);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(sql3);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(sql4);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(sql5);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(sql6);
            ps.executeUpdate();
            ps.close();
            con.commit();

            Object[] obj = new Object[] { orderNo };
            cro = this.getData(sql, obj);
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());

        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

}
