프로젝트

일반

사용자정보

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

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

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

    
96
            if (!string.IsNullOrEmpty(isToIsDiscussion))
97
            {
98
                sbWhere.Append(" and doc.ToIsDiscussion=@ToIsDiscussion ");
99
                parameters.Add("ToIsDiscussion", isToIsDiscussion);
100
            }
101

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

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

    
114
            if (!string.IsNullOrEmpty(id2Status))
115
            {
116
                sbWhere.Append(" and doc.ID2Status=@ID2Status ");
117
                parameters.Add("ID2Status", id2Status);
118
            }
119
            if (!string.IsNullOrEmpty(avevaStatus))
120
            {
121
                sbWhere.Append(" and doc.AVEVAStatus=@AVEVAStatus ");
122
                parameters.Add("AVEVAStatus", avevaStatus);
123
            }
124

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

    
146
            try
147
            {
148

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

    
171
select @Total=count(*) from dbo.Documents doc where doc.IsDeleted=0 {sbTotalWhere}
172
select @Total;";
173

    
174
                if (parameters.Count > 0)
175
                {
176
                    dynamicParameters.AddDynamicParams(parameters);
177
                }
178

    
179
                var docDictionary = new Dictionary<string, Documents>();
180

    
181
                var ret = MultiQuery<Documents, AttFileInfo, MarkupText, Documents>(query,
182
                                (document, attfile, markusText) =>
183
                                {
184
                                    Documents doc;
185

    
186
                                    if (!docDictionary.TryGetValue(document.DocID, out doc))
187
                                    {
188
                                        doc = document;
189
                                        docDictionary.Add(doc.DocID, doc);
190
                                    }
191

    
192
                                    if (markusText != null)
193
                                    {
194
                                        doc.Markups = doc.Markups ?? new List<MarkupText>();
195

    
196
                                        if (!doc.Markups.Any(x => x.MARKUP_DATA_ID == markusText.MARKUP_DATA_ID))
197
                                        {
198
                                            doc.Markups.Add(markusText);
199
                                        }
200
                                    }
201

    
202
                                    if (attfile != null)
203
                                    {
204
                                        doc.AttFiles = doc.AttFiles ?? new List<AttFileInfo>();
205
                                        System.Diagnostics.Debug.WriteLine(attfile.FileName);
206
                                        if (!doc.AttFiles.Any(x => x.FileID == attfile.FileID))
207
                                        {
208
                                            doc.AttFiles.Add(attfile);
209
                                        }
210
                                    }
211

    
212
                                    return doc;
213

    
214
                                }, dynamicParameters, splitOn: "DocID,FileID,MARKUP_DATA_ID").Distinct();
215

    
216
                int totalCount = dynamicParameters.Get<int>("Total");
217

    
218
                return (ret, totalCount);
219
            }
220
            catch (Exception ex)
221
            {
222
                throw ex;
223
            }
224
        }
225

    
226
        public bool SetDocumentData(List<Documents> docList, List<Documents> delDocList, string userId)
227
        {
228
            bool isSuccess = false;
229

    
230
            try
231
            {
232
                using (var transaction = base.BeginTransaction())
233
                {
234
                    string query = string.Empty;
235

    
236
                    if (delDocList.Count > 0)
237
                    {
238
                        string docIDList = string.Join("','", delDocList.Where(x => !string.IsNullOrEmpty(x.DocID)).Select(x => x.DocID).ToList());
239

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

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

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

    
414
if @@rowcount > 0
415
begin
416
    select @DocID
417
end
418
else
419
begin
420
    select ''
421
end;";
422
                        }
423
                        string refID = base.QueryFirstOrDefault<string>(query, doc, transaction);
424

    
425
                        if (doc.AttFiles != null && doc.AttFiles.Count > 0)
426
                        {
427
                            string attDelIDList = string.Join("','", doc.AttFiles.Where(x => !string.IsNullOrEmpty(x.FileID)).Select(x => x.FileID).ToList());
428

    
429
                            if (!string.IsNullOrEmpty(refID) && attDelIDList.Length > 0)
430
                            {
431
                                query = $@"
432
delete from dbo.AttachFIles
433
where  RefID=@RefID and FileID in ('{attDelIDList}');";
434
                                base.Execute(query, new { RefID = refID }, transaction);
435
                            }
436

    
437
                            foreach (AttFileInfo attFile in doc.AttFiles)
438
                            {
439
                                attFile.RefID = refID;
440
                                attFile.Creator = userId;
441

    
442
                                query = $@"
443
insert into dbo.AttachFIles (FileID,RefID,Category,FileType,FileName,FilePath,FileExtension,FileData,Creator)
444
values
445
(
446
    lower(newid())
447
   ,@RefID
448
   ,@Category
449
   ,@FileType
450
   ,@FileName
451
   ,@FilePath
452
   ,@FileExtension
453
   ,@FileData
454
   ,@Creator
455
)";
456

    
457
                                base.Execute(query, attFile, transaction);
458
                            }
459
                        }
460
                    }
461

    
462
                    transaction.Commit();
463
                    isSuccess = true;
464
                }
465
            }
466
            catch (Exception ex)
467
            {
468
                throw ex;
469
            }
470

    
471
            return isSuccess;
472
        }
473

    
474
        public Documents SetDocumentDataField(Documents doc, string userId)
475
        {
476
            Documents resultData = null;
477

    
478
            try
479
            {
480
                using (var transaction = base.BeginTransaction())
481
                {
482
                    string query = string.Empty;
483

    
484
                    if (!string.IsNullOrEmpty(doc.DocID))
485
                    {
486
                        StringBuilder sbSet = new StringBuilder();
487
                        var parameters = new Dictionary<string, object>();
488

    
489
                        #region Update 할 목록
490
                        if (doc.ID2StartDate != null)
491
                        {
492
                            sbSet.Append(" ,ID2StartDate=(case when ID2StartDate is null then @DateTimeNow else ID2StartDate end) ");
493
                            parameters.Add("ID2StartDate", doc.ID2StartDate);
494
                        }
495

    
496
                        if (doc.Worker != null)
497
                        {
498
                            sbSet.Append(" ,Worker=@Worker ");
499
                            parameters.Add("Worker", doc.Worker);
500
                        }
501
                        #endregion
502

    
503
                        if (parameters.Count > 0)
504
                        {
505
                            sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
506
                            parameters.Add("ModifiedUser", userId);
507

    
508
                            parameters.Add("DocID", doc.DocID);
509

    
510
                            query = $@"
511
declare @DateTimeNow datetime
512
set @DateTimeNow = getdate()
513

    
514
update dbo.Documents
515
set    ModifiedDate=@DateTimeNow {sbSet}
516
where  [DocID]=@DocID
517

    
518
if @@rowcount > 0
519
begin
520
    select *, datediff(SECOND, ID2StartDate, ID2EndDate) as ID2JobTime from dbo.Documents where DocID=@DocID
521
end
522
else
523
begin
524
    select *, 0 as ID2JobTime from dbo.Documents where 1=2
525
end;";
526
                            resultData = base.QueryFirstOrDefault<Documents>(query, parameters, transaction);
527
                        }
528
                    }
529

    
530
                    transaction.Commit();
531
                }
532
            }
533
            catch (Exception ex)
534
            {
535
                throw ex;
536
            }
537

    
538
            return resultData;
539
        }
540

    
541
        public bool SetDocumentDatasField(List<Documents> docs, string userId)
542
        {
543
            bool isSuccess = false;
544

    
545
            try
546
            {
547
                using (var transaction = base.BeginTransaction())
548
                {
549
                    foreach (Documents doc in docs)
550
                    {
551
                        string query = string.Empty;
552

    
553
                        if (!string.IsNullOrEmpty(doc.DocID))
554
                        {
555
                            StringBuilder sbSet = new StringBuilder();
556
                            var parameters = new Dictionary<string, object>();
557

    
558
                            #region Update 할 목록
559
                            if (doc.ID2EndDate != null)
560
                            {
561
                                sbSet.Append(" ,ID2EndDate=@ID2EndDate ");
562
                                parameters.Add("ID2EndDate", doc.ID2EndDate);
563
                            }
564
                            #endregion
565

    
566
                            if (parameters.Count > 0)
567
                            {
568
                                sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
569
                                parameters.Add("ModifiedUser", userId);
570

    
571
                                parameters.Add("DocID", doc.DocID);
572

    
573
                                query = $@"
574
update dbo.Documents
575
set    ModifiedDate=getdate() {sbSet}
576
where  [DocID]=@DocID;";
577
                                base.Execute(query, parameters, transaction);
578
                            }
579
                        }
580
                    }
581
                    transaction.Commit();
582
                    isSuccess = true;
583
                }
584
            }
585
            catch (Exception ex)
586
            {
587
                throw ex;
588
            }
589

    
590
            return isSuccess;
591
        }
592

    
593

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