足力健后端,使用.netcore版本,合并1个项目使用

UserAccountController.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IRepositories;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Threading.Tasks;
  7. using System.Common;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.AspNetCore.Mvc;
  10. using SqlSugar;
  11. using System.Model;
  12. using TVShoppingCallCenter_ZLJ.Models.Inputs;
  13. using Microsoft.Extensions.Configuration;
  14. namespace TVShoppingCallCenter_ZLJ.Controllers
  15. {
  16. [Authorize]
  17. [Produces("application/json")]
  18. [Route("api/[controller]")]
  19. public class UserAccountController : BaseController
  20. {
  21. private readonly ISys_UserAccountRepository _sys_useraccountRepository;
  22. private readonly ISys_RoleInfoRepository _sys_role_infoRepository;
  23. private readonly IConfiguration _configuration;
  24. private readonly ISys_DepartmentRepository _sys_departmentRepository;
  25. public UserAccountController(IConfiguration configuration, ISys_UserAccountRepository sys_useraccountRepository, ISys_RoleInfoRepository sys_role_infoRepository,
  26. ISys_DepartmentRepository sys_departmentRepository )
  27. {
  28. _configuration = configuration;
  29. _sys_useraccountRepository = sys_useraccountRepository;
  30. _sys_role_infoRepository = sys_role_infoRepository;
  31. _sys_departmentRepository = sys_departmentRepository;
  32. }
  33. /// <summary>
  34. /// 用户信息列表 by page
  35. /// </summary>
  36. /// <param name="pageindex">当前页</param>
  37. /// <param name="pagesize">每页条数</param>
  38. /// <returns></returns>
  39. [HttpGet("getlistbypage")]
  40. public async Task<IActionResult> GetListByPageAsync(string keyword, string deptid, string roleid, int pageindex = 1, int pagesize = 20)
  41. {
  42. List<IConditionalModel> conModels = new List<IConditionalModel>();
  43. #region 筛选
  44. conModels.Add(new ConditionalModel() { FieldName = "F_DeleteFlag", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumUserCountState.Enabled).ToString() });
  45. if (!string.IsNullOrEmpty(deptid))
  46. conModels.Add(new ConditionalModel() { FieldName = "F_DeptId", ConditionalType = ConditionalType.Equal, FieldValue = deptid });
  47. if (!string.IsNullOrEmpty(roleid))
  48. conModels.Add(new ConditionalModel() { FieldName = "F_RoleId", ConditionalType = ConditionalType.Equal, FieldValue = roleid });
  49. if (!string.IsNullOrEmpty(keyword))
  50. conModels.Add(new ConditionalCollections()
  51. {
  52. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  53. {
  54. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_UserCode", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
  55. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_UserName", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
  56. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or, new ConditionalModel() { FieldName = "F_Mobile", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
  57. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or, new ConditionalModel() { FieldName = "F_Telephone", ConditionalType = ConditionalType.Like, FieldValue = keyword })
  58. }
  59. });
  60. #endregion
  61. int recordCount = 0;
  62. var list = await _sys_useraccountRepository.GetListViewByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
  63. return Success("获取成功", list);
  64. }
  65. [HttpGet("getlistdrop")]
  66. public async Task<IActionResult> GetListDropAsync(string deptid, string roleid, string rolecode)
  67. {
  68. List<IConditionalModel> conModels = new List<IConditionalModel>();
  69. #region 筛选
  70. conModels.Add(new ConditionalModel() { FieldName = "F_DeleteFlag", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumUserCountState.Enabled).ToString() });
  71. if (!string.IsNullOrEmpty(deptid))
  72. conModels.Add(new ConditionalModel() { FieldName = "F_DeptId", ConditionalType = ConditionalType.Equal, FieldValue = deptid });
  73. if (!string.IsNullOrEmpty(roleid))
  74. conModels.Add(new ConditionalModel() { FieldName = "F_RoleId", ConditionalType = ConditionalType.Equal, FieldValue = roleid });
  75. if (!string.IsNullOrEmpty(rolecode))
  76. {
  77. var rolemodel = await _sys_role_infoRepository.GetSingle(x => x.F_RoleCode == rolecode && x.F_State == (int)EnumDelState.Enabled);
  78. if (rolemodel != null)
  79. conModels.Add(new ConditionalModel() { FieldName = "F_RoleId", ConditionalType = ConditionalType.Equal, FieldValue = rolemodel.F_RoleId.ToString() });
  80. else
  81. conModels.Add(new ConditionalModel() { FieldName = "F_RoleId", ConditionalType = ConditionalType.Equal, FieldValue = "-1" });
  82. }
  83. #endregion
  84. var list = await _sys_useraccountRepository.GetListALL(conModels, "F_CreateOn desc");
  85. var result = list.Select(x => new
  86. {
  87. x.F_UserCode,
  88. x.F_UserName,
  89. x.F_DeptId,
  90. x.F_RoleId,
  91. x.F_UserId
  92. });
  93. return Success("获取成功", result);
  94. }
  95. /// <summary>
  96. /// 获取用户详情 by id
  97. /// </summary>
  98. /// <param name="id">设备id</param>
  99. /// <returns></returns>
  100. [HttpGet("getdetails")]
  101. public async Task<IActionResult> GetDetailsAsync(int id)
  102. {
  103. if (id <= 0)
  104. return Error("参数错误");
  105. var model = await _sys_useraccountRepository.GetSingle(x => x.F_UserId == id && x.F_DeleteFlag == (int)EnumUserCountState.Enabled);
  106. if (model != null)
  107. {
  108. return Success("获取成功!", GetDeptName(model));
  109. }
  110. return Error("获取失败");
  111. }
  112. private T_Sys_UserAccount GetDeptName(T_Sys_UserAccount model)
  113. {
  114. if (model.F_DeptId>0)
  115. {
  116. var dept = _sys_departmentRepository.GetSingle(x => x.F_DeptId == model.F_DeptId ).Result;
  117. if (model != null)
  118. model.F_Dept = dept.F_DeptName;
  119. }
  120. return model;
  121. }
  122. /// <summary>
  123. /// 添加用户信息
  124. /// </summary>
  125. /// <param name="input">用户信息参数</param>
  126. /// <returns></returns>
  127. [HttpPost("add")]
  128. public async Task<IActionResult> Add(UserAccountInput input)
  129. {
  130. #region 验证判断
  131. if (string.IsNullOrEmpty(input.usercode))
  132. return Error("账号不能为空");
  133. if (string.IsNullOrEmpty(input.password))
  134. return Error("密码不能为空");
  135. //if (string.IsNullOrEmpty(input.mobile))
  136. // return Error("手机号码不能为空");
  137. if (await _sys_useraccountRepository.GetCount(p => p.F_UserCode == input.usercode && p.F_DeleteFlag == (int)EnumUserCountState.Enabled) > 0)
  138. {
  139. return Error("账号已经存在");
  140. }
  141. if (await _sys_useraccountRepository.GetCount(p =>( p.F_Mobile == input.mobile||p.F_Telephone== input.mobile) && p.F_DeleteFlag == (int)EnumUserCountState.Enabled) > 0)
  142. {
  143. return Error("手机号已经存在");
  144. }
  145. #endregion
  146. var model = new T_Sys_UserAccount();
  147. model.F_UserCode = input.usercode;
  148. model.F_UserName = input.username;
  149. model.F_Password = input.password;
  150. model.F_SexFlag = input.sexflag;
  151. model.F_DeptId = input.deptid;
  152. model.F_ExtensionNumber = input.extensionnumber;
  153. model.F_RoleId = input.roleid;
  154. model.F_SeartGroupID = input.seartgroupiD;
  155. model.F_SeatFlag = input.seatflag;
  156. model.F_SeatLevel = input.seatlevel;
  157. model.F_SeatRight = input.seatright;
  158. model.F_RemindFlag = input.remindflag;
  159. model.F_Remark = input.remark;
  160. model.F_HeadImg = input.headimg;
  161. model.F_WXNo = input.wxno;
  162. model.F_Telephone = input.telephone;
  163. model.F_Mobile = input.mobile;
  164. model.F_HomePhone = input.remark;
  165. model.F_WxOpenId = "";
  166. model.F_DutyId = input.dutyid;
  167. model.F_DeptTeamId = input.deptteamid;
  168. model.F_EntryDate = input.entrydate;
  169. model.F_FullDate = input.fulldate;
  170. model.F_TermDate = input.termdate;
  171. model.F_DeleteFlag = (int)EnumUserCountState.Enabled;
  172. model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  173. model.F_CreateOn = DateTime.Now;
  174. var b = await _sys_useraccountRepository.Add(model);
  175. if (b > 0)
  176. return Success("添加成功");
  177. else
  178. return Error("添加失败");
  179. }
  180. /// <summary>
  181. /// 修改用户信息
  182. /// </summary>
  183. /// <param name="input"></param>
  184. /// <returns></returns>
  185. [HttpPost("update")]
  186. public async Task<IActionResult> Update(UserAccountInput input)
  187. {
  188. #region 验证判断
  189. if (string.IsNullOrEmpty(input.usercode))
  190. return Error("账号不能为空");
  191. if (await _sys_useraccountRepository.GetCount(p => p.F_UserCode == input.usercode && p.F_UserId != input.userid && p.F_DeleteFlag == (int)EnumUserCountState.Enabled) > 0)
  192. {
  193. return Error("账号已经存在");
  194. }
  195. #endregion
  196. if (await _sys_useraccountRepository.GetCount(p =>p.F_UserId!= input.userid && (p.F_Mobile == input.mobile || p.F_Telephone == input.mobile) && p.F_DeleteFlag == (int)EnumUserCountState.Enabled) > 0)
  197. {
  198. return Error("手机号已经存在");
  199. }
  200. var model = new T_Sys_UserAccount();
  201. model = await _sys_useraccountRepository.GetSingle(p => p.F_UserId == input.userid && p.F_DeleteFlag == (int)EnumUserCountState.Enabled);
  202. if (model == null)
  203. return Error("获取信息失败");
  204. model.F_UserCode = input.usercode;
  205. model.F_UserName = input.username;
  206. model.F_SexFlag = input.sexflag;
  207. model.F_DeptId = input.deptid;
  208. model.F_ExtensionNumber = input.extensionnumber;
  209. model.F_RoleId = input.roleid;
  210. model.F_SeartGroupID = input.seartgroupiD;
  211. model.F_SeatFlag = input.seatflag;
  212. model.F_SeatLevel = input.seatlevel;
  213. model.F_SeatRight = input.seatright;
  214. model.F_RemindFlag = input.remindflag;
  215. model.F_Remark = input.remark;
  216. model.F_HeadImg = input.headimg;
  217. model.F_WXNo = input.wxno;
  218. model.F_Telephone = input.telephone;
  219. model.F_Mobile = input.mobile;
  220. model.F_HomePhone = input.homephone;
  221. model.F_DutyId = input.dutyid;
  222. model.F_DeptTeamId = input.deptteamid;
  223. model.F_EntryDate = input.entrydate;
  224. model.F_FullDate = input.fulldate;
  225. model.F_TermDate = input.termdate;
  226. model.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  227. model.F_LastModifyOn = DateTime.Now;
  228. bool b = await _sys_useraccountRepository.Update(model);
  229. if (b)
  230. return Success("保存成功");
  231. else
  232. return Error("保存失败");
  233. }
  234. /// <summary>
  235. /// 逻辑删除用户
  236. /// </summary>
  237. /// <param name="ids"></param>
  238. /// <returns></returns>
  239. [HttpPost("delete")]
  240. public async Task<IActionResult> Delete(int[] ids)
  241. {
  242. string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  243. //使用逻辑删除
  244. //物理删除的数据无法恢复
  245. if (ids != null && ids.Length > 0)
  246. {
  247. foreach (var item in ids)
  248. {
  249. var eq = await _sys_useraccountRepository.GetSingle(p => p.F_UserId == item);
  250. if (eq != null)
  251. {
  252. eq.F_DeleteFlag = (int)EnumUserCountState.Delete;
  253. eq.F_DeleteBy = usercode;
  254. eq.F_DeleteOn = DateTime.Now.ToLocalTime();
  255. bool bl = await _sys_useraccountRepository.Update(eq);
  256. }
  257. }
  258. return Success("删除成功");
  259. }
  260. else
  261. return Error("请选择要删除的记录");
  262. }
  263. /// <summary>
  264. /// 获取当前用户信息
  265. /// </summary>
  266. /// <returns></returns>
  267. [HttpGet("getnowuser")]
  268. public async Task<IActionResult> GetNowUser()
  269. {
  270. string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  271. var userModel = await _sys_useraccountRepository.GetSingle(u => u.F_UserCode == nowusercode && u.F_DeleteFlag == (int)EnumUserCountState.Enabled);
  272. if (userModel == null)
  273. return Error("获取失败");
  274. userModel.F_HeadImg = _configuration["appSettings:ApiHost"].ToString() + userModel.F_HeadImg;
  275. return Success("获取成功", userModel);
  276. }
  277. /// <summary>
  278. /// 修改用户密码
  279. /// </summary>
  280. /// <param name="input"></param>
  281. /// <returns></returns>
  282. [HttpPost("updatepassword")]
  283. public async Task<IActionResult> UpdatePassword(string usercode, string password, string surepassword)
  284. {
  285. #region 验证判断
  286. if (string.IsNullOrEmpty(usercode))
  287. return Error("请选择账号");
  288. if (string.IsNullOrEmpty(password))
  289. return Error("密码不能为空");
  290. if (string.IsNullOrEmpty(surepassword))
  291. return Error("确认密码不能为空");
  292. if (password != surepassword)
  293. return Error("两次输入的密码不一致");
  294. #endregion
  295. var model = await _sys_useraccountRepository.GetSingle(p => p.F_UserCode == usercode && p.F_DeleteFlag == (int)EnumUserCountState.Enabled);
  296. if (model == null)
  297. return Error("操作失败");
  298. model.F_Password = password;
  299. bool b = await _sys_useraccountRepository.Update(model);
  300. if (b)
  301. return Success("保存成功");
  302. return Error("保存失败");
  303. }
  304. /// <summary>
  305. /// 更新头像
  306. /// </summary>
  307. /// <param name="input"></param>
  308. /// <returns></returns>
  309. [HttpPost("updateavatar")]
  310. public async Task<IActionResult> UpdateAvatar(string usercode, string headimg)
  311. {
  312. #region 验证判断
  313. if (string.IsNullOrEmpty(usercode))
  314. return Error("请选择账号");
  315. if (string.IsNullOrEmpty(headimg))
  316. return Error("头像不能为空");
  317. #endregion
  318. T_Sys_UserAccount model = await _sys_useraccountRepository.GetSingle(p => p.F_UserCode == usercode && p.F_DeleteFlag == (int)EnumUserCountState.Enabled);
  319. if (model == null)
  320. return Error("操作失败");
  321. model.F_HeadImg = headimg;
  322. List<T_Sys_UserAccount> list = new List<T_Sys_UserAccount>();
  323. list.Add(model);
  324. bool b = await _sys_useraccountRepository.UpdateListToColumns(list, s => new { s.F_HeadImg });
  325. if (b)
  326. return Success("保存成功");
  327. return Error("保存失败");
  328. }
  329. /// <summary>
  330. /// 修改我的密码
  331. /// </summary>
  332. /// <param name="input"></param>
  333. /// <returns></returns>
  334. [HttpPost("updatemypassword")]
  335. public async Task<IActionResult> UpdateMyPassword(string oldpassword, string password, string surepassword)
  336. {
  337. #region 验证判断
  338. if (string.IsNullOrEmpty(oldpassword))
  339. return Error("原密码不能为空");
  340. if (string.IsNullOrEmpty(surepassword))
  341. return Error("确认密码不能为空");
  342. if (password != surepassword)
  343. return Error("两次输入的密码不一致");
  344. #endregion
  345. var usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  346. var model = await _sys_useraccountRepository.GetSingle(p => p.F_UserCode == usercode && p.F_Password == oldpassword && p.F_DeleteFlag == (int)EnumUserCountState.Enabled);
  347. if (model == null)
  348. return Error("原密码输入错误");
  349. model.F_Password = password;
  350. bool b = await _sys_useraccountRepository.Update(model);
  351. if (b)
  352. return Success("更新成功");
  353. return Error("更新失败");
  354. }
  355. [HttpGet("getseatlist")]
  356. public async Task<IActionResult> GetSeatList(string usercode, string rolecode, string deptid = "")
  357. {
  358. List<IConditionalModel> conModels = new List<IConditionalModel>();
  359. if (!string.IsNullOrEmpty(rolecode))
  360. {
  361. T_Sys_RoleInfo rolemodelList = await _sys_role_infoRepository.GetFirst(x => x.F_RoleCode == rolecode && x.F_State == (int)EnumDelState.Enabled);
  362. conModels.Add(new ConditionalModel() { FieldName = "F_SeatFlag", ConditionalType = ConditionalType.Equal, FieldValue = "1" });
  363. }
  364. #region 筛选
  365. conModels.Add(new ConditionalModel() { FieldName = "F_DeleteFlag", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumUserCountState.Enabled).ToString() });
  366. conModels.Add(new ConditionalModel() { FieldName = "F_SeatFlag", ConditionalType = ConditionalType.Equal, FieldValue = "1" });
  367. if (!string.IsNullOrEmpty(deptid))
  368. conModels.Add(new ConditionalModel() { FieldName = "F_DeptId", ConditionalType = ConditionalType.Equal, FieldValue = deptid });
  369. if (!string.IsNullOrEmpty(usercode))
  370. conModels.Add(new ConditionalModel() { FieldName = "F_UserCode", ConditionalType = ConditionalType.Equal, FieldValue = usercode });
  371. #endregion
  372. var list = await _sys_useraccountRepository.GetListALL(conModels, "F_CreateOn desc");
  373. var result = list.Select(x => new
  374. {
  375. x.F_UserCode,
  376. x.F_UserName,
  377. x.F_DeptId,
  378. x.F_RoleId,
  379. x.F_UserId,
  380. x.F_SeatRight,
  381. x.F_SeartGroupID,
  382. x.F_ExtensionNumber
  383. });
  384. return Success("获取成功", result);
  385. }
  386. }
  387. }