AdminUserController.cs 8.8 KB

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