package com.steering.mes.mcp.Report;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.PlnZyRclMEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 热处理台帐查询
 * @author Administrator
 */
public class FrmRclReport extends DBCommon {

    /**
     * 查询外观检验台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryAppResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryAppResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }

        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);

        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询上料台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryLoadResult(String bc, String bz,
           String[] plinCodes, String start, String end) {
             return doQueryLoadResult( bc,  bz,plinCodes,  start,  end,"");
    }

    public CoreReturnObject doQueryLoadResult(String bc, String bz,
                                              String[] plinCodes, String start, String end,String JudgeStove) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryLoadResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.LOAD_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.LOAD_GROUP ='" + bz + "'";
        }
        if (!JudgeStove.equals("")) {
            sql += " AND M.JUDGE_STOVE_NO ='" + JudgeStove + "'";
        }
        return this.getData(sql);
    }

    /**
     * 查询高温台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryHeatResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryHeatResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询淬火台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryQuencResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryQuencResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询低温台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryLheatResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryLheatResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询定径台账
     * @param list
     * @return
     */
    public CoreReturnObject doQuerySizeResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQuerySizeResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询热矫台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryHlatrResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryHlatrResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询冷矫台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryClastrResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryClastrResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }
    /**
     * 查询探伤台账
     * @param list
     * @return
     */
    public CoreReturnObject doQueryCrackResult(String bc, String bz,
           String[] plinCodes, String start, String end) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmRclReport.doQueryCrackResult");
        if (!start.equals("")) {
            sql += " AND T.CREATE_TIME >=TO_DATE('"
                    + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.CREATE_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.LOAD_GROUP ='" + bz + "'";
        }
        return this.getData(sql);
    }



    public CoreReturnObject doQueryOffLine(ArrayList<String> list,String[] status,
                                           String[] plineCode) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory
                .getSql("FrmRclReport.doQueryOffLine");
        if (!list.get(0).equals("")) {
            sql += "  AND T.PRO_TIME BETWEEN TO_DATE('" + list.get(0)
                    + "','YYYY-MM-DD HH24:MI:SS') " + " AND TO_DATE('"
                    + list.get(1) + "','YYYY-MM-DD HH24:MI:SS')";
        }
        if (!list.get(2).equals("")) {
            sql += " AND T.JUDGE_STOVE_NO  LIKE '" + list.get(2)
            + "' || '%'  OR  EXISTS (SELECT 1 FROM HTT_GROUP_RESULT WHERE JUDGE_STOVE_NO LIKE '"+ list.get(2)+ "%'  AND GROUP_ID =T.GROUP_ID)";

        }
        if (!list.get(3).equals("")) {
            sql += " AND T.ZY_BATCH_ID  LIKE  '" + list.get(3)
                    + "' || '%'";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
        if (!list.get(4).equals("")) {
            sql += " AND T.STATION_CODE ='" + list.get(4) + "'";
        }

        if (!list.get(5).equals("-1")) {
            sql += " AND T.GROUP_LOG ='" + list.get(5) + "'";
        }


        if(status[0].equals("1") && status[1].equals("1") && status[2].equals("1") && status[3].equals("1")){

        }else {
            String statusSql = "";

            if (status[0].equals("1")) {
                    statusSql = statusSql + (statusSql.equals("")? "":" OR ")  + " ((TO_NUMBER(NVL(T.OFFLINE_NUM, 0)) - TO_NUMBER(NVL(T.GROUP_NUM, 0)) -\n" +
                            "       TO_NUMBER(NVL(T.RE_NUM, 0)) - TO_NUMBER(NVL(T.SUBCONTRACT_NUM, 0)) +\n" +
                            "       TO_NUMBER(NVL(T.SUBCONTRACTCOMPLETE_NUM, 0))) <>0)";
            }
            if(status[1].equals("1")){
                statusSql  = statusSql + (statusSql.equals("")? "":" OR ")  + " T.RE_NUM > 0 ";
            }

            if(status[2].equals("1")){
                statusSql  = statusSql + (statusSql.equals("")? "":" OR ")  + " T.GROUP_NUM > 0 ";
            }

            if(status[3].equals("1")){
                statusSql  = statusSql + (statusSql.equals("")? "":" OR ")  + " (NVL(T.SUBCONTRACT_NUM,0) > 0 " +
                        "AND NVL(SUBCONTRACTCOMPLETE_NUM,0)=0   AND  NVL(HB_FP_NUM,0)=0)";
            }

            if(!statusSql.equals("")){
                sql+= "AND  ("+statusSql +")";
            }

        }

        sql +=" ORDER BY T.JUDGE_STOVE_NO,T.BATCH_NO, T.PRO_TIME";
        return this.getData(sql);
    }


    public CoreReturnObject updateSource(String[] ResultNo,String Source) throws Exception {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            if(ResultNo.length<=0){

                throw new Exception("请选择需要修改的实绩！");
            }

            String sql =  CoreXmlSqlParsersFactory.getSql("FrmRclReport.updateSource");

            sql+= CoreConverter.toSqlConditionString("T.RESULT_NO", ResultNo);

            PreparedStatement patsUpdate = connection.prepareStatement(sql);
            patsUpdate.setObject(1,Source);
            patsUpdate.addBatch();
            patsUpdate.executeBatch();

            core.setV_errMsg("修改成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            core.setV_errCode(-1);
            core.setResult(1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }


    public CoreReturnObject ChangePline(String[] LoadInfo) throws Exception {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            if(LoadInfo.length<=0){

                throw new Exception("请选择需要修改的上料信息！");
            }


            String sql1 =  CoreXmlSqlParsersFactory.getSql("FrmRclReport.ChangePline1");
            PreparedStatement patsChangePline1 = connection.prepareStatement(sql1);

            String sql2 =  CoreXmlSqlParsersFactory.getSql("FrmRclReport.ChangePline2");
            PreparedStatement patsChangePline2 = connection.prepareStatement(sql2);

            for (int i = 0; i <LoadInfo.length ; i++) {
                String[] Info = LoadInfo[i].split(",");
                if(Info.length< 2){
                    throw new Exception("数据异常！");
                }
                String HeatPlanNo = Info[0];
                String PlineCode = Info[1];


                PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
                        HeatPlanNo);


                if(PlineCode.equals(pln.getPlineCode())){
                    throw new Exception("炉号"+ pln.getJudgeStoveNo()+"未委内或者已经更换产线！");
                }

                patsChangePline1.setObject(1,pln.getPlineCode());
                patsChangePline1.setObject(2,pln.getPlineName());
                patsChangePline1.setObject(3,HeatPlanNo);
                patsChangePline1.addBatch();


                patsChangePline2.setObject(1,pln.getPlineCode());
                patsChangePline2.setObject(2,pln.getPlineName());
                patsChangePline2.setObject(3,HeatPlanNo);
                patsChangePline2.addBatch();
            }

            patsChangePline1.executeBatch();
            patsChangePline2.executeBatch();

            core.setV_errMsg("修改成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            core.setV_errCode(-1);
            core.setResult(1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }




    /**
     * 查询超声探伤台账
     *
     * @param list
     * @return
     */
    public CoreReturnObject doQueryUtResult(String bc, String bz,
                                            String[] plinCodes, String start, String end, String OrderNo,
                                            String JudgeStove,String BathNo) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmRclReport.doQueryUtResult");
        if (!start.equals("")) {
            sql += " AND T.COL_TIME >=TO_DATE('" + start
                    + "', 'YYYY-MM-DD HH24:MI:SS')AND T.COL_TIME <=TO_DATE('"
                    + end + "', 'YYYY-MM-DD HH24:MI:SS') ";
        }
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCodes);
        if (!bc.equals("")) {
            sql += " AND T.COL_SHIFT ='" + bc + "'";
        }
        if (!bz.equals("")) {
            sql += " AND T.COL_GROUP ='" + bz + "'";
        }
        if (!OrderNo.equals("")) {
            sql += " AND M.ORDER_NO||'/'|| M.ORDER_SEQ ='" + OrderNo + "'";
        }

        if (!JudgeStove.equals("")) {
            sql += " AND T.JUDGE_STOVE_NO ='" + JudgeStove + "'";
        }

        if(!BathNo.equals("")){
            sql += " AND T.BATCH_NO ='" + BathNo + "'";
        }


        sql += " Order by T.COL_TIME";
        return this.getData(sql);
    }



}
