프로젝트

일반

사용자정보

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

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / DocumentRepository.cs @ 08499f5f

이력 | 보기 | 이력해설 | 다운로드 (19 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
                using (ID2.Manager.Dapper.Repository.AttFileRepository attFileRepository = new AttFileRepository(this._DbConnection.ConnectionString))
150
                {
151
                   var test = attFileRepository.GetAttFileList("3ef4c3cd-2204-4213-8333-5a77c2b9f8cd", "toreview");
152

    
153
                    test.ToList().ForEach(
154
                        x =>
155
                        System.Diagnostics.Debug.WriteLine(x.FileID));
156
                }
157

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

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

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

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

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

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

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

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

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

    
221
                                    return doc;
222

    
223
                                }, dynamicParameters, splitOn: "DocID,FileID,MARKUP_DATA_ID").Distinct();
224

    
225
                int totalCount = dynamicParameters.Get<int>("Total");
226

    
227
                return (ret, totalCount);
228
            }
229
            catch (Exception ex)
230
            {
231
                throw ex;
232
            }
233
        }
234

    
235
        public bool SetDocumentData(List<Documents> docList, List<Documents> delDocList, string userId)
236
        {
237
            bool isSuccess = false;
238

    
239
            try
240
            {
241
                using (var transaction = base.BeginTransaction())
242
                {
243
                    string query = string.Empty;
244

    
245
                    if (delDocList.Count > 0)
246
                    {
247
                        string docIDList = string.Join("','", delDocList.Where(x => !string.IsNullOrEmpty(x.DocID)).Select(x => x.DocID).ToList());
248

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

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

    
415
                    transaction.Commit();
416
                    isSuccess = true;
417
                }
418
            }
419
            catch (Exception ex)
420
            {
421
                throw ex;
422
            }
423

    
424
            return isSuccess;
425
        }
426

    
427
        public Documents SetDocumentDataField(Documents doc, string userId)
428
        {
429
            Documents resultData = null;
430

    
431
            try
432
            {
433
                using (var transaction = base.BeginTransaction())
434
                {
435
                    string query = string.Empty;
436

    
437
                    if (!string.IsNullOrEmpty(doc.DocID))
438
                    {
439
                        StringBuilder sbSet = new StringBuilder();
440
                        var parameters = new Dictionary<string, object>();
441

    
442
                        #region Update 할 목록
443
                        if (doc.ID2StartDate != null)
444
                        {
445
                            sbSet.Append(" ,ID2StartDate=(case when ID2StartDate is null then @DateTimeNow else ID2StartDate end) ");
446
                            parameters.Add("ID2StartDate", doc.ID2StartDate);
447
                        }
448

    
449
                        if (doc.Worker != null)
450
                        {
451
                            sbSet.Append(" ,Worker=@Worker ");
452
                            parameters.Add("Worker", doc.Worker);
453
                        }
454
                        #endregion
455

    
456
                        if (parameters.Count > 0)
457
                        {
458
                            sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
459
                            parameters.Add("ModifiedUser", userId);
460

    
461
                            parameters.Add("DocID", doc.DocID);
462

    
463
                            query = $@"
464
declare @DateTimeNow datetime
465
set @DateTimeNow = getdate()
466

    
467
update dbo.Documents
468
set    ModifiedDate=@DateTimeNow {sbSet}
469
where  [DocID]=@DocID
470

    
471
if @@rowcount > 0
472
begin
473
    select *, datediff(SECOND, ID2StartDate, ID2EndDate) as ID2JobTime from dbo.Documents where DocID=@DocID
474
end
475
else
476
begin
477
    select *, 0 as ID2JobTime from dbo.Documents where 1=2
478
end;";
479
                            resultData = base.QueryFirstOrDefault<Documents>(query, parameters, transaction);
480
                        }
481
                    }
482

    
483
                    transaction.Commit();
484
                }
485
            }
486
            catch (Exception ex)
487
            {
488
                throw ex;
489
            }
490

    
491
            return resultData;
492
        }
493

    
494
        public bool SetDocumentDatasField(List<Documents> docs, string userId)
495
        {
496
            bool isSuccess = false;
497

    
498
            try
499
            {
500
                using (var transaction = base.BeginTransaction())
501
                {
502
                    foreach (Documents doc in docs)
503
                    {
504
                        string query = string.Empty;
505

    
506
                        if (!string.IsNullOrEmpty(doc.DocID))
507
                        {
508
                            StringBuilder sbSet = new StringBuilder();
509
                            var parameters = new Dictionary<string, object>();
510

    
511
                            #region Update 할 목록
512
                            if (doc.ID2EndDate != null)
513
                            {
514
                                sbSet.Append(" ,ID2EndDate=@ID2EndDate ");
515
                                parameters.Add("ID2EndDate", doc.ID2EndDate);
516
                            }
517
                            #endregion
518

    
519
                            if (parameters.Count > 0)
520
                            {
521
                                sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
522
                                parameters.Add("ModifiedUser", userId);
523

    
524
                                parameters.Add("DocID", doc.DocID);
525

    
526
                                query = $@"
527
update dbo.Documents
528
set    ModifiedDate=getdate() {sbSet}
529
where  [DocID]=@DocID;";
530
                                base.Execute(query, parameters, transaction);
531
                            }
532
                        }
533
                    }
534
                    transaction.Commit();
535
                    isSuccess = true;
536
                }
537
            }
538
            catch (Exception ex)
539
            {
540
                throw ex;
541
            }
542

    
543
            return isSuccess;
544
        }
545

    
546

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