forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartmentController.cs
More file actions
222 lines (189 loc) · 7.19 KB
/
DepartmentController.cs
File metadata and controls
222 lines (189 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using Blog.Core.Common.Helper;
using Blog.Core.Controllers;
using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Blog.Core.Api.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class DepartmentController : BaseApiController
{
private readonly IDepartmentServices _departmentServices;
private readonly IWebHostEnvironment _env;
public DepartmentController(IDepartmentServices departmentServices, IWebHostEnvironment env)
{
_departmentServices = departmentServices;
_env = env;
}
[HttpGet]
public async Task<MessageModel<PageModel<Department>>> Get(int page = 1, string key = "", int intPageSize = 50)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
Expression<Func<Department, bool>> whereExpression = a => true;
return new MessageModel<PageModel<Department>>()
{
msg = "获取成功",
success = true,
response = await _departmentServices.QueryPage(whereExpression, page, intPageSize)
};
}
[HttpGet("{id}")]
public async Task<MessageModel<Department>> Get(string id)
{
return new MessageModel<Department>()
{
msg = "获取成功",
success = true,
response = await _departmentServices.QueryById(id)
};
}
/// <summary>
/// 查询树形 Table
/// </summary>
/// <param name="f">父节点</param>
/// <param name="key">关键字</param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel<List<Department>>> GetTreeTable(long f = 0, string key = "")
{
List<Department> departments = new List<Department>();
var departmentList = await _departmentServices.Query(d => d.IsDeleted == false);
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
if (key != "")
{
departments = departmentList.Where(a => a.Name.Contains(key)).OrderBy(a => a.OrderSort).ToList();
}
else
{
departments = departmentList.Where(a => a.Pid == f).OrderBy(a => a.OrderSort).ToList();
}
foreach (var item in departments)
{
List<int> pidarr = new() { };
var parent = departmentList.FirstOrDefault(d => d.Id == item.Pid);
while (parent != null)
{
pidarr.Add(parent.Id);
parent = departmentList.FirstOrDefault(d => d.Id == parent.Pid);
}
pidarr.Reverse();
pidarr.Insert(0, 0);
item.PidArr = pidarr;
item.hasChildren = departmentList.Where(d => d.Pid == item.Id).Any();
}
return Success(departments, "获取成功");
}
/// <summary>
/// 获取部门树
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<DepartmentTree>> GetDepartmentTree(int pid = 0)
{
var departments = await _departmentServices.Query(d => d.IsDeleted == false);
var departmentTrees = (from child in departments
where child.IsDeleted == false
orderby child.Id
select new DepartmentTree
{
value = child.Id,
label = child.Name,
Pid = child.Pid,
order = child.OrderSort,
}).ToList();
DepartmentTree rootRoot = new DepartmentTree
{
value = 0,
Pid = 0,
label = "根节点"
};
departmentTrees = departmentTrees.OrderBy(d => d.order).ToList();
RecursionHelper.LoopToAppendChildren(departmentTrees, rootRoot, pid);
return Success(rootRoot, "获取成功");
}
[HttpPost]
public async Task<MessageModel<string>> Post([FromBody] Department request)
{
var data = new MessageModel<string>();
var id = await _departmentServices.Add(request);
data.success = id > 0;
if (data.success)
{
data.response = id.ObjToString();
data.msg = "添加成功";
}
return data;
}
[HttpPut]
public async Task<MessageModel<string>> Put([FromBody] Department request)
{
var data = new MessageModel<string>();
data.success = await _departmentServices.Update(request);
if (data.success)
{
data.msg = "更新成功";
data.response = request?.Id.ObjToString();
}
return data;
}
[HttpDelete]
public async Task<MessageModel<string>> Delete(int id)
{
var data = new MessageModel<string>();
var model = await _departmentServices.QueryById(id);
model.IsDeleted = true;
data.success = await _departmentServices.Update(model);
if (data.success)
{
data.msg = "删除成功";
data.response = model?.Id.ObjToString();
}
return data;
}
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel<string>> SaveData2Tsv()
{
var data = new MessageModel<string>() { success = true, msg = "" };
if (_env.IsDevelopment())
{
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
var rolesJson = JsonConvert.SerializeObject(await _departmentServices.Query(d => d.IsDeleted == false), microsoftDateFormatSettings);
FileHelper.WriteFile(Path.Combine(_env.WebRootPath, "BlogCore.Data.json", "Department_New.tsv"), rolesJson, Encoding.UTF8);
data.success = true;
data.msg = "生成成功!";
}
else
{
data.success = false;
data.msg = "当前不处于开发模式,代码生成不可用!";
}
return data;
}
}
}