hytos / ID2.Manager / ID2.Manager.Dapper / Repository / ProjectRepository.cs @ 989966d4
이력 | 보기 | 이력해설 | 다운로드 (7.31 KB)
1 | cab8a4a1 | yoush97 | 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 | 87bcedb4 | yoush97 | 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(ID2Path,'') ID2Path |
||
25 | ,isnull(ParentID,'') GroupID |
||
26 | ,convert(varchar(255),'') GroupName |
||
27 | ,1 [Level] |
||
28 | from dbo.Projects |
||
29 | where ParentID is null |
||
30 | |||
31 | UNION ALL |
||
32 | |||
33 | select p.ProjectID |
||
34 | ,p.Code |
||
35 | ,p.[Name] |
||
36 | ,p.Description |
||
37 | ,p.ID2Path |
||
38 | ,p.ParentID GroupID |
||
39 | ,pp.[Name] GroupName |
||
40 | ,pp.[Level]+1 [Level] |
||
41 | FROM dbo.Projects p |
||
42 | inner join Prj pp ON p.ParentID=pp.ProjectID |
||
43 | ) |
||
44 | select * |
||
45 | from prj |
||
46 | order by [Level], [Name];"; |
||
47 | |||
48 | return Query<ProjectInfo>(query); |
||
49 | } |
||
50 | |||
51 | 8e373ccf | yoush97 | public IEnumerable<ProjectInfo> GetProjectGroupList() |
52 | cab8a4a1 | yoush97 | { |
53 | string query = $@" |
||
54 | select ProjectID |
||
55 | ,Code |
||
56 | ,[Name] |
||
57 | 8e373ccf | yoush97 | ,isnull(Description,'') Description |
58 | ,isnull(ID2Path,'') ID2Path |
||
59 | cab8a4a1 | yoush97 | ,isnull(ParentID,'') GroupID |
60 | ,convert(varchar(255),'') GroupName |
||
61 | ,1 [Level] |
||
62 | from dbo.Projects |
||
63 | where ParentID is null |
||
64 | order by [Name];"; |
||
65 | |||
66 | return Query<ProjectInfo>(query); |
||
67 | } |
||
68 | |||
69 | public IEnumerable<ProjectInfo> GetProjectList(string ProjectID, bool IsGroup) |
||
70 | { |
||
71 | string query = $@" |
||
72 | ;with Prj as |
||
73 | ( |
||
74 | select ProjectID |
||
75 | ,Code |
||
76 | ,[Name] |
||
77 | 8e373ccf | yoush97 | ,isnull(Description,'') Description |
78 | ,isnull(ID2Path,'') ID2Path |
||
79 | cab8a4a1 | yoush97 | ,isnull(ParentID,'') GroupID |
80 | ,convert(varchar(255),'') GroupName |
||
81 | ,1 [Level] |
||
82 | from dbo.Projects |
||
83 | where ParentID is null and ProjectID=@ProjectID |
||
84 | |||
85 | UNION ALL |
||
86 | |||
87 | select p.ProjectID |
||
88 | ,p.Code |
||
89 | ,p.[Name] |
||
90 | 8e373ccf | yoush97 | ,p.Description |
91 | ,p.ID2Path |
||
92 | cab8a4a1 | yoush97 | ,p.ParentID GroupID |
93 | ,pp.[Name] GroupName |
||
94 | ,pp.[Level]+1 [Level] |
||
95 | FROM dbo.Projects p |
||
96 | inner join Prj pp ON p.ParentID=pp.ProjectID)"; |
||
97 | if (IsGroup) |
||
98 | { |
||
99 | query += $@" |
||
100 | select * |
||
101 | from prj |
||
102 | order by [Level], [Name];"; |
||
103 | } |
||
104 | else |
||
105 | { |
||
106 | query += $@" |
||
107 | select * |
||
108 | from prj |
||
109 | where ProjectID <> @ProjectID |
||
110 | order by [Level], [Name];"; |
||
111 | } |
||
112 | |||
113 | return Query<ProjectInfo>(query, new { ProjectID }); |
||
114 | } |
||
115 | |||
116 | public ProjectInfo GetProjectInfo(string ProjectID) |
||
117 | { |
||
118 | string query = $@" |
||
119 | a23d0a0c | yoush97 | ;with Prj as |
120 | ( |
||
121 | select ProjectID |
||
122 | ,Code |
||
123 | ,[Name] |
||
124 | ,isnull(Description,'') Description |
||
125 | ,isnull(ID2Path,'') ID2Path |
||
126 | ,isnull(ParentID,'') GroupID |
||
127 | ,convert(varchar(255),'') GroupName |
||
128 | ,1 [Level] |
||
129 | from dbo.Projects |
||
130 | where ParentID is null |
||
131 | |||
132 | UNION ALL |
||
133 | |||
134 | select p.ProjectID |
||
135 | ,p.Code |
||
136 | ,p.[Name] |
||
137 | ,p.Description |
||
138 | ,p.ID2Path |
||
139 | ,p.ParentID GroupID |
||
140 | ,pp.[Name] GroupName |
||
141 | ,pp.[Level]+1 [Level] |
||
142 | FROM dbo.Projects p |
||
143 | inner join Prj pp ON p.ParentID=pp.ProjectID |
||
144 | ) |
||
145 | select * |
||
146 | from prj |
||
147 | where ProjectID=@ProjectID"; |
||
148 | cab8a4a1 | yoush97 | |
149 | return Query<ProjectInfo>(query, new { ProjectID }).FirstOrDefault(); |
||
150 | } |
||
151 | a23d0a0c | yoush97 | |
152 | public bool SetProjectData(ProjectInfo projectInfo, List<ID2ProjectInfo> id2ProjectList) |
||
153 | { |
||
154 | bool isSuccess = false; |
||
155 | |||
156 | try |
||
157 | { |
||
158 | using (var transaction = base.BeginTransaction()) |
||
159 | { |
||
160 | string query = string.Empty; |
||
161 | |||
162 | if (projectInfo.ProjectID == null) |
||
163 | { |
||
164 | query = $@" |
||
165 | declare @newproject_uid table(projectId varchar(36)) |
||
166 | insert into dbo.Projects(ProjectID, Code, [Name], [Description]) |
||
167 | output inserted.ProjectID into @newproject_uid |
||
168 | values (lower(newid()), @Code, @Name, @Description) |
||
169 | if @@rowcount > 0 |
||
170 | begin |
||
171 | select projectId from @newproject_uid |
||
172 | end |
||
173 | else |
||
174 | begin |
||
175 | select '' projectId |
||
176 | end;"; |
||
177 | |||
178 | projectInfo.ProjectID = base.ExecuteScalar<string>(query, projectInfo, transaction); |
||
179 | } |
||
180 | else |
||
181 | { |
||
182 | query = $@" |
||
183 | update dbo.Projects |
||
184 | set Code=@Code |
||
185 | ,[Name]=@Name |
||
186 | ,[Description]=@Description |
||
187 | where ProjectID=@ProjectID;"; |
||
188 | base.Execute(query, projectInfo, transaction); |
||
189 | } |
||
190 | |||
191 | if (id2ProjectList.Count > 0) |
||
192 | { |
||
193 | var parameters = new Dictionary<string, object> |
||
194 | { |
||
195 | { "ParentID", projectInfo.ProjectID } |
||
196 | }; |
||
197 | |||
198 | string id2NameList = string.Join("','", id2ProjectList.Select(x => x.Name).ToList()); |
||
199 | |||
200 | if (id2NameList.Length > 0) |
||
201 | { |
||
202 | query = $@"delete dbo.Projects where ParentID=@ParentID and [Code] not in ('{id2NameList}');"; |
||
203 | base.Execute(query, parameters, transaction); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | foreach (ID2ProjectInfo prj in id2ProjectList) |
||
208 | { |
||
209 | if (!string.IsNullOrEmpty(prj.Name)) |
||
210 | { |
||
211 | var parameters = new Dictionary<string, object> |
||
212 | { |
||
213 | { "ParentID", projectInfo.ProjectID }, |
||
214 | { "Name", prj.Name }, |
||
215 | { "Desc", prj.Desc }, |
||
216 | { "Path", prj.Path } |
||
217 | }; |
||
218 | |||
219 | query = $@" |
||
220 | if exists(select * from dbo.Projects where ParentID=@ParentID and Code=@Name) |
||
221 | begin |
||
222 | update dbo.Projects |
||
223 | set [Description]=@Desc |
||
224 | ,ID2Path=@Path |
||
225 | where ParentID=@ParentID and Code=@Name |
||
226 | end |
||
227 | else |
||
228 | begin |
||
229 | insert into dbo.Projects |
||
230 | ( |
||
231 | ProjectID |
||
232 | ,ParentID |
||
233 | ,Code |
||
234 | ,[Name] |
||
235 | ,[Description] |
||
236 | ,ID2Path |
||
237 | ) |
||
238 | values |
||
239 | ( |
||
240 | lower(newid()) |
||
241 | ,@ParentID |
||
242 | ,@Name |
||
243 | ,@Name |
||
244 | ,@Desc |
||
245 | ,@Path |
||
246 | ) |
||
247 | end;"; |
||
248 | base.Execute(query, parameters, transaction); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | transaction.Commit(); |
||
253 | isSuccess = true; |
||
254 | } |
||
255 | } |
||
256 | catch (Exception ex) |
||
257 | { |
||
258 | throw ex; |
||
259 | } |
||
260 | |||
261 | return isSuccess; |
||
262 | } |
||
263 | |||
264 | public bool SetProjectGroupData(ProjectInfo projectInfo) |
||
265 | { |
||
266 | bool isSuccess = false; |
||
267 | |||
268 | try |
||
269 | { |
||
270 | using (var transaction = base.BeginTransaction()) |
||
271 | { |
||
272 | string query = string.Empty; |
||
273 | |||
274 | query = $@" |
||
275 | delete dbo.Projects where ParentID=@ProjectID; |
||
276 | delete dbo.Projects where ProjectID=@ProjectID;"; |
||
277 | |||
278 | base.Execute(query, projectInfo, transaction); |
||
279 | |||
280 | transaction.Commit(); |
||
281 | isSuccess = true; |
||
282 | } |
||
283 | } |
||
284 | catch (Exception ex) |
||
285 | { |
||
286 | throw ex; |
||
287 | } |
||
288 | |||
289 | return isSuccess; |
||
290 | } |
||
291 | cab8a4a1 | yoush97 | } |
292 | 8e373ccf | yoush97 | } |