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

RoleInfoController.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IRepositories;
  4. using System.Linq;
  5. using System.Model;
  6. using System.Security.Claims;
  7. using System.Threading.Tasks;
  8. using System.Common;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. using SqlSugar;
  12. using TVShoppingCallCenter_ZLJ.Models.Inputs;
  13. namespace TVShoppingCallCenter_ZLJ.Controllers.System
  14. {
  15. [Authorize]
  16. [Produces("application/json")]
  17. [Route("api/[controller]")]
  18. public class RoleInfoController : BaseController
  19. {
  20. private readonly ISys_RoleInfoRepository _sys_role_infoRepository;
  21. public RoleInfoController(ISys_RoleInfoRepository sys_role_infoRepository)
  22. {
  23. _sys_role_infoRepository = sys_role_infoRepository;
  24. }
  25. /// <summary>
  26. /// 获取角色分页
  27. /// </summary>
  28. /// <param name="keyword"></param>
  29. /// <param name="pageindex"></param>
  30. /// <param name="pagesize"></param>
  31. /// <returns></returns>
  32. [HttpGet("getlistbypage")]
  33. public async Task<IActionResult> GetListsByPageAsync(string keyword, int pageindex = 1, int pagesize = 20)
  34. {
  35. List<IConditionalModel> conModels = new List<IConditionalModel>();
  36. #region 条件筛选
  37. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  38. if (!string.IsNullOrEmpty(keyword))
  39. {
  40. conModels.Add(new ConditionalModel() { FieldName = "F_RoleCode", ConditionalType = ConditionalType.Like, FieldValue = keyword });
  41. }
  42. #endregion
  43. int recordCount = 0;
  44. var list = await _sys_role_infoRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
  45. return Success("获取数据成功", list);
  46. }
  47. /// <summary>
  48. /// 获取角色下拉列表
  49. /// </summary>
  50. /// <returns></returns>
  51. [HttpGet("getlistdrop")]
  52. public async Task<IActionResult> GetListDropAsync()
  53. {
  54. var list = await _sys_role_infoRepository.GetListALL(x => x.F_State == (int)EnumDelState.Enabled, o => o.F_CreateOn, OrderByType.Desc);
  55. var result = list.Select(x => new {
  56. x.F_RoleCode,
  57. x.F_RoleName,
  58. x.F_RoleId
  59. });
  60. return Success("获取全部数据成功", result);
  61. }
  62. /// <summary>
  63. /// 获取角色
  64. /// </summary>
  65. /// <param name="id">角色id</param>
  66. /// <returns></returns>
  67. [HttpGet("getdetails")]
  68. public async Task<IActionResult> GetDetailsAsync(int id)
  69. {
  70. if (id <= 0)
  71. return Error("参数错误");
  72. var model = await _sys_role_infoRepository.GetSingle(x => x.F_RoleId == id && x.F_State == (int)EnumDelState.Enabled);
  73. if (model == null)
  74. {
  75. return Error("获取失败");
  76. }
  77. return Success("获取成功!", model);
  78. }
  79. /// <summary>
  80. /// 添加角色
  81. /// </summary>
  82. /// <param name="input"></param>
  83. /// <returns></returns>
  84. [HttpPost("add")]
  85. public async Task<IActionResult> AddAsync(RoleInfoInput input)
  86. {
  87. if (string.IsNullOrEmpty(input.role_name))
  88. return Error("请输入角色名称");
  89. if (string.IsNullOrEmpty(input.role_Code))
  90. return Error("请输入角色代码");
  91. var model = new T_Sys_RoleInfo();
  92. model.F_RoleName = input.role_name;
  93. model.F_Remark = input.role_remark;
  94. model.F_RoleCode = input.role_Code;
  95. model.F_State = input.state;
  96. model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  97. model.F_CreateOn = DateTime.Now;
  98. if (await _sys_role_infoRepository.GetCount(x => x.F_RoleCode == input.role_Code && x.F_State != 2) > 0)
  99. {
  100. return Error("添加失败,存在相同的角色代码");
  101. }
  102. var res = await _sys_role_infoRepository.Add(model);
  103. if (res>0)
  104. {
  105. return Success("添加成功");
  106. }
  107. else
  108. {
  109. return Error("添加失败");
  110. }
  111. }
  112. /// <summary>
  113. /// 修改角色信息
  114. /// </summary>
  115. [HttpPost("update")]
  116. public async Task<IActionResult> UpdateAsync(RoleInfoInput input)
  117. {
  118. if (string.IsNullOrEmpty(input.role_name))
  119. return Error("请输入角色名称");
  120. if (string.IsNullOrEmpty(input.role_Code))
  121. return Error("请输入角色代码");
  122. var model = await _sys_role_infoRepository.GetSingle(x => x.F_RoleId == input.role_id && x.F_State == (int)EnumDelState.Enabled);
  123. if (model == null)
  124. return Error("操作失败");
  125. model.F_RoleName = input.role_name;
  126. model.F_Remark = input.role_remark;
  127. model.F_RoleCode = input.role_Code;
  128. model.F_LastModifyOn = DateTime.Now.ToLocalTime();
  129. model.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  130. if (await _sys_role_infoRepository.GetCount(x => x.F_RoleCode == input.role_Code && x.F_RoleId !=input.role_id && x.F_State != 2) > 0)
  131. {
  132. return Error("修改失败,存在相同的角色代码");
  133. }
  134. bool b = await _sys_role_infoRepository.Update(model);
  135. if (b)
  136. return Success("修改成功");
  137. return Error("修改失败");
  138. }
  139. /// <summary>
  140. /// 删除角色信息 by ids
  141. /// </summary>
  142. /// <param name="ids"></param>
  143. /// <returns></returns>
  144. [HttpPost("delete")]
  145. public async Task<IActionResult> Remove(int[] ids)
  146. {
  147. var res = 0;
  148. if (ids != null && ids.Length > 0)
  149. {
  150. foreach (var item in ids)
  151. {
  152. var ml = await _sys_role_infoRepository.GetSingle(x => x.F_RoleId == item);
  153. ml.F_State = (int)EnumDelState.Delete;
  154. ml.F_DeleteOn = DateTime.Now.ToLocalTime();
  155. ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  156. if (_sys_role_infoRepository.Update(ml).Result)
  157. res += 1;
  158. }
  159. if (res == ids.Length)
  160. return Success("删除成功");
  161. else if (res > 0 && res < ids.Length)
  162. return Error("部分删除失败,请查看后重新操作");
  163. else
  164. return Error("删除失败,请查看后重新操作");
  165. }
  166. else
  167. return Error("请选择要删除的记录");
  168. }
  169. }
  170. }