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