AdminUserController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ExcelDataReader;
  8. using GxPress.Auth;
  9. using GxPress.Common.Exceptions;
  10. using GxPress.Common.Page;
  11. using GxPress.Entity;
  12. using GxPress.Repository.Interface;
  13. using GxPress.Repository.Interface.DepartmentUser;
  14. using GxPress.Request.User;
  15. using GxPress.Result.User;
  16. using GxPress.Service.Interface;
  17. using Microsoft.AspNetCore.Authorization;
  18. using Microsoft.AspNetCore.Mvc;
  19. using Microsoft.Extensions.Logging;
  20. using Microsoft.Extensions.Options;
  21. namespace GxPress.Api.AdminControllers
  22. {
  23. /// <summary>
  24. /// 用户
  25. /// </summary>
  26. [Route("/api/admin/user")]
  27. [ApiController]
  28. [Authorize]
  29. public class AdminUserController : ControllerBase
  30. {
  31. private readonly JwtOptions _jwtOptions;
  32. private readonly ILogger<AdminUserController> _logger;
  33. private readonly IUserRepository _userRepository;
  34. private readonly IDepartmentRepository _departmentRepository;
  35. private readonly IUserService _userService;
  36. public AdminUserController(IUserRepository userRepository, IOptions<JwtOptions> jwtOptions,
  37. ILogger<AdminUserController> logger, IDepartmentRepository departmentRepository, IUserService userService)
  38. {
  39. _userRepository = userRepository;
  40. _departmentRepository = departmentRepository;
  41. _jwtOptions = jwtOptions.Value;
  42. _logger = logger;
  43. _userService = userService;
  44. }
  45. /// <summary>
  46. /// 添加
  47. /// </summary>
  48. /// <param name="request"></param>
  49. /// <returns></returns>
  50. [HttpPost("add")]
  51. public async Task<User> Add([FromBody] User request)
  52. {
  53. if (!string.IsNullOrEmpty(request.Description) && request.Description.Length > 500)
  54. {
  55. throw new Exception("保存失败,字符数不能超出500");
  56. }
  57. request.Id = await _userRepository.InsertAsync(request);
  58. if (request.Id > 0)
  59. {
  60. //添加环信
  61. await _userRepository.CreateMiUserAsync(request);
  62. }
  63. return request;
  64. }
  65. /// <summary>
  66. /// 用户详情
  67. /// </summary>
  68. /// <param name="id"></param>
  69. /// <returns></returns>
  70. [HttpGet("{id}")]
  71. public async Task<UserInfoResult> GetDetail(int id)
  72. {
  73. return await _userService.GetUserInfoAsync(id);
  74. }
  75. /// <summary>
  76. /// 删除用户
  77. /// </summary>
  78. /// <param name="id"></param>
  79. /// <returns></returns>
  80. [HttpDelete("{id}")]
  81. public async Task<bool> Delete(int id)
  82. {
  83. return await _userRepository.DeleteAsync(id);
  84. }
  85. /// <summary>
  86. /// 删除用户
  87. /// </summary>
  88. /// <param name="userIds"></param>
  89. /// <returns></returns>
  90. [HttpDelete]
  91. public async Task<bool> Delete([FromBody] IEnumerable<int> userIds)
  92. {
  93. return await _userService.DeleteUsersAsync(userIds);
  94. }
  95. /// <summary>
  96. /// 更新用户信息
  97. /// </summary>
  98. /// <param name="id"></param>
  99. /// <param name="request"></param>
  100. /// <returns></returns>
  101. [HttpPut("{id}")]
  102. public async Task<bool> Update(int id, [FromBody] UserInfoRequest request)
  103. {
  104. if (!string.IsNullOrEmpty(request.Description) && request.Description.Length > 500)
  105. {
  106. throw new Exception("保存失败,字符数不能超出500");
  107. }
  108. return await _userService.UpdateAsync(id, request);
  109. }
  110. /// <summary>
  111. /// 分页列表
  112. /// </summary>
  113. /// <param name="request"></param>
  114. /// <returns></returns>
  115. [HttpPost("page")]
  116. public async Task<PagedList<UserResult>> GetPagedList([FromBody] UserPageSearchRequest request)
  117. {
  118. var result = await _userService.GetPagedListAsync(request);
  119. return result;
  120. }
  121. [HttpPost("list")]
  122. public async Task<PagedList<User>> GetListAsync([FromBody] UserPageSearchRequest request)
  123. {
  124. var result = await _userService.GetPageListAsync(request);
  125. foreach (var item in result.Items)
  126. {
  127. if (item.IsRenewal)
  128. item.Remark = "自动续费";
  129. else if (item.EndTime < DateTime.Now)
  130. item.Remark = "已过期";
  131. else
  132. item.Remark = "在费";
  133. }
  134. return result;
  135. }
  136. /// <summary>
  137. /// 分页列表
  138. /// </summary>
  139. /// <param name="request"></param>
  140. /// <returns></returns>
  141. [HttpPost("all")]
  142. public async Task<List<UserResult>> GetList([FromBody] ArticleSearchRequest request)
  143. {
  144. var result = await _userRepository.GetListAsync(request);
  145. return result;
  146. }
  147. /// <summary>
  148. /// 修改用户角色
  149. /// </summary>
  150. /// <param name="request"></param>
  151. /// <returns></returns>
  152. [HttpPost("actions/change_role")]
  153. public async Task<bool> ChangeRole([FromBody] UserChangeRoleRequest request)
  154. {
  155. return await _userRepository.ChangeRoleAsync(request.UserIds, request.RoleId);
  156. }
  157. /// <summary>
  158. /// 修改用户角色
  159. /// </summary>
  160. /// <param name="request"></param>
  161. /// <returns></returns>
  162. [HttpPost("actions/change_department")]
  163. public async Task<bool> ChangeDepartment([FromBody] UserChangeDepartmentRequest request)
  164. {
  165. return await _userRepository.ChangeDepartmentAsync(request.UserIds, request.DepartmentId);
  166. }
  167. /// <summary>
  168. /// 分析Excel数据
  169. /// </summary>
  170. /// <param name="request"></param>
  171. /// <returns></returns>
  172. [HttpPost("upload_user")]
  173. public async Task<bool> UserExcelUpload([FromBody] UserExcelUploadRequest request)
  174. {
  175. if (request.DepartmentId == 0)
  176. throw new BusinessException("请选择部门");
  177. var relativePath = request.ExcelUrl;
  178. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  179. //获取excel分析
  180. var stream = System.IO.File.Open("wwwroot/" + relativePath, FileMode.Open, FileAccess.Read);
  181. IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream);
  182. var result = reader.AsDataSet();
  183. var sheet = result.Tables["Sheet1"];
  184. var error = string.Empty;
  185. var listUser = new List<User>();
  186. for (int i = 1; i < sheet.Rows.Count; i++) //行
  187. {
  188. var user = new User();
  189. if (i == 1)
  190. continue;
  191. var name = sheet.Rows[i][1].ToString().Trim(); //姓名
  192. if (string.IsNullOrEmpty(name))
  193. error = "姓名必填";
  194. user.Name = name;
  195. var phone = sheet.Rows[i][2].ToString().Trim(); //手机号码
  196. if (phone.Length != 11)
  197. error = "手机号码有误";
  198. user.Phone = phone;
  199. var email = sheet.Rows[i][3].ToString().Trim(); //邮箱
  200. if (!string.IsNullOrWhiteSpace(email))
  201. {
  202. if (email.Contains("@"))
  203. email = email.Replace("@", "@");
  204. if (!GxPress.Common.Tools.VerifyHelper.IsEmailValid(email))
  205. error = "邮箱有误";
  206. }
  207. user.Email = email;
  208. user.DepartmentId = request.DepartmentId;
  209. var sex = sheet.Rows[i][4].ToString().Trim(); //性别
  210. user.Gender = sex == "男" ? "Male" : "Female";
  211. var city = sheet.Rows[i][5].ToString().Trim(); //城市
  212. user.City = city;
  213. var isLeader = sheet.Rows[i][6].ToString().Trim(); //是否部门负责人
  214. var entryDataTime = sheet.Rows[i][7].ToString().Trim(); //入职时间
  215. user.IsLeader = isLeader == "是" ? true : false;
  216. if (!string.IsNullOrEmpty(entryDataTime))
  217. user.EntryDataTime = DateTime.Parse(entryDataTime);
  218. if (!string.IsNullOrEmpty(error))
  219. break;
  220. listUser.Add(user);
  221. }
  222. //判断重复
  223. foreach (var user in listUser)
  224. if (listUser.Count(n => n.Email == user.Phone) > 1)
  225. throw new BusinessException("手机有重复请检查");
  226. if (!string.IsNullOrEmpty(error)) throw new BusinessException(error);
  227. //计算
  228. await _userRepository.UpdateUserExcelAsync(listUser);
  229. return true;
  230. }
  231. /// <summary>
  232. /// 筛选用户导出
  233. /// </summary>
  234. /// <param name="userIds"></param>
  235. /// <returns></returns>
  236. [HttpPost("excel")]
  237. public async Task<FileStreamResult> ExcelUser(List<int> userIds)
  238. {
  239. if (userIds.Count == 0)
  240. throw new BusinessException("请选择用户");
  241. var fileName = await _userService.ExcelUserAsync(userIds);
  242. return File(new FileStream(fileName, FileMode.Open), "application/octet-stream", "员工数据.xls");
  243. }
  244. /// <summary>
  245. /// vip管理
  246. /// </summary>
  247. /// <param name="request"></param>
  248. /// <returns></returns>
  249. [HttpPost("vip")]
  250. public async Task<PagedList<UserDetail>> GetUserVipDetail(UserSearchVipRequest request)
  251. {
  252. var result = await _userRepository.GetUserVipDetail(request);
  253. foreach (var item in result.Items)
  254. {
  255. if (item.IsRenewal)
  256. item.VipStatus = 1;
  257. if (item.EndTime < DateTime.Now)
  258. item.VipStatus = 3;
  259. else
  260. item.VipStatus = 2;
  261. }
  262. return result;
  263. }
  264. /// <summary>
  265. /// 修改用户黑名单
  266. /// </summary>
  267. /// <param name="request"></param>
  268. /// <returns></returns>
  269. [HttpPut("update-disable")]
  270. public async Task<bool> UpdateUserAsync(UserUpdateDisableRequest request)
  271. {
  272. return await _userRepository.UpdateDisableAsync(request);
  273. }
  274. }
  275. }