dogchang70
2016-01-15 81c46add6b31e0f347da357feaff7ebbdb6a2c36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
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.Data.OracleClient;
 
namespace CCSTrace.CCS.Function
{
    public class Leach
    {
        private CCS.Function.MergeCase MergeCase = new CCS.Function.MergeCase();
        private RecordLog _PLog;
        private OracleConnection _ConnectionTPC;
        private OracleTransaction _Transaction;
        private String _traceConnectionString = String.Empty;
 
        public Leach(OracleConnection _Conn, OracleTransaction _Trx, String _traceConnection, RecordLog _Log)
        {
            _ConnectionTPC = _Conn;
            _Transaction = _Trx;
            _traceConnectionString = _traceConnection;
            _PLog = _Log;
        }
 
        public int LeachCase(CCS.Object.EventRecord m_NewRec)
        {
 
            switch (SameCase(m_NewRec))
            {
                case CCS.LocalVariable.Success:
                    return this.Judge(m_NewRec, false);
                case CCS.LocalVariable.CaseTypeChanged:
                    return this.Judge(m_NewRec, true);
                case CCS.LocalVariable.SameCustomer:
                    return CCS.LocalVariable.SameCustomer;
                case CCS.LocalVariable.Failure_By_DB:
                    return CCS.LocalVariable.Failure_By_DB;
                case CCS.LocalVariable.CaseTransfer:
                    return CCS.LocalVariable.CaseTransfer;
                default:
                    return CCS.LocalVariable.Failure_By_DB;
            }
        }
 
        public CCS.EventAI.Equipment getOldEquipment()
        {
            return this.MergeCase.getOldEquipment();
        }
 
        private int SameCase(CCS.Object.EventRecord m_NewRec)
        {
            String SqlStmt = "SELECT R.ACCEPTNUM,R.CASEID as CASEID,R.IMPORTCASE as IMPORTCASE,E.CASETYPE as CASETYPE FROM EOS.EVENTRECORD R,"
                           + "EOS.EVENTS E WHERE ";
            int m_Result = -1;
            int ImportCase = -1;
            int TmpCaseID = 0;
            int CaseType = -1;
            _PLog.Info("利用電號或地址檢查是否有已成立之案件.");
            // 檢查資料庫中此電號或地址是否已成立案件
            
            if (m_NewRec.getMeter().Trim().Length != 0)
            {
                SqlStmt = SqlStmt + "R.CUSTOMERMETER = '" + m_NewRec.getMeter() + "' AND E.CASEID = R.CASEID AND E.CASESTATUS <= "
                        + CCS.LocalVariable.WaitForSponsor;
            }
            else if (m_NewRec.getAddr().Trim().Length != 0)
            {
                SqlStmt = SqlStmt + "R.CUSTOMERADDR = '" + m_NewRec.getAddr() + "' AND E.CASEID = R.CASEID AND E.CASESTATUS <= "
                        + CCS.LocalVariable.WaitForSponsor;
            }
 
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
            OracleDataReader reader = Command.ExecuteReader();
 
            try
            {
                if (reader.Read())
                {
                    ImportCase = Convert.ToInt32(reader["IMPORTCASE"].ToString());
                    TmpCaseID = Convert.ToInt32(reader["CASEID"].ToString());
                    CaseType = Convert.ToInt32(reader["CASETYPE"].ToString());
                    m_NewRec.setTmpCaseID(TmpCaseID);
                    _PLog.Info("案件已成立:" + reader["ACCEPTNUM"].ToString() + " " + TmpCaseID + " " + CaseType);
                }
            }
            catch (Exception e)
            {
                _PLog.Error(e.Message);
                Console.WriteLine(e.StackTrace);
                return CCS.LocalVariable.Failure_By_DB;
            }
            finally
            { 
                reader.Close();
                Command.Dispose();
            }
 
            // 非同一用戶重複來電
            if (TmpCaseID == 0)
            {
                return CCS.LocalVariable.Success;
            }
            switch (CaseType)
            {
                case CCS.LocalVariable.OriginalCase:
                case CCS.LocalVariable.ParentCase:
                    SqlStmt = "SELECT TO_CHAR(CLOSETIME,'YYYY/MM/DD HH24:MI:SS') as CLOSETIME,COUNT(*) as COUNT FROM EOS.EVENTFACILITY " + "WHERE CASEID = "
                            + TmpCaseID + " GROUP BY CLOSETIME ORDER BY CLOSETIME DESC";
                    break;
                case CCS.LocalVariable.ChildCase:
                    SqlStmt = "SELECT TO_CHAR(CLOSETIME,'YYYY/MM/DD HH24:MI:SS') as CLOSETIME,COUNT(*) as COUNT FROM EOS.EVENTFACILITY "
                            + "WHERE CASEID = (SELECT PARENTID FROM EOS.MERGECASE WHERE CASEID = " + TmpCaseID + ") GROUP BY CLOSETIME "
                            + "ORDER BY CLOSETIME DESC";
                    break;
                default:
                    _PLog.Error("無法辨別案件種類.");
                    return CCS.LocalVariable.Failure_By_DB;
            }
            _PLog.Info("檢查該案件有無切開關紀錄.");
 
            try
            {
                Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
                reader = Command.ExecuteReader();
 
                if (reader.Read())
                {
                    // 有開關尚未恢復
                    if (reader["CLOSETIME"] == null)
                    {
                        _PLog.Info("有切開關且尚未恢復.");
                        if ((m_NewRec.getImportCase() == ImportCase) || (ImportCase == CCS.LocalVariable.isImportCase))
                        {
                            m_Result = this.AddCount(TmpCaseID);
                        }
                        else
                        {
                            m_Result = CCS.LocalVariable.CaseTypeChanged;
                        }
                    }
                    else
                    {
                        // 開關都已恢復(同用戶須重新受理)
                        _PLog.Info("有切開關且已經恢復.");
                        int CaseID;
 
                        // 找出案件編號需以母案件編號或該案件編號去找casestatus
                        if (TmpCaseID == 0)
                        {
                            CaseID = m_NewRec.getCaseID();
                        }
                        else
                        {
                            CaseID = TmpCaseID;
                        }
                        // 判斷該案件或母案件是否移轉作後續處理
                        if (this.getCastStatus(CaseID) == CCS.LocalVariable.EventTrasnfer)
                        {
                            return CCS.LocalVariable.CaseTransfer;
                        }
                        else
                        {
                            m_Result = CCS.LocalVariable.Success;
                        }
                    }
                }
                else
                {
                    // 未切過開關
                    _PLog.Info("無切開關紀錄.");
                    if ((m_NewRec.getImportCase() == ImportCase) || (ImportCase == CCS.LocalVariable.isImportCase))
                    {
                        m_Result = this.AddCount(TmpCaseID);
                    }
                    else
                    {
                        int CaseID;
                        // 找出案件編號需以母案件編號或該案件編號去找casestatus
                        if (TmpCaseID == 0)
                        {
                            CaseID = m_NewRec.getCaseID();
                        }
                        else
                        {
                            CaseID = TmpCaseID;
 
                            // 判斷該案件或母案件是否移轉作後續處理
                        }
                        if (this.getCastStatus(CaseID) == CCS.LocalVariable.EventTrasnfer)
                        {
                            return CCS.LocalVariable.CaseTransfer;
                        }
                        else
                        {
                            m_Result = CCS.LocalVariable.CaseTypeChanged;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _PLog.Error(e.Message);
                Console.WriteLine(e.StackTrace);
                return CCS.LocalVariable.Failure_By_DB;
            }
            finally
            { 
                reader.Close();
                Command.Dispose();
            }
            return m_Result;
        }
 
        private int AddCount(int m_CaseID)
        {
            String sqlStmt = "UPDATE EOS.EVENTS SET COUNT = COUNT + 1 WHERE CASEID = " + m_CaseID;
            OracleCommand Command = new OracleCommand(sqlStmt, _ConnectionTPC, _Transaction);
 
            try
            {
                if (Command.ExecuteNonQuery() > 0)
                    return CCS.LocalVariable.SameCustomer;
                else
                    return CCS.LocalVariable.Failure_By_DB;
            }
            catch (Exception)
            {
                return CCS.LocalVariable.Failure_By_DB;
            }
            finally
            { 
                Command.Dispose();
            }
        }
 
        private int getCastStatus(int CaseID)
        {
            String SqlStmt;
            int Status = 0;
 
            SqlStmt = "SELECT CASESTATUS FROM CCS.EVENTQUERY WHERE CCSID IN (SELECT CCSID FROM CCS.NUM_CONTRAST " + "WHERE CASEID = " + CaseID + ")";
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
            OracleDataReader reader = null;
 
 
            try
            {
                reader = Command.ExecuteReader();
 
                if (reader.Read())
                    Status = Convert.ToInt32(reader["CASESTATUS"].ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            { 
                if ( reader != null )
                    reader.Close();
 
                Command.Dispose();
            }
            return Status;
        }
 
        private int Judge(CCS.Object.EventRecord m_NewRec, bool ChangeCaseType)
        {
            int m_Result = CCS.LocalVariable.Failure_By_DB;
 
            if ((m_NewRec.getMeter().Trim().Length == 0))
            {
                return CCS.LocalVariable.NoMeter;
            }
 
            /* 已知損壞設備(ONLY FOR 饋線全停才知道損壞設備的FSC) */
            try
            {
                if (m_NewRec.getFsc() != 0)
                {
                    m_NewRec.getLocateEquipment().setFSC(CCS.LocalVariable.Breaker);
                    m_NewRec.getLocateEquipment().setUFID(m_NewRec.getUfid());
                    _PLog.Info("饋線全停:" + m_NewRec.getFsc() + ", " + m_NewRec.getUfid());
                    MergeCase.Merge(m_NewRec, _ConnectionTPC, _Transaction, _traceConnectionString, _PLog);
                    return CCS.LocalVariable.Success;
                }
            }
            catch (CCS.EventAI.TraceLoopException)
            {
                return CCS.LocalVariable.Trace_Counts_3;
            }
            catch (TraceException)
            {
                return CCS.LocalVariable.Trace_Failure;
            }
            catch (Exception ex)
            {
                _PLog.Error(ex.Message);
                return CCS.LocalVariable.Failure_By_DB;
            }
 
            try
            {
                _PLog.Info("定位損壞設備!");
                int result = getEquipment(m_NewRec);
                _PLog.Info("FSC:" + m_NewRec.getFsc() + ", Ufid:" + m_NewRec.getUfid() + ", Tpclid:" + m_NewRec.getTpclid() + ", FeederID:" + m_NewRec.getFdrID());
 
                switch (result)
                { // get fsc,ufid,fdrid of sxfmr by meter
                    case CCS.LocalVariable.Success:
                        if (m_NewRec.getFdrID() == 0)
                        {
                            m_Result = CCS.LocalVariable.NoSupplyElc;
                        }
                        else
                        {
                            // 需在未併案前作土木設備設定
                            this.getLocateEquipment(m_NewRec);
 
                            if (m_NewRec.getBrief().Length !=0 && m_NewRec.getBrief().Substring(0,1).Equals("A"))
                            { // A類事故原因,做追蹤合併
                                _PLog.Info("<A類案件>");
                                MergeCase.Merge(m_NewRec, _ConnectionTPC, _Transaction, _traceConnectionString, _PLog);
                            }
                            else
                            {
                                _PLog.Info("<非A類案件>");
                            }
 
                            if (ChangeCaseType)
                            {
                                m_Result = CCS.LocalVariable.CaseTypeChanged;
                            }
                            else
                            {
                                m_Result = CCS.LocalVariable.Success;
                            }
                        }
                        break;
                    case CCS.LocalVariable.No_Sxfmr:
                        m_Result = CCS.LocalVariable.No_Sxfmr;
                        break;
                    case CCS.LocalVariable.Failure_By_DB:
                        m_Result = CCS.LocalVariable.Failure_By_DB;
                        break;
                }
            }
            catch (CCS.EventAI.TraceLoopException)
            {
                return CCS.LocalVariable.Trace_Counts_3;
            }
            catch (TraceException)
            {
                m_Result = CCS.LocalVariable.Trace_Failure;
            }
            catch (Exception ex1)
            {
                _PLog.Error(ex1.Message);
                m_Result = CCS.LocalVariable.Failure_By_DB;
            }
            return m_Result;
        }
 
        private int getEquipment(CCS.Object.EventRecord m_NewRec)
        {
            String Temp;
            String m_OwnerTpclid;
            String m_Group;
            String SqlStmt = "select tran_cord from basedb.meter where metr_numb = '" + m_NewRec.getMeter() + "'";
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC,_Transaction);
            OracleDataReader reader = Command.ExecuteReader();
 
            try
            {
                if (reader.Read())
                {
                    Temp = reader["tran_cord"].ToString().Trim();
                    if ((Temp.Length == 9) || (Temp.Length == 11)) /* 架空變壓器 */
                    {
                        m_OwnerTpclid = Temp;
                        reader.Close();
                        Command.Dispose();
                        m_NewRec.setTpclid(m_OwnerTpclid);
                        return getSxfmr(m_OwnerTpclid, null, m_NewRec); /* Find the Sxfmr */
                    }
                    else
                    {
                        m_OwnerTpclid = Temp.Substring(0, 11).Trim(); /* 地下變壓器 */
                        m_Group = Temp.Substring(14);
                        reader.Close();
                        Command.Dispose();
                        m_NewRec.setTpclid(m_OwnerTpclid + " " + m_Group);
                        return getSxfmr(m_OwnerTpclid, m_Group, m_NewRec);
                        /* Find the Sxfmr */
                    }
                }
                else
                {
                    return CCS.LocalVariable.No_Sxfmr;
                }
            }
            catch (Exception e)
            {
                _PLog.Error(e.Message);
                Console.WriteLine(e.StackTrace);
                return CCS.LocalVariable.Failure_By_DB;
            }
            finally
            { 
                reader.Close();
                Command.Dispose();
            }
        }
 
        private int getSxfmr(String m_Tpclid, String m_Group, CCS.Object.EventRecord m_NewRec)
        {
            String SqlStmt;
            int m_Status = CCS.LocalVariable.Failure_By_DB;
 
            if (m_Group == null)
            {
                SqlStmt = "SELECT UFID,FDR1 FROM BASEDB.CONNECTIVITY WHERE FSC = " + CCS.LocalVariable.Sxfmr + " AND UFID IN "
                        + "(SELECT UFID FROM BASEDB.SXFMR WHERE OWNERTPCLID = '" + m_Tpclid + "')";
            }
            else
            {
                SqlStmt = "SELECT UFID,FDR1 FROM BASEDB.CONNECTIVITY WHERE FSC = " + CCS.LocalVariable.Sxfmr + " AND UFID IN "
                        + "(SELECT UFID FROM BASEDB.SXFMR WHERE OWNERTPCLID = '" + m_Tpclid + "' AND GROUP1 = '" + m_Group + "')";
 
            }
 
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
            OracleDataReader reader = Command.ExecuteReader();
 
            try
            {
                if (reader.Read())
                {
                    m_NewRec.setFsc(CCS.LocalVariable.Sxfmr);
                    m_NewRec.setUfid(Convert.ToInt32(reader["ufid"].ToString()));
                    m_NewRec.setFdrID(Convert.ToInt32(reader["fdr1"].ToString()));
                    m_Status = CCS.LocalVariable.Success;
                }
                else
                    m_Status = getHicustomer(m_Tpclid, m_Group, m_NewRec);
            }
            catch (Exception ex)
            {
                _PLog.Error(ex.Message);
            }
            finally
            { 
                reader.Close();
                Command.Dispose();
            }
            return m_Status;
        }
 
 
        private int getHicustomer(String m_Tpclid, String m_Group, CCS.Object.EventRecord m_NewRec)
        {
            String SqlStmt;
 
            if (m_Group == null)
            {
                SqlStmt = "SELECT UFID,FDR1 FROM BASEDB.CONNECTIVITY WHERE FSC = " + CCS.LocalVariable.Hicustomer + " AND UFID IN "
                        + "(SELECT UFID FROM BASEDB.HICUSTOMER WHERE OWNERTPCLID = '" + m_Tpclid + "')";
            }
            else
            {
                SqlStmt = "SELECT UFID,FDR1 FROM BASEDB.CONNECTIVITY WHERE FSC = " + CCS.LocalVariable.Hicustomer + " AND UFID IN "
                        + "(SELECT UFID FROM BASEDB.HICUSTOMER WHERE OWNERTPCLID = '" + m_Tpclid + "' AND GROUP1 = '" + m_Group + "')";
 
            }
 
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
            OracleDataReader reader = Command.ExecuteReader();
 
            try
            {
                if (reader.Read())
                {
                    m_NewRec.setFsc(CCS.LocalVariable.Hicustomer);
                    m_NewRec.setUfid(Convert.ToInt32(reader["ufid"].ToString()));
                    m_NewRec.setFdrID(Convert.ToInt32(reader["fdr1"].ToString()));
                    return CCS.LocalVariable.Success;
                }
                else
                    return CCS.LocalVariable.No_Sxfmr;
            }
            catch (Exception ex)
            {
                _PLog.Error(ex.Message);
                return CCS.LocalVariable.Failure_By_DB;
            }
            finally
            { 
                reader.Close();
                Command.Dispose();
            }
        }
 
        private void getLocateEquipment(CCS.Object.EventRecord m_NewRec)
        {
            String SqlStmt;
 
            SqlStmt = "SELECT OWNERFSC as FSC,OWNERUFID as UFID,OWNERTPCLID as TPCLID FROM ";
            switch (m_NewRec.getFsc())
            {
                case CCS.LocalVariable.Sxfmr:
                    SqlStmt = SqlStmt + "BASEDB.SXFMR WHERE UFID = " + m_NewRec.getUfid();
                    break;
                case CCS.LocalVariable.Hicustomer:
                    SqlStmt = SqlStmt + "BASEDB.HICUSTOMER WHERE UFID = " + m_NewRec.getUfid();
                    break;
            }
 
            OracleCommand Command = new OracleCommand(SqlStmt, _ConnectionTPC, _Transaction);
            OracleDataReader reader = Command.ExecuteReader();
 
            try
            {
                if (reader.Read())
                {
                    m_NewRec.getLocateEquipment().setFSC(Convert.ToInt32(reader["FSC"].ToString()));
                    m_NewRec.getLocateEquipment().setUFID(Convert.ToInt32(reader["UFID"].ToString()));
                    m_NewRec.getLocateEquipment().setTPCLID(reader["TPCLID"].ToString());
                }
            }
            catch (Exception e)
            {
                _PLog.Error(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            { 
                reader.Close();
                Command.Dispose();
            }
        }
    }
 
}