yuanhung
2024-02-05 876ccb53468872e1156a9f93d4cb72af7fb49c8e
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
using MailQuery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MailQuery.Filter;
using System.Xml.Linq;
using System.Web.Helpers;
 
namespace MailQuery.Controllers
{
 
 
    [AllowedIP]
    public class queryController : ApiController
    {
 
        private static string datePattern = "=([0-9]{4})[-/]([1-9]|[0][1-9]|[1][0-2])[-/]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])=";
        private System.Text.RegularExpressions.Regex regexdate = new System.Text.RegularExpressions.Regex(datePattern);//,System.Text.RegularExpressions.RegexOptions.Compiled);
        private static string inputPattern = "={0}=";  //19999/12/32  testing 
        private static string replaceEsc = "=";
        private static string[] nothingString = new string[] { };
        private static CsmsResult[] nothingCSMSResult = new CsmsResult[] { };
 
        private static CsmsResult[] toofewCSMSResult = new CsmsResult[] { new CsmsResult { codedesc = "條件過少" } };
        private static string msgnoQueryPattern = "{0}%"; //len must>6
 
        private static string powernoQueryPattern = "{0}%"; //len must>5
        public class HelpPageApiModel
        {
            public string ResponseDocumentation { get; set; }
 
            // existing code
        }
       
        private string[] getmatchDateString(string sDate)
        {
            if (regexdate.IsMatch(string.Format(inputPattern,sDate)) ) {
 
                return regexdate.Match(string.Format(inputPattern, sDate)).Value
                    .Replace(replaceEsc , "")
                    .Split("/-".ToCharArray()) ;
            }
            return nothingString;
        }
        private bool checkAcceptAPI(queryCond cond)
        {
            if(cond is null) return false;
            string szClient = cond.client;
            if(szClient == null) return false;
 
            bool bPass = false;
            
            using (developEntities entities = new developEntities())
            {
                var allowSource =( from lst601 in entities.SysEnvVar
                                  where lst601.CodeID == 601 
                                  select lst601.CodeValue).ToList();
                if (allowSource.Count==0) { bPass = false; }
                else
                {
                    List<string> lstCode = new List<string>();
                    foreach (string code in allowSource)
                    {
                        lstCode.AddRange(code.Split(new char[] { ',' }));
                    }
                    lstCode.RemoveAll(x => x == "");
                    if(!String.IsNullOrEmpty( szClient))
                    {
                      
                        if( lstCode.Contains(szClient))
                            bPass = true; //accept
                    }
                }
            }
 
            return bPass;
        }
     
        
        [AllowedIP]
        public IEnumerable<CsmsResult> query(queryCond cond)
        {
            if (!checkAcceptAPI(cond)) return nothingCSMSResult;
 
            int check_condsize = 0;
            
            List<CsmsResult> results = new List<CsmsResult>();
            
            using (developEntities entities = new developEntities())
            {
                //string entitySQL = "select MsgNo, Email,MailorName ,MailorTel,UnitName,CateName FROM FeaturesResult  ";
                string entitySQL = "select * FROM FeaturesResult   ";
                List < System.Data.SqlClient.SqlParameter> queryParam = new List<System.Data.SqlClient.SqlParameter>();
 
                List<string> condarray = new List<string>();
                if (cond.email != null) { check_condsize++; condarray.Add(" Email = @email"); queryParam.Add(new System.Data.SqlClient.SqlParameter("@email", cond.email )); }
                if (cond.mailorname != null) { check_condsize++; condarray.Add(" MailorName = @mailorname "); queryParam.Add(new System.Data.SqlClient.SqlParameter("@mailorname", cond.mailorname)); }
                if (cond.mailortel != null) {  string tel = cond.mailortel.Replace(" ", "").Replace("%", ""); if (tel.Length >= 6) { tel = "%" + tel + "%"; check_condsize++; condarray.Add(" MailorTel like @mailortel"); queryParam.Add(new System.Data.SqlClient.SqlParameter("@mailortel","%"+ tel+ "%" )); } else { } }
                if (cond.msgno != null && cond.msgno.Length>=6) { check_condsize++; condarray.Add(" MsgNo like @msgno"); queryParam.Add(new System.Data.SqlClient.SqlParameter("@msgno", string.Format(msgnoQueryPattern, cond.msgno.Replace("%","").Trim() +"%" ))  ) ; }
                if (cond.powerno != null && cond.powerno.Length >= 6) { check_condsize++; condarray.Add(" PowerNo = @powerno"); queryParam.Add(new System.Data.SqlClient.SqlParameter("@powerno",string.Format( powernoQueryPattern, cond.powerno.Replace("%", "").Trim() + "%" ))); }
                if (cond.createdate_from != null) {
                    try { 
                    string[] from_dt = getmatchDateString(cond.createdate_from);
                    if (from_dt.Length==3)
                    {
//
                        DateTime dtFrom = new DateTime(
                            Convert.ToInt16(from_dt[0]), Convert.ToInt16(from_dt[1]), Convert.ToInt16(from_dt[2])
                            );
                            //[SelfReceivedDate]
                            condarray.Add(" CreatedDate >= @dtFrom"); queryParam.Add(new System.Data.SqlClient.SqlParameter("@dtFrom", dtFrom));
                    }
                    }catch(Exception ex)
                    {
                        //faildt
                    }
                }
                if (cond.createdate_to != null)
                {
                    try
                    {
                        string[] to_dt = getmatchDateString(cond.createdate_to);
                        if (to_dt.Length == 3)
                        {
                            DateTime dtFrom = new DateTime(
                                Convert.ToInt16(to_dt[0]), Convert.ToInt16(to_dt[1]), Convert.ToInt16(to_dt[2])
                                );
 
                            condarray.Add(" CreatedDate <= @dateto"); queryParam.Add(new System.Data.SqlClient.SqlParameter("@dateto", dtFrom));
                        }
                    }
                    catch (Exception ex)
                    {
                        //faildt
                    }
                }
                //must have one  or more cond
                if (check_condsize ==0) {
                    return toofewCSMSResult;
                }                   
 
                for (int i=0; i< condarray.Count; i++)       
                {
                    if (i == 0)
                    {
                        entitySQL += " WHERE ";
                    }else
                    {
                        entitySQL += " AND ";
                    }
                    entitySQL+= condarray[i];
                }
 
                var res = entities.FeaturesResult.SqlQuery(entitySQL, queryParam.ToArray()).
                    ToList().OrderByDescending(x => x.currentProsID);
                   
                var allunit = entities.SYS_UNIT_M.ToList();
                CsmsResult csmsResult = null;
                string tempMsgno = "";
                foreach (var oneRec in res)
                {
                    
                    var thisunit=allunit.Find(f => f.UnitID == oneRec.ToUnit);
                    var qunit = ((thisunit.MasterUnit != null && thisunit.MasterUnit != "-1") ? thisunit.MasterUnit : thisunit.UnitID);
                    var masterunit = allunit.Find(f => f.UnitID == qunit);
                    string dtAllow_d = (oneRec.AllowedDate != null ? oneRec.AllowedDate.Value.ToString("yyyy/MM/dd HH:mm:ss") : "NULL");
                    //string dtAllow_t = (oneRec.AllowedDate != null ? oneRec.AllowedDate.Value.ToLongTimeString() : "");
                    string dtSelf = (oneRec.SelfDonedDate != null ? oneRec.SelfDonedDate.Value.ToString("yyyy/MM/dd") : "NULL");
 
                    csmsResult = new CsmsResult { msgno = oneRec.MsgNo, mailorname = oneRec.MailorName, mailortel = oneRec.MailorTel,
                        email = oneRec.Email, catename = oneRec.CateName ,powerno= oneRec.PowerNo,
                        allowedddate= dtAllow_d  ,selfdoneddate= dtSelf,codedesc= (oneRec.MsgState==9 ? "已回覆" :"辦理中"),
                        unitname =thisunit.UnitName ,masterunit=masterunit.UnitName,ismaster= (oneRec.IsMaster==1 ? "主案件" :"") };
                      
                    /*
                    results.Add(new CsmsResult { msgno = oneRec.MsgNo, mailorname = oneRec.MailorName, mailortel = oneRec.MailorTel,
                        email = oneRec.Email, catename = oneRec.CateName ,powerno= oneRec.PowerNo,
                        allowedddate= dtAllow_d  ,selfdoneddate= dtSelf,codedesc= (oneRec.MsgState==9 ? "已回覆" :"辦理中"),
                        unitname =thisunit.UnitName ,masterunit=masterunit.UnitName,ismaster= (oneRec.IsMaster==1 ? "主案件" :"") });
                    */
 
                    if (oneRec == null)
                    {
                        continue;
                    }
 
 
 
                    //如果相等,已有寫入過此編號
                    // 111 -- toxxx
                    // 111 -- toooo
                    // 111 -- toroot
 
                    // 112  <-
 
                    if (oneRec.MsgNo.Trim().Equals(tempMsgno))
                    {
                   
                        // 111 -- toooo [1+]
                        // 111 -- toroot [1+] <---
                        // 112  <--[1+]
                        //not 1, 只要有就不加入前面的
                        if (!oneRec.ToUnit.Trim().Equals("root"))
                        {
                            //toroot 不要 其他都加
                            results.Add(csmsResult);
 
                        }
                    }
                    else
                    {
 
 
                        // 111 -- toxxx [1]
                        // 111 -- toooo [1+]
                        // 111 -- toroot [1+]
                        // 112  <--[1+]
 
                     //   if (!tempMsgno.Equals(""))
                     //   {
                            // 111 -- toxxx [1]
                            // 112  <--[1+]
                            //save 111
//                            results.Add(csmsResult);
                    //    }
                        // 111 -- toroot [1]
                        // 111 -- toxxx [1+]
 
                        tempMsgno = oneRec.MsgNo.Trim();
 
                        string toMsgNo = oneRec.MsgNo.Trim().Equals(oneRec.MsgID.Trim()) ? "未受理" : oneRec.MsgNo.Trim();
                    
 
                        csmsResult.msgno = toMsgNo;
 
                        if (!csmsResult.msgno.Equals("未受理"))
                            results.Add(csmsResult); //latest
 
                        
                        
 
                        //檢查是否為併案信件
                        //if (item.IsCombin == 0 && item.FileCombinNo.Length > 0)
                        
                    }
                    
                }
                        
                    
 
            }
            return results.ToArray();
        }
    }
}