ulysseskao
2016-04-29 b0c18d369abd06075c83759b0e19823c2a11d716
CCSTrace/CCS/EventAI/JudgeCase.cs
@@ -1,12 +1,4 @@
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Data.OracleClient;
@@ -15,317 +7,330 @@
{
    public class JudgeCase
    {
        TraceSubject m_TraceSubject;
        System.Collections.ArrayList m_Case = new System.Collections.ArrayList();
        int Damage_FdrID;
        int Damage_Fsc; // 新案件的損壞設備種類
        int Damage_Ufid; // 新案件的損壞設備編號
        int New_CaseID;
        String AcceptDate;
        System.Collections.ArrayList New_Case;
        System.Collections.Hashtable Old_Case;
        int Infer_Fsc; // 推測出可能受損的設備種類
        int Infer_Ufid; // 推測出可能受損的設備編號
        int Parent_CaseID = 0;
        CCS.EventAI.Equipment m_Equip;
        private TraceSubject _mTraceSubject;
        private readonly ArrayList _mCase = new ArrayList();
        private readonly int _damageFdrId;
        private readonly int _damageFsc; // 新案件的損壞設備種類
        private readonly int _damageUfid; // 新案件的損壞設備編號
        private int _newCaseId;
        private string _acceptDate;
        private ArrayList _newCase;
        private Hashtable _oldCase;
        private int _inferFsc; // 推測出可能受損的設備種類
        private int _inferUfid; // 推測出可能受損的設備編號
        private int _parentCaseId = 0;
        private Equipment _mEquip;
        private OracleConnection _ConnectionTPC;
        private OracleTransaction _Transaction;
        private RecordLog _PLog;
        private String _traceConnectionString = String.Empty;
        private readonly OracleConnection _connectionTpc;
        private readonly OracleTransaction _transaction;
        private readonly RecordLog _pLog;
        private readonly string _traceConnectionString;
        // int Range = 360; //比事故案件還早多久時間內的案件才作合併 單位:分
        public JudgeCase(int m_FdrID, int m_fsc, int m_ufid, int m_CaseID, String m_AcceptDate, OracleConnection _Connection, OracleTransaction _Trx, String _traceConnection, RecordLog _Log)
        public JudgeCase(int mFdrId, int mFsc, int mUfid, int mCaseId, string mAcceptDate, OracleConnection connection, OracleTransaction trx, string traceConnection, RecordLog log)
        {
            this.Damage_FdrID = m_FdrID;
            this.Damage_Fsc = m_fsc;
            this.Damage_Ufid = m_ufid;
            this.New_CaseID = m_CaseID;
            this.AcceptDate = m_AcceptDate;
            _ConnectionTPC = _Connection;
            _Transaction = _Trx;
            _traceConnectionString = _traceConnection;
            _PLog = _Log;
            _damageFdrId = mFdrId;
            _damageFsc = mFsc;
            _damageUfid = mUfid;
            _newCaseId = mCaseId;
            _acceptDate = mAcceptDate;
            _connectionTpc = connection;
            _transaction = trx;
            _traceConnectionString = traceConnection;
            _pLog = log;
        }
        public void AIBegin() {
            _PLog.Info("進行案件追蹤合併.");
            String SqlStmt;
        public void AiBegin()
        {
            _pLog.Info("進行案件追蹤合併.");
            // String ToTime = AcceptDate;
            SqlStmt = "SELECT R.ACCEPTNUM,R.CASEID as CASEID,R.FDRID as FDRID,R.FSC as FSC,R.UFID as UFID,E.CASESTATUS as CASESTATUS,R.EVENTBRIEF as BRIEF "
                    + "FROM EOS.EVENTRECORD R,EOS.EVENTS E WHERE E.CASEID = R.CASEID AND R.TRACE_FINISH = " + CCS.LocalVariable.Trace_Still
                    + " AND E.CASETYPE <> " + CCS.LocalVariable.ChildCase + " AND R.FDRID = " + this.Damage_FdrID;
            var sqlStmt = "SELECT R.ACCEPTNUM,R.CASEID as CASEID,R.FDRID as FDRID,R.FSC as FSC,R.UFID as UFID,E.CASESTATUS as CASESTATUS,R.EVENTBRIEF as BRIEF "
                             + "FROM EOS.EVENTRECORD R,EOS.EVENTS E WHERE E.CASEID = R.CASEID AND R.TRACE_FINISH = " + GlobalVariable.TraceStill
                             + " AND E.CASETYPE <> " + GlobalVariable.ChildCase + " AND R.FDRID = " + _damageFdrId;
            OracleCommand Command = null;
            OracleCommand command = null;
            OracleDataReader reader = null;
            try
            {
                _PLog.Info("搜尋同一饋線上可供合併之母案件.");
                Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
                reader = Command.ExecuteReader();
                _pLog.Info("搜尋同一饋線上可供合併之母案件.");
                command = new OracleCommand(sqlStmt, _connectionTpc, _transaction);
                reader = command.ExecuteReader();
                // 找出同一饋線上處理中的母案件(可作合併的)
                // 該饋線上有可能有二件或兩件以上的母案件
                while (reader.Read())
                {
                    System.Collections.ArrayList Record = new System.Collections.ArrayList();
                    Record.Add(Convert.ToInt32(reader["CASEID"].ToString()));
                    Record.Add(Convert.ToInt32(reader["FDRID"].ToString()));
                    Record.Add(Convert.ToInt32(reader["FSC"].ToString()));
                    Record.Add(Convert.ToInt32(reader["UFID"].ToString()));
                    Record.Add(Convert.ToInt32(reader["CASESTATUS"].ToString()));
                    CCS.EventAI.Equipment Tmp = new CCS.EventAI.Equipment(Convert.ToInt32(reader["FSC"].ToString()), Convert.ToInt32(reader["UFID"].ToString()));
                    Record.Add(Tmp);
                    Record.Add(reader["BRIEF"].ToString());
                    Record.Add(reader["ACCEPTNUM"].ToString());
                    m_Case.Add(Record);
                    _PLog.Info(Record[7].ToString() + ", " + Record[0]);
                    ArrayList record = new ArrayList();
                    record.Add(Convert.ToInt32(reader["CASEID"].ToString()));
                    record.Add(Convert.ToInt32(reader["FDRID"].ToString()));
                    record.Add(Convert.ToInt32(reader["FSC"].ToString()));
                    record.Add(Convert.ToInt32(reader["UFID"].ToString()));
                    record.Add(Convert.ToInt32(reader["CASESTATUS"].ToString()));
                    Equipment tmp = new Equipment(Convert.ToInt32(reader["FSC"].ToString()), Convert.ToInt32(reader["UFID"].ToString()));
                    record.Add(tmp);
                    record.Add(reader["BRIEF"].ToString());
                    record.Add(reader["ACCEPTNUM"].ToString());
                    _mCase.Add(record);
                    _pLog.Info(record[7].ToString() + ", " + record[0]);
                }
                reader.Close();
            }
            catch (Exception e)
            {
                _PLog.Error(e.Message);
                _pLog.Error(e.Message);
                Console.WriteLine(e.StackTrace);
                _PLog.Error("尋找母案件時發生錯誤.");
                _pLog.Error("尋找母案件時發生錯誤.");
                throw e;
            }
            finally
            {
                if (Command != null)
                    Command.Dispose();
                if (command != null)
                    command.Dispose();
                if (reader != null)
                    reader.Close();
            }
            // 若事故案件為該饋線上的第一件事故案件時,無須再作追蹤比較
            if (m_Case.Count == 0) {
                Infer_Fsc = this.Damage_Fsc;
                Infer_Ufid = this.Damage_Ufid;
                _PLog.Info("此案件為饋線上第一件案件");
                _PLog.Info("追蹤合併結束.");
            if (_mCase.Count == 0)
            {
                _inferFsc = _damageFsc;
                _inferUfid = _damageUfid;
                _pLog.Info("此案件為饋線上第一件案件");
                _pLog.Info("追蹤合併結束.");
                return;
            }
            // 判斷是否為同一變壓器,如果是就不必作追蹤
            try {
                for (int i = 0; i < m_Case.Count; i++) {
                    if ( (Convert.ToInt32(((ArrayList) m_Case[i])[2].ToString()) == Damage_Fsc) &&
                         (Convert.ToInt32(((ArrayList) m_Case[i])[3].ToString()) == Damage_Ufid))
            try
            {
                for (int i = 0; i < _mCase.Count; i++)
                {
                    if ((Convert.ToInt32(((ArrayList)_mCase[i])[2].ToString()) == _damageFsc) &&
                         (Convert.ToInt32(((ArrayList)_mCase[i])[3].ToString()) == _damageUfid))
                    {
                        if (((ArrayList) m_Case[i])[6].ToString().StartsWith("A"))  // 若母案件事故原因為A類才做合併
                        {
                            Parent_CaseID = Convert.ToInt32(((ArrayList) m_Case[i])[0].ToString());
                            Infer_Fsc = Convert.ToInt32(((ArrayList) m_Case[i])[2].ToString());
                            Infer_Ufid = Convert.ToInt32(((ArrayList) m_Case[i])[3].ToString());
                            _PLog.Info("相同變壓器");
                            _PLog.Info("與案件"+((ArrayList) m_Case[i])[7]+"合併,故障點Fsc: "+Infer_Fsc+" Ufid: "+Infer_Ufid);
                        if (((ArrayList)_mCase[i])[6].ToString().StartsWith("A"))  // 若母案件事故原因為A類才做合併
                        {
                            _parentCaseId = Convert.ToInt32(((ArrayList)_mCase[i])[0].ToString());
                            _inferFsc = Convert.ToInt32(((ArrayList)_mCase[i])[2].ToString());
                            _inferUfid = Convert.ToInt32(((ArrayList)_mCase[i])[3].ToString());
                            _pLog.Info("相同變壓器");
                            _pLog.Info("與案件" + ((ArrayList)_mCase[i])[7] + "合併,故障點Fsc: " + _inferFsc + " Ufid: " + _inferUfid);
                            return;
                        }
                    }
                }
            } catch (Exception ex) {
                _PLog.Error(ex.Message);
            }
            catch (Exception ex)
            {
                _pLog.Error(ex.Message);
                throw ex;
            }
            _PLog.Info("不同變壓器,開始追蹤(New Version)...");
            _pLog.Info("不同變壓器,開始追蹤(New Version)...");
            this.TraceCase();
            _PLog.Warn("追蹤合併結束");
            TraceCase();
            _pLog.Warn("追蹤合併結束");
        }
        private void TraceCase() {
            int m_Key;
        private void TraceCase()
        {
            int mKey;
            try {
                this.m_TraceSubject = new CCS.EventAI.TraceSubject(_ConnectionTPC, _Transaction, _traceConnectionString, _PLog);
                _PLog.Info("TraceSubject Initial....");
            try
            {
                _mTraceSubject = new TraceSubject(_connectionTpc, _transaction, _traceConnectionString, _pLog);
                _pLog.Info("TraceSubject Startup....");
            }
            catch (Exception ex) {
                _PLog.Error("TraceObject.dll無法初始化,請確認該元件是否存在或jre是否為 1.4 版,並確認是否註冊.");
              throw ex;
            catch (Exception ex)
            {
                _pLog.Error("TraceObject.dll無法初始化,請確認該元件是否存在或jre是否為 1.4 版,並確認是否註冊.");
                throw ex;
            }
            try {
                this.m_TraceSubject.startTrace(Damage_Fsc, Damage_Ufid, true);
                New_Case = this.m_TraceSubject.getNewResult(); // 新案件所建出來的linkedlist(往上追到breaker)
                _PLog.Info("追蹤新案件完成");
            try
            {
                _mTraceSubject.StartTrace(_damageFsc, _damageUfid, true);
                _newCase = _mTraceSubject.GetNewResult(); // 新案件所建出來的linkedlist(往上追到breaker)
                _pLog.Info("追蹤新案件完成");
                for (int i = 0; i < m_Case.Count; i++) {
                    if (this.AlreadyFinishCase(Convert.ToInt32(((ArrayList) m_Case[i])[0].ToString()))) {
                for (int i = 0; i < _mCase.Count; i++)
                {
                    if (AlreadyFinishCase(Convert.ToInt32(((ArrayList)_mCase[i])[0].ToString())))
                    {
                        continue;
                    }
                    ArrayList FacilitySet = this.RealDamageEquipment(Convert.ToInt32(((ArrayList)m_Case[i])[0].ToString())); // 切開的設備
                    ArrayList facilitySet = RealDamageEquipment(Convert.ToInt32(((ArrayList)_mCase[i])[0].ToString())); // 切開的設備
                    if (FacilitySet.Count == 0) {
                        this.m_TraceSubject.startTrace(Convert.ToInt32(((ArrayList)m_Case[i])[2].ToString()),
                                                       Convert.ToInt32(((ArrayList)m_Case[i])[3].ToString()), false);
                        Old_Case = this.m_TraceSubject.getOldResult(); // 母案件所建出來的TreeMap(往上追到breaker)
                        _PLog.Info("追蹤母案件完成");
                    if (facilitySet.Count == 0)
                    {
                        _mTraceSubject.StartTrace(Convert.ToInt32(((ArrayList)_mCase[i])[2].ToString()),
                                                       Convert.ToInt32(((ArrayList)_mCase[i])[3].ToString()), false);
                        _oldCase = _mTraceSubject.GetOldResult(); // 母案件所建出來的TreeMap(往上追到breaker)
                        _pLog.Info("追蹤母案件完成");
                        for (int j = 0; j < New_Case.Count; j++) {
                            m_Key = ((Equipment) New_Case[j]).getUFID();
                        for (int j = 0; j < _newCase.Count; j++)
                        {
                            mKey = ((Equipment)_newCase[j]).Ufid;
                            if (Old_Case.ContainsKey(m_Key))
                            if (_oldCase.ContainsKey(mKey))
                            {
                                if (((ArrayList)m_Case[i])[6].ToString().StartsWith("A"))  // 若母案件事故原因為A類才做合併
                                if (((ArrayList)_mCase[i])[6].ToString().StartsWith("A"))  // 若母案件事故原因為A類才做合併
                                {
                                    this.Parent_CaseID = Convert.ToInt32(((ArrayList)m_Case[i])[0].ToString());
                                    Infer_Fsc = ((Equipment) New_Case[j]).getFSC();
                                    Infer_Ufid = ((Equipment) New_Case[j]).getUFID();
                                    this.m_Equip = (CCS.EventAI.Equipment) ((ArrayList) m_Case[i])[5];
                                    _PLog.Info("與案件"+((ArrayList) m_Case[i])[7]+"合併,故障點Fsc: "+Infer_Fsc+" Ufid: "+Infer_Ufid);
                                    _parentCaseId = Convert.ToInt32(((ArrayList)_mCase[i])[0].ToString());
                                    _inferFsc = ((Equipment)_newCase[j]).Fsc;
                                    _inferUfid = ((Equipment)_newCase[j]).Ufid;
                                    _mEquip = (Equipment)((ArrayList)_mCase[i])[5];
                                    _pLog.Info("與案件" + ((ArrayList)_mCase[i])[7] + "合併,故障點Fsc: " + _inferFsc + " Ufid: " + _inferUfid);
                                    return;
                                }
                            }
                        }
                    } else {
                        for (int k = 0; k < FacilitySet.Count; k++)
                    }
                    else
                    {
                        for (int k = 0; k < facilitySet.Count; k++)
                        {
                            bool Merge = false; // 損壞設備是否在所切開的設備下游
                            Equipment DamageEquipment = (Equipment) FacilitySet[k]; // 切開的設備
                            for (int z = 0; z < New_Case.Count; z++)
                            bool merge = false; // 損壞設備是否在所切開的設備下游
                            Equipment damageEquipment = (Equipment)facilitySet[k]; // 切開的設備
                            for (int z = 0; z < _newCase.Count; z++)
                            {
                                if ( (((Equipment) New_Case[z]).getFSC() == DamageEquipment.getFSC()) &&
                                     (((Equipment) New_Case[z]).getUFID() == DamageEquipment.getUFID()))
                                if ((((Equipment)_newCase[z]).Fsc== damageEquipment.Fsc) &&
                                     (((Equipment)_newCase[z]).Ufid== damageEquipment.Ufid))
                                {
                                    Merge = true;
                                    merge = true;
                                    break;
                                }
                            }
                            // 當發現在所切開開關的下游,就將損壞設備指向切開的設備並return
                            if (Merge)
                            if (merge)
                            {
                                if (((ArrayList)m_Case[i])[6].ToString().StartsWith("A"))  // 若母案件事故原因為A類才做合併
                                if (((ArrayList)_mCase[i])[6].ToString().StartsWith("A"))  // 若母案件事故原因為A類才做合併
                                {
                                    this.Parent_CaseID = Convert.ToInt32(((ArrayList)m_Case[i])[0].ToString());
                                    Infer_Fsc = DamageEquipment.getFSC();
                                    Infer_Ufid = DamageEquipment.getUFID();
                                    this.m_Equip = (CCS.EventAI.Equipment) ((ArrayList) m_Case[i])[5];
                                    _PLog.Info("與案件"+((ArrayList) m_Case[i])[7]+"合併,故障點Fsc: "+Infer_Fsc+" Ufid: "+Infer_Ufid);
                                    _parentCaseId = Convert.ToInt32(((ArrayList)_mCase[i])[0].ToString());
                                    _inferFsc = damageEquipment.Fsc;
                                    _inferUfid = damageEquipment.Ufid;
                                    _mEquip = (Equipment)((ArrayList)_mCase[i])[5];
                                    _pLog.Info("與案件" + ((ArrayList)_mCase[i])[7] + "合併,故障點Fsc: " + _inferFsc + " Ufid: " + _inferUfid);
                                    return;
                                }
                            }
                        } // for loop(k)
                    } // else
                } // for loop(i)
            } catch (Exception ex) {
                _PLog.Error(ex.Message);
                _PLog.Error("追蹤時發生錯誤.");
                throw ex;
            }
            catch (Exception ex)
            {
                _pLog.Error(ex.Message);
                _pLog.Error("追蹤時發生錯誤.");
                throw;
            }
            finally
            {
                this.m_TraceSubject.discardEngine();
                _mTraceSubject.DiscardEngine();
            }
            Infer_Fsc = this.Damage_Fsc;
            Infer_Ufid = this.Damage_Ufid;
            this.m_TraceSubject.discardEngine();
            return;
            _inferFsc = _damageFsc;
            _inferUfid = _damageUfid;
            _mTraceSubject.DiscardEngine();
        }
        public Equipment getInferEquipment() {
            Equipment Equip = new Equipment(this.Infer_Fsc, this.Infer_Ufid);
            return Equip;
        public Equipment GetInferEquipment()
        {
            Equipment equip = new Equipment(_inferFsc, _inferUfid);
            return equip;
        }
        public Equipment getOldrEquipment() {
            return m_Equip;
        public Equipment GetOldrEquipment()
        {
            return _mEquip;
        }
        public int getParentCaseID() {
            return Parent_CaseID;
        public int GetParentCaseId()
        {
            return _parentCaseId;
        }
        private bool AlreadyFinishCase(int CaseID) {
            String SqlStmt;
            bool Result = false;
        private bool AlreadyFinishCase(int caseId)
        {
            bool result = false;
            SqlStmt = "SELECT TO_CHAR(CLOSETIME,'YYYY/MM/DD HH24:MI:SS') as CLOSETIME,COUNT(*) as COUNT FROM EOS.EVENTFACILITY " + "WHERE CASEID = "
                    + CaseID + " GROUP BY CLOSETIME ORDER BY CLOSETIME DESC";
            var sqlStmt = "SELECT TO_CHAR(CLOSETIME,'YYYY/MM/DD HH24:MI:SS') as CLOSETIME,COUNT(*) as COUNT FROM EOS.EVENTFACILITY " + "WHERE CASEID = "
                             + caseId + " GROUP BY CLOSETIME ORDER BY CLOSETIME DESC";
            OracleCommand Command = null;
            OracleCommand command = null;
            OracleDataReader reader = null;
            try
            {
                Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
                reader = Command.ExecuteReader();
                command = new OracleCommand(sqlStmt, _connectionTpc, _transaction);
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    if (Convert.ToString(reader["CLOSETIME"]).Equals("null") || Convert.ToString(reader["CLOSETIME"]).Length == 0 )
                        Result = false; // 有切開開關未恢復(Case in Process)
                    if (Convert.ToString(reader["CLOSETIME"]).Equals("null") || Convert.ToString(reader["CLOSETIME"]).Length == 0)
                        result = false; // 有切開開關未恢復(Case in Process)
                    else
                        Result = true; // 有切開關但全都恢復(Case Finish)
                        result = true; // 有切開關但全都恢復(Case Finish)
                }
                else
                    Result = false; // 未切過開關
                    result = false; // 未切過開關
                reader.Close();
            }
            catch (Exception e)
            {
                _PLog.Error("Problems occur: " + e.Message);
                _pLog.Error("Problems occur: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (Command != null)
                    Command.Dispose();
                command?.Dispose();
                if (reader != null)
                    reader.Close();
                reader?.Close();
            }
            return Result;
            return result;
        }
        private System.Collections.ArrayList RealDamageEquipment(int CaseID) {
            System.Collections.ArrayList Result = new System.Collections.ArrayList();
            String SqlStmt;
        private ArrayList RealDamageEquipment(int caseId)
        {
            ArrayList result = new ArrayList();
            SqlStmt = "SELECT FSC,UFID FROM EOS.EVENTFACILITY WHERE CASEID = " + CaseID + " AND CLOSETIME IS NULL";
            var sqlStmt = "SELECT FSC,UFID FROM EOS.EVENTFACILITY WHERE CASEID = " + caseId + " AND CLOSETIME IS NULL";
            OracleCommand Command = null;
            OracleCommand command = null;
            OracleDataReader reader = null;
            try
            {
                Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
                reader = Command.ExecuteReader();
                command = new OracleCommand(sqlStmt, _connectionTpc, _transaction);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Equipment Tmp = new Equipment(Convert.ToInt32(reader["FSC"]), Convert.ToInt32(reader["UFID"]));
                    Result.Add(Tmp);
                    Equipment tmp = new Equipment(Convert.ToInt32(reader["FSC"]), Convert.ToInt32(reader["UFID"]));
                    result.Add(tmp);
                }
            }
            catch (Exception e)
            {
                // CCS.LocalVariable.ErrorLog.setErrorLog("Error in JudgeCase(RealDamageEquipment) :" + e.getMessage());
                _PLog.Error("Problems occur: " + e.Message);
                // CCS.GlobalVariable.ErrorLog.setErrorLog("Error in JudgeCase(RealDamageEquipment) :" + e.getMessage());
                _pLog.Error("Problems occur: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (reader != null )
                    reader.Close();
            {
                reader?.Close();
                if (Command != null )
                    Command.Dispose();
                command?.Dispose();
            }
            return Result;
            return result;
        }
    }
}
}