AdminUserController.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. [HttpPost("list")]
  121. public async Task<PagedList<User>> GetListAsync([FromBody] UserPageSearchRequest request)
  122. {
  123. var result = await _userService.GetPageListAsync(request);
  124. foreach (var item in result.Items)
  125. {
  126. if (item.IsRenewal)
  127. item.Remark = "自动续费";
  128. else if (item.EndTime < DateTime.Now)
  129. item.Remark = "已过期";
  130. else
  131. item.Remark = "在费";
  132. }
  133. return result;
  134. }
  135. /// <summary>
  136. /// 分页列表
  137. /// </summary>
  138. /// <param name="request"></param>
  139. /// <returns></returns>
  140. [HttpPost("all")]
  141. public async Task<List<UserResult>> GetList([FromBody] ArticleSearchRequest request)
  142. {
  143. var result = await _userRepository.GetListAsync(request);
  144. return result;
  145. }
  146. /// <summary>
  147. /// 修改用户角色
  148. /// </summary>
  149. /// <param name="request"></param>
  150. /// <returns></returns>
  151. [HttpPost("actions/change_role")]
  152. public async Task<bool> ChangeRole([FromBody] UserChangeRoleRequest request)
  153. {
  154. return await _userRepository.ChangeRoleAsync(request.UserIds, request.RoleId);
  155. }
  156. /// <summary>
  157. /// 修改用户角色
  158. /// </summary>
  159. /// <param name="request"></param>
  160. /// <returns></returns>
  161. [HttpPost("actions/change_department")]
  162. public async Task<bool> ChangeDepartment([FromBody] UserChangeDepartmentRequest request)
  163. {
  164. return await _userRepository.ChangeDepartmentAsync(request.UserIds, request.DepartmentId);
  165. }
  166. /// <summary>
  167. /// 分析Excel数据
  168. /// </summary>
  169. /// <param name="request"></param>
  170. /// <returns></returns>
  171. [HttpPost("upload_user")]
  172. public async Task<bool> UserExcelUpload([FromBody] UserExcelUploadRequest request)
  173. {
  174. if (request.DepartmentId == 0)
  175. throw new BusinessException("请选择部门");
  176. var relativePath = request.ExcelUrl;
  177. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  178. //获取excel分析
  179. var stream = System.IO.File.Open("wwwroot/" + relativePath, FileMode.Open, FileAccess.Read);
  180. IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream);
  181. var result = reader.AsDataSet();
  182. var sheet = result.Tables["Sheet1"];
  183. var error = string.Empty;
  184. var listUser = new List<User>();
  185. for (int i = 1; i < sheet.Rows.Count; i++) //行
  186. {
  187. var user = new User();
  188. if (i == 1)
  189. continue;
  190. var name = sheet.Rows[i][1].ToString().Trim(); //姓名
  191. if (string.IsNullOrEmpty(name))
  192. error = "姓名必填";
  193. user.Name = name;
  194. var phone = sheet.Rows[i][2].ToString().Trim(); //手机号码
  195. if (phone.Length != 11)
  196. error = "手机号码有误";
  197. user.Phone = phone;
  198. var email = sheet.Rows[i][3].ToString().Trim(); //邮箱
  199. if (!string.IsNullOrWhiteSpace(email))
  200. {
  201. if (email.Contains("@"))
  202. email = email.Replace("@", "@");
  203. if (!GxPress.Common.Tools.VerifyHelper.IsEmailValid(email))
  204. error = "邮箱有误";
  205. }
  206. user.Email = email;
  207. user.DepartmentId = request.DepartmentId;
  208. var sex = sheet.Rows[i][4].ToString().Trim(); //性别
  209. user.Gender = sex == "男" ? "Male" : "Female";
  210. var city = sheet.Rows[i][5].ToString().Trim(); //城市
  211. user.City = city;
  212. var isLeader = sheet.Rows[i][6].ToString().Trim(); //是否部门负责人
  213. var entryDataTime = sheet.Rows[i][7].ToString().Trim(); //入职时间
  214. user.IsLeader = isLeader == "是" ? true : false;
  215. if (!string.IsNullOrEmpty(entryDataTime))
  216. user.EntryDataTime = DateTime.Parse(entryDataTime);
  217. if (!string.IsNullOrEmpty(error))
  218. break;
  219. listUser.Add(user);
  220. }
  221. //判断重复
  222. foreach (var user in listUser)
  223. if (listUser.Count(n => n.Email == user.Phone) > 1)
  224. throw new BusinessException("手机有重复请检查");
  225. if (!string.IsNullOrEmpty(error)) throw new BusinessException(error);
  226. //计算
  227. await _userRepository.UpdateUserExcelAsync(listUser);
  228. return true;
  229. }
  230. /// <summary>
  231. /// 筛选用户导出
  232. /// </summary>
  233. /// <param name="userIds"></param>
  234. /// <returns></returns>
  235. [HttpPost("excel")]
  236. public async Task<FileStreamResult> ExcelUser(List<int> userIds)
  237. {
  238. if (userIds.Count == 0)
  239. throw new BusinessException("请选择用户");
  240. var fileName = await _userService.ExcelUserAsync(userIds);
  241. return File(new FileStream(fileName, FileMode.Open), "application/octet-stream", "员工数据.xls");
  242. }
  243. }
  244. }