hytos / ID2.Manager / ID2.Manager.Dapper / Repository / ProjectRepository.cs @ 473ee829
이력 | 보기 | 이력해설 | 다운로드 (5.62 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 ID2.Manager.Data.Models; |
8 |
|
9 |
namespace ID2.Manager.Dapper.Repository |
10 |
{ |
11 |
public class ProjectRepository : BaseRepository |
12 |
{ |
13 |
public ProjectRepository(string connectionStr) : base(connectionStr) { } |
14 |
|
15 |
public IEnumerable<ProjectInfo> GetAllProjectList() |
16 |
{ |
17 |
string query = $@" |
18 |
;with Prj as |
19 |
( |
20 |
select ProjectID |
21 |
,Code |
22 |
,[Name] |
23 |
,isnull(Description,'') Description |
24 |
,isnull(ParentID,'') GroupID |
25 |
,convert(varchar(255),'') GroupName |
26 |
,1 [Level] |
27 |
from dbo.Projects |
28 |
where ParentID is null |
29 |
|
30 |
UNION ALL |
31 |
|
32 |
select p.ProjectID |
33 |
,p.Code |
34 |
,p.[Name] |
35 |
,p.Description |
36 |
,p.ParentID GroupID |
37 |
,pp.[Name] GroupName |
38 |
,pp.[Level]+1 [Level] |
39 |
FROM dbo.Projects p |
40 |
inner join Prj pp ON p.ParentID=pp.ProjectID |
41 |
) |
42 |
select * |
43 |
from prj |
44 |
order by [Level], [Name];"; |
45 |
|
46 |
return Query<ProjectInfo>(query); |
47 |
} |
48 |
|
49 |
public ProjectInfo GetProjectInfo(string ProjectID) |
50 |
{ |
51 |
string query = $@" |
52 |
;with Prj as |
53 |
( |
54 |
select ProjectID |
55 |
,Code |
56 |
,[Name] |
57 |
,isnull(Description,'') Description |
58 |
,isnull(ParentID,'') GroupID |
59 |
,convert(varchar(255),'') GroupName |
60 |
,1 [Level] |
61 |
from dbo.Projects |
62 |
where ParentID is null |
63 |
|
64 |
UNION ALL |
65 |
|
66 |
select p.ProjectID |
67 |
,p.Code |
68 |
,p.[Name] |
69 |
,p.Description |
70 |
,p.ParentID GroupID |
71 |
,pp.[Name] GroupName |
72 |
,pp.[Level]+1 [Level] |
73 |
FROM dbo.Projects p |
74 |
inner join Prj pp ON p.ParentID=pp.ProjectID |
75 |
) |
76 |
select * |
77 |
from prj |
78 |
where ProjectID=@ProjectID"; |
79 |
|
80 |
return Query<ProjectInfo>(query, new { ProjectID }).FirstOrDefault(); |
81 |
} |
82 |
|
83 |
public bool SetProjectData(ProjectInfo projectInfo, List<ID2ProjectInfo> id2ProjectList) |
84 |
{ |
85 |
bool isSuccess = false; |
86 |
|
87 |
try |
88 |
{ |
89 |
using (var transaction = base.BeginTransaction()) |
90 |
{ |
91 |
string query = string.Empty; |
92 |
|
93 |
if (projectInfo.ProjectID == null) |
94 |
{ |
95 |
query = $@" |
96 |
declare @newproject_uid table(projectId varchar(36)) |
97 |
insert into dbo.Projects(ProjectID, Code, [Name], [Description]) |
98 |
output inserted.ProjectID into @newproject_uid |
99 |
values (lower(newid()), @Code, @Name, @Description) |
100 |
if @@rowcount > 0 |
101 |
begin |
102 |
select projectId from @newproject_uid |
103 |
end |
104 |
else |
105 |
begin |
106 |
select '' projectId |
107 |
end;"; |
108 |
|
109 |
projectInfo.ProjectID = base.ExecuteScalar<string>(query, projectInfo, transaction); |
110 |
} |
111 |
else |
112 |
{ |
113 |
query = $@" |
114 |
update dbo.Projects |
115 |
set Code=@Code |
116 |
,[Name]=@Name |
117 |
,[Description]=@Description |
118 |
where ProjectID=@ProjectID;"; |
119 |
base.Execute(query, projectInfo, transaction); |
120 |
} |
121 |
|
122 |
if (id2ProjectList.Count > 0) |
123 |
{ |
124 |
var parameters = new Dictionary<string, object> |
125 |
{ |
126 |
{ "ParentID", projectInfo.ProjectID } |
127 |
}; |
128 |
|
129 |
string id2NameList = string.Join("','", id2ProjectList.Select(x => x.Name).ToList()); |
130 |
|
131 |
if (id2NameList.Length > 0) |
132 |
{ |
133 |
query = $@"delete dbo.Projects where ParentID=@ParentID and [Code] not in ('{id2NameList}');"; |
134 |
base.Execute(query, parameters, transaction); |
135 |
} |
136 |
} |
137 |
|
138 |
foreach (ID2ProjectInfo prj in id2ProjectList) |
139 |
{ |
140 |
if (!string.IsNullOrEmpty(prj.Name)) |
141 |
{ |
142 |
var parameters = new Dictionary<string, object> |
143 |
{ |
144 |
{ "ParentID", projectInfo.ProjectID }, |
145 |
{ "Name", prj.Name }, |
146 |
{ "Desc", prj.Desc } |
147 |
}; |
148 |
|
149 |
query = $@" |
150 |
if exists(select * from dbo.Projects where ParentID=@ParentID and Code=@Name) |
151 |
begin |
152 |
update dbo.Projects |
153 |
set [Description]=@Desc |
154 |
where ParentID=@ParentID and Code=@Name |
155 |
end |
156 |
else |
157 |
begin |
158 |
insert into dbo.Projects |
159 |
( |
160 |
ProjectID |
161 |
,ParentID |
162 |
,Code |
163 |
,[Name] |
164 |
,[Description] |
165 |
) |
166 |
values |
167 |
( |
168 |
lower(newid()) |
169 |
,@ParentID |
170 |
,@Name |
171 |
,@Name |
172 |
,@Desc |
173 |
) |
174 |
end;"; |
175 |
base.Execute(query, parameters, transaction); |
176 |
} |
177 |
} |
178 |
|
179 |
transaction.Commit(); |
180 |
isSuccess = true; |
181 |
} |
182 |
} |
183 |
catch (Exception ex) |
184 |
{ |
185 |
throw ex; |
186 |
} |
187 |
|
188 |
return isSuccess; |
189 |
} |
190 |
|
191 |
public bool SetProjectGroupData(ProjectInfo projectInfo) |
192 |
{ |
193 |
bool isSuccess = false; |
194 |
|
195 |
try |
196 |
{ |
197 |
using (var transaction = base.BeginTransaction()) |
198 |
{ |
199 |
string query = string.Empty; |
200 |
|
201 |
query = $@" |
202 |
delete dbo.Projects where ParentID=@ProjectID; |
203 |
delete dbo.Projects where ProjectID=@ProjectID;"; |
204 |
|
205 |
base.Execute(query, projectInfo, transaction); |
206 |
|
207 |
transaction.Commit(); |
208 |
isSuccess = true; |
209 |
} |
210 |
} |
211 |
catch (Exception ex) |
212 |
{ |
213 |
throw ex; |
214 |
} |
215 |
|
216 |
return isSuccess; |
217 |
} |
218 |
} |
219 |
} |