프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (20.6 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
                parameters.Add("RefGroupID", projectGroupID);
78
            }
79
            else
80
            {
81
                sbWhere.Append(" and doc.RefProjectCode=@RefProjectCode ");
82
                sbTotalWhere.Append(" and doc.RefProjectCode=@RefProjectCode ");
83
                parameters.Add("RefProjectCode", projectCode);
84
            }
85
            if (!string.IsNullOrEmpty(personIncharge))
86
            {
87
                sbWhere.Append(" and doc.PersonInCharge=@PersonInCharge ");
88
                parameters.Add("PersonInCharge", personIncharge);
89
            }
90
            if (!string.IsNullOrEmpty(jobLevel))
91
            {
92
                sbWhere.Append(" and doc.JobLevel=@JobLevel ");
93
                parameters.Add("JobLevel", jobLevel);
94
            }
95
            if (!string.IsNullOrEmpty(documentNo))
96
            {
97
                sbWhere.Append(" and doc.DocumentNo like '%' + @DocumentNo +'%' ");
98
                parameters.Add("DocumentNo", documentNo);
99
            }
100

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

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

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

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

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

    
151
            try
152
            {
153

    
154
                //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,
155
                    string query = $@"
156
                            select  doc.*, datediff(SECOND, doc.ID2StartDate, doc.ID2EndDate) as ID2JobTime,
157
                            files.[FileID] as FileID, files.*,
158
                            markus.MARKUP_DATA_ID as MARKUP_DATA_ID, markus.*, 
159
                            datediff(SECOND, doc.ID2StartDate, doc.ID2EndDate) as ID2JobTime
160
                            from     dbo.Documents doc
161
	                        LEFT OUTER JOIN AttachFIles files ON doc.DocID = fIles.RefID 
162
                            LEFT OUTER JOIN 
163
                                    (SELECT [MARKUP_DATA_ID]
164
                                           ,[PROJECT_NO] as PROJECT_NO
165
                                          ,[DOCUMENT_ID] as DocumentNo
166
                                          
167
                                          ,[PAGENUMBER]
168
                                          ,[Text] as TEXT
169
                                          ,[CREATE_DATE] as CREATE_DATE
170
                                          ,[NAME] as CREATE_USER
171
                                      FROM ViewMarkupData) markus 
172
                            ON doc.DocumentNo = markus.DocumentNo
173
                            where    doc.IsDeleted=0 {sbWhere}
174
                            order by doc.Seq
175

    
176
select @Total=count(*) from dbo.Documents doc where doc.IsDeleted=0 {sbTotalWhere}
177
select @Total;";
178

    
179
                if (parameters.Count > 0)
180
                {
181
                    dynamicParameters.AddDynamicParams(parameters);
182
                }
183

    
184
                var docDictionary = new Dictionary<string, Documents>();
185

    
186
                var ret = MultiQuery<Documents, AttFileInfo, MarkupText, Documents>(query,
187
                                (document, attfile, markusText) =>
188
                                {
189
                                    Documents doc;
190

    
191
                                    if (!docDictionary.TryGetValue(document.DocID, out doc))
192
                                    {
193
                                        doc = document;
194
                                        docDictionary.Add(doc.DocID, doc);
195
                                    }
196

    
197
                                    if (markusText != null)
198
                                    {
199
                                        doc.Markups = doc.Markups ?? new List<MarkupText>();
200

    
201
                                        if (!doc.Markups.Any(x => x.MARKUP_DATA_ID == markusText.MARKUP_DATA_ID))
202
                                        {
203
                                            doc.Markups.Add(markusText);
204
                                        }
205
                                    }
206

    
207
                                    if (attfile != null)
208
                                    {
209
                                        doc.AttFiles = doc.AttFiles ?? new List<AttFileInfo>();
210
                                        System.Diagnostics.Debug.WriteLine(attfile.FileName);
211
                                        if (!doc.AttFiles.Any(x => x.FileID == attfile.FileID))
212
                                        {
213
                                            doc.AttFiles.Add(attfile);
214
                                        }
215
                                    }
216

    
217
                                    return doc;
218

    
219
                                }, dynamicParameters, splitOn: "DocID,FileID,MARKUP_DATA_ID").Distinct();
220

    
221
                int totalCount = dynamicParameters.Get<int>("Total");
222

    
223
                return (ret, totalCount);
224
            }
225
            catch (Exception ex)
226
            {
227
                throw ex;
228
            }
229
        }
230

    
231
        public bool SetDocumentData(List<Documents> docList, List<Documents> delDocList, string userId)
232
        {
233
            bool isSuccess = false;
234

    
235
            try
236
            {
237
                using (var transaction = base.BeginTransaction())
238
                {
239
                    string query = string.Empty;
240

    
241
                    if (delDocList.Count > 0)
242
                    {
243
                        string docIDList = string.Join("','", delDocList.Where(x => !string.IsNullOrEmpty(x.DocID)).Select(x => x.DocID).ToList());
244

    
245
                        if (docIDList.Length > 0)
246
                        {
247
                            query = $@"
248
update dbo.Documents
249
set    IsDeleted=1
250
      ,DeletedDate=getdate(),
251
      ,DeletedUser=@DeletedUser
252
where  DocID in ('{docIDList}');";
253
                            base.Execute(query, new { DeletedUser = userId }, transaction);
254
                        }
255
                    }
256

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

    
360
if @@rowcount > 0
361
begin
362
    select docid from @tbdoc
363
end
364
else
365
begin
366
    select ''
367
end;";
368
                        }
369
                        else
370
                        {
371
                            doc.ModifiedUser = userId;
372
                            query = $@"
373
update dbo.Documents
374
set    DocumentNo=@DocumentNo
375
      ,RevisonNo=@RevisonNo
376
      ,RefProjectCode=@RefProjectCode
377
      ,DocFilePath=@DocFilePath
378
      ,DocFileName=@DocFileName
379
      ,JobLevel=@JobLevel
380
      ,IsTypical=@IsTypical
381
      ,PersonInCharge=@PersonInCharge
382
      ,ModifiedDate=getdate()
383
      ,ModifiedUser=@ModifiedUser
384
      ,ToIsDiscussion=@ToIsDiscussion
385
      ,ToRemarks=@ToRemarks
386
      ,ToCreator=@ToCreator
387
      ,ToCapture=@ToCapture
388
      ,ToIsMarkup=@ToIsMarkup
389
      ,FrReviewStatus=@FrReviewStatus
390
      ,FrRemarks=@FrRemarks
391
      ,FrCreator=@FrCreator
392
      ,FrCapture=@FrCapture
393
      ,FrIsMarkup=@FrIsMarkup
394
      ,IsID2Work=@IsID2Work
395
      ,ID2Connection=@ID2Connection
396
      ,ID2StartDate=@ID2StartDate
397
      ,ID2EndDate=@ID2EndDate
398
      ,ID2Status=@ID2Status
399
      ,ID2Issues=@ID2Issues
400
      ,AVEVAConnection=@AVEVAConnection
401
      ,AVEVAConvertDate=@AVEVAConvertDate
402
      ,AVEVAReviewDate=@AVEVAReviewDate
403
      ,AVEVAStatus=@AVEVAStatus
404
      ,AVEVAIssues=@AVEVAIssues
405
      ,ReviewFilePath=@ReviewFilePath
406
      ,ReviewFileName=@ReviewFileName
407
      ,ProdReviewer=@ProdReviewer
408
      ,ProdIsResult=@ProdIsResult
409
      ,ProdRemarks=@ProdRemarks
410
      ,ClientReviewer=@ClientReviewer
411
      ,ClientIsResult=@ClientIsResult
412
      ,ClientRemarks=@ClientRemarks
413
      ,DTIsGateWay=@DTIsGateWay
414
      ,DTIsImport=@DTIsImport
415
      ,DTIsRegSystem=@DTIsRegSystem
416
      ,DTRemarks=@DTRemarks
417
where  DocID=@DocID
418

    
419
if @@rowcount > 0
420
begin
421
    select @DocID
422
end
423
else
424
begin
425
    select ''
426
end;";
427
                        }
428
                        string refID = base.QueryFirstOrDefault<string>(query, doc, transaction);
429

    
430
                        if (doc.AttFiles != null && doc.AttFiles.Count > 0)
431
                        {
432
                            string attDelIDList = string.Join("','", doc.AttFiles.Where(x => !string.IsNullOrEmpty(x.FileID)).Select(x => x.FileID).ToList());
433

    
434
                            if (!string.IsNullOrEmpty(refID) && attDelIDList.Length > 0)
435
                            {
436
                                query = $@"
437
delete from dbo.AttachFIles
438
where  RefID=@RefID and FileID in ('{attDelIDList}');";
439
                                base.Execute(query, new { RefID = refID }, transaction);
440
                            }
441

    
442
                            foreach (AttFileInfo attFile in doc.AttFiles)
443
                            {
444
                                if (string.IsNullOrEmpty(attFile.RefID))
445
                                {
446
                                    attFile.RefID = refID;
447
                                    attFile.Creator = userId;
448

    
449
                                    query = $@"
450
insert into dbo.AttachFIles (FileID,RefID,Category,FileType,FileName,FilePath,FileExtension,FileData,Creator)
451
values
452
(
453
    lower(newid())
454
   ,@RefID
455
   ,@Category
456
   ,@FileType
457
   ,@FileName
458
   ,@FilePath
459
   ,@FileExtension
460
   ,@FileData
461
   ,@Creator
462
)";
463

    
464
                                    base.Execute(query, attFile, transaction);
465
                                }
466
                            }
467
                        }
468
                    }
469

    
470
                    transaction.Commit();
471
                    isSuccess = true;
472
                }
473
            }
474
            catch (Exception ex)
475
            {
476
                throw ex;
477
            }
478

    
479
            return isSuccess;
480
        }
481

    
482
        public Documents SetDocumentDataField(Documents doc, string userId)
483
        {
484
            Documents resultData = null;
485

    
486
            try
487
            {
488
                using (var transaction = base.BeginTransaction())
489
                {
490
                    string query = string.Empty;
491

    
492
                    if (!string.IsNullOrEmpty(doc.DocID))
493
                    {
494
                        StringBuilder sbSet = new StringBuilder();
495
                        var parameters = new Dictionary<string, object>();
496

    
497
                        #region Update 할 목록
498
                        if (doc.ID2StartDate != null)
499
                        {
500
                            sbSet.Append(" ,ID2StartDate=(case when ID2StartDate is null then @DateTimeNow else ID2StartDate end) ");
501
                            parameters.Add("ID2StartDate", doc.ID2StartDate);
502
                        }
503

    
504
                        if (doc.Worker != null)
505
                        {
506
                            sbSet.Append(" ,Worker=@Worker ");
507
                            parameters.Add("Worker", doc.Worker);
508
                        }
509
                        #endregion
510

    
511
                        if (parameters.Count > 0)
512
                        {
513
                            sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
514
                            parameters.Add("ModifiedUser", userId);
515

    
516
                            parameters.Add("DocID", doc.DocID);
517

    
518
                            query = $@"
519
declare @DateTimeNow datetime
520
set @DateTimeNow = getdate()
521

    
522
update dbo.Documents
523
set    ModifiedDate=@DateTimeNow {sbSet}
524
where  [DocID]=@DocID
525

    
526
if @@rowcount > 0
527
begin
528
    select *, datediff(SECOND, ID2StartDate, ID2EndDate) as ID2JobTime from dbo.Documents where DocID=@DocID
529
end
530
else
531
begin
532
    select *, 0 as ID2JobTime from dbo.Documents where 1=2
533
end;";
534
                            resultData = base.QueryFirstOrDefault<Documents>(query, parameters, transaction);
535
                        }
536
                    }
537

    
538
                    transaction.Commit();
539
                }
540
            }
541
            catch (Exception ex)
542
            {
543
                throw ex;
544
            }
545

    
546
            return resultData;
547
        }
548

    
549
        public bool SetDocumentDatasField(List<Documents> docs, string userId)
550
        {
551
            bool isSuccess = false;
552

    
553
            try
554
            {
555
                using (var transaction = base.BeginTransaction())
556
                {
557
                    foreach (Documents doc in docs)
558
                    {
559
                        string query = string.Empty;
560

    
561
                        if (!string.IsNullOrEmpty(doc.DocID))
562
                        {
563
                            StringBuilder sbSet = new StringBuilder();
564
                            var parameters = new Dictionary<string, object>();
565

    
566
                            #region Update 할 목록
567
                            if (doc.ID2EndDate != null)
568
                            {
569
                                sbSet.Append(" ,ID2EndDate=@ID2EndDate ");
570
                                parameters.Add("ID2EndDate", doc.ID2EndDate);
571
                            }
572
                            #endregion
573

    
574
                            if (parameters.Count > 0)
575
                            {
576
                                sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
577
                                parameters.Add("ModifiedUser", userId);
578

    
579
                                parameters.Add("DocID", doc.DocID);
580

    
581
                                query = $@"
582
update dbo.Documents
583
set    ModifiedDate=getdate() {sbSet}
584
where  [DocID]=@DocID;";
585
                                base.Execute(query, parameters, transaction);
586
                            }
587
                        }
588
                    }
589
                    transaction.Commit();
590
                    isSuccess = true;
591
                }
592
            }
593
            catch (Exception ex)
594
            {
595
                throw ex;
596
            }
597

    
598
            return isSuccess;
599
        }
600

    
601

    
602
        //ID2
603
        public IEnumerable<ID2Drawings> GetID2DrawingsByProject(ID2ProjectInfo id2Info)
604
        {
605
            try
606
            {
607
                string query = $@"
608
select @Name PROJECTNAME
609
      ,dw.[UID], dw.[NAME], left(dw.[NAME], len(dw.[NAME]) - dw.lastidx) DOCNAME
610
      ,case when rtrim(ltrim(isnull(convert(varchar, dw.[DATETIME]),''))) = '' then null else convert(datetime, convert(varchar, dw.[DATETIME])) end [DATETIME]
611
      ,dw.OCCUPIED, dw.[Image]
612
from
613
(
614
    select [UID], [NAME], [DATETIME], OCCUPIED, [Image], charindex('.', reverse([NAME])) lastidx
615
    from   dbo.Drawings
616
) dw;";
617
                return Query<ID2Drawings>(query, id2Info);
618
            }
619
            catch (Exception ex)
620
            {
621
                throw ex;
622
            }
623
        }
624
    }
625
}
클립보드 이미지 추가 (최대 크기: 500 MB)