프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.DataBase / ConvertDatabase.cs @ 218e5002

이력 | 보기 | 이력해설 | 다운로드 (37.7 KB)

1
using Markus.EntityModel;
2
using Markus.Message;
3
using Markus.Service.Extensions;
4
using System;
5
using System.Collections.Generic;
6
using System.IO;
7
using System.Linq;
8
using static Markus.Service.DataBase.ConvertDatabase;
9
using System.Windows;
10
using System.Linq.Expressions;
11
using Markus.Service.Interface;
12
using Markus.Service.DataBase.Data;
13
using Markus.Service.DataBase.Extensions;
14

    
15

    
16
namespace Markus.Service.DataBase
17
{
18
    public class ConvertDatabase : IDisposable
19
    {
20
        #region Constructor
21
        /// <summary>
22
        ///해당 프로젝트의 데이터베이스 연결
23
        /// </summary>
24

    
25
        Markus.EntityModel.MarkusModel entities;
26

    
27
        public ConvertDatabase(string ConnectionString)
28
        {
29
            try
30
            {
31
                entities = new Markus.EntityModel.MarkusModel(ConnectionString);
32
            }
33
            catch (Exception ex)
34
            {
35
                System.Diagnostics.Debug.WriteLine(ex.ToString());
36
                throw ex;
37
            }
38
        }
39

    
40
        public void Dispose()
41
        {
42
            entities.Dispose();
43
        }
44

    
45
        #endregion
46

    
47
        #region DataBaseItemsModel Function
48

    
49
        #region Get WaitConvertItems
50

    
51
        /// <summary>
52
        /// service에서 대기중이거나 오류인 아이템을 가져가서 컨버터 프로세스를 호출한다.
53
        /// 컨버터 상태가 에러이거나 2보다 작은 경우 
54
        /// UniqueKey는 각 데이터베이스의 문서 정보에서 유일한 키이며 컨버터저장에서 같은 폴더로의 저장을 방지 한다.
55
        /// 
56
        /// </summary>
57
        /// <param name="aliveItems"></param>
58
        /// <param name="TakeCount"></param>
59
        /// <returns></returns>
60
        public IEnumerable<CONVERTER_DOC> GetWaitConvertItems(List<string> projectList, int TakeCount)
61
        {
62
            var convertWaitingItems = entities.CONVERTER_DOC.OrderBy(x => x.CREATE_DATETIME).Where(
63
                                        x =>
64
                                        //projectList.Contains(x.PROJECT_NO) && x.RECONVERTER < 2 &&
65
                                         x.RECONVERTER < 2 &&
66
                                        (x.STATUS == (int)StatusCodeType.None || x.STATUS == (int)StatusCodeType.FileError || x.STATUS == (int)StatusCodeType.Error))
67
                                    .GroupBy(x => x.DOCUMENT_ID).Select(x => x.FirstOrDefault())
68
                                    .Take(TakeCount);
69

    
70
            return convertWaitingItems;
71
        }
72

    
73
        #endregion
74

    
75
        #region Get FilterConvertItems
76

    
77
        /// <summary>
78
        /// 상단 그리드 선택된 Status에 따라 DB에서 값 가져온다.
79
        /// </summary>
80
        public IEnumerable<Interface.ConvertItem> GetConvertProjects(int status,string ProjectNumberFilter, string UniqueKeyFilter, string Docuemnt_NameFilter, string Service_IDFilter, string Docuemnt_NOFilter, int? ReconverterFilter
81
            , string RevisionFilter)
82
        {
83
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
84
            var query = from items in entities.CONVERTER_DOC
85
                        join documents in entities.DOCUMENT_ITEM on items.DOCUMENT_ID equals documents.DOCUMENT_ID into GroupItems
86
                        from subpet in GroupItems.DefaultIfEmpty()
87
                        where items.STATUS == status &&
88
                        //
89
                        (
90
                         items.PROJECT_NO.Contains(ProjectNumberFilter)
91
                         || subpet.DOCUMENT_NAME.Contains(Docuemnt_NameFilter)
92
                         || items.SERVICE_ID.Contains(Service_IDFilter)
93
                         || subpet.ID.Contains(UniqueKeyFilter)
94
                         || subpet.DOCUMENT_NO.Contains(Docuemnt_NOFilter)
95
                         || items.RECONVERTER == ReconverterFilter
96
                         || subpet.REVISION.Contains(RevisionFilter)
97
                         )
98
                       //
99
                        orderby items.CREATE_DATETIME descending
100
                        select new
101
                        {
102
                            ID = items.ID,
103
                            DOCUMENT_URL = items.DOCUMENT_URL,
104
                            RECONVERTER = items.RECONVERTER,
105
                            SERVICE_ID = items.SERVICE_ID,
106
                            STATUS = items.STATUS,
107
                            CREATE_DATETIME = items.CREATE_DATETIME,
108
                            START_DATETIME = items.START_DATETIME,
109
                            END_DATETIME = items.END_DATETIME,
110
                            TOTAL_PAGE = items.TOTAL_PAGE,
111
                            CURRENT_PAGE = items.CURRENT_PAGE,
112
                            EXCEPTION = items.EXCEPTION,
113
                            PROJECT_NO = items.PROJECT_NO,
114
                            DOCUMENT_ID = items.DOCUMENT_ID,
115
                            REVISION = subpet.REVISION,
116
                            DOCUMENT_NO = subpet.DOCUMENT_NO,
117
                            DOCUMENT_NAME = subpet.DOCUMENT_NAME,
118
                            GROUP_NO = subpet.GROUP_NO,
119
                        };
120

    
121
            if (query.Count() > 0)//세미
122
            {
123
                foreach (var item in query)
124
                {
125
                    Interface.ConvertItem convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
126
                    {
127

    
128
                        ReConverter = item.RECONVERTER,
129
                        ServiceID = item.SERVICE_ID,
130
                        ConvertState = (StatusCodeType)item.STATUS,
131
                        CreateTime = item.CREATE_DATETIME,
132
                        StartTime = (DateTime)item.START_DATETIME,
133
                        EndTime = (DateTime)item.END_DATETIME,
134
                        TotalPage = item.TOTAL_PAGE,
135
                        CurrentPageNo = item.CURRENT_PAGE,
136
                        Exception = item.EXCEPTION,
137
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID),
138

    
139
                        Revision = item.REVISION,
140
                        DocumnetNo = item.DOCUMENT_NO,
141
                        DocumnetName = item.DOCUMENT_NAME,
142
                        GroupNo = item.GROUP_NO,
143
                        DocumentID = item.DOCUMENT_ID,
144

    
145
                        MarkusLink = "kcom://" + Markus.Service.DataBase.Extensions.MarkusAPI.CreateMarkusParam(item.PROJECT_NO, item.DOCUMENT_ID, "doftech")
146
                    };
147
                    convertItems.Add(convertitem);
148
                }
149
            }
150
            return convertItems;
151

    
152
        }
153

    
154
       
155
        public IEnumerable<Interface.ConvertItem> GetConvertProjects(int status)
156
        {
157
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
158
            var query = from items in entities.CONVERTER_DOC
159
                        join documents in entities.DOCUMENT_ITEM on items.DOCUMENT_ID equals documents.DOCUMENT_ID into GroupItems
160
                        from subpet in GroupItems.DefaultIfEmpty()
161
                        where items.STATUS == status
162
                        orderby items.CREATE_DATETIME descending
163
                        select new
164
                        {
165
                            ID = items.ID,
166
                            DOCUMENT_URL = items.DOCUMENT_URL,
167
                            RECONVERTER = items.RECONVERTER,
168
                            SERVICE_ID = items.SERVICE_ID,
169
                            STATUS = items.STATUS,
170
                            CREATE_DATETIME = items.CREATE_DATETIME,
171
                            START_DATETIME = items.START_DATETIME,
172
                            END_DATETIME = items.END_DATETIME,
173
                            TOTAL_PAGE = items.TOTAL_PAGE,
174
                            CURRENT_PAGE = items.CURRENT_PAGE,
175
                            EXCEPTION = items.EXCEPTION,
176
                            PROJECT_NO = items.PROJECT_NO,
177
                            DOCUMENT_ID = items.DOCUMENT_ID,
178
                            REVISION = subpet.REVISION,
179
                            DOCUMENT_NO = subpet.DOCUMENT_NO,
180
                            DOCUMENT_NAME = subpet.DOCUMENT_NAME,
181
                            GROUP_NO = subpet.GROUP_NO,
182
                        };
183

    
184
            if (query.Count() > 0)//세미
185
            {
186
                foreach (var item in query)
187
                {
188
                    Interface.ConvertItem convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
189
                    {
190

    
191
                        ReConverter = item.RECONVERTER,
192
                        ServiceID = item.SERVICE_ID,
193
                        ConvertState = (StatusCodeType)item.STATUS,
194
                        CreateTime = item.CREATE_DATETIME,
195
                        StartTime = (DateTime)item.START_DATETIME,
196
                        EndTime = (DateTime)item.END_DATETIME,
197
                        TotalPage = item.TOTAL_PAGE,
198
                        CurrentPageNo = item.CURRENT_PAGE,
199
                        Exception = item.EXCEPTION,
200
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID),
201

    
202
                        Revision = item.REVISION,
203
                        DocumnetNo = item.DOCUMENT_NO,
204
                        DocumnetName = item.DOCUMENT_NAME,
205
                        GroupNo = item.GROUP_NO,
206
                        DocumentID = item.DOCUMENT_ID,
207

    
208
                        MarkusLink = "kcom://" + Markus.Service.DataBase.Extensions.MarkusAPI.CreateMarkusParam(item.PROJECT_NO, item.DOCUMENT_ID, "doftech")
209
                    };
210
                    convertItems.Add(convertitem);
211
                }
212
            }
213
            return convertItems;
214

    
215
        }
216

    
217
        public string[] GetSearchConvertPathAndMarkus(string PROJECT_NO, string DOCUMENT_ID)
218
        {
219
            string[] ConvertPath = new string[2];
220
            ConvertPath[0] = GetConvertPath(PROJECT_NO, DOCUMENT_ID);
221
            ConvertPath[1] = "kcom://" + Markus.Service.DataBase.Extensions.MarkusAPI.CreateMarkusParam(PROJECT_NO, DOCUMENT_ID, "doftech");
222

    
223
            return ConvertPath;
224
        }
225

    
226
        #endregion
227

    
228
        #region Update RealDataSelect 
229

    
230
        /// <summary>
231
        /// 상단 그리드에서 선택된 RealDataSelect
232
        /// 용도: 중앙그리드 Items Update
233
        /// </summary>
234
        public IEnumerable<Interface.ConvertItem> GetConvertProjects(System.Collections.ObjectModel.ObservableCollection<Interface.ConvertItem> selectedConvert)
235
        {
236
            List<Interface.ConvertItem> StatusKeep = new List<Interface.ConvertItem>();
237

    
238
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
239

    
240
            foreach (var selectedConvertItem in selectedConvert)
241
            {
242
                var query = from items in entities.CONVERTER_DOC //세미
243
                            join documents in entities.DOCUMENT_ITEM on items.DOCUMENT_ID equals documents.DOCUMENT_ID into GroupItems
244
                            from subpet in GroupItems.DefaultIfEmpty()
245
                            where items.PROJECT_NO == selectedConvertItem.ProjectNumber && items.ID == selectedConvertItem.ConvertID
246
                            select new
247
                            {
248
                                ID = items.ID,
249
                                DOCUMENT_URL = items.DOCUMENT_URL,
250
                                RECONVERTER = items.RECONVERTER,
251
                                SERVICE_ID = items.SERVICE_ID,
252
                                STATUS = items.STATUS,
253
                                CREATE_DATETIME = items.CREATE_DATETIME,
254
                                START_DATETIME  = items.START_DATETIME,
255
                                END_DATETIME = items.END_DATETIME,
256
                                TOTAL_PAGE = items.TOTAL_PAGE,
257
                                CURRENT_PAGE = items.CURRENT_PAGE,
258
                                EXCEPTION = items.EXCEPTION,
259
                                PROJECT_NO = items.PROJECT_NO,
260
                                DOCUMENT_ID = items.DOCUMENT_ID,
261
                                REVISION = subpet.REVISION,
262
                                DOCUMENT_NO = subpet.DOCUMENT_NO,
263
                                DOCUMENT_NAME = subpet.DOCUMENT_NAME,
264
                                GROUP_NO = subpet.GROUP_NO
265
                            };
266

    
267

    
268
                if (query.Count() > 0)//세미
269
                {
270
                    foreach (var item in query)
271
                    {
272
                        Interface.ConvertItem convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
273
                        {
274

    
275
                            ReConverter = item.RECONVERTER,
276
                            ServiceID = item.SERVICE_ID,
277
                            ConvertState = (StatusCodeType)item.STATUS,
278
                            CreateTime = item.CREATE_DATETIME,
279
                            StartTime = item.START_DATETIME,
280
                            EndTime = item.END_DATETIME,
281
                            TotalPage = item.TOTAL_PAGE,
282
                            CurrentPageNo = item.CURRENT_PAGE,
283
                            Exception = item.EXCEPTION,
284
                            ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID),
285

    
286
                            Revision = item.REVISION,
287
                            DocumnetNo = item.DOCUMENT_NO,
288
                            DocumnetName = item.DOCUMENT_NAME,
289
                            GroupNo = item.GROUP_NO,
290

    
291
                            MarkusLink = "kcom://" + MarkusAPI.CreateMarkusParam(item.PROJECT_NO, item.DOCUMENT_ID, "doftech")
292
                        };
293

    
294
                        StatusKeep.Add(convertitem);
295

    
296
                    }
297
                }
298
            }
299
            return StatusKeep;
300
        }
301

    
302
        #endregion
303

    
304
        #region Data Convert
305

    
306
        /// <summary>
307
        /// 상단 그리드에서 선택된 Convert 버튼 
308
        /// 용도: 중앙 그리드로 Convert된 row 값 저장 하기 위해서 입니다.
309
        /// </summary>
310
        public IEnumerable<Interface.ConvertItem> GetConvertProjects(Markus.Service.Interface.ConvertItem selectedConvert)
311
        {
312
            List<Interface.ConvertItem> StatusKeep = new List<Interface.ConvertItem>();
313

    
314

    
315
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
316

    
317
            stopwatch.Start();
318

    
319

    
320
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();//디비 다 조인 후 조건 비교 후 그리드 출력
321
            var query = from items in entities.CONVERTER_DOC //세미
322
                        join documents in entities.DOCUMENT_ITEM on items.DOCUMENT_ID equals documents.DOCUMENT_ID into GroupItems
323
                        from subpet in GroupItems.DefaultIfEmpty()
324
                        where items.PROJECT_NO == selectedConvert.ProjectNumber && items.DOCUMENT_ID == selectedConvert.DocumentID
325
                        orderby items.CREATE_DATETIME descending
326
                        select new
327
                        {
328
                            ID = items.ID,
329
                            DOCUMENT_URL = items.DOCUMENT_URL,
330
                            RECONVERTER = items.RECONVERTER,
331
                            SERVICE_ID = items.SERVICE_ID,
332
                            STATUS = items.STATUS,
333
                            CREATE_DATETIME = items.CREATE_DATETIME,
334
                            START_DATETIME = items.START_DATETIME,
335
                            END_DATETIME = items.END_DATETIME,
336
                            TOTAL_PAGE = items.TOTAL_PAGE,
337
                            CURRENT_PAGE = items.CURRENT_PAGE,
338
                            EXCEPTION = items.EXCEPTION,
339
                            PROJECT_NO = items.PROJECT_NO,
340
                            DOCUMENT_ID = items.DOCUMENT_ID,
341
                            REVISION = subpet.REVISION,
342
                            DOCUMENT_NO = subpet.DOCUMENT_NO,
343
                            DOCUMENT_NAME = subpet.DOCUMENT_NAME,
344
                            GROUP_NO = subpet.GROUP_NO
345
                        };
346

    
347
            if (query.Count() > 0)//세미
348
            {
349
                foreach (var item in query)
350
                {
351
                    Interface.ConvertItem convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
352
                    {
353

    
354
                        ReConverter = item.RECONVERTER,
355
                        ServiceID = item.SERVICE_ID,
356
                        ConvertState = (StatusCodeType)item.STATUS,
357
                        CreateTime = item.CREATE_DATETIME,
358
                        StartTime = item.START_DATETIME,
359
                        EndTime = item.END_DATETIME,
360
                        TotalPage = item.TOTAL_PAGE,
361
                        CurrentPageNo = item.CURRENT_PAGE,
362
                        Exception = item.EXCEPTION,
363
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID),
364
                        Revision = item.REVISION,
365
                        DocumnetNo = item.DOCUMENT_NO,
366
                        DocumnetName = item.DOCUMENT_NAME,
367
                        GroupNo = item.GROUP_NO,
368

    
369
                        MarkusLink = "kcom://" + Markus.Service.DataBase.Extensions.MarkusAPI.CreateMarkusParam(item.PROJECT_NO, item.DOCUMENT_ID, "doftech")
370
                    };
371
                    StatusKeep.Add(convertitem);
372
                }
373
            }
374
            System.Diagnostics.Debug.WriteLine(new TimeSpan(stopwatch.ElapsedTicks).ToString());
375
            return StatusKeep;
376

    
377
        }
378

    
379
        #endregion
380

    
381
        #region SetCleanUpItem
382
        /// <summary>
383
        /// 오류나 wait으로 남아 있는 데이터베이스의 Item을 StatusCodeType.None으로 복구
384
        /// </summary>
385
        /// <param name="convertID">StatusCodeType을 변경하고자 하는 ConvertItem의 ID</param>
386
        /// <param name="ReConvert">ReConvert가 2이면 다시 Convert를 시도 안하기 때문에 기존 값으로 유지 하고 계속 시도 할수 있도록 0을 주는 경우가 있다.</param>
387
        /// <returns></returns>
388
        /// 
389
        public int SetCleanUpItem(ConvertItem _ConvertItem)
390
        {
391
            int result = 0;
392

    
393
            var items = entities.CONVERTER_DOC.Where(x => x.ID == _ConvertItem.ConvertID);
394

    
395
            result = items.Count();
396

    
397
            if (items.Count() > 0)
398
            {
399
                var item = items.First();
400

    
401
                item.RECONVERTER = item.RECONVERTER - _ConvertItem.ReConverter;
402

    
403
                item.CREATE_DATETIME = DateTime.Now;
404
                item.STATUS = (int)StatusCodeType.None;
405
                item.END_DATETIME = null;
406

    
407
                result = entities.SaveChanges();
408
            }
409

    
410
            return result;
411
        }
412

    
413
        public int SetCleanUpItem(string convertID, int ReConvert)
414
        {
415
            int result = 0;
416

    
417
            var items = entities.CONVERTER_DOC.Where(x => x.ID == convertID);
418

    
419
            result = items.Count();
420

    
421
            if (items.Count() > 0)
422
            {
423
                var item = items.First();
424

    
425
                item.RECONVERTER = item.RECONVERTER + ReConvert;
426
                item.CREATE_DATETIME = DateTime.Now;
427
                //item.START_DATETIME = null;
428
                item.END_DATETIME = null;
429
                item.STATUS = (int)StatusCodeType.None;
430

    
431
                result = entities.SaveChanges();
432
            }
433

    
434
            return result;
435
        }
436
        #endregion
437

    
438
        #region Convert Path
439

    
440
        private string GetConvertPath(string ProjectNo, string DocumentID)
441
        {
442
            try
443
            {
444
                return Path.Combine(BaseStorage(ProjectNo), ProjectNo + "_Tile", (System.Convert.ToInt64(DocumentID) / 100).ToString(), DocumentID);
445
            }
446
            catch (Exception)
447
            {
448
                throw new Exception("GetConvertPath Error");
449
            }
450

    
451
        }
452

    
453
        private string BaseStorage(string ProjectNo)
454
        {
455
            string result = "";
456

    
457
            var properties = entities.PROPERTIES.Where(f => f.PROPERTY == ProjectNo && f.TYPE == PROPERTIES_DEFINE.TILE_SOURCE_STORAGE);
458

    
459
            if (properties.Count() > 0)
460
            {
461
                result = properties.First().VALUE;
462
            }
463
            else
464
            {
465
                throw new Exception($"Project {ProjectNo} BaseStorage Not Setting.");
466
            }
467

    
468
            return result;
469
        }
470

    
471
        #endregion
472

    
473
        #region Remove
474

    
475
        /// <summary>
476
        /// Item 삭제
477
        /// </summary>
478
        /// <returns></returns>
479
        public int RemoveItem(string convertID)
480
        {
481
            int result = 0;
482

    
483
            var items = entities.CONVERTER_DOC.Where(x => x.ID == convertID);
484

    
485
            if (items.Count() > 0)
486
            {
487
                CONVERTER_DOC item = items.First();
488
                entities.CONVERTER_DOC.Remove(item);
489

    
490
                result = entities.SaveChanges();
491
            }
492

    
493
            return result;
494
        }
495

    
496

    
497
        #endregion
498

    
499
        #region MarkusLink
500

    
501

    
502
        ////// userID는 관리자 계정인 doftech
503
        ////string markusLink = "kcom://" + MarkusAPI.CreateMarkusParam(projectNo, documentID, userID);
504
        //public static string CreateMarkusParam(string projectNo, string documentID, string userID)
505
        //{
506
        //    ViewInfo viewInfo = new ViewInfo();
507

    
508
        //    viewInfo.DocumentItemID = documentID;
509
        //    viewInfo.ProjectNO = projectNo;
510
        //    viewInfo.UserID = userID;
511

    
512
        //    return ParamEncoding(JsonConvert.SerializeObject(viewInfo));
513

    
514
        //}
515

    
516
        //public static string ParamEncoding(string EncodingText, System.Text.Encoding oEncoding = null)
517
        //{
518

    
519
        //    if (oEncoding == null)
520
        //        oEncoding = System.Text.Encoding.UTF8;
521

    
522
        //    return Convert.ToBase64String(oEncoding.GetBytes(EncodingText));
523

    
524
        //}
525

    
526

    
527
        #endregion
528

    
529
        #endregion
530

    
531
        #region DataBaseByStatusViewModel Function
532
        /// <summary>
533
        /// 전체 컨버터를 가져온다. 비동기로 변경하여야 한다.
534
        /// </summary>
535
        /// <returns></returns>
536
        public List<Interface.ConvertItem> GetConvertItems(StatusCodeType statusCodeType, Func<CONVERTER_DOC, bool> @where, int takeCount = 100)
537
        {
538
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
539

    
540

    
541
            var items = entities.CONVERTER_DOC.Where(x => x.STATUS == (int)statusCodeType).Take(takeCount).ToList();
542

    
543
            if (items.Count() > 0)
544
            {
545
                foreach (var item in items)
546
                {
547
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
548
                    {
549
                        ReConverter = item.RECONVERTER,
550
                        ServiceID = item.SERVICE_ID,
551
                        ConvertState = ((StatusCodeType)item.STATUS),
552
                        CreateTime = item.CREATE_DATETIME,
553
                        TotalPage = item.TOTAL_PAGE,
554
                        CurrentPageNo = item.CURRENT_PAGE,
555

    
556
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID)
557
                    };
558

    
559
                    convertItems.Add(convertitem);
560
                }
561
            }
562
            return convertItems;
563
        }
564
        #endregion
565

    
566
        #region No 참조 
567

    
568
        /// <summary>
569
        /// 컨버터서비스를 이용하는 문서 정보 저장
570
        /// UniqueID
571
        /// using Markus.Service.Extensions;  new Guid().CreateUniqueGuid().ToString();
572
        /// </summary>
573
        /// <param name="ProjectNo">프로젝트</param>
574
        /// <param name="UniqueID">컨버터 서비스의 아이템 ID</param>
575
        /// <param name="originfilePath"></param>
576
        /// <param name="DocumentID"></param>
577
        /// <returns></returns>
578
        public bool SetConvertDoc(string ProjectNo, string UniqueID, string originfilePath, string DocumentID)
579
        {
580
            bool result = false;
581

    
582
            if (ProjectNo.IsNullOrEmpty())
583
            {
584
                throw new Exception("ProjectNo is Null");
585
            }
586

    
587
            if (UniqueID.IsNullOrEmpty())
588
            {
589
                throw new Exception("UniqueID is Null");
590
            }
591

    
592
            if (originfilePath.IsNullOrEmpty())
593
            {
594
                throw new Exception("originfilePath is Null");
595
            }
596

    
597
            if (DocumentID.IsNullOrEmpty())
598
            {
599
                throw new Exception("DocumentID is Null");
600
            }
601

    
602
            if (DocumentID.IsNullOrEmpty())
603
            {
604
                long _lastDocId = 0;
605

    
606
                if (entities.CONVERTER_DOC.Count() > 0)
607
                {
608
                    try
609
                    {
610
                        string qry = @"SELECT * FROM CONVERTER_DOC order by CAST(DOCUMENT_ID as int) desc";
611
                        var _item = entities.CONVERTER_DOC.SqlQuery(qry).FirstOrDefault();
612

    
613
                        if (_item != null)
614
                        {
615
                            _lastDocId = Convert.ToInt64(_item.DOCUMENT_ID) + 1;
616
                        }
617
                    }
618
                    catch (Exception)
619
                    {
620

    
621
                        throw;
622
                    }
623
                }
624

    
625
                DocumentID = _lastDocId.ToString();
626
            }
627

    
628
            CONVERTER_DOC convert_doc = new CONVERTER_DOC
629
            {
630
                ID = UniqueID,
631
                PROJECT_NO = ProjectNo,
632
                DOCUMENT_URL = originfilePath,
633
                DOCUMENT_ID = DocumentID,
634
                CREATE_DATETIME = DateTime.Now,
635
                STATUS = 0,
636
                TOTAL_PAGE = 0,
637
                CURRENT_PAGE = 0,
638
                RECONVERTER = 0
639
            };
640

    
641
            
642
            entities.CONVERTER_DOC.Add(convert_doc);
643

    
644
            if (entities.SaveChanges() == 1)
645
            {
646
                result = true;
647
            }
648

    
649
            return result;
650
        }
651

    
652
        /// <summary>
653
        /// 컨버터서비스를 이용하는 문서 정보 저장
654
        /// UniqueID
655
        /// using Markus.Service.Extensions;  new Guid().CreateUniqueGuid().ToString();
656
        /// </summary>
657
        /// <param name="ProjectNo">프로젝트</param>
658
        /// <param name="UniqueID">컨버터 서비스의 아이템 ID</param>
659
        /// <param name="originfilePath"></param>
660
        /// <param name="DocumentID"></param>
661
        /// <returns></returns>
662
        public bool SetDocumentInfo(string ConvertID, int totalPage, List<DOCPAGE> pageinfo)
663
        {
664
            bool result = false;
665

    
666
            var convertDocItems = entities.CONVERTER_DOC.Where(f => f.ID == ConvertID);
667

    
668
            if (convertDocItems.Count() > 0)
669
            {
670
                var convertDoc = convertDocItems.First();
671

    
672
                string docId = new Guid().CreateUniqueGuid().ToString();
673

    
674
                var docinfo = entities.DOCINFO.FirstOrDefault(x => x.PROJECT_NO == convertDoc.PROJECT_NO && x.DOCUMENT_ID == convertDoc.DOCUMENT_ID);
675
                
676
                if (docinfo != null)
677
                {
678
                    // 기존 Docpage 삭제.
679
                    entities.DOCPAGE.RemoveRange(docinfo.DOCPAGE);
680

    
681
                    // 기존 코멘트 유지를 위한 Docinfo update                    
682
                    docinfo.PROJECT_NO = convertDoc.PROJECT_NO;
683
                    docinfo.ORIGINAL_FILE = convertDoc.DOCUMENT_URL;
684
                    docinfo.PAGE_COUNT = totalPage;                    
685
                    docinfo.DOCPAGE = pageinfo;                    
686
                    entities.SaveChanges();
687
                    result = true;
688
                }
689
                else
690
                {
691
                    DOCINFO instace = new DOCINFO
692
                    {
693
                        ID = docId,
694
                        PROJECT_NO = convertDoc.PROJECT_NO,
695
                        ORIGINAL_FILE = convertDoc.DOCUMENT_URL,
696
                        DOCUMENT_ID = convertDoc.DOCUMENT_ID,
697
                        PAGE_COUNT = totalPage,
698
                        DOCPAGE = pageinfo
699
                    };
700

    
701
                    entities.DOCINFO.Add(instace);
702
                    var save = entities.SaveChanges();
703

    
704
                    if (save == pageinfo.Count() + 1)
705
                    {
706
                        result = true;
707
                    }
708
                }
709

    
710
                
711
                
712
            }
713

    
714
            return result;
715
        }
716

    
717
        public Interface.ConvertItem GetConvertItem(string ProjectNo, string UniqueID)
718
        {
719
            Interface.ConvertItem result = null;
720

    
721
            //var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID);
722
            var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.ID == UniqueID);
723

    
724
            if (items.Count() > 0)
725
            {
726
                var convertItem = items.First();
727

    
728
                string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID);
729

    
730
                result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath);
731
            }
732

    
733
            return result;
734
        }
735

    
736

    
737
        public Interface.ConvertItem GetConvertItembyDocumentID(string ProjectNo, string DocumentID)
738
        {
739
            Interface.ConvertItem result = null;
740

    
741
            //var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID);
742
            var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.DOCUMENT_ID == DocumentID);
743

    
744
            if (items.Count() > 0)
745
            {
746
                var convertItem = items.First();
747
                string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID);
748

    
749
                if (convertItem.STATUS == (int)StatusCodeType.Completed)
750
                {
751
                    result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath,
752
                                                        (StatusCodeType)convertItem.STATUS, convertItem.CURRENT_PAGE, convertItem.TOTAL_PAGE, convertItem.EXCEPTION);
753
                }
754
                else
755
                {
756
                    result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath);
757
                }
758
            }
759

    
760
            return result;
761
        }
762

    
763
        public List<CONVERTER_DOC> GetConvertingItems(List<string> projectList)
764
        {
765
            var convertWaitingItems = entities.CONVERTER_DOC.OrderBy(x => x.CREATE_DATETIME).Where(
766
                                        x =>
767
                                        projectList.Contains(x.PROJECT_NO) &&
768
                                         (x.STATUS > (int)StatusCodeType.None && x.STATUS < (int)StatusCodeType.Completed) && x.RECONVERTER < 2);
769

    
770
            return convertWaitingItems.ToList();
771
        }
772

    
773
        /// <summary>
774
        /// 전체 컨버터를 가져온다. 비동기로 변경하여야 한다.
775
        /// </summary>
776
        /// <returns></returns>
777
        public List<Interface.ConvertItem> GetConvertItems(StatusCodeType statusCodeType)
778
        {
779
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
780

    
781
            var items = entities.CONVERTER_DOC.Where(x => x.STATUS == (int)statusCodeType).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo);
782

    
783
            if (items.Count() > 0)
784
            {
785
                foreach (var item in items)
786
                {
787
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
788
                    {
789
                        ReConverter = item.RECONVERTER,
790
                        ServiceID = item.SERVICE_ID,
791
                        ConvertState = ((StatusCodeType)item.STATUS),
792
                        CreateTime = item.CREATE_DATETIME,
793
                        TotalPage = item.TOTAL_PAGE,
794
                        CurrentPageNo = item.CURRENT_PAGE,
795

    
796
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID)
797
                    };
798

    
799

    
800
                    convertItems.Add(convertitem);
801
                }
802
            }
803
            return convertItems;
804
        }
805

    
806
        public List<Interface.ConvertItem> GetConvertProjectsStatus(Func<CONVERTER_DOC, bool> @where)
807
        {
808
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
809

    
810

    
811
            //var items = entities.CONVERTER_DOC.Where(x => x.STATUS == (int)statusCodeType).Take(takeCount).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo);
812
            var items = entities.CONVERTER_DOC.Where(x => x.STATUS <= 4);
813

    
814
            if (items.Count() > 0)
815
            {
816
                foreach (var item in items)
817
                {
818
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
819
                    {
820
                        ReConverter = item.RECONVERTER,
821
                        ServiceID = item.SERVICE_ID,
822
                        //ConvertState = ((StatusCodeType)item.STATUS).ToString(),
823
                        //ConvertState = (item.STATUS).ToString(),
824
                        ConvertState = (StatusCodeType)item.STATUS,
825
                        CreateTime = item.CREATE_DATETIME,
826
                        TotalPage = item.TOTAL_PAGE,
827
                        CurrentPageNo = item.CURRENT_PAGE,
828
                        Exception = item.EXCEPTION,
829
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID)
830
                    };
831

    
832

    
833
                    convertItems.Add(convertitem);
834
                }
835
            }
836

    
837
            return convertItems;
838
        }
839

    
840

    
841
        public bool SetConvertState(string ServiceID, string UniqueID, int State, int CurrentPage, int TotalPage, string Error)
842
        {
843
            bool result = false;
844

    
845
            try
846
            {
847
                var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.ID == UniqueID);
848
                //var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID);
849

    
850
                if (items != null)
851
                {
852
                    items.SERVICE_ID = ServiceID;
853
                    items.STATUS = State;
854
                    items.CURRENT_PAGE = CurrentPage;
855
                    items.TOTAL_PAGE = TotalPage;
856

    
857
                    StatusCodeType statusCode = (StatusCodeType)State;
858

    
859
                    if (items.START_DATETIME == null)
860
                    {
861
                        items.START_DATETIME = DateTime.Now;
862
                    }
863

    
864
                    switch (statusCode)
865
                    {
866
                        case StatusCodeType.None:
867
                            break;
868
                        case StatusCodeType.Wait:
869
                            items.START_DATETIME = DateTime.Now;
870
                            break;
871
                        case StatusCodeType.PageLoading:
872
                            break;
873
                        case StatusCodeType.Saving:
874
                            break;
875
                        case StatusCodeType.Completed:
876
                            items.CURRENT_PAGE = items.TOTAL_PAGE;
877
                            items.END_DATETIME = DateTime.Now;
878
                            break;
879
                        case StatusCodeType.FileError:
880
                            Error = $" file Error :" + Error;
881
                            items.END_DATETIME = DateTime.Now;
882
                            break;
883
                        case StatusCodeType.PageError:
884
                            Error = $" Page No {CurrentPage} Error :" + Error;
885
                            break;
886
                        case StatusCodeType.NeedsPassword:
887
                            break;
888
                        case StatusCodeType.Error:
889
                            Error = $" Error :" + Error;
890
                            items.END_DATETIME = DateTime.Now;
891
                            break;
892
                        default:
893
                            break;
894
                    }
895

    
896
                    if (items.EXCEPTION == null)
897
                    {
898
                        items.EXCEPTION = "";
899
                    }
900

    
901
                    if (items.EXCEPTION.Length < 255)
902
                    {
903
                        items.EXCEPTION = items.EXCEPTION + Error;
904
                    }
905

    
906
                    entities.SaveChanges();
907
                    result = true;
908
                }
909
            }
910
            catch (Exception ex)
911
            {
912
                throw new Exception("SetConvertState", ex);
913
            }
914

    
915
            return result;
916
        }
917

    
918
        public SERVICE_PROPERTIES GetServiceProperties(string ServiceID)
919
        {
920
            SERVICE_PROPERTIES result = null;
921

    
922
            var items = entities.SERVICE_PROPERTIES.Where(x => x.ID == ServiceID);
923

    
924
            if (items.Count() > 0)
925
            {
926
                result = items.First();
927
            }
928

    
929
            return result;
930
        }
931

    
932

    
933
        /// <summary>
934
        /// 현재 서비스에서 컨버터중인 아이템.
935
        /// </summary>
936
        /// <returns></returns>
937
        public List<Interface.ConvertItem> GetServiceAliveItems(string ServiceID)
938
        {
939
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
940

    
941
            var items = entities.CONVERTER_DOC.Where(x => x.SERVICE_ID == ServiceID && x.STATUS > (int)StatusCodeType.None && x.STATUS < (int)StatusCodeType.Completed).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo);
942

    
943
            if (items.Count() > 0)
944
            {
945
                foreach (var item in items)
946
                {
947
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL);
948
                    convertItems.Add(convertitem);
949
                }
950
            }
951
            return convertItems;
952
        }
953

    
954
        #endregion
955

    
956
    }
957
}
클립보드 이미지 추가 (최대 크기: 500 MB)