프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / DocumentRepository.cs @ e8a86b9b

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6

    
7
using System.Data;
8

    
9
using ID2.Manager.Data.Models;
10

    
11
using Dapper;
12
using System.ComponentModel;
13
using System.Reflection;
14

    
15
namespace ID2.Manager.Dapper.Repository
16
{
17
    public class DocumentRepository : BaseRepository
18
    {
19
        public DocumentRepository(string connectionStr) : base(connectionStr) { }
20

    
21
        public IEnumerable<Documents> GetAllDocuments(string projectGroupID)
22
        {
23
            var dynamicParameters = new DynamicParameters();
24
            StringBuilder sbWhere = new StringBuilder();
25
            var parameters = new Dictionary<string, object>();
26
            if (!string.IsNullOrEmpty(projectGroupID))
27
            {
28
                sbWhere.Append(" and doc.RefProjectCode in (select Code from dbo.Projects where ParentID=@RefGroupID) ");
29
                parameters.Add("RefGroupID", projectGroupID);
30
            }
31

    
32
            try
33
            {
34
                string query = $@"
35
select   doc.*
36
from     dbo.Documents doc
37
where    doc.IsDeleted=0 {sbWhere}
38
order by doc.Seq;";
39

    
40
                if (parameters.Count > 0)
41
                {
42
                    dynamicParameters.AddDynamicParams(parameters);
43
                }
44

    
45
                return Query<Documents>(query, dynamicParameters);
46
            }
47
            catch (Exception ex)
48
            {
49
                throw ex;
50
            }
51
        }
52
        static string GetDescriptionFromAttribute(MemberInfo member)
53
        {
54
            if (member == null) return null;
55

    
56
            var attrib = (DescriptionAttribute)Attribute.GetCustomAttribute(member, typeof(DescriptionAttribute), false);
57
            return (attrib?.Description ?? member.Name).ToLower();
58
        }
59

    
60

    
61
        public (IEnumerable<Documents> dwgs, int totalCnt) GetDocuments(string projectGroupID, string projectCode, string personIncharge, string jobLevel, string documentNo, string isToIsDiscussion, string isFrReviewStatus, string isID2Work, string id2Status, string avevaStatus, string prodIsResult, string clientIsResult, string isGateWay, string isRegSystem)
62
        {
63
            var map = new CustomPropertyTypeMap(typeof(AttFileInfo), (type, columnName)
64
     => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName.ToLower()));
65
            SqlMapper.SetTypeMap(typeof(AttFileInfo), map);
66

    
67

    
68
            var dynamicParameters = new DynamicParameters();
69
            dynamicParameters.Add("Total", dbType: DbType.Int32, direction: ParameterDirection.Output);
70

    
71
            StringBuilder sbWhere = new StringBuilder();
72
            StringBuilder sbTotalWhere = new StringBuilder();
73
            var parameters = new Dictionary<string, object>();
74
            if (string.IsNullOrEmpty(projectCode))
75
            {
76
                sbWhere.Append(" and isnull(doc.RefProjectCode,'') in (select Code from dbo.Projects where ParentID=@RefGroupID union all select '') ");
77
                sbTotalWhere.Append(" and isnull(doc.RefProjectCode,'') in (select Code from dbo.Projects where ParentID=@RefGroupID union all select '') ");
78
                parameters.Add("RefGroupID", projectGroupID);
79
            }
80
            else
81
            {
82
                sbTotalWhere.Append(" and isnull(doc.RefProjectCode,'') in (select Code from dbo.Projects where ParentID=@RefGroupID union all select '') ");
83
                parameters.Add("RefGroupID", projectGroupID);
84

    
85
                sbWhere.Append(" and doc.RefProjectCode=@RefProjectCode ");
86
                parameters.Add("RefProjectCode", projectCode);
87
            }
88
            if (!string.IsNullOrEmpty(personIncharge))
89
            {
90
                sbWhere.Append(" and doc.PersonInCharge=@PersonInCharge ");
91
                parameters.Add("PersonInCharge", personIncharge);
92
            }
93
            if (!string.IsNullOrEmpty(jobLevel))
94
            {
95
                sbWhere.Append(" and doc.JobLevel=@JobLevel ");
96
                parameters.Add("JobLevel", jobLevel);
97
            }
98
            if (!string.IsNullOrEmpty(documentNo))
99
            {
100
                sbWhere.Append(" and doc.DocumentNo like '%' + @DocumentNo +'%' ");
101
                parameters.Add("DocumentNo", documentNo);
102
            }
103

    
104
            if (!string.IsNullOrEmpty(isToIsDiscussion))
105
            {
106
                sbWhere.Append(" and doc.ToIsDiscussion=@ToIsDiscussion ");
107
                parameters.Add("ToIsDiscussion", isToIsDiscussion);
108
            }
109

    
110
            if (!string.IsNullOrEmpty(isFrReviewStatus))
111
            {
112
                sbWhere.Append(" and doc.FrReviewStatus=@FrReviewStatus ");
113
                parameters.Add("FrReviewStatus", isFrReviewStatus);
114
            }
115

    
116
            if (!string.IsNullOrEmpty(isID2Work))
117
            {
118
                sbWhere.Append(" and doc.IsID2Work=@IsID2Work ");
119
                parameters.Add("IsID2Work", isID2Work);
120
            }
121

    
122
            if (!string.IsNullOrEmpty(id2Status))
123
            {
124
                sbWhere.Append(" and doc.ID2Status=@ID2Status ");
125
                parameters.Add("ID2Status", id2Status);
126
            }
127
            if (!string.IsNullOrEmpty(avevaStatus))
128
            {
129
                sbWhere.Append(" and doc.AVEVAStatus=@AVEVAStatus ");
130
                parameters.Add("AVEVAStatus", avevaStatus);
131
            }
132

    
133
            if (!string.IsNullOrEmpty(prodIsResult))
134
            {
135
                sbWhere.Append(" and doc.ProdIsResult=@ProdIsResult ");
136
                parameters.Add("ProdIsResult", prodIsResult);
137
            }
138
            if (!string.IsNullOrEmpty(clientIsResult))
139
            {
140
                sbWhere.Append(" and doc.ClientIsResult=@ClientIsResult ");
141
                parameters.Add("ClientIsResult", clientIsResult);
142
            }
143
            if (!string.IsNullOrEmpty(isGateWay))
144
            {
145
                sbWhere.Append(" and doc.DTIsGateWay=@DTIsGateWay ");
146
                parameters.Add("DTIsGateWay", isGateWay);
147
            }
148
            if (!string.IsNullOrEmpty(isRegSystem))
149
            {
150
                sbWhere.Append(" and doc.DTIsRegSystem=@DTIsRegSystem ");
151
                parameters.Add("DTIsRegSystem", isRegSystem);
152
            }
153

    
154
            try
155
            {
156

    
157
                //files.[FileID],files.[FileID] as AttFileID, files.RefID, files.Category, files.FileType, files.FileName, files.FilePath, files.FileExtension, files.FileData, files.Remark, files.CreatedDate, files.Creator,
158
                    string query = $@"
159
                            select  doc.*, datediff(SECOND, doc.ID2StartDate, doc.ID2EndDate) as ID2JobTime,
160
                            files.[FileID] as FileID, files.*,
161
                            markus.MARKUP_DATA_ID as MARKUP_DATA_ID, markus.*, 
162
                            datediff(SECOND, doc.ID2StartDate, doc.ID2EndDate) as ID2JobTime
163
                            from     dbo.Documents doc
164
	                        LEFT OUTER JOIN AttachFIles files ON doc.DocID = fIles.RefID 
165
                            LEFT OUTER JOIN 
166
                                    (SELECT [MARKUP_DATA_ID]
167
                                           ,[PROJECT_NO] as PROJECT_NO
168
                                          ,[DOCUMENT_ID] as DocumentNo
169
                                          
170
                                          ,[PAGENUMBER]
171
                                          ,[Text] as TEXT
172
                                          ,[CREATE_DATE] as CREATE_DATE
173
                                          ,[NAME] as CREATE_USER
174
                                      FROM ViewMarkupData) markus 
175
                            ON doc.DocumentNo = markus.DocumentNo
176
                            where    doc.IsDeleted=0 {sbWhere}
177
                            order by doc.Seq
178

    
179
select @Total=count(*) from dbo.Documents doc where doc.IsDeleted=0 {sbTotalWhere}
180
select @Total;";
181

    
182
                if (parameters.Count > 0)
183
                {
184
                    dynamicParameters.AddDynamicParams(parameters);
185
                }
186

    
187
                var docDictionary = new Dictionary<string, Documents>();
188

    
189
                var ret = MultiQuery<Documents, AttFileInfo, MarkupText, Documents>(query,
190
                                (document, attfile, markusText) =>
191
                                {
192
                                    Documents doc;
193

    
194
                                    if (!docDictionary.TryGetValue(document.DocID, out doc))
195
                                    {
196
                                        doc = document;
197
                                        docDictionary.Add(doc.DocID, doc);
198
                                    }
199

    
200
                                    if (markusText != null)
201
                                    {
202
                                        doc.Markups = doc.Markups ?? new List<MarkupText>();
203

    
204
                                        if (!doc.Markups.Any(x => x.MARKUP_DATA_ID == markusText.MARKUP_DATA_ID))
205
                                        {
206
                                            doc.Markups.Add(markusText);
207
                                        }
208
                                    }
209

    
210
                                    if (attfile != null)
211
                                    {
212
                                        doc.AttFiles = doc.AttFiles ?? new List<AttFileInfo>();
213
                                        System.Diagnostics.Debug.WriteLine(attfile.FileName);
214
                                        if (!doc.AttFiles.Any(x => x.FileID == attfile.FileID))
215
                                        {
216
                                            switch (attfile.Category)
217
                                            {
218
                                                case "toreview":
219
                                                    doc.ToCapture++;
220
                                                    break;
221
                                                case "frreview":
222
                                                    doc.FrCapture++;
223
                                                    break;
224
                                                case "prodvalidation":
225
                                                    doc.ProdCapture++;
226
                                                    break;
227
                                                case "clientvalidation":
228
                                                    doc.ClientCapture++;
229
                                                    break;
230
                                            }
231

    
232
                                            doc.AttFiles.Add(attfile);
233
                                        }
234
                                    }
235

    
236
                                    return doc;
237

    
238
                                }, dynamicParameters, splitOn: "DocID,FileID,MARKUP_DATA_ID").Distinct();
239

    
240
                int totalCount = dynamicParameters.Get<int>("Total");
241

    
242
                return (ret, totalCount);
243
            }
244
            catch (Exception ex)
245
            {
246
                throw ex;
247
            }
248
        }
249

    
250
        public bool SetDocumentData(List<Documents> docList, List<Documents> delDocList, string userId)
251
        {
252
            bool isSuccess = false;
253

    
254
            try
255
            {
256
                using (var transaction = base.BeginTransaction())
257
                {
258
                    string query = string.Empty;
259

    
260
                    if (delDocList.Count > 0)
261
                    {
262
                        string docIDList = string.Join("','", delDocList.Where(x => !string.IsNullOrEmpty(x.DocID)).Select(x => x.DocID).ToList());
263

    
264
                        if (docIDList.Length > 0)
265
                        {
266
                            query = $@"
267
update dbo.Documents
268
set    IsDeleted=1
269
      ,DeletedDate=getdate(),
270
      ,DeletedUser=@DeletedUser
271
where  DocID in ('{docIDList}');";
272
                            base.Execute(query, new { DeletedUser = userId }, transaction);
273
                        }
274
                    }
275

    
276
                    foreach (Documents doc in docList)
277
                    {
278
                        if (string.IsNullOrEmpty(doc.DocID))
279
                        {
280
                            doc.RegisteredUser = userId;
281
                            query = $@"
282
declare @tbdoc table(docid varchar(36))
283
insert into dbo.Documents
284
(
285
     DocID
286
    ,DocumentNo
287
    ,RevisonNo
288
    ,RefProjectCode
289
    ,DocFilePath
290
    ,DocFileName
291
    ,JobLevel
292
    ,IsTypical
293
    ,PersonInCharge
294
    ,RegisteredDate
295
    ,RegisteredUser
296
    ,ToIsDiscussion
297
    ,ToRemarks
298
    ,ToCreator
299
    ,ToCapture
300
    ,ToIsMarkup
301
    ,FrReviewStatus
302
    ,FrRemarks
303
    ,FrCreator
304
    ,FrCapture
305
    ,FrIsMarkup
306
    ,IsID2Work
307
    ,ID2Connection
308
    ,ID2StartDate
309
    ,ID2EndDate
310
    ,ID2Status
311
    ,ID2Issues
312
    ,AVEVAConnection
313
    ,AVEVAConvertDate
314
    ,AVEVAReviewDate
315
    ,AVEVAStatus
316
    ,AVEVAIssues
317
    ,ReviewFilePath
318
    ,ReviewFileName
319
    ,ProdReviewer
320
    ,ProdIsResult
321
    ,ProdRemarks
322
    ,ClientReviewer
323
    ,ClientIsResult
324
    ,ClientRemarks
325
    ,DTIsGateWay
326
    ,DTIsImport
327
    ,DTIsRegSystem
328
    ,DTRemarks
329
)
330
output inserted.DocID into @tbdoc
331
values 
332
(
333
     lower(newid())
334
    ,@DocumentNo
335
    ,@RevisonNo
336
    ,@RefProjectCode
337
    ,@DocFilePath
338
    ,@DocFileName
339
    ,@JobLevel
340
    ,@IsTypical
341
    ,@PersonInCharge
342
    ,getdate()
343
    ,@RegisteredUser
344
    ,@ToIsDiscussion
345
    ,@ToRemarks
346
    ,@ToCreator
347
    ,@ToCapture
348
    ,@ToIsMarkup
349
    ,@FrReviewStatus
350
    ,@FrRemarks
351
    ,@FrCreator
352
    ,@FrCapture
353
    ,@FrIsMarkup
354
    ,@IsID2Work
355
    ,@ID2Connection
356
    ,@ID2StartDate
357
    ,@ID2EndDate
358
    ,@ID2Status
359
    ,@ID2Issues
360
    ,@AVEVAConnection
361
    ,@AVEVAConvertDate
362
    ,@AVEVAReviewDate
363
    ,@AVEVAStatus
364
    ,@AVEVAIssues
365
    ,@ReviewFilePath
366
    ,@ReviewFileName
367
    ,@ProdReviewer
368
    ,@ProdIsResult
369
    ,@ProdRemarks
370
    ,@ClientReviewer
371
    ,@ClientIsResult
372
    ,@ClientRemarks
373
    ,@DTIsGateWay
374
    ,@DTIsImport
375
    ,@DTIsRegSystem
376
    ,@DTRemarks
377
)
378

    
379
if @@rowcount > 0
380
begin
381
    select docid from @tbdoc
382
end
383
else
384
begin
385
    select ''
386
end;";
387
                        }
388
                        else
389
                        {
390
                            doc.ModifiedUser = userId;
391
                            query = $@"
392
update dbo.Documents
393
set    DocumentNo=@DocumentNo
394
      ,RevisonNo=@RevisonNo
395
      ,RefProjectCode=@RefProjectCode
396
      ,DocFilePath=@DocFilePath
397
      ,DocFileName=@DocFileName
398
      ,JobLevel=@JobLevel
399
      ,IsTypical=@IsTypical
400
      ,PersonInCharge=@PersonInCharge
401
      ,ModifiedDate=getdate()
402
      ,ModifiedUser=@ModifiedUser
403
      ,ToIsDiscussion=@ToIsDiscussion
404
      ,ToRemarks=@ToRemarks
405
      ,ToCreator=@ToCreator
406
      ,ToCapture=@ToCapture
407
      ,ToIsMarkup=@ToIsMarkup
408
      ,FrReviewStatus=@FrReviewStatus
409
      ,FrRemarks=@FrRemarks
410
      ,FrCreator=@FrCreator
411
      ,FrCapture=@FrCapture
412
      ,FrIsMarkup=@FrIsMarkup
413
      ,IsID2Work=@IsID2Work
414
      ,ID2Connection=@ID2Connection
415
      ,ID2StartDate=@ID2StartDate
416
      ,ID2EndDate=@ID2EndDate
417
      ,ID2Status=@ID2Status
418
      ,ID2Issues=@ID2Issues
419
      ,AVEVAConnection=@AVEVAConnection
420
      ,AVEVAConvertDate=@AVEVAConvertDate
421
      ,AVEVAReviewDate=@AVEVAReviewDate
422
      ,AVEVAStatus=@AVEVAStatus
423
      ,AVEVAIssues=@AVEVAIssues
424
      ,ReviewFilePath=@ReviewFilePath
425
      ,ReviewFileName=@ReviewFileName
426
      ,ProdReviewer=@ProdReviewer
427
      ,ProdIsResult=@ProdIsResult
428
      ,ProdRemarks=@ProdRemarks
429
      ,ClientReviewer=@ClientReviewer
430
      ,ClientIsResult=@ClientIsResult
431
      ,ClientRemarks=@ClientRemarks
432
      ,DTIsGateWay=@DTIsGateWay
433
      ,DTIsImport=@DTIsImport
434
      ,DTIsRegSystem=@DTIsRegSystem
435
      ,DTRemarks=@DTRemarks
436
where  DocID=@DocID
437

    
438
if @@rowcount > 0
439
begin
440
    select @DocID
441
end
442
else
443
begin
444
    select ''
445
end;";
446
                        }
447
                        string refID = base.QueryFirstOrDefault<string>(query, doc, transaction);
448

    
449
                        if (doc.AttFiles != null && doc.AttFiles.Count > 0)
450
                        {
451
                            string attDelIDList = string.Join("','", doc.AttFiles.Where(x => !string.IsNullOrEmpty(x.FileID)).Select(x => x.FileID).ToList());
452

    
453
                            if (!string.IsNullOrEmpty(refID) && attDelIDList.Length > 0)
454
                            {
455
                                query = $@"
456
delete from dbo.AttachFIles
457
where  RefID=@RefID and FileID in ('{attDelIDList}');";
458
                                base.Execute(query, new { RefID = refID }, transaction);
459
                            }
460

    
461
                            foreach (AttFileInfo attFile in doc.AttFiles)
462
                            {
463
                                if (string.IsNullOrEmpty(attFile.RefID))
464
                                {
465
                                    attFile.RefID = refID;
466
                                    attFile.Creator = userId;
467

    
468
                                    query = $@"
469
insert into dbo.AttachFIles (FileID,RefID,Category,FileType,FileName,FilePath,FileExtension,FileData,Creator)
470
values
471
(
472
    lower(newid())
473
   ,@RefID
474
   ,@Category
475
   ,@FileType
476
   ,@FileName
477
   ,@FilePath
478
   ,@FileExtension
479
   ,@FileData
480
   ,@Creator
481
)";
482

    
483
                                    base.Execute(query, attFile, transaction);
484
                                }
485
                            }
486
                        }
487
                    }
488

    
489
                    transaction.Commit();
490
                    isSuccess = true;
491
                }
492
            }
493
            catch (Exception ex)
494
            {
495
                throw ex;
496
            }
497

    
498
            return isSuccess;
499
        }
500

    
501
        public Documents SetDocumentDataField(Documents doc, string userId)
502
        {
503
            Documents resultData = null;
504

    
505
            try
506
            {
507
                using (var transaction = base.BeginTransaction())
508
                {
509
                    string query = string.Empty;
510

    
511
                    if (!string.IsNullOrEmpty(doc.DocID))
512
                    {
513
                        StringBuilder sbSet = new StringBuilder();
514
                        var parameters = new Dictionary<string, object>();
515

    
516
                        #region Update 할 목록
517
                        if (doc.ID2StartDate != null)
518
                        {
519
                            sbSet.Append(" ,ID2StartDate=(case when ID2StartDate is null then @DateTimeNow else ID2StartDate end) ");
520
                            parameters.Add("ID2StartDate", doc.ID2StartDate);
521
                        }
522

    
523
                        if (doc.Worker != null)
524
                        {
525
                            sbSet.Append(" ,Worker=@Worker ");
526
                            parameters.Add("Worker", doc.Worker);
527
                        }
528
                        #endregion
529

    
530
                        if (parameters.Count > 0)
531
                        {
532
                            sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
533
                            parameters.Add("ModifiedUser", userId);
534

    
535
                            parameters.Add("DocID", doc.DocID);
536

    
537
                            query = $@"
538
declare @DateTimeNow datetime
539
set @DateTimeNow = getdate()
540

    
541
update dbo.Documents
542
set    ModifiedDate=@DateTimeNow {sbSet}
543
where  [DocID]=@DocID
544

    
545
if @@rowcount > 0
546
begin
547
    select *, datediff(SECOND, ID2StartDate, ID2EndDate) as ID2JobTime from dbo.Documents where DocID=@DocID
548
end
549
else
550
begin
551
    select *, 0 as ID2JobTime from dbo.Documents where 1=2
552
end;";
553
                            resultData = base.QueryFirstOrDefault<Documents>(query, parameters, transaction);
554
                        }
555
                    }
556

    
557
                    transaction.Commit();
558
                }
559
            }
560
            catch (Exception ex)
561
            {
562
                throw ex;
563
            }
564

    
565
            return resultData;
566
        }
567

    
568
        public bool SetDocumentDatasField(List<Documents> docs, string userId)
569
        {
570
            bool isSuccess = false;
571

    
572
            try
573
            {
574
                using (var transaction = base.BeginTransaction())
575
                {
576
                    foreach (Documents doc in docs)
577
                    {
578
                        string query = string.Empty;
579

    
580
                        if (!string.IsNullOrEmpty(doc.DocID))
581
                        {
582
                            StringBuilder sbSet = new StringBuilder();
583
                            var parameters = new Dictionary<string, object>();
584

    
585
                            #region Update 할 목록
586
                            if (doc.ID2EndDate != null)
587
                            {
588
                                sbSet.Append(" ,ID2EndDate=@ID2EndDate ");
589
                                parameters.Add("ID2EndDate", doc.ID2EndDate);
590
                            }
591
                            #endregion
592

    
593
                            if (parameters.Count > 0)
594
                            {
595
                                sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
596
                                parameters.Add("ModifiedUser", userId);
597

    
598
                                parameters.Add("DocID", doc.DocID);
599

    
600
                                query = $@"
601
update dbo.Documents
602
set    ModifiedDate=getdate() {sbSet}
603
where  [DocID]=@DocID;";
604
                                base.Execute(query, parameters, transaction);
605
                            }
606
                        }
607
                    }
608
                    transaction.Commit();
609
                    isSuccess = true;
610
                }
611
            }
612
            catch (Exception ex)
613
            {
614
                throw ex;
615
            }
616

    
617
            return isSuccess;
618
        }
619

    
620

    
621
        //ID2
622
        public IEnumerable<ID2Drawings> GetID2DrawingsByProject(ID2ProjectInfo id2Info)
623
        {
624
            try
625
            {
626
                string query = $@"
627
select @Name PROJECTNAME
628
      ,dw.[UID], dw.[NAME], left(dw.[NAME], len(dw.[NAME]) - dw.lastidx) DOCNAME
629
      ,case when rtrim(ltrim(isnull(convert(varchar, dw.[DATETIME]),''))) = '' then null else convert(datetime, convert(varchar, dw.[DATETIME])) end [DATETIME]
630
      ,dw.OCCUPIED, dw.[Image]
631
from
632
(
633
    select [UID], [NAME], [DATETIME], OCCUPIED, [Image], charindex('.', reverse([NAME])) lastidx
634
    from   dbo.Drawings
635
) dw;";
636
                return Query<ID2Drawings>(query, id2Info);
637
            }
638
            catch (Exception ex)
639
            {
640
                throw ex;
641
            }
642
        }
643
    }
644
}
클립보드 이미지 추가 (최대 크기: 500 MB)