UserController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Bowin.Common.ServiceToken.Permission;
  9. using Bowin.Common.WebModels;
  10. using YLShipBuildLandMap.Entity;
  11. using YLShipBuildLandMap.Entity.ViewModel;
  12. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  13. using YLShipBuildLandMap.Services.SystemSetting;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Newtonsoft.Json.Linq;
  18. using Bowin.Common.Data;
  19. using System.Data;
  20. using Bowin.Common.Office;
  21. namespace YLShipBuildLandMap.Web.Controllers.SystemSetting
  22. {
  23. [Route("api/systemsetting/[controller]/[action]")]
  24. [ApiController]
  25. [Authorize]
  26. public class UserController : ControllerBase
  27. {
  28. private IRoleService RoleService { get; set; }
  29. private IUserService UserService { get; set; }
  30. public UserController(IRoleService roleService, IUserService userService)
  31. {
  32. RoleService = roleService;
  33. UserService = userService;
  34. }
  35. [HttpPost]
  36. [FunctionCode("9903")]
  37. public ResultMessage GetUserList([FromBody] dynamic inputObj)
  38. {
  39. try
  40. {
  41. string userName = inputObj.userName;
  42. Guid? roleId = inputObj.RoleID;
  43. int? RecordStatus = inputObj.recordStatus;
  44. int? pageIndex = inputObj.pageIndex;
  45. int? pageSize = inputObj.pageSize;
  46. var userList = UserService.GetUserViewList(userName, roleId,RecordStatus,null, pageIndex, pageSize);
  47. return ResultMessage.Success(userList);
  48. }
  49. catch (Exception ex)
  50. {
  51. return ResultMessage.GetError(ex.Message);
  52. }
  53. }
  54. [HttpPost]
  55. [FunctionCode("9903")]
  56. public ResultMessage GetUser([FromBody] dynamic inputObj)
  57. {
  58. try
  59. {
  60. Guid userID = inputObj.userID;
  61. var user = UserService.GetUserView(userID);
  62. return ResultMessage.Success(user);
  63. }
  64. catch (Exception ex)
  65. {
  66. return ResultMessage.GetError(ex.Message);
  67. }
  68. }
  69. [HttpPost]
  70. [FunctionCode("990301")]
  71. public async Task<ResultMessage> Add([FromBody] JObject inputObj)
  72. {
  73. try
  74. {
  75. UserView user = inputObj["user"].ToObject<UserView>();
  76. UserView result = UserService.GetUserViewByName(user.LoginID, user.Name);
  77. if (result != null)
  78. {
  79. return ResultMessage.GetError("用户名或账号已存在");
  80. }
  81. await UserService.Save(user, LoginUser.Current.UserID);
  82. return ResultMessage.Success();
  83. }
  84. catch (Exception ex)
  85. {
  86. return ResultMessage.GetError(ex.Message);
  87. }
  88. }
  89. [HttpPost]
  90. [FunctionCode("990302")]
  91. public async Task<ResultMessage> Edit([FromBody] dynamic inputObj)
  92. {
  93. try
  94. {
  95. UserView user = inputObj["user"].ToObject<UserView>();
  96. if (!user.UserID.HasValue)
  97. {
  98. throw new Exception("实体ID不能为空。");
  99. }
  100. await UserService.Save(user, LoginUser.Current.UserID);
  101. return ResultMessage.Success();
  102. }
  103. catch (Exception ex)
  104. {
  105. return ResultMessage.GetError(ex.Message);
  106. }
  107. }
  108. [HttpPost]
  109. [FunctionCode("990303")]
  110. public async Task<ResultMessage> Delete([FromBody] dynamic inputObj)
  111. {
  112. try
  113. {
  114. List<Guid> userIDList = inputObj.userIDs.ToObject<List<Guid>>();
  115. await UserService.Delete(userIDList);
  116. return ResultMessage.Success();
  117. }
  118. catch (Exception ex)
  119. {
  120. return ResultMessage.GetError(ex.Message);
  121. }
  122. }
  123. [HttpGet]
  124. [FunctionCode("9903")]
  125. public ResultMessage GetUserListByUserName(string userName)
  126. {
  127. try
  128. {
  129. var userList = UserService.GetUserViewList(userName,null, null, null);
  130. var resultList = userList.rows.AsEnumerable().Select(x => new
  131. {
  132. loginId = x.LoginID,
  133. name = x.Name,
  134. id = x.UserID
  135. }).ToList();
  136. return ResultMessage.Success(resultList);
  137. }
  138. catch (Exception ex)
  139. {
  140. return ResultMessage.GetError(ex.Message);
  141. }
  142. }
  143. [HttpPost]
  144. public async Task<ResultMessage> UpdatePwd([FromBody] dynamic inputObj)
  145. {
  146. string newPassword = inputObj.newPassword;
  147. await UserService.UpdatePwd(LoginUser.Current.UserID, newPassword);
  148. return ResultMessage.Success();
  149. }
  150. [HttpGet]
  151. [FunctionCode("990304")]
  152. public async Task<ResultMessage> ResetPwd(Guid Id)
  153. {
  154. await UserService.ResetPwd(Id, LoginUser.Current.UserID);
  155. return ResultMessage.Success();
  156. }
  157. [HttpPost]
  158. public ResultMessage GetUserRoleList([FromBody] dynamic inputObj)
  159. {
  160. try
  161. {
  162. var list= UserService.GetUserRoleList();
  163. return ResultMessage.Success(list);
  164. }
  165. catch (Exception ex)
  166. {
  167. throw ex;
  168. }
  169. }
  170. [HttpPost]
  171. [FunctionCode("990305")]
  172. public ResultMessage ExportPort([FromBody] dynamic inputObj)
  173. {
  174. string userName = inputObj.userName;
  175. Guid? roleId = inputObj.RoleID;
  176. int? RecordStatus = inputObj.recordStatus;
  177. int? pageIndex = inputObj.pageIndex;
  178. int? pageSize = inputObj.pageSize;
  179. string userId = inputObj.userId;
  180. List<Guid> useridList = !string.IsNullOrEmpty(userId) ? userId.Split(",").Where(s => !string.IsNullOrEmpty(s)).Select(s => Guid.Parse(s)).ToList() : null;
  181. var dataList = (UserService.GetUserViewList(userName, roleId, RecordStatus, useridList, pageIndex, pageSize).rows.
  182. Select((r, i) => new
  183. {
  184. index = (i + 1),
  185. r.Name,
  186. r.LoginID,
  187. r.CmWorkLinkAccountPsid,
  188. r.RoleNames,
  189. RecordStatus = r.RecordStatus == 1 ? "启用" : "停用"
  190. })).ToTable();
  191. dataList.Columns["index"].ColumnName = "序号";
  192. dataList.Columns["Name"].ColumnName = "用户名";
  193. dataList.Columns["LoginID"].ColumnName = "账号";
  194. dataList.Columns["CmWorkLinkAccountPsid"].ColumnName = "集团主账号";
  195. dataList.Columns["RoleNames"].ColumnName = "用户角色";
  196. dataList.Columns["RecordStatus"].ColumnName = "状态";
  197. List<string> headerList = new List<string>();
  198. foreach (DataColumn column in dataList.Columns)
  199. {
  200. headerList.Add(column.ColumnName);
  201. }
  202. AsposeExcelHelper excel = new AsposeExcelHelper();
  203. var fileName = excel.ExportToFile(dataList, headerList.ToArray(), Configuration.Current.AppSettings.TemplatePhysicalPath);
  204. return ResultMessage.Success(fileName);
  205. }
  206. }
  207. }