markus / ConvertService / ServiceBase / Markus.Service.DataBase / ConvertDatabase.cs @ 41fa3d98
이력 | 보기 | 이력해설 | 다운로드 (16.7 KB)
1 |
using Markus.Message; |
---|---|
2 |
using Markus.Service.Extensions; |
3 |
using System; |
4 |
using System.Collections.Generic; |
5 |
using System.IO; |
6 |
using System.Linq; |
7 |
|
8 |
namespace Markus.Service.DataBase |
9 |
{ |
10 |
public class ConvertDatabase : IDisposable |
11 |
{ |
12 |
MarkusEntities entities; |
13 |
|
14 |
public ConvertDatabase(string ConnectionString) |
15 |
{ |
16 |
try |
17 |
{ |
18 |
|
19 |
//해당 프로젝트의 데이터베이스 연결 |
20 |
string databaseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"); |
21 |
entities = new MarkusEntities(ConnectionString); |
22 |
} |
23 |
catch (Exception ex) |
24 |
{ |
25 |
System.Diagnostics.Debug.WriteLine(ex.ToString()); |
26 |
throw; |
27 |
} |
28 |
} |
29 |
|
30 |
public void Dispose() |
31 |
{ |
32 |
entities.Dispose(); |
33 |
} |
34 |
|
35 |
/// <summary> |
36 |
/// 컨버터서비스를 이용하는 문서 정보 저장 |
37 |
/// UniqueID |
38 |
/// using Markus.Service.Extensions; new Guid().CreateUniqueGuid().ToString(); |
39 |
/// </summary> |
40 |
/// <param name="ProjectNo">프로젝트</param> |
41 |
/// <param name="UniqueID">컨버터 서비스의 아이템 ID</param> |
42 |
/// <param name="originfilePath"></param> |
43 |
/// <param name="DocumentID"></param> |
44 |
/// <returns></returns> |
45 |
public bool SetConvertDoc(string ProjectNo, string UniqueID, string originfilePath, string DocumentID) |
46 |
{ |
47 |
bool result = false; |
48 |
|
49 |
if (ProjectNo.IsNullOrEmpty()) |
50 |
{ |
51 |
throw new Exception("ProjectNo is Null"); |
52 |
} |
53 |
|
54 |
if (UniqueID.IsNullOrEmpty()) |
55 |
{ |
56 |
throw new Exception("UniqueID is Null"); |
57 |
} |
58 |
|
59 |
if (originfilePath.IsNullOrEmpty()) |
60 |
{ |
61 |
throw new Exception("originfilePath is Null"); |
62 |
} |
63 |
|
64 |
if (DocumentID.IsNullOrEmpty()) |
65 |
{ |
66 |
throw new Exception("DocumentID is Null"); |
67 |
} |
68 |
|
69 |
if (DocumentID.IsNullOrEmpty()) |
70 |
{ |
71 |
long _lastDocId = 0; |
72 |
|
73 |
if (entities.CONVERTER_DOC.Count() > 0) |
74 |
{ |
75 |
try |
76 |
{ |
77 |
string qry = @"SELECT * FROM CONVERTER_DOC order by CAST(DOCUMENT_ID as int) desc"; |
78 |
var _item = entities.CONVERTER_DOC.SqlQuery(qry).FirstOrDefault(); |
79 |
|
80 |
if (_item != null) |
81 |
{ |
82 |
_lastDocId = Convert.ToInt64(_item.DOCUMENT_ID) + 1; |
83 |
} |
84 |
} |
85 |
catch (Exception) |
86 |
{ |
87 |
|
88 |
throw; |
89 |
} |
90 |
} |
91 |
|
92 |
DocumentID = _lastDocId.ToString(); |
93 |
} |
94 |
|
95 |
CONVERTER_DOC convert_doc = new CONVERTER_DOC |
96 |
{ |
97 |
ID = UniqueID, |
98 |
PROJECT_NO = ProjectNo, |
99 |
DOCUMENT_URL = originfilePath, |
100 |
DOCUMENT_ID = DocumentID, |
101 |
CREATE_DATETIME = DateTime.Now, |
102 |
STATUS = 0, |
103 |
TOTAL_PAGE = 0, |
104 |
CURRENT_PAGE = 0, |
105 |
RECONVERTER = 0 |
106 |
}; |
107 |
|
108 |
entities.CONVERTER_DOC.Add(convert_doc); |
109 |
|
110 |
if (entities.SaveChanges() == 1) |
111 |
{ |
112 |
result = true; |
113 |
} |
114 |
|
115 |
return result; |
116 |
} |
117 |
|
118 |
/// <summary> |
119 |
/// 컨버터서비스를 이용하는 문서 정보 저장 |
120 |
/// UniqueID |
121 |
/// using Markus.Service.Extensions; new Guid().CreateUniqueGuid().ToString(); |
122 |
/// </summary> |
123 |
/// <param name="ProjectNo">프로젝트</param> |
124 |
/// <param name="UniqueID">컨버터 서비스의 아이템 ID</param> |
125 |
/// <param name="originfilePath"></param> |
126 |
/// <param name="DocumentID"></param> |
127 |
/// <returns></returns> |
128 |
public bool SetDocumentInfo(string ConvertID, int totalPage, List<DOCPAGE> pageinfo) |
129 |
{ |
130 |
bool result = false; |
131 |
|
132 |
var convertDocItems = entities.CONVERTER_DOC.Where(f => f.ID == ConvertID); |
133 |
|
134 |
if (convertDocItems.Count() > 0) |
135 |
{ |
136 |
var convertDoc = convertDocItems.First(); |
137 |
|
138 |
string docId = new Guid().CreateUniqueGuid().ToString(); |
139 |
|
140 |
var docinfo = entities.DOCINFO.FirstOrDefault(x => x.PROJECT_NO == convertDoc.PROJECT_NO && x.DOCUMENT_ID == convertDoc.DOCUMENT_ID); |
141 |
|
142 |
if (docinfo != null) |
143 |
{ |
144 |
// 기존 Docpage 삭제. |
145 |
entities.DOCPAGE.RemoveRange(docinfo.DOCPAGE); |
146 |
|
147 |
// 기존 코멘트 유지를 위한 Docinfo update |
148 |
docinfo.PROJECT_NO = convertDoc.PROJECT_NO; |
149 |
docinfo.ORIGINAL_FILE = convertDoc.DOCUMENT_URL; |
150 |
docinfo.PAGE_COUNT = totalPage; |
151 |
docinfo.DOCPAGE = pageinfo; |
152 |
entities.SaveChanges(); |
153 |
result = true; |
154 |
} |
155 |
else |
156 |
{ |
157 |
DOCINFO instace = new DOCINFO |
158 |
{ |
159 |
ID = docId, |
160 |
PROJECT_NO = convertDoc.PROJECT_NO, |
161 |
ORIGINAL_FILE = convertDoc.DOCUMENT_URL, |
162 |
DOCUMENT_ID = convertDoc.DOCUMENT_ID, |
163 |
PAGE_COUNT = totalPage, |
164 |
DOCPAGE = pageinfo |
165 |
}; |
166 |
|
167 |
entities.DOCINFO.Add(instace); |
168 |
var save = entities.SaveChanges(); |
169 |
|
170 |
if (save == pageinfo.Count() + 1) |
171 |
{ |
172 |
result = true; |
173 |
} |
174 |
} |
175 |
|
176 |
|
177 |
|
178 |
} |
179 |
|
180 |
return result; |
181 |
} |
182 |
|
183 |
public Interface.ConvertItem GetConvertItem(string ProjectNo, string UniqueID) |
184 |
{ |
185 |
Interface.ConvertItem result = null; |
186 |
|
187 |
//var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID); |
188 |
var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.ID == UniqueID); |
189 |
|
190 |
if (items.Count() > 0) |
191 |
{ |
192 |
var convertItem = items.First(); |
193 |
string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID); |
194 |
|
195 |
result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath); |
196 |
} |
197 |
|
198 |
return result; |
199 |
} |
200 |
|
201 |
|
202 |
public Interface.ConvertItem GetConvertItembyDocumentID(string ProjectNo, string DocumentID) |
203 |
{ |
204 |
Interface.ConvertItem result = null; |
205 |
|
206 |
//var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID); |
207 |
var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.DOCUMENT_ID == DocumentID); |
208 |
|
209 |
if (items.Count() > 0) |
210 |
{ |
211 |
var convertItem = items.First(); |
212 |
string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID); |
213 |
|
214 |
if (convertItem.STATUS == (int)StatusCodeType.Completed) |
215 |
{ |
216 |
result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath, |
217 |
convertItem.STATUS.ToString(), convertItem.CURRENT_PAGE, convertItem.TOTAL_PAGE, convertItem.EXCEPTION); |
218 |
} |
219 |
else |
220 |
{ |
221 |
result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath); |
222 |
} |
223 |
} |
224 |
|
225 |
return result; |
226 |
} |
227 |
|
228 |
/// <summary> |
229 |
/// service에서 대기중이거나 오류인 아이템을 가져가서 컨버터 프로세스를 호출한다. |
230 |
/// 컨버터 상태가 에러이거나 2보다 작은 경우 |
231 |
/// UniqueKey는 각 데이터베이스의 문서 정보에서 유일한 키이며 컨버터저장에서 같은 폴더로의 저장을 방지 한다. |
232 |
/// |
233 |
/// </summary> |
234 |
/// <param name="aliveItems"></param> |
235 |
/// <param name="TakeCount"></param> |
236 |
/// <returns></returns> |
237 |
public List<Interface.ConvertItem> GetWaitConvertItems(List<string> projectList, IEnumerable<Interface.ConvertItem> aliveItems, int TakeCount) |
238 |
{ |
239 |
List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>(); |
240 |
|
241 |
var aliveItemList = aliveItems.Select(x => x.ProjectNumber + x.UniqueKey).ToList(); |
242 |
|
243 |
var convertWaitingItems = entities.CONVERTER_DOC.OrderBy(x => x.CREATE_DATETIME).Where( |
244 |
x => |
245 |
projectList.Contains(x.PROJECT_NO) && |
246 |
(x.STATUS == (int)StatusCodeType.None || (x.STATUS == (int)StatusCodeType.Error && x.RECONVERTER < 2))); |
247 |
|
248 |
System.Diagnostics.Debug.WriteLine(convertWaitingItems.Count()); |
249 |
|
250 |
var noneAliveItems = convertWaitingItems.Where(x => |
251 |
!aliveItemList.Contains(x.PROJECT_NO + x.DOCUMENT_ID)); |
252 |
|
253 |
System.Diagnostics.Debug.WriteLine(noneAliveItems.Count()); |
254 |
|
255 |
var items = noneAliveItems.GroupBy(f => f.DOCUMENT_ID).Select(x => x.FirstOrDefault()).Take(TakeCount).ToList(); |
256 |
|
257 |
if (items.Count() > 0) |
258 |
{ |
259 |
foreach (var convertItem in items) |
260 |
{ |
261 |
// 다시 컨버터 하는 경우 |
262 |
if (convertItem.STATUS == (int)StatusCodeType.Error) |
263 |
{ |
264 |
convertItem.RECONVERTER = convertItem.RECONVERTER + 1; |
265 |
|
266 |
} |
267 |
|
268 |
// 프로세스 실행전 시작시간 추가 |
269 |
convertItem.STATUS = (int)StatusCodeType.Wait; |
270 |
convertItem.START_DATETIME = DateTime.Now; |
271 |
|
272 |
entities.SaveChanges(); |
273 |
|
274 |
var convertitem = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL); |
275 |
convertitem.ConvertPath = GetConvertPath(convertItem.PROJECT_NO,convertItem.DOCUMENT_ID); |
276 |
|
277 |
convertItems.Add(convertitem); |
278 |
} |
279 |
} |
280 |
|
281 |
return convertItems; |
282 |
} |
283 |
|
284 |
private string GetConvertPath(string ProjectNo,string DocumentID) |
285 |
{ |
286 |
try |
287 |
{ |
288 |
return Path.Combine(BaseStorage(ProjectNo), ProjectNo + "_Tile", (System.Convert.ToInt64(DocumentID) / 100).ToString(), DocumentID); |
289 |
} |
290 |
catch (Exception) |
291 |
{ |
292 |
throw new Exception("GetConvertPath Error"); |
293 |
} |
294 |
|
295 |
} |
296 |
|
297 |
|
298 |
/// <summary> |
299 |
/// 오류나 wait으로 남아 있는 데이터베이스의 Item을 StatusCodeType.None으로 복구 |
300 |
/// </summary> |
301 |
/// <param name="aliveItems"></param> |
302 |
/// <param name="TakeCount"></param> |
303 |
/// <returns></returns> |
304 |
public int SetCleanUpItems(List<string> ProjectList) |
305 |
{ |
306 |
int result = 0; |
307 |
|
308 |
var items = entities.CONVERTER_DOC.Where( |
309 |
x => |
310 |
ProjectList.Contains(x.PROJECT_NO) && |
311 |
x.STATUS > (int)StatusCodeType.None && x.STATUS < (int)StatusCodeType.Completed |
312 |
|| (x.STATUS > (int)StatusCodeType.Error || x.STATUS > (int)StatusCodeType.FileError) && x.RECONVERTER < 2); |
313 |
|
314 |
result = items.Count(); |
315 |
|
316 |
if (items.Count() > 0) |
317 |
{ |
318 |
foreach (var item in items) |
319 |
{ |
320 |
item.RECONVERTER = item.RECONVERTER + 1; |
321 |
item.CREATE_DATETIME = DateTime.Now; |
322 |
item.START_DATETIME = null; |
323 |
item.END_DATETIME = null; |
324 |
item.STATUS = (int)StatusCodeType.None; |
325 |
} |
326 |
|
327 |
entities.SaveChanges(); |
328 |
} |
329 |
|
330 |
return result; |
331 |
} |
332 |
|
333 |
/// <summary> |
334 |
/// 전체 컨버터를 가져온다. 비동기로 변경하여야 한다. |
335 |
/// </summary> |
336 |
/// <returns></returns> |
337 |
public List<Interface.ConvertItem> GetConvertItems( StatusCodeType statusCodeType) |
338 |
{ |
339 |
List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>(); |
340 |
|
341 |
var items = entities.CONVERTER_DOC.Where(x=>x.STATUS == (int)statusCodeType).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo); |
342 |
|
343 |
if (items.Count() > 0) |
344 |
{ |
345 |
foreach (var item in items) |
346 |
{ |
347 |
var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL); |
348 |
|
349 |
convertitem.ConvertState = ((StatusCodeType)item.STATUS).ToString(); |
350 |
convertitem.CreateTime = item.CREATE_DATETIME; |
351 |
convertitem.TotalPage = item.TOTAL_PAGE; |
352 |
convertitem.CurrentPageNo = item.CURRENT_PAGE; |
353 |
convertitem.ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID); |
354 |
|
355 |
|
356 |
convertItems.Add(convertitem); |
357 |
} |
358 |
} |
359 |
return convertItems; |
360 |
} |
361 |
|
362 |
|
363 |
public bool SetConvertState(string UniqueID,int State,int CurrentPage,int TotalPage,string Error) |
364 |
{ |
365 |
bool result = false; |
366 |
|
367 |
try |
368 |
{ |
369 |
var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.ID == UniqueID); |
370 |
//var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID); |
371 |
|
372 |
if (items != null) |
373 |
{ |
374 |
items.STATUS = State; |
375 |
items.CURRENT_PAGE = CurrentPage; |
376 |
items.TOTAL_PAGE = TotalPage; |
377 |
|
378 |
StatusCodeType statusCode = (StatusCodeType)State; |
379 |
|
380 |
switch (statusCode) |
381 |
{ |
382 |
case StatusCodeType.None: |
383 |
break; |
384 |
case StatusCodeType.Wait: |
385 |
items.START_DATETIME = DateTime.Now; |
386 |
break; |
387 |
case StatusCodeType.PageLoading: |
388 |
break; |
389 |
case StatusCodeType.Saving: |
390 |
break; |
391 |
case StatusCodeType.Completed: |
392 |
items.END_DATETIME= DateTime.Now; |
393 |
break; |
394 |
case StatusCodeType.FileError: |
395 |
Error = $" file Error :" + Error; |
396 |
items.END_DATETIME = DateTime.Now; |
397 |
break; |
398 |
case StatusCodeType.PageError: |
399 |
Error = $" Page No {CurrentPage} Error :" + Error; |
400 |
break; |
401 |
case StatusCodeType.NeedsPassword: |
402 |
break; |
403 |
case StatusCodeType.Error: |
404 |
Error = $" Error :" + Error; |
405 |
items.END_DATETIME = DateTime.Now; |
406 |
break; |
407 |
default: |
408 |
break; |
409 |
} |
410 |
|
411 |
if(items.EXCEPTION == null) |
412 |
{ |
413 |
items.EXCEPTION = ""; |
414 |
} |
415 |
|
416 |
if (items.EXCEPTION.Length < 255) |
417 |
{ |
418 |
items.EXCEPTION = items.EXCEPTION + Error; |
419 |
} |
420 |
|
421 |
entities.SaveChanges(); |
422 |
result = true; |
423 |
} |
424 |
} |
425 |
catch (Exception ex) |
426 |
{ |
427 |
throw new Exception("SetConvertState",ex); |
428 |
} |
429 |
|
430 |
return result; |
431 |
} |
432 |
|
433 |
private string BaseStorage(string ProjectNo) |
434 |
{ |
435 |
string result = ""; |
436 |
|
437 |
var properties = entities.PROPERTIES.Where(f => f.PROPERTY == ProjectNo && f.TYPE == PROPERTIES_DEFINE.TILE_SOURCE_STORAGE); |
438 |
|
439 |
if(properties.Count() > 0) |
440 |
{ |
441 |
result = properties.First().VALUE; |
442 |
} |
443 |
else |
444 |
{ |
445 |
throw new Exception($"Project {ProjectNo} BaseStorage Not Setting."); |
446 |
} |
447 |
|
448 |
return result; |
449 |
} |
450 |
|
451 |
} |
452 |
} |