markus / ConvertService / ServiceBase / Markus.Service.DataBase / ConvertDatabase.cs @ 53c9637d
이력 | 보기 | 이력해설 | 다운로드 (16.1 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 |
var docId = new Guid().CreateUniqueGuid(); |
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 |
entities.DOCPAGE.RemoveRange(docinfo.DOCPAGE); |
145 |
entities.DOCINFO.Remove(docinfo); |
146 |
entities.SaveChanges(); |
147 |
} |
148 |
|
149 |
DOCINFO instace = new DOCINFO |
150 |
{ |
151 |
ID = docId.ToString(), |
152 |
PROJECT_NO = convertDoc.PROJECT_NO, |
153 |
ORIGINAL_FILE = convertDoc.DOCUMENT_URL, |
154 |
DOCUMENT_ID = convertDoc.DOCUMENT_ID, |
155 |
PAGE_COUNT = totalPage, |
156 |
DOCPAGE = pageinfo |
157 |
}; |
158 |
|
159 |
entities.DOCINFO.Add(instace); |
160 |
var save = entities.SaveChanges(); |
161 |
|
162 |
if (save == pageinfo.Count() + 1) |
163 |
{ |
164 |
result = true; |
165 |
} |
166 |
} |
167 |
|
168 |
return result; |
169 |
} |
170 |
|
171 |
public Interface.ConvertItem GetConvertItem(string ProjectNo, string UniqueID) |
172 |
{ |
173 |
Interface.ConvertItem result = null; |
174 |
|
175 |
//var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID); |
176 |
var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.ID == UniqueID); |
177 |
|
178 |
if (items.Count() > 0) |
179 |
{ |
180 |
var convertItem = items.First(); |
181 |
string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID); |
182 |
|
183 |
result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath); |
184 |
} |
185 |
|
186 |
return result; |
187 |
} |
188 |
|
189 |
|
190 |
public Interface.ConvertItem GetConvertItembyDocumentID(string ProjectNo, string DocumentID) |
191 |
{ |
192 |
Interface.ConvertItem result = null; |
193 |
|
194 |
//var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID); |
195 |
var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.DOCUMENT_ID == DocumentID); |
196 |
|
197 |
if (items.Count() > 0) |
198 |
{ |
199 |
var convertItem = items.First(); |
200 |
string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID); |
201 |
|
202 |
if (convertItem.STATUS == (int)StatusCodeType.Completed) |
203 |
{ |
204 |
result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath, |
205 |
convertItem.STATUS.ToString(), convertItem.CURRENT_PAGE, convertItem.TOTAL_PAGE, convertItem.EXCEPTION); |
206 |
} |
207 |
else |
208 |
{ |
209 |
result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath); |
210 |
} |
211 |
} |
212 |
|
213 |
return result; |
214 |
} |
215 |
|
216 |
/// <summary> |
217 |
/// service에서 대기중이거나 오류인 아이템을 가져가서 컨버터 프로세스를 호출한다. |
218 |
/// 컨버터 상태가 에러이거나 2보다 작은 경우 |
219 |
/// UniqueKey는 각 데이터베이스의 문서 정보에서 유일한 키이며 컨버터저장에서 같은 폴더로의 저장을 방지 한다. |
220 |
/// |
221 |
/// </summary> |
222 |
/// <param name="aliveItems"></param> |
223 |
/// <param name="TakeCount"></param> |
224 |
/// <returns></returns> |
225 |
public List<Interface.ConvertItem> GetWaitConvertItems(List<string> projectList, IEnumerable<Interface.ConvertItem> aliveItems, int TakeCount) |
226 |
{ |
227 |
List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>(); |
228 |
|
229 |
var aliveItemList = aliveItems.Select(x => x.ProjectNumber + x.UniqueKey).ToList(); |
230 |
|
231 |
var convertWaitingItems = entities.CONVERTER_DOC.OrderBy(x => x.CREATE_DATETIME).Where( |
232 |
x => |
233 |
projectList.Contains(x.PROJECT_NO) && |
234 |
(x.STATUS == (int)StatusCodeType.None || (x.STATUS == (int)StatusCodeType.Error && x.RECONVERTER < 2))); |
235 |
|
236 |
System.Diagnostics.Debug.WriteLine(convertWaitingItems.Count()); |
237 |
|
238 |
var noneAliveItems = convertWaitingItems.Where(x => |
239 |
!aliveItemList.Contains(x.PROJECT_NO + x.DOCUMENT_ID)); |
240 |
|
241 |
System.Diagnostics.Debug.WriteLine(noneAliveItems.Count()); |
242 |
|
243 |
var items = noneAliveItems.GroupBy(f => f.DOCUMENT_ID).Select(x => x.FirstOrDefault()).Take(TakeCount).ToList(); |
244 |
|
245 |
if (items.Count() > 0) |
246 |
{ |
247 |
foreach (var convertItem in items) |
248 |
{ |
249 |
// 다시 컨버터 하는 경우 |
250 |
if (convertItem.STATUS == (int)StatusCodeType.Error) |
251 |
{ |
252 |
convertItem.RECONVERTER = convertItem.RECONVERTER + 1; |
253 |
|
254 |
} |
255 |
|
256 |
// 프로세스 실행전 시작시간 추가 |
257 |
convertItem.STATUS = (int)StatusCodeType.Wait; |
258 |
convertItem.START_DATETIME = DateTime.Now; |
259 |
|
260 |
entities.SaveChanges(); |
261 |
|
262 |
var convertitem = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL); |
263 |
convertitem.ConvertPath = GetConvertPath(convertItem.PROJECT_NO,convertItem.DOCUMENT_ID); |
264 |
|
265 |
convertItems.Add(convertitem); |
266 |
} |
267 |
} |
268 |
|
269 |
return convertItems; |
270 |
} |
271 |
|
272 |
private string GetConvertPath(string ProjectNo,string DocumentID) |
273 |
{ |
274 |
try |
275 |
{ |
276 |
return Path.Combine(BaseStorage(ProjectNo), ProjectNo + "_Tile", (System.Convert.ToInt64(DocumentID) / 100).ToString(), DocumentID); |
277 |
} |
278 |
catch (Exception) |
279 |
{ |
280 |
throw new Exception("GetConvertPath Error"); |
281 |
} |
282 |
|
283 |
} |
284 |
|
285 |
|
286 |
/// <summary> |
287 |
/// 오류나 wait으로 남아 있는 데이터베이스의 Item을 StatusCodeType.None으로 복구 |
288 |
/// </summary> |
289 |
/// <param name="aliveItems"></param> |
290 |
/// <param name="TakeCount"></param> |
291 |
/// <returns></returns> |
292 |
public int SetCleanUpItems(List<string> ProjectList) |
293 |
{ |
294 |
int result = 0; |
295 |
|
296 |
var items = entities.CONVERTER_DOC.Where( |
297 |
x => |
298 |
ProjectList.Contains(x.PROJECT_NO) && |
299 |
x.STATUS > (int)StatusCodeType.None && x.STATUS < (int)StatusCodeType.Completed |
300 |
|| (x.STATUS > (int)StatusCodeType.Error || x.STATUS > (int)StatusCodeType.FileError) && x.RECONVERTER < 2); |
301 |
|
302 |
result = items.Count(); |
303 |
|
304 |
if (items.Count() > 0) |
305 |
{ |
306 |
foreach (var item in items) |
307 |
{ |
308 |
item.RECONVERTER = item.RECONVERTER + 1; |
309 |
item.CREATE_DATETIME = DateTime.Now; |
310 |
item.START_DATETIME = null; |
311 |
item.END_DATETIME = null; |
312 |
item.STATUS = (int)StatusCodeType.None; |
313 |
} |
314 |
|
315 |
entities.SaveChanges(); |
316 |
} |
317 |
|
318 |
return result; |
319 |
} |
320 |
|
321 |
/// <summary> |
322 |
/// 전체 컨버터를 가져온다. 비동기로 변경하여야 한다. |
323 |
/// </summary> |
324 |
/// <returns></returns> |
325 |
public List<Interface.ConvertItem> GetConvertItems( StatusCodeType statusCodeType) |
326 |
{ |
327 |
List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>(); |
328 |
|
329 |
var items = entities.CONVERTER_DOC.Where(x=>x.STATUS == (int)statusCodeType).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo); |
330 |
|
331 |
if (items.Count() > 0) |
332 |
{ |
333 |
foreach (var item in items) |
334 |
{ |
335 |
var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL); |
336 |
|
337 |
convertitem.ConvertState = ((StatusCodeType)item.STATUS).ToString(); |
338 |
convertitem.CreateTime = item.CREATE_DATETIME; |
339 |
convertitem.TotalPage = item.TOTAL_PAGE; |
340 |
convertitem.CurrentPageNo = item.CURRENT_PAGE; |
341 |
convertitem.ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID); |
342 |
|
343 |
|
344 |
convertItems.Add(convertitem); |
345 |
} |
346 |
} |
347 |
return convertItems; |
348 |
} |
349 |
|
350 |
|
351 |
public bool SetConvertState(string UniqueID,int State,int CurrentPage,int TotalPage,string Error) |
352 |
{ |
353 |
bool result = false; |
354 |
|
355 |
try |
356 |
{ |
357 |
var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.ID == UniqueID); |
358 |
//var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID); |
359 |
|
360 |
if (items != null) |
361 |
{ |
362 |
items.STATUS = State; |
363 |
items.CURRENT_PAGE = CurrentPage; |
364 |
items.TOTAL_PAGE = TotalPage; |
365 |
|
366 |
StatusCodeType statusCode = (StatusCodeType)State; |
367 |
|
368 |
switch (statusCode) |
369 |
{ |
370 |
case StatusCodeType.None: |
371 |
break; |
372 |
case StatusCodeType.Wait: |
373 |
items.START_DATETIME = DateTime.Now; |
374 |
break; |
375 |
case StatusCodeType.PageLoading: |
376 |
break; |
377 |
case StatusCodeType.Saving: |
378 |
break; |
379 |
case StatusCodeType.Completed: |
380 |
items.END_DATETIME= DateTime.Now; |
381 |
break; |
382 |
case StatusCodeType.FileError: |
383 |
Error = $" file Error :" + Error; |
384 |
items.END_DATETIME = DateTime.Now; |
385 |
break; |
386 |
case StatusCodeType.PageError: |
387 |
Error = $" Page No {CurrentPage} Error :" + Error; |
388 |
break; |
389 |
case StatusCodeType.NeedsPassword: |
390 |
break; |
391 |
case StatusCodeType.Error: |
392 |
Error = $" Error :" + Error; |
393 |
items.END_DATETIME = DateTime.Now; |
394 |
break; |
395 |
default: |
396 |
break; |
397 |
} |
398 |
|
399 |
if(items.EXCEPTION == null) |
400 |
{ |
401 |
items.EXCEPTION = ""; |
402 |
} |
403 |
|
404 |
if (items.EXCEPTION.Length < 255) |
405 |
{ |
406 |
items.EXCEPTION = items.EXCEPTION + Error; |
407 |
} |
408 |
|
409 |
entities.SaveChanges(); |
410 |
result = true; |
411 |
} |
412 |
} |
413 |
catch (Exception ex) |
414 |
{ |
415 |
throw new Exception("SetConvertState",ex); |
416 |
} |
417 |
|
418 |
return result; |
419 |
} |
420 |
|
421 |
private string BaseStorage(string ProjectNo) |
422 |
{ |
423 |
string result = ""; |
424 |
|
425 |
var properties = entities.PROPERTIES.Where(f => f.PROPERTY == ProjectNo && f.TYPE == PROPERTIES_DEFINE.TILE_SOURCE_STORAGE); |
426 |
|
427 |
if(properties.Count() > 0) |
428 |
{ |
429 |
result = properties.First().VALUE; |
430 |
} |
431 |
else |
432 |
{ |
433 |
throw new Exception($"Project {ProjectNo} BaseStorage Not Setting."); |
434 |
} |
435 |
|
436 |
return result; |
437 |
} |
438 |
|
439 |
} |
440 |
} |