-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operation.cs
340 lines (288 loc) · 12.3 KB
/
Operation.cs
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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace RegisterParcelsFromPC
{
public class Operation
{
public string connStr;
public Operation(string connStr)
{
this.connStr = connStr;
}
public void execute_sql(string sqrstr)
{
using (var conn = new SqlConnection(connStr))
{
try
{
//接続
conn.Open();
//コマンド実行
SqlCommand command_register = new SqlCommand(sqrstr, conn);
command_register.ExecuteNonQuery();
//接続断
conn.Close();
}catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public List<string> get_all_uid(string sqlstr)
{//受取時:ある寮生名義の事務室にある荷物の荷物番号をすべて取得しList<int>で返す→11/3 stringで返すよう変更
//定期チェック時:削除可能なイベントのuidをすべて取得しListで返す
List<string> CurrentParcels = new List<string>();
SqlConnection con = new SqlConnection(connStr);
con.Open();
try
{
SqlCommand com = new SqlCommand(sqlstr, con);
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read() == true)
{
string uid = (string)sdr["uid"];
CurrentParcels.Add(uid);
}
sdr.Close();
com.Dispose();
}catch(Exception e)
{
NLogService.PrintInfoLog("例外_getalluid");
NLogService.PrintInfoLog(e.ToString());
}
finally
{
con.Close();
}
return CurrentParcels;
}
public string select_one_xx(string sqlstr, string xx)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
string uid = null ;
//int uidint = 0;
try
{
SqlCommand com = new SqlCommand(sqlstr, con);
SqlDataReader sdr = com.ExecuteReader();
sdr.Read();
if (sdr[xx] == DBNull.Value)
{
uid = "null";
}
else
{
uid = (string)sdr[xx];
//uidint = (int)sdr[xx];
}
sdr.Close();
com.Dispose();
}
catch(Exception e)
{
NLogService.PrintInfoLog("例外_selectonexx");
NLogService.PrintInfoLog(e.ToString());
}
finally
{
con.Close();
}
return uid;
}
public int select_one_xx_int(string sqlstr, string xx)
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
int uid = 0;
//int uidint = 0;
try
{
SqlCommand com = new SqlCommand(sqlstr, con);
SqlDataReader sdr = com.ExecuteReader();
sdr.Read();
if (sdr[xx] == DBNull.Value)
{
uid = 0;
}
else
{
uid = (int)sdr[xx];
//uidint = (int)sdr[xx];
}
sdr.Close();
com.Dispose();
}
catch (Exception e)
{
NLogService.PrintInfoLog("例外_selectonexx");
NLogService.PrintInfoLog(e.ToString());
}
finally
{
con.Close();
}
return uid;
}
public string calculate_registered_time(List<string> ParcelID, DateTime now, string owner_uid)//ここはリファクタリングなど一切していない
{
List<DateTime> RegiTime_list = new List<DateTime>();
string total_past_waittime = "";
string ParcelID_List = "";
foreach (string aParcelID in ParcelID)
{
ParcelID_List += "'";
ParcelID_List += aParcelID.ToString();
ParcelID_List += "'";
ParcelID_List += ",";
}
ParcelID_List = ParcelID_List.Trim(',');
SqlConnection con = new SqlConnection(connStr);
con.Open();
try
{
//荷物の登録時間をすべて取得し、regi_time_listに格納
string sqlstr = $@"select register_datetime from parcels where uid in ({ParcelID_List})";
SqlCommand com = new SqlCommand(sqlstr, con);
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read() == true)
{
DateTime regi_time = (DateTime)sdr["register_datetime"];
RegiTime_list.Add(regi_time);
}
sdr.Close();
com.Dispose();
//寮生のこれまでの累計時間を取得し、total_past_waittimeに格納
string sqlstr2 = $@"select parcels_total_waittime from ryosei where uid='{owner_uid}'";
SqlCommand com2 = new SqlCommand(sqlstr2, con);
SqlDataReader sdr2 = com2.ExecuteReader();
while (sdr2.Read() == true)
{
total_past_waittime = (string)sdr2["parcels_total_waittime"];
}
sdr2.Close();
com2.Dispose();
}
finally
{
con.Close();
}
//もっといいやり方があるかもしれないが、一旦これで良し
int total_second = 0;
//これまでの累計時間を秒になおして格納
string[] past_working = total_past_waittime.Split(':');//23:15:24 23時間15分24秒
total_second += int.Parse(past_working[0]) * 3600;
total_second += int.Parse(past_working[1]) * 60;
total_second += int.Parse(past_working[2]);
//今回取得し加算する時間を秒に直して格納
TimeSpan totalTimeSpan = new TimeSpan(0, 0, 0, 0);//{370.06:16:25.6079800} 370日6時間16分25秒 24時間を超えない場合は{00:00:03.8976284}のように、~日の部分がないので処理を変える
foreach (DateTime aRegiTime in RegiTime_list)
{
totalTimeSpan += now - aRegiTime;
}
string[] working1 = totalTimeSpan.ToString().Split('.');//"370.06:16:25.6079800"
if (working1.Length == 3)//24時間を超える場合
{
total_second += int.Parse(working1[0]) * 24 * 60 * 60;
string[] working2 = working1[1].Split(':');//06.16.25
total_second += int.Parse(working2[0]) * 60 * 60;
total_second += int.Parse(working2[1]) * 60;
total_second += int.Parse(working2[2]);
}
else//2しかあり得ないはず、24時間を超えない場合
{
string[] working2 = working1[0].Split(':');//06.16.25
total_second += int.Parse(working2[0]) * 60 * 60;
total_second += int.Parse(working2[1]) * 60;
total_second += int.Parse(working2[2]);
}
//秒数を、hour:min:secの形のstringに変換
string new_totalTime = "";
new_totalTime += (total_second / 3600).ToString();
new_totalTime += ":";
total_second %= 3600;
new_totalTime += (total_second / 60).ToString("00");
new_totalTime += ":";
total_second %= 60;
new_totalTime += total_second.ToString("00");
return new_totalTime;
}
public void calculate_registered_time2(string parcelID)//ここはリファクタリングなど一切していない
{
DateTime regi_time, rele_time;
string owner_uid;
string total_past_waittime;
SqlConnection con = new SqlConnection(connStr);
con.Open();
try
{
//荷物の登録時間をすべて取得し、regi_time_listに格納
string sqlstr = $@"select register_datetime,release_datetime,owner_uid from parcels where uid ='{parcelID}'";
SqlCommand com = new SqlCommand(sqlstr, con);
SqlDataReader sdr = com.ExecuteReader();
sdr.Read();
owner_uid = (string)sdr["owner_uid"];
regi_time = (DateTime)sdr["register_datetime"];
rele_time = (DateTime)sdr["release_datetime"];
sdr.Close();
com.Dispose();
//寮生のこれまでの累計時間を取得し、total_past_waittimeに格納
string sqlstr2 = $@"select parcels_total_waittime from ryosei where uid='{owner_uid}'";
SqlCommand com2 = new SqlCommand(sqlstr2, con);
SqlDataReader sdr2 = com2.ExecuteReader();
sdr2.Read();
total_past_waittime = (string)sdr2["parcels_total_waittime"];
sdr2.Close();
com2.Dispose();
//もっといいやり方があるかもしれないが、一旦これで良し
int total_second = 0;
//これまでの累計時間を秒になおして格納
string[] past_working = total_past_waittime.Split(':');//23:15:24 23時間15分24秒
total_second += int.Parse(past_working[0]) * 3600;
total_second += int.Parse(past_working[1]) * 60;
total_second += int.Parse(past_working[2]);
//今回取得し加算する時間を秒に直して格納
TimeSpan totalTimeSpan = new TimeSpan(0, 0, 0, 0);//{370.06:16:25.6079800} 370日6時間16分25秒 24時間を超えない場合は{00:00:03.8976284}のように、~日の部分がないので処理を変える
totalTimeSpan += rele_time - regi_time;
string[] working1 = totalTimeSpan.ToString().Split('.');//"370.06:16:25.6079800" もしくは "06:16:25.6079800"
if (working1.Length == 3)//24時間を超える場合
{
total_second += int.Parse(working1[0]) * 24 * 60 * 60;
string[] working2 = working1[1].Split(':');//06.16.25
total_second += int.Parse(working2[0]) * 60 * 60;
total_second += int.Parse(working2[1]) * 60;
total_second += int.Parse(working2[2]);
}
else//2しかあり得ないはず、24時間を超えない場合
{
string[] working2 = working1[0].Split(':');//06.16.25
total_second += int.Parse(working2[0]) * 60 * 60;
total_second += int.Parse(working2[1]) * 60;
total_second += int.Parse(working2[2]);
}
//秒数を、hour:min:secの形のstringに変換
string new_totalTime = "";
new_totalTime += (total_second / 3600).ToString();
new_totalTime += ":";
total_second %= 3600;
new_totalTime += (total_second / 60).ToString("00");
new_totalTime += ":";
total_second %= 60;
new_totalTime += total_second.ToString("00");
MakeSQLCommand make = new MakeSQLCommand();
string sql = make.toUpdate_ryosei_totalwaittime(owner_uid, new_totalTime);
execute_sql(sql);
}
catch(Exception e)
{
NLogService.PrintInfoLog("例外_calculated_register_datetime2");
NLogService.PrintInfoLog(e.ToString());
}
finally
{
con.Close();
}
}
}
}