package com.steering.mes.mcp.common;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.common.StringTool;
import com.steering.mes.mcp.coup.BaseHelper;
import com.steering.mes.mcp.entity.YdmHttInitialEntity;
import com.steering.mes.mcp.entity.YdmZcInitialJgHskEntity;

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

/**
 * Created by 朱少波 on 2019/3/25.
 */
public class FrmHttRunTimeRpt extends DBCommon {

    public CoreReturnObject doQueryHtt(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String[] ProStation,String Type,String JudgeStove) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttRunTimeRpt.doQueryHttNew");
        String ProStationSql = "";
        String PlnCondition ="";
        String JudgeStoveStr = "";
        String JudgeStoveStr2= "";

        if(!ProStation.equals("")){
 //FACTORY_CODE in '"+ProStation+"'
        	String str = CoreConverter.toSqlConditionString("FACTORY_CODE", ProStation).replace("AND", "");
            ProStationSql+=" AND T.PLINE_CODE IN (SELECT PLINE_CODE FROM COM_BASE_PLINE WHERE "+str+")";
        }

        if(Type.equals("0")){
            PlnCondition+=" AND  PRODUCCODE  IN ('S000094','S000082','S000040')";
        } else  if(Type.equals("1")){
            PlnCondition+=" AND  PRODUCCODE  NOT IN ('S000094','S000082','S000040')";
        }

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

        sql = sql.replace("#PLINECODE#",CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode)
        )

                .replace("#PROSTATION#",ProStationSql)
                .replace("#BALYEARMONTH#",BalYearMonth)
            .replace("#BALYEARMONTH_NEXT#",NextBalYearMonth)
            .replace("#PLN#",PlnCondition).replace("#JUDGE_STOVE_NO#",JudgeStoveStr).replace("#JUDGE_STOVE_NO2#",JudgeStoveStr2);
        System.out.print(sql);

        return this.getData(sql);
    }

    public CoreReturnObject doQueryHtt(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation) {

      return  doQueryHtt( BalYearMonth,  NextBalYearMonth, plineCode ,  ProStation,"");
    }

    public CoreReturnObject doQueryHtt(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation,String Type) {

        return  doQueryHtt( BalYearMonth,  NextBalYearMonth, plineCode ,  ProStation,Type,"");
    }

    public CoreReturnObject doQueryMch(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation,String Type,String JudgeStove) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttRunTimeRpt.doQueryMchNew");
        String ProStationSql = "";
        String PlnCondition ="";
        String JudgeStoveStr = "";
        String JudgeStoveStr2= "";

        if(!ProStation.equals("")){
        	String str = CoreConverter.toSqlConditionString("FACTORY_CODE", ProStation).replace("AND", "");
            ProStationSql+=" AND T.PLINE_CODE IN (SELECT PLINE_CODE FROM COM_BASE_PLINE WHERE "+str+")";
        }

        if(Type.equals("0")){
            PlnCondition+=" AND  PRODUCCODE  IN ('S000094','S000082','S000040')";
        } else  if(Type.equals("1")){
            PlnCondition+=" AND  PRODUCCODE  NOT IN ('S000094','S000082','S000040')";
        }


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


        sql = sql.replace("#PLINECODE#",CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode)
        )

                .replace("#PROSTATION#",ProStationSql)
                .replace("#BALYEARMONTH#",BalYearMonth)
                .replace("#BALYEARMONTH_NEXT#",NextBalYearMonth)
                .replace("#PLN#",PlnCondition).replace("#JUDGE_STOVE_NO#",JudgeStoveStr).replace("#JUDGE_STOVE_NO2#",JudgeStoveStr2);

        return this.getData(sql);
    }


    public CoreReturnObject doQueryMch(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation){
        return  doQueryMch( BalYearMonth,  NextBalYearMonth, plineCode ,  ProStation,"");
    }

    public CoreReturnObject doQueryMch(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation,String Type){
        return  doQueryMch( BalYearMonth,  NextBalYearMonth, plineCode ,  ProStation,Type,"");
    }



    public CoreReturnObject doQueryVrp(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation,String Type,String JudgeStove){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttRunTimeRpt.doQueryVrpNew");
        String ProStationSql = "";
        String PlnCondition ="";
        String JudgeStoveStr = "";
        String JudgeStoveStr2= "";

        if(!ProStation.equals("")){
        	String str = CoreConverter.toSqlConditionString("FACTORY_CODE", ProStation).replace("AND", "");
            ProStationSql+=" AND T.PLINE_CODE IN (SELECT PLINE_CODE FROM COM_BASE_PLINE WHERE "+str+")";
        }

        if(Type.equals("0")){
            PlnCondition+=" AND  PRODUCCODE  IN ('S000094','S000082','S000040')";
        } else  if(Type.equals("1")){
            PlnCondition+=" AND  PRODUCCODE  NOT IN ('S000094','S000082','S000040')";
        }

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

        sql = sql.replace("#PLINECODE#",CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode)
        )

                .replace("#PROSTATION#",ProStationSql)
                .replace("#BALYEARMONTH#",BalYearMonth)
                .replace("#BALYEARMONTH_NEXT#",NextBalYearMonth)
                .replace("#PLN#",PlnCondition).replace("#JUDGE_STOVE_NO#",JudgeStoveStr).replace("#JUDGE_STOVE_NO2#",JudgeStoveStr2);


        return this.getData(sql);
    }

    public CoreReturnObject doQueryVrp(String BalYearMonth, String NextBalYearMonth, String[] plineCode , String []ProStation,String Type){

        return doQueryVrp( BalYearMonth,  NextBalYearMonth, plineCode ,  ProStation, Type,"");
    }

    public CoreReturnObject doQuery(String BalYearMonth, String[] PlineCode,String JudgeStove){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttRunTimeRpt.doQuery");
        String ProStationSql = "";
        String PlnCondition ="";

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


        sql = sql
                .replace("#BALYEARMONTH#",BalYearMonth)
                .replace("#PLINECODE#",CoreConverter.toSqlConditionString("M.PLINE_CODE", PlineCode))
                  .replace("#PlnCondition#",PlnCondition);
        return this.getData(sql);
    }
    
    public CoreReturnObject doQueryJg(String BalYearMonth,  String[] plineCode , String JudgeStove) {

        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttRunTimeRpt.doQueryJg");
        String JudgeStoveStr = "";

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

        sql = sql.replace("#PLINECODE#",CoreConverter.toSqlConditionString("AA.PLINE_CODE", plineCode)
        )
                .replace("#BALYEARMONTH#",BalYearMonth)
                .replace("#JUDGE_STOVE_NO#",JudgeStoveStr);

        return this.getData(sql);
    }


    /**
     * 结转
     * @param SubNo
     * @param SubSeq
     * @return
     */
    public CoreReturnObject insertTal(String BalYearMonth,String NextMonth, ArrayList<String> Data,String[]  PlineCode ) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;

        try {
            connection.setAutoCommit(false); //改25号结转
            if( StringTool.GetBalYear("120504").compareTo(BalYearMonth)<0){
                core.setV_errMsg(BaseHelper.addMonth2(BalYearMonth,-1) +  "月未过结算日期, 请过了结算日期进行月结！");
                return core;
            }

            Integer Count = 0;
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.SelectCount");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta = connection.prepareStatement(sql);
            psta.setString(1, NextMonth);

            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                Count = rs.getInt("COUNT");
            }


            if (Count>0) {
                core.setV_errMsg("已存在下月期初,无法结转该月数据！");
                return core;
            }
            rs.close();
            psta.close();

             sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.DeleteTal");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta  = connection.prepareStatement(sql);
            psta.setObject(1,BalYearMonth);
            psta.addBatch();
            psta.executeBatch();
            psta.close();

            for (int i = 0; i <Data.size() ; i++) {
                YdmHttInitialEntity entity = EntityTool.getEntityByJSONStr(Data.get(i),YdmHttInitialEntity.class);
                 sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttRunTimeRpt.insertTal");
                entity.setBalYearMonth(BalYearMonth);

                sql = com.steering.common.EntityTool.fillSqlByEntity(sql, entity);
                psta  = connection.prepareStatement(sql);
                psta.addBatch();
                psta.executeBatch();
                psta.close();
            }


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



    /**
     *  撤销结转
     * @param SubNo
     * @param SubSeq
     * @return
     */
    public CoreReturnObject cancelinsertTal(String BalYearMonth,String NextMonth,String[]  PlineCode ) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;

        try {
            connection.setAutoCommit(false); //


            Integer Count = 0;
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.SelectCount");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta = connection.prepareStatement(sql);
            psta.setString(1, NextMonth);

            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                Count = rs.getInt("COUNT");
            }


            if (Count>0) {
                core.setV_errMsg("已存在下月期初,无法撤销结转该月数据！");
                return core;
            }
            rs.close();
            psta.close();

            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.DeleteTal");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta  = connection.prepareStatement(sql);
            psta.setObject(1,BalYearMonth);
            psta.addBatch();
            psta.executeBatch();
            psta.close();



            core.setV_errMsg("撤销成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
    
    /**
     * 接箍结转-核算科
     * @param BalYearMonth
     * @param NextMonth
     * @param Data
     * @param PlineCode
     * @param userName
     * @return
     */
    public CoreReturnObject insertTalJg(String BalYearMonth,String NextMonth, ArrayList<String> Data,String[]  PlineCode,String userName ) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;

        try {
            connection.setAutoCommit(false); //

            Integer Count = 0;
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.SelectCountJg");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta = connection.prepareStatement(sql);
            psta.setString(1, NextMonth);

            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                Count = rs.getInt("COUNT");
            }


            if (Count>0) {
                core.setV_errMsg("已存在下月期初,无法结转该月数据！");
                return core;
            }
            rs.close();
            psta.close();

             sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.DeleteTalJg");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta  = connection.prepareStatement(sql);
            psta.setObject(1,BalYearMonth);
            psta.addBatch();
            psta.executeBatch();
            psta.close();

            for (int i = 0; i <Data.size() ; i++) {
            	YdmZcInitialJgHskEntity entity = EntityTool.getEntityByJSONStr(Data.get(i),YdmZcInitialJgHskEntity.class);
                 sql = CoreXmlSqlParsersFactory
                        .getSql("FrmHttRunTimeRpt.insertTalJg");
                entity.setBalYearMonth(BalYearMonth);
                entity.setCreateName(userName);

                sql = com.steering.common.EntityTool.fillSqlByEntity(sql, entity);
                psta  = connection.prepareStatement(sql);
                psta.addBatch();
                psta.executeBatch();
                psta.close();
            }

            core.setV_errMsg("结转成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
    
    /**
     * 撤销结转-核算科
     * @param BalYearMonth
     * @param NextMonth
     * @param PlineCode
     * @return
     */
    public CoreReturnObject cancelinsertTalJg(String BalYearMonth,String NextMonth,String[]  PlineCode ) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;

        try {
            connection.setAutoCommit(false); //

            Integer Count = 0;
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.SelectCountJg");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta = connection.prepareStatement(sql);
            psta.setString(1, NextMonth);

            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                Count = rs.getInt("COUNT");
            }


            if (Count>0) {
                core.setV_errMsg("已存在下月期初,无法撤销结转该月数据！");
                return core;
            }
            rs.close();
            psta.close();

            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmHttRunTimeRpt.DeleteTalJg");
            sql += CoreConverter.toSqlConditionString("PLINE_CODE", PlineCode);
            psta  = connection.prepareStatement(sql);
            psta.setObject(1,BalYearMonth);
            psta.addBatch();
            psta.executeBatch();
            psta.close();
            
            core.setV_errMsg("撤销成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }



}
