프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / DocumentRepository.cs @ 8d7ea9de

이력 | 보기 | 이력해설 | 다운로드 (18.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 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.*,
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
insert into dbo.Documents
259
(
260
     DocID
261
    ,DocumentNo
262
    ,RevisonNo
263
    ,RefProjectCode
264
    ,DocFilePath
265
    ,DocFileName
266
    ,JobLevel
267
    ,IsTypical
268
    ,PersonInCharge
269
    ,RegisteredDate
270
    ,RegisteredUser
271
    ,ToIsDiscussion
272
    ,ToRemarks
273
    ,ToCreator
274
    ,ToCapture
275
    ,ToIsMarkup
276
    ,FrReviewStatus
277
    ,FrRemarks
278
    ,FrCreator
279
    ,FrCapture
280
    ,FrIsMarkup
281
    ,IsID2Work
282
    ,ID2Connection
283
    ,ID2StartDate
284
    ,ID2EndDate
285
    ,ID2Status
286
    ,ID2Issues
287
    ,AVEVAConnection
288
    ,AVEVAConvertDate
289
    ,AVEVAReviewDate
290
    ,AVEVAStatus
291
    ,AVEVAIssues
292
    ,ReviewFilePath
293
    ,ReviewFileName
294
    ,ProdReviewer
295
    ,ProdIsResult
296
    ,ProdRemarks
297
    ,ClientReviewer
298
    ,ClientIsResult
299
    ,ClientRemarks
300
    ,DTIsGateWay
301
    ,DTIsImport
302
    ,DTIsRegSystem
303
    ,DTRemarks
304
)
305
values 
306
(
307
     lower(newid())
308
    ,@DocumentNo
309
    ,@RevisonNo
310
    ,@RefProjectCode
311
    ,@DocFilePath
312
    ,@DocFileName
313
    ,@JobLevel
314
    ,@IsTypical
315
    ,@PersonInCharge
316
    ,getdate()
317
    ,@RegisteredUser
318
    ,@ToIsDiscussion
319
    ,@ToRemarks
320
    ,@ToCreator
321
    ,@ToCapture
322
    ,@ToIsMarkup
323
    ,@FrReviewStatus
324
    ,@FrRemarks
325
    ,@FrCreator
326
    ,@FrCapture
327
    ,@FrIsMarkup
328
    ,@IsID2Work
329
    ,@ID2Connection
330
    ,@ID2StartDate
331
    ,@ID2EndDate
332
    ,@ID2Status
333
    ,@ID2Issues
334
    ,@AVEVAConnection
335
    ,@AVEVAConvertDate
336
    ,@AVEVAReviewDate
337
    ,@AVEVAStatus
338
    ,@AVEVAIssues
339
    ,@ReviewFilePath
340
    ,@ReviewFileName
341
    ,@ProdReviewer
342
    ,@ProdIsResult
343
    ,@ProdRemarks
344
    ,@ClientReviewer
345
    ,@ClientIsResult
346
    ,@ClientRemarks
347
    ,@DTIsGateWay
348
    ,@DTIsImport
349
    ,@DTIsRegSystem
350
    ,@DTRemarks
351
);";
352
                        }
353
                        else
354
                        {
355
                            doc.ModifiedUser = userId;
356
                            query = $@"
357
update dbo.Documents
358
set    DocumentNo=@DocumentNo
359
      ,RevisonNo=@RevisonNo
360
      ,RefProjectCode=@RefProjectCode
361
      ,DocFilePath=@DocFilePath
362
      ,DocFileName=@DocFileName
363
      ,JobLevel=@JobLevel
364
      ,IsTypical=@IsTypical
365
      ,PersonInCharge=@PersonInCharge
366
      ,ModifiedDate=getdate()
367
      ,ModifiedUser=@ModifiedUser
368
      ,ToIsDiscussion=@ToIsDiscussion
369
      ,ToRemarks=@ToRemarks
370
      ,ToCreator=@ToCreator
371
      ,ToCapture=@ToCapture
372
      ,ToIsMarkup=@ToIsMarkup
373
      ,FrReviewStatus=@FrReviewStatus
374
      ,FrRemarks=@FrRemarks
375
      ,FrCreator=@FrCreator
376
      ,FrCapture=@FrCapture
377
      ,FrIsMarkup=@FrIsMarkup
378
      ,IsID2Work=@IsID2Work
379
      ,ID2Connection=@ID2Connection
380
      ,ID2StartDate=@ID2StartDate
381
      ,ID2EndDate=@ID2EndDate
382
      ,ID2Status=@ID2Status
383
      ,ID2Issues=@ID2Issues
384
      ,AVEVAConnection=@AVEVAConnection
385
      ,AVEVAConvertDate=@AVEVAConvertDate
386
      ,AVEVAReviewDate=@AVEVAReviewDate
387
      ,AVEVAStatus=@AVEVAStatus
388
      ,AVEVAIssues=@AVEVAIssues
389
      ,ReviewFilePath=@ReviewFilePath
390
      ,ReviewFileName=@ReviewFileName
391
      ,ProdReviewer=@ProdReviewer
392
      ,ProdIsResult=@ProdIsResult
393
      ,ProdRemarks=@ProdRemarks
394
      ,ClientReviewer=@ClientReviewer
395
      ,ClientIsResult=@ClientIsResult
396
      ,ClientRemarks=@ClientRemarks
397
      ,DTIsGateWay=@DTIsGateWay
398
      ,DTIsImport=@DTIsImport
399
      ,DTIsRegSystem=@DTIsRegSystem
400
      ,DTRemarks=@DTRemarks
401
where  DocID=@DocID;";
402
                        }
403
                        base.Execute(query, doc, transaction);
404
                    }
405

    
406
                    transaction.Commit();
407
                    isSuccess = true;
408
                }
409
            }
410
            catch (Exception ex)
411
            {
412
                throw ex;
413
            }
414

    
415
            return isSuccess;
416
        }
417

    
418
        public Documents SetDocumentDataField(Documents doc, string userId)
419
        {
420
            Documents resultData = null;
421

    
422
            try
423
            {
424
                using (var transaction = base.BeginTransaction())
425
                {
426
                    string query = string.Empty;
427

    
428
                    if (!string.IsNullOrEmpty(doc.DocID))
429
                    {
430
                        StringBuilder sbSet = new StringBuilder();
431
                        var parameters = new Dictionary<string, object>();
432

    
433
                        #region Update 할 목록
434
                        if (doc.ID2StartDate != null)
435
                        {
436
                            sbSet.Append(" ,ID2StartDate=(case when ID2StartDate is null then @DateTimeNow else ID2StartDate end) ");
437
                            parameters.Add("ID2StartDate", doc.ID2StartDate);
438
                        }
439

    
440
                        if (doc.Worker != null)
441
                        {
442
                            sbSet.Append(" ,Worker=@Worker ");
443
                            parameters.Add("Worker", doc.Worker);
444
                        }
445
                        #endregion
446

    
447
                        if (parameters.Count > 0)
448
                        {
449
                            sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
450
                            parameters.Add("ModifiedUser", userId);
451

    
452
                            parameters.Add("DocID", doc.DocID);
453

    
454
                            query = $@"
455
declare @DateTimeNow datetime
456
set @DateTimeNow = getdate()
457

    
458
update dbo.Documents
459
set    ModifiedDate=@DateTimeNow {sbSet}
460
where  [DocID]=@DocID
461

    
462
if @@rowcount > 0
463
begin
464
    select *, datediff(SECOND, ID2StartDate, ID2EndDate) as ID2JobTime from dbo.Documents where DocID=@DocID
465
end
466
else
467
begin
468
    select *, 0 as ID2JobTime from dbo.Documents where 1=2
469
end;";
470
                            resultData = base.QueryFirstOrDefault<Documents>(query, parameters, transaction);
471
                        }
472
                    }
473

    
474
                    transaction.Commit();
475
                }
476
            }
477
            catch (Exception ex)
478
            {
479
                throw ex;
480
            }
481

    
482
            return resultData;
483
        }
484

    
485
        public bool SetDocumentDatasField(List<Documents> docs, string userId)
486
        {
487
            bool isSuccess = false;
488

    
489
            try
490
            {
491
                using (var transaction = base.BeginTransaction())
492
                {
493
                    foreach (Documents doc in docs)
494
                    {
495
                        string query = string.Empty;
496

    
497
                        if (!string.IsNullOrEmpty(doc.DocID))
498
                        {
499
                            StringBuilder sbSet = new StringBuilder();
500
                            var parameters = new Dictionary<string, object>();
501

    
502
                            #region Update 할 목록
503
                            if (doc.ID2EndDate != null)
504
                            {
505
                                sbSet.Append(" ,ID2EndDate=@ID2EndDate ");
506
                                parameters.Add("ID2EndDate", doc.ID2EndDate);
507
                            }
508
                            #endregion
509

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

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

    
517
                                query = $@"
518
update dbo.Documents
519
set    ModifiedDate=getdate() {sbSet}
520
where  [DocID]=@DocID;";
521
                                base.Execute(query, parameters, transaction);
522
                            }
523
                        }
524
                    }
525
                    transaction.Commit();
526
                    isSuccess = true;
527
                }
528
            }
529
            catch (Exception ex)
530
            {
531
                throw ex;
532
            }
533

    
534
            return isSuccess;
535
        }
536

    
537

    
538
        //ID2
539
        public IEnumerable<ID2Drawings> GetID2DrawingsByProject(ID2ProjectInfo id2Info)
540
        {
541
            try
542
            {
543
                string query = $@"
544
select @Name PROJECTNAME
545
      ,dw.[UID], dw.[NAME], left(dw.[NAME], len(dw.[NAME]) - dw.lastidx) DOCNAME
546
      ,case when rtrim(ltrim(isnull(convert(varchar, dw.[DATETIME]),''))) = '' then null else convert(datetime, convert(varchar, dw.[DATETIME])) end [DATETIME]
547
      ,dw.OCCUPIED, dw.[Image]
548
from
549
(
550
    select [UID], [NAME], [DATETIME], OCCUPIED, [Image], charindex('.', reverse([NAME])) lastidx
551
    from   dbo.Drawings
552
) dw;";
553
                return Query<ID2Drawings>(query, id2Info);
554
            }
555
            catch (Exception ex)
556
            {
557
                throw ex;
558
            }
559
        }
560
    }
561
}
클립보드 이미지 추가 (최대 크기: 500 MB)