프로젝트

일반

사용자정보

통계
| 개정판:

hytos / ID2.Manager / ID2.Manager.Dapper / Repository / DocumentRepository.cs @ 1018a498

이력 | 보기 | 이력해설 | 다운로드 (33.3 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
using System.ComponentModel;
9
using System.Reflection;
10

    
11
using ID2.Manager.Data.Models;
12

    
13
using Dapper;
14

    
15
using Newtonsoft.Json;
16

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

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

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

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

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

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

    
62

    
63
        public (IEnumerable<Documents> dwgs, int totalCnt) GetDocuments(string projectGroupID, List<string> dateTypes, DateTime? frDate, DateTime? toDate,  string team, string projectCode, string personIncharge, string jobLevel, string documentNo, string isToIsDiscussion, string isFrReviewStatus, string id2Status, string id2Issues, string avevaStatus, string avevaIssues, string prodIsResult, string clientIsResult, string isGateWay, string isRegSystem)
64
        {
65
            var map = new CustomPropertyTypeMap(typeof(AttFileInfo), (type, columnName)
66
     => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName.ToLower()));
67
            SqlMapper.SetTypeMap(typeof(AttFileInfo), map);
68

    
69

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

    
73
            StringBuilder sbWhere = new StringBuilder();
74
            StringBuilder sbTotalWhere = new StringBuilder();
75
            var parameters = new Dictionary<string, object>();
76
            if (dateTypes.Count > 0 && (frDate != null || toDate != null))
77
            {
78
                List<string> subWheres = new List<string>();
79
                foreach (string dateType in dateTypes)
80
                {
81
                    StringBuilder sbSubWhere = new StringBuilder();
82
                    if (frDate != null)
83
                    {
84
                        sbSubWhere.Insert(0, $@"convert(datetime, '{Convert.ToDateTime(frDate).AddDays(-1):yyyy-MM-dd 23:59:59.997}') < doc.{dateType}");
85
                    }
86
                    if (toDate != null)
87
                    {
88
                        if (frDate != null)
89
                            sbSubWhere.Append($@" and ");
90
                        sbSubWhere.Append($@"doc.{dateType} < convert(datetime, '{Convert.ToDateTime(toDate).AddDays(1):yyyy-MM-dd 00:00:00.000}')");
91
                    }
92

    
93
                    if (sbSubWhere.Length > 0)
94
                    {
95
                        subWheres.Add("(" + sbSubWhere.ToString() + ")");
96
                    }
97
                }
98

    
99
                if (subWheres.Count > 0)
100
                {
101
                    sbWhere.Append(" and (" + string.Join(" or ", subWheres) + ") ");
102
                }
103
            }
104
            if (!string.IsNullOrEmpty(team))
105
            {
106
                sbWhere.Append(" and prj.Team=@Team ");
107
                parameters.Add("Team", team);
108
            }
109
            if (string.IsNullOrEmpty(projectCode))
110
            {
111
                sbWhere.Append(" and isnull(doc.RefProjectCode,'') in (select Code from dbo.Projects where ParentID=@RefGroupID union all select '') ");
112
                sbTotalWhere.Append(" and isnull(doc.RefProjectCode,'') in (select Code from dbo.Projects where ParentID=@RefGroupID union all select '') ");
113
                parameters.Add("RefGroupID", projectGroupID);
114
            }
115
            else
116
            {
117
                sbTotalWhere.Append(" and isnull(doc.RefProjectCode,'') in (select Code from dbo.Projects where ParentID=@RefGroupID union all select '') ");
118
                parameters.Add("RefGroupID", projectGroupID);
119

    
120
                sbWhere.Append(" and doc.RefProjectCode=@RefProjectCode ");
121
                parameters.Add("RefProjectCode", projectCode);
122
            }
123
            if (!string.IsNullOrEmpty(personIncharge))
124
            {
125
                sbWhere.Append(" and doc.PersonInCharge=@PersonInCharge ");
126
                parameters.Add("PersonInCharge", personIncharge);
127
            }
128
            if (!string.IsNullOrEmpty(jobLevel))
129
            {
130
                sbWhere.Append(" and doc.JobLevel=@JobLevel ");
131
                parameters.Add("JobLevel", jobLevel);
132
            }
133
            if (!string.IsNullOrEmpty(documentNo))
134
            {
135
                sbWhere.Append(" and doc.DocumentNo like '%' + @DocumentNo +'%' ");
136
                parameters.Add("DocumentNo", documentNo);
137
            }
138

    
139
            if (!string.IsNullOrEmpty(isToIsDiscussion))
140
            {
141
                sbWhere.Append(" and doc.ToIsDiscussion=@ToIsDiscussion ");
142
                parameters.Add("ToIsDiscussion", isToIsDiscussion);
143
            }
144

    
145
            if (!string.IsNullOrEmpty(isFrReviewStatus))
146
            {
147
                sbWhere.Append(" and doc.FrReviewStatus=@FrReviewStatus ");
148
                parameters.Add("FrReviewStatus", isFrReviewStatus);
149
            }
150

    
151
            if (!string.IsNullOrEmpty(id2Status))
152
            {
153
                sbWhere.Append(" and doc.ID2Status=@ID2Status ");
154
                parameters.Add("ID2Status", id2Status);
155
            }
156

    
157
            if (!string.IsNullOrEmpty(id2Issues))
158
            {
159
                sbWhere.Append(" and (case when isnull(ltrim(rtrim(doc.ID2Issues)),'') = '' then 'No' else 'Yes' end)=@ID2Issues ");
160
                parameters.Add("ID2Issues", id2Issues);
161
            }
162
            
163
            if (!string.IsNullOrEmpty(avevaStatus))
164
            {
165
                sbWhere.Append(" and doc.AVEVAStatus=@AVEVAStatus ");
166
                parameters.Add("AVEVAStatus", avevaStatus);
167
            }
168

    
169
            if (!string.IsNullOrEmpty(avevaIssues))
170
            {
171
                sbWhere.Append(" and (case when isnull(ltrim(rtrim(doc.AVEVAIssues)),'') = '' then 'No' else 'Yes' end)=@AVEVAIssues ");
172
                parameters.Add("AVEVAIssues", avevaIssues);
173
            }
174

    
175
            if (!string.IsNullOrEmpty(prodIsResult))
176
            {
177
                sbWhere.Append(" and doc.ProdIsResult=@ProdIsResult ");
178
                parameters.Add("ProdIsResult", prodIsResult);
179
            }
180
            if (!string.IsNullOrEmpty(clientIsResult))
181
            {
182
                sbWhere.Append(" and doc.ClientIsResult=@ClientIsResult ");
183
                parameters.Add("ClientIsResult", clientIsResult);
184
            }
185
            if (!string.IsNullOrEmpty(isGateWay))
186
            {
187
                sbWhere.Append(" and doc.DTIsGateWay=@DTIsGateWay ");
188
                parameters.Add("DTIsGateWay", isGateWay);
189
            }
190
            if (!string.IsNullOrEmpty(isRegSystem))
191
            {
192
                sbWhere.Append(" and doc.DTIsRegSystem=@DTIsRegSystem ");
193
                parameters.Add("DTIsRegSystem", isRegSystem);
194
            }
195

    
196
            try
197
            {
198

    
199
                //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,
200
                string query = $@"
201
select  doc.*,
202
prj.Team,
203
files.[FileID] as FileID, files.*,
204
markus.PROJECT_NO as PROJECT_NO, markus.*
205
from     dbo.Documents doc
206
left outer join dbo.Projects prj on doc.RefProjectCode=prj.Code
207
LEFT OUTER JOIN AttachFIles files ON doc.DocID = fIles.RefID 
208
LEFT OUTER JOIN 
209
        (SELECT [PROJECT_NO] as PROJECT_NO
210
                ,[DOCUMENT_ID] as DocumentNo
211
                ,[PAGENUMBER]
212
                ,[Text] as TEXT
213
                ,[CREATE_DATE] as CREATE_DATE
214
                ,[NAME] as CREATE_USER
215
            FROM ViewMarkupData) markus 
216
ON doc.RefProjectCode = markus.Project_NO AND doc.DocumentNo = markus.DocumentNo
217
where    doc.IsDeleted=0 {sbWhere}
218
order by doc.Seq
219

    
220
select @Total=count(*) from dbo.Documents doc where doc.IsDeleted=0 {sbTotalWhere}
221
select @Total;";
222

    
223
                if (parameters.Count > 0)
224
                {
225
                    dynamicParameters.AddDynamicParams(parameters);
226
                }
227

    
228
                var docDictionary = new Dictionary<string, Documents>();
229

    
230
                var ret = MultiQuery<Documents, AttFileInfo, MarkupText, Documents>(query,
231
                                (document, attfile, markusText) =>
232
                                {
233
                                    Documents doc;
234

    
235
                                    if (!docDictionary.TryGetValue(document.DocID, out doc))
236
                                    {
237
                                        doc = document;
238
                                        docDictionary.Add(doc.DocID, doc);
239
                                    }
240

    
241
                                    if (markusText != null)
242
                                    {
243
                                        doc.ConvertStatus = 4;
244

    
245
                                        if (markusText.TEXT != null && markusText.CREATE_DATE != null && markusText.CREATE_USER != null)
246
                                        {
247
                                            doc.Markups = doc.Markups ?? new List<MarkupText>();
248

    
249
                                            if (!doc.Markups.Any(x => x.Equals(markusText)))
250
                                            {
251
                                                doc.Markups.Add(markusText);
252
                                            }
253
                                        }
254
                                    }
255

    
256
                                    if (attfile != null)
257
                                    {
258
                                        doc.AttFiles = doc.AttFiles ?? new List<AttFileInfo>();
259
                                        System.Diagnostics.Debug.WriteLine(attfile.FileName);
260
                                        if (!doc.AttFiles.Any(x => x.FileID == attfile.FileID))
261
                                        {
262
                                            switch (attfile.Category)
263
                                            {
264
                                                case "toreview":
265
                                                    doc.ToCapture++;
266
                                                    break;
267
                                                case "frreview":
268
                                                    doc.FrCapture++;
269
                                                    break;
270
                                                case "id2work":
271
                                                    doc.ID2Capture++;
272
                                                    break;
273
                                            }
274

    
275
                                            doc.AttFiles.Add(attfile);
276
                                        }
277
                                    }
278

    
279
                                    return doc;
280

    
281
                                }, dynamicParameters, splitOn: "DocID,FileID,PROJECT_NO").Distinct();
282

    
283
                int totalCount = dynamicParameters.Get<int>("Total");
284

    
285
                return (ret, totalCount);
286
            }
287
            catch (Exception ex)
288
            {
289
                throw ex;
290
            }
291
        }
292

    
293
        public int ExistsDocument(string projectGroupID, List<string> newDwgNos)
294
        {
295
            int existCount = 0;
296

    
297
            int paramMaxCount = 2000;
298
            int execCount = (newDwgNos.Count / paramMaxCount) + 1;
299

    
300
            for (int i = 0; i < execCount; i++)
301
            {
302
                var dynamicParameters = new DynamicParameters();
303
                StringBuilder sbWhere = new StringBuilder();
304
                var parameters = new Dictionary<string, object>();
305
                if (!string.IsNullOrEmpty(projectGroupID))
306
                {
307
                    sbWhere.Append(" and doc.RefProjectCode in (select Code from dbo.Projects where ParentID=@RefGroupID) ");
308
                    parameters.Add("RefGroupID", projectGroupID);
309
                }
310

    
311
                var limitDwgNos = newDwgNos.Skip(paramMaxCount * i).Take(paramMaxCount).ToList();
312
                sbWhere.Append($@" and doc.DocumentNo in @limitDwgNos ");
313
                parameters.Add("limitDwgNos", limitDwgNos);
314

    
315
                try
316
                {
317
                    string query = $@"
318
select   count(*) cnt
319
from     dbo.Documents doc
320
where    doc.IsDeleted=0 {sbWhere}";
321

    
322
                    if (parameters.Count > 0)
323
                    {
324
                        dynamicParameters.AddDynamicParams(parameters);
325
                    }
326

    
327
                    existCount += ExecuteScalar<int>(query, dynamicParameters);
328
                }
329
                catch (Exception ex)
330
                {
331
                    throw ex;
332
                }
333
            }
334

    
335
            return existCount;
336
        }
337

    
338
        public bool SetDocumentData(string projectGroupID, List<Documents> docList, List<string> delDocList, string userId)
339
        {
340
            bool isSuccess = false;
341

    
342
            try
343
            {
344
                using (var transaction = base.BeginTransaction())
345
                {
346
                    string query = string.Empty;
347

    
348
                    if (delDocList.Count > 0)
349
                    {
350
                        int paramMaxCount = 2000;
351
                        int execCount = (delDocList.Count / paramMaxCount) + 1;
352

    
353
                        for (int i = 0; i < execCount; i++)
354
                        {
355
                            var dynamicParameters = new DynamicParameters();
356
                            StringBuilder sbWhere = new StringBuilder();
357
                            var parameters = new Dictionary<string, object>();
358

    
359
                            parameters.Add("DeletedUser", userId);
360

    
361
                            var limitDwgNos = delDocList.Skip(paramMaxCount * i).Take(paramMaxCount).ToList();
362
                            sbWhere.Append($@" and DocID in @limitDwgNos ");
363
                            parameters.Add("limitDwgNos", limitDwgNos);
364

    
365
                            query = $@"
366
update dbo.Documents
367
set    IsDeleted=1
368
      ,DeletedDate=getdate()
369
      ,DeletedUser=@DeletedUser
370
where  IsDeleted=0 {sbWhere}";
371
                            if (parameters.Count > 0)
372
                            {
373
                                dynamicParameters.AddDynamicParams(parameters);
374
                            }
375

    
376
                            base.Execute(query, dynamicParameters, transaction);
377
                        }
378
                    }
379

    
380
                    foreach (Documents doc in docList)
381
                    {
382
                        if (string.IsNullOrEmpty(doc.DocID))
383
                        {
384
                            doc.RegisteredUser = userId;
385
                            doc.ModifiedUser = userId;
386

    
387
                            query = $@"
388
if exists(select * from dbo.Documents where RefProjectCode=@RefProjectCode and DocumentNo=@DocumentNo and IsDeleted=0)
389
begin
390
    --update
391
    update dbo.Documents
392
    set    RevisonNo=case when isnull(@RevisonNo,'')='' then '0' else @RevisonNo end
393
          ,System=@System
394
          ,SubSystemCode=@SubSystemCode
395
          ,JobLevel=@JobLevel
396
          ,Simularity=@Simularity
397
          ,PersonInCharge=@PersonInCharge
398
          ,Worker=@Worker
399
          ,ModifiedDate=getdate()
400
          ,ModifiedUser=@ModifiedUser
401
          ,ToIsDiscussion=@ToIsDiscussion
402
          ,ToRemarks=@ToRemarks
403
          ,ToCreator=@ToCreator
404
          ,FrReviewStatus=@FrReviewStatus
405
          ,FrRemarks=@FrRemarks
406
          ,FrCreator=@FrCreator
407
          ,ID2StartDate=@ID2StartDate
408
          ,ID2EndDate=@ID2EndDate
409
          ,ID2Status=@ID2Status
410
          ,ID2Issues=@ID2Issues
411
          ,ReplyModifications=@ReplyModifications
412
          ,ReplyRequester=@ReplyRequester
413
          ,IsConvert=@IsConvert
414
          ,AVEVAPersonInCharge=@AVEVAPersonInCharge
415
          ,AVEVAWorker=@AVEVAWorker
416
          ,AVEVAConvertDate=@AVEVAConvertDate
417
          ,AVEVAReviewDate=@AVEVAReviewDate
418
          ,AVEVAWorkDate=@AVEVAWorkDate
419
          ,AVEVAStatus=@AVEVAStatus
420
          ,AVEVAIssues=@AVEVAIssues
421
          ,ProdReviewer=@ProdReviewer
422
          ,ProdIsResult=@ProdIsResult
423
          ,ProdRemarks=@ProdRemarks
424
          ,ClientReviewer=@ClientReviewer
425
          ,ClientIsResult=@ClientIsResult
426
          ,ClientRemarks=@ClientRemarks
427
          ,DTIsGateWay=@DTIsGateWay
428
          ,DTIsImport=@DTIsImport
429
          ,DTIsRegSystem=@DTIsRegSystem
430
          ,DTRemarks=@DTRemarks
431
    where  RefProjectCode=@RefProjectCode and DocumentNo=@DocumentNo and IsDeleted=0
432

    
433
    if @@rowcount > 0
434
    begin
435
        select @DocID
436
    end
437
    else
438
    begin
439
        select ''
440
    end
441
end
442
else
443
begin
444
    --insert
445
    declare @tbdoc table(docid varchar(36))
446
    insert into dbo.Documents
447
    (
448
         DocID
449
        ,DocumentNo
450
        ,RevisonNo
451
        ,System
452
        ,SubSystemCode
453
        ,RefProjectCode
454
        ,JobLevel
455
        ,Simularity
456
        ,PersonInCharge
457
        ,Worker
458
        ,RegisteredDate
459
        ,RegisteredUser
460
        ,ToIsDiscussion
461
        ,ToRemarks
462
        ,ToCreator
463
        ,FrReviewStatus
464
        ,FrRemarks
465
        ,FrCreator
466
        ,ID2StartDate
467
        ,ID2EndDate
468
        ,ID2Status
469
        ,ID2Issues
470
        ,ReplyModifications
471
        ,ReplyRequester
472
        ,IsConvert
473
        ,AVEVAPersonInCharge
474
        ,AVEVAWorker
475
        ,AVEVAConvertDate
476
        ,AVEVAReviewDate
477
        ,AVEVAWorkDate
478
        ,AVEVAStatus
479
        ,AVEVAIssues
480
        ,ProdReviewer
481
        ,ProdIsResult
482
        ,ProdRemarks
483
        ,ClientReviewer
484
        ,ClientIsResult
485
        ,ClientRemarks
486
        ,DTIsGateWay
487
        ,DTIsImport
488
        ,DTIsRegSystem
489
        ,DTRemarks
490
    )
491
    output inserted.DocID into @tbdoc
492
    values 
493
    (
494
         lower(newid())
495
        ,@DocumentNo
496
        ,case when isnull(@RevisonNo,'')='' then '0' else @RevisonNo end
497
        ,@System
498
        ,@SubSystemCode
499
        ,@RefProjectCode
500
        ,@JobLevel
501
        ,@Simularity
502
        ,@PersonInCharge
503
        ,@Worker
504
        ,getdate()
505
        ,@RegisteredUser
506
        ,@ToIsDiscussion
507
        ,@ToRemarks
508
        ,@ToCreator
509
        ,@FrReviewStatus
510
        ,@FrRemarks
511
        ,@FrCreator
512
        ,@ID2StartDate
513
        ,@ID2EndDate
514
        ,@ID2Status
515
        ,@ID2Issues
516
        ,@ReplyModifications
517
        ,@ReplyRequester
518
        ,@IsConvert
519
        ,@AVEVAPersonInCharge
520
        ,@AVEVAWorker
521
        ,@AVEVAConvertDate
522
        ,@AVEVAReviewDate
523
        ,@AVEVAWorkDate
524
        ,@AVEVAStatus
525
        ,@AVEVAIssues
526
        ,@ProdReviewer
527
        ,@ProdIsResult
528
        ,@ProdRemarks
529
        ,@ClientReviewer
530
        ,@ClientIsResult
531
        ,@ClientRemarks
532
        ,@DTIsGateWay
533
        ,@DTIsImport
534
        ,@DTIsRegSystem
535
        ,@DTRemarks
536
    )
537

    
538
    if @@rowcount > 0
539
    begin
540
        select docid from @tbdoc
541
    end
542
    else
543
    begin
544
        select ''
545
    end
546
end";
547
                        }
548
                        else
549
                        {
550
                            doc.ModifiedUser = userId;
551
                            query = $@"
552
update dbo.Documents
553
set    RevisonNo=case when isnull(@RevisonNo,'')='' then '0' else @RevisonNo end
554
      ,System=@System
555
      ,SubSystemCode=@SubSystemCode
556
      ,RefProjectCode=@RefProjectCode
557
      ,JobLevel=@JobLevel
558
      ,Simularity=@Simularity
559
      ,PersonInCharge=@PersonInCharge
560
      ,Worker=@Worker
561
      ,ModifiedDate=getdate()
562
      ,ModifiedUser=@ModifiedUser
563
      ,ToIsDiscussion=@ToIsDiscussion
564
      ,ToRemarks=@ToRemarks
565
      ,ToCreator=@ToCreator
566
      ,FrReviewStatus=@FrReviewStatus
567
      ,FrRemarks=@FrRemarks
568
      ,FrCreator=@FrCreator
569
      ,ID2StartDate=@ID2StartDate
570
      ,ID2EndDate=@ID2EndDate
571
      ,ID2Status=@ID2Status
572
      ,ID2Issues=@ID2Issues
573
      ,ReplyModifications=@ReplyModifications
574
      ,ReplyRequester=@ReplyRequester
575
      ,IsConvert=@IsConvert
576
      ,AVEVAPersonInCharge=@AVEVAPersonInCharge
577
      ,AVEVAWorker=@AVEVAWorker
578
      ,AVEVAConvertDate=@AVEVAConvertDate
579
      ,AVEVAReviewDate=@AVEVAReviewDate
580
      ,AVEVAWorkDate=@AVEVAWorkDate
581
      ,AVEVAStatus=@AVEVAStatus
582
      ,AVEVAIssues=@AVEVAIssues
583
      ,ProdReviewer=@ProdReviewer
584
      ,ProdIsResult=@ProdIsResult
585
      ,ProdRemarks=@ProdRemarks
586
      ,ClientReviewer=@ClientReviewer
587
      ,ClientIsResult=@ClientIsResult
588
      ,ClientRemarks=@ClientRemarks
589
      ,DTIsGateWay=@DTIsGateWay
590
      ,DTIsImport=@DTIsImport
591
      ,DTIsRegSystem=@DTIsRegSystem
592
      ,DTRemarks=@DTRemarks
593
where  DocID=@DocID
594

    
595
if @@rowcount > 0
596
begin
597
    select @DocID
598
end
599
else
600
begin
601
    select ''
602
end;";
603
                        }
604
                        string refID = base.QueryFirstOrDefault<string>(query, doc, transaction);
605

    
606
                        if (doc.AttFiles != null && doc.AttFiles.Count > 0)
607
                        {
608
                            string attDelIDList = string.Join("','", doc.AttFiles.Where(x => !string.IsNullOrEmpty(x.FileID)).Select(x => x.FileID).ToList());
609

    
610
                            if (!string.IsNullOrEmpty(refID) && attDelIDList.Length > 0)
611
                            {
612
                                query = $@"
613
delete from dbo.AttachFIles
614
where  RefID=@RefID and FileID in ('{attDelIDList}');";
615
                                base.Execute(query, new { RefID = refID }, transaction);
616
                            }
617

    
618
                            foreach (AttFileInfo attFile in doc.AttFiles)
619
                            {
620
                                if (string.IsNullOrEmpty(attFile.RefID))
621
                                {
622
                                    attFile.RefID = refID;
623
                                    attFile.Creator = userId;
624

    
625
                                    query = $@"
626
insert into dbo.AttachFIles (FileID,RefID,Category,FileType,FileName,FilePath,FileExtension,FileData,Creator)
627
values
628
(
629
    lower(newid())
630
   ,@RefID
631
   ,@Category
632
   ,@FileType
633
   ,@FileName
634
   ,@FilePath
635
   ,@FileExtension
636
   ,@FileData
637
   ,@Creator
638
)";
639

    
640
                                    base.Execute(query, attFile, transaction);
641
                                }
642
                            }
643
                        }
644
                    }
645

    
646
                    transaction.Commit();
647
                    isSuccess = true;
648
                }
649
            }
650
            catch (Exception ex)
651
            {
652
                throw ex;
653
            }
654

    
655
            return isSuccess;
656
        }
657

    
658
        public Documents SetDocumentDataField(Documents doc, string userId)
659
        {
660
            Documents resultData = null;
661

    
662
            try
663
            {
664
                using (var transaction = base.BeginTransaction())
665
                {
666
                    string query = string.Empty;
667

    
668
                    if (!string.IsNullOrEmpty(doc.DocID))
669
                    {
670
                        StringBuilder sbSet = new StringBuilder();
671
                        var parameters = new Dictionary<string, object>();
672

    
673
                        #region Update 할 목록
674
                        if (doc.ID2StartDate != null)
675
                        {
676
                            sbSet.Append(" ,ID2StartDate=(case when ID2StartDate is null then @DateTimeNow else ID2StartDate end) ");
677
                            parameters.Add("ID2StartDate", doc.ID2StartDate);
678
                        }
679

    
680
                        if (!string.IsNullOrEmpty(doc.Worker))
681
                        {
682
                            sbSet.Append(" ,Worker=@Worker ");
683
                            parameters.Add("Worker", doc.Worker);
684
                        }
685
                        #endregion
686

    
687
                        if (parameters.Count > 0)
688
                        {
689
                            sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
690
                            parameters.Add("ModifiedUser", userId);
691

    
692
                            parameters.Add("DocID", doc.DocID);
693

    
694
                            query = $@"
695
declare @DateTimeNow datetime
696
set @DateTimeNow = getdate()
697

    
698
update dbo.Documents
699
set    ModifiedDate=@DateTimeNow {sbSet}
700
where  [DocID]=@DocID
701

    
702
if @@rowcount > 0
703
begin
704
    select * from dbo.Documents where DocID=@DocID
705
end
706
else
707
begin
708
    select * from dbo.Documents where 1=2
709
end;";
710
                            resultData = base.QueryFirstOrDefault<Documents>(query, parameters, transaction);
711
                        }
712
                    }
713

    
714
                    transaction.Commit();
715
                }
716
            }
717
            catch (Exception ex)
718
            {
719
                throw ex;
720
            }
721

    
722
            return resultData;
723
        }
724

    
725
        public bool SetDocumentDatasField(List<Documents> docs, string userId)
726
        {
727
            bool isSuccess = false;
728

    
729
            try
730
            {
731
                using (var transaction = base.BeginTransaction())
732
                {
733
                    foreach (Documents doc in docs)
734
                    {
735
                        string query = string.Empty;
736

    
737
                        if (!string.IsNullOrEmpty(doc.DocID))
738
                        {
739
                            StringBuilder sbSet = new StringBuilder();
740
                            var parameters = new Dictionary<string, object>();
741

    
742
                            #region Update 할 목록
743
                            if (doc.ID2EndDate != null)
744
                            {
745
                                sbSet.Append(" ,ID2EndDate=@ID2EndDate ");
746
                                parameters.Add("ID2EndDate", doc.ID2EndDate);
747
                            }
748
                            #endregion
749

    
750
                            if (parameters.Count > 0)
751
                            {
752
                                sbSet.Append(" ,ModifiedUser=@ModifiedUser ");
753
                                parameters.Add("ModifiedUser", userId);
754

    
755
                                parameters.Add("DocID", doc.DocID);
756

    
757
                                query = $@"
758
update dbo.Documents
759
set    ModifiedDate=getdate() {sbSet}
760
where  [DocID]=@DocID;";
761
                                base.Execute(query, parameters, transaction);
762
                            }
763
                        }
764
                    }
765
                    transaction.Commit();
766
                    isSuccess = true;
767
                }
768
            }
769
            catch (Exception ex)
770
            {
771
                throw ex;
772
            }
773

    
774
            return isSuccess;
775
        }
776

    
777

    
778
        //ID2
779
        public IEnumerable<ID2Drawings> GetID2DrawingsByProject(ID2ProjectInfo id2Info)
780
        {
781
            try
782
            {
783
                string query = $@"
784
select @Name PROJECTNAME
785
      ,dw.[UID], dw.[NAME], left(dw.[NAME], len(dw.[NAME]) - dw.lastidx) DOCNAME
786
      ,case when rtrim(ltrim(isnull(convert(varchar, dw.[DATETIME]),''))) = '' then null else convert(datetime, convert(varchar, dw.[DATETIME])) end [DATETIME]
787
      ,dw.OCCUPIED, dw.[Image]
788
from
789
(
790
    select [UID], [NAME], [DATETIME], OCCUPIED, [Image], charindex('.', reverse([NAME])) lastidx
791
    from   dbo.Drawings
792
) dw;";
793
                return Query<ID2Drawings>(query, id2Info);
794
            }
795
            catch (Exception ex)
796
            {
797
                throw ex;
798
            }
799
        }
800

    
801

    
802
        //Transactions
803
        public int GetTranKey(string userId, string projectID)
804
        {
805
            int result = -1;
806

    
807
            try
808
            {
809
                var dynamicParameters = new DynamicParameters();
810

    
811
                var parameters = new Dictionary<string, object>()
812
                {
813
                    { "UserId", userId },
814
                    { "ProjectID", projectID }
815
                };
816

    
817
                dynamicParameters.AddDynamicParams(parameters);
818

    
819
                using (var transaction = base.BeginTransaction())
820
                {
821
                    string query = $@"
822
--log master 입력
823
insert into dbo.Transactions (UserId, ProjectID)
824
values (@UserId, @ProjectID);
825
select scope_identity();";
826

    
827
                    result = base.ExecuteScalar<int>(query, dynamicParameters, transaction);
828

    
829
                    transaction.Commit();
830
                }
831
            }
832
            catch (Exception ex)
833
            {
834
                throw ex;
835
            }
836

    
837
            return result;
838
        }
839

    
840
        public bool SetTran(int seq, bool isMgt, bool isStart, int itemCount, int markusItemCount)
841
        {
842
            bool result = false;
843

    
844
            try
845
            {
846
                var dynamicParameters = new DynamicParameters();                
847
                var parameters = new Dictionary<string, object>()
848
                {
849
                    { "Seq", seq },
850
                    { "ItemCount", itemCount },
851
                    { "MarkusItemCount", markusItemCount }
852
                };
853
                dynamicParameters.AddDynamicParams(parameters);
854
                StringBuilder sbWhere = new StringBuilder();
855
                if (isMgt)
856
                {
857
                    if (isStart)
858
                    {
859
                        sbWhere.Append("StartDate=getdate(), ItemCount=@ItemCount");
860
                    }
861
                    else
862
                    {
863
                        sbWhere.Append("EndDate=getdate()");
864
                    }
865
                    
866
                }
867
                else
868
                {
869
                    if (isStart)
870
                    {
871
                        sbWhere.Append("MarkusStartDate=getdate(), MarkusItemCount=@MarkusItemCount");
872
                    }
873
                    else
874
                    {
875
                        sbWhere.Append("MarkusEndDate=getdate()");
876
                    }
877
                }
878

    
879
                using (var transaction = base.BeginTransaction())
880
                {
881
                    string query = $@"
882
update dbo.Transactions
883
set    {sbWhere}
884
where Seq=@Seq;";
885
                    base.Execute(query, dynamicParameters, transaction);
886

    
887
                    transaction.Commit();
888

    
889
                    result = true;
890
                }
891
            }
892
            catch (Exception ex)
893
            {
894
                throw ex;
895
            }
896

    
897
            return result;
898
        }
899

    
900
        public string GetTranData(int seq)
901
        {
902
            try
903
            {
904
                var dynamicParameters = new DynamicParameters();
905
                var parameters = new Dictionary<string, object>()
906
                {
907
                    { "Seq", seq }
908
                };
909
                dynamicParameters.AddDynamicParams(parameters);
910

    
911
                string query = $@"
912
select *
913
from   dbo.Transactions
914
where  Seq=@Seq";
915

    
916
                IEnumerable<dynamic> rows = base.Query<dynamic>(query, dynamicParameters);
917

    
918
                return this.ToSerializeData(rows);
919
            }
920
            catch (Exception ex)
921
            {
922
                throw ex;
923
            }
924
        }
925

    
926
        public string ToSerializeData(IEnumerable<dynamic> items)
927
        {
928
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
929

    
930
            if (items == null) return string.Empty;
931
            var datas = items.ToArray();
932
            if (datas.Length == 0) return string.Empty;
933

    
934
            foreach (var data in datas)
935
            {
936
                var dicItem = new Dictionary<string, object>();
937
                foreach (var pair in ((IDictionary<string, object>)data))
938
                {
939
                    dicItem.Add(pair.Key, pair.Value);
940
                }
941
                list.Add(dicItem);
942
            }
943

    
944
            return JsonConvert.SerializeObject(list);
945
        }
946

    
947
        public IEnumerable<Documents> GetTrDocuments(int seq)
948
        {
949
            var dynamicParameters = new DynamicParameters();
950
            StringBuilder sbWhere = new StringBuilder();
951
            var parameters = new Dictionary<string, object>()
952
            {
953
                { "Seq", seq }
954
            };
955
            dynamicParameters.AddDynamicParams(parameters);
956

    
957
            try
958
            {
959
                string query = $@"
960
declare @CreatedDate datetime
961
declare @EndDate datetime
962
declare @ProjectGroupID varchar(36)
963

    
964
select top 1 @CreatedDate=CreatedDate, @EndDate=EndDate, @ProjectGroupID=ProjectID
965
from   dbo.Transactions
966
where  Seq=@Seq
967

    
968
--select RefProjectCode, DocumentNo
969
--from   dbo.Documents
970
--where  RefProjectCode in (select Code from dbo.Projects where ParentID=@ProjectGroupID)
971
--   and RegisteredDate between @CreatedDate and @EndDate
972

    
973
select RefProjectCode, DocumentNo
974
from   dbo.Documents
975
where  (RegisteredDate between @CreatedDate and @EndDate) or (ModifiedDate between @CreatedDate and @EndDate)";
976

    
977
                if (parameters.Count > 0)
978
                {
979
                    dynamicParameters.AddDynamicParams(parameters);
980
                }
981

    
982
                return Query<Documents>(query, dynamicParameters);
983
            }
984
            catch (Exception ex)
985
            {
986
                throw ex;
987
            }
988
        }
989

    
990
        public class MarkusConvert
991
        {
992
            public string PROJECT_NO { get; set; }
993
            public string DOCUMENT_ID { get; set; }
994
            public int STATUS { get; set; }
995
        }
996

    
997
    }
998
}
클립보드 이미지 추가 (최대 크기: 500 MB)