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

PositionController.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.IRepositories;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Model;
  8. using System.Security.Claims;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.AspNetCore.Mvc;
  12. using SqlSugar;
  13. namespace TVShoppingCallCenter_ZLJ.Controllers.System
  14. {
  15. [Authorize]
  16. [Produces("application/json")]
  17. [Route("api/[controller]")]
  18. public class PositionController : BaseController
  19. {
  20. private readonly ISys_PositionRepository _sys_positionRepository;
  21. public PositionController(ISys_PositionRepository sys_positionRepository)
  22. {
  23. _sys_positionRepository = sys_positionRepository;
  24. }
  25. /// <summary>
  26. /// 获取列表
  27. /// </summary>
  28. /// <param name="filter"></param>
  29. /// <returns></returns>
  30. [Authorize]
  31. [HttpGet("getlistbypage")]
  32. public async Task<IActionResult> GetListsByPage(string key, int pageindex = 1, int pagesize = 20)
  33. {
  34. List<IConditionalModel> conModels = new List<IConditionalModel>();
  35. #region 条件筛选
  36. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  37. if (!string.IsNullOrEmpty(key))
  38. {
  39. conModels.Add(new ConditionalCollections()
  40. {
  41. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  42. {
  43. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = key }),
  44. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Des", ConditionalType = ConditionalType.Like, FieldValue = key })
  45. }
  46. });
  47. }
  48. #endregion
  49. int recordCount = 0;
  50. var list = await _sys_positionRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
  51. return Success("成功", list);
  52. }
  53. [Authorize]
  54. [HttpGet("getlistdrop")]
  55. public async Task<IActionResult> GetListDrop(string key)
  56. {
  57. List<IConditionalModel> conModels = new List<IConditionalModel>();
  58. #region 条件筛选
  59. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  60. if (!string.IsNullOrEmpty(key))
  61. {
  62. conModels.Add(new ConditionalCollections()
  63. {
  64. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  65. {
  66. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = key }),
  67. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Des", ConditionalType = ConditionalType.Like, FieldValue = key })
  68. }
  69. });
  70. }
  71. #endregion
  72. var list = await _sys_positionRepository.GetListALL(conModels, "F_CreateOn desc");
  73. var result = list.Select(x => new {
  74. x.F_Id,
  75. x.F_Name,
  76. x.F_Des
  77. });
  78. return Success("获取全部数据成功", result);
  79. }
  80. /// <summary>
  81. /// 获取实体
  82. /// </summary>
  83. [Authorize]
  84. [HttpGet("getdetails")]
  85. public async Task<IActionResult> GetDetails(int id)
  86. {
  87. if (id > 0)
  88. {
  89. var dModel = await _sys_positionRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  90. if (dModel != null)
  91. {
  92. return Success("获取成功", dModel);
  93. }
  94. else
  95. {
  96. return Error("获取失败");
  97. }
  98. }
  99. else
  100. {
  101. return Error("获取参数失败");
  102. }
  103. }
  104. /// <summary>
  105. /// 添加
  106. /// </summary>
  107. [Authorize]
  108. [HttpPost("add")]
  109. public async Task<IActionResult> Add(string name,string des)
  110. {
  111. #region 验证
  112. if (string.IsNullOrWhiteSpace(name))
  113. return Error("职位名称不能为空");
  114. if (await GetExistByNameAsync(name, 0))
  115. return Error("职位名称不能重复,请重新输入!");
  116. #endregion
  117. T_Sys_Position clmodel = new T_Sys_Position();
  118. clmodel.F_Name = name;
  119. clmodel.F_Des = des;
  120. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  121. clmodel.F_CreateOn = DateTime.Now;
  122. clmodel.F_State = (int)EnumDelState.Enabled;
  123. var res = await _sys_positionRepository.Add(clmodel);
  124. if (res > 0)
  125. return Success("保存成功");
  126. else
  127. {
  128. return Error("保存失败");
  129. }
  130. }
  131. /// <summary>
  132. /// 修改
  133. /// </summary>
  134. [Authorize]
  135. [HttpPost("update")]
  136. public async Task<IActionResult> Edit(string name, string des,int id)
  137. {
  138. #region
  139. if (id <= 0)
  140. return Error("请选择要编辑的");
  141. if (string.IsNullOrWhiteSpace(name))
  142. return Error("职位名称不能为空");
  143. if (await GetExistByNameAsync(name, id))
  144. return Error("职位名称不能重复,请重新输入!");
  145. #endregion
  146. var clmodel = await _sys_positionRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  147. if (clmodel == null)
  148. return Error("信息获取失败");
  149. clmodel.F_Name = name;
  150. clmodel.F_Des = des;
  151. clmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  152. clmodel.F_LastModifyOn = DateTime.Now;
  153. var res = await _sys_positionRepository.Update(clmodel);
  154. if (res)
  155. return Success("保存成功");
  156. else
  157. {
  158. return Error("保存失败");
  159. }
  160. }
  161. [Authorize]
  162. [HttpPost("delete")]
  163. public async Task<IActionResult> Remove(int[] ids)
  164. {
  165. //使用逻辑删除
  166. //物理删除的数据无法恢复
  167. var res = 0;
  168. if (ids != null && ids.Length > 0)
  169. {
  170. foreach (var item in ids)
  171. {
  172. var ml = await _sys_positionRepository.GetSingle(x => x.F_Id == item);
  173. ml.F_State = (int)EnumDelState.Delete;
  174. if (await _sys_positionRepository.Update(ml))
  175. res += 1;
  176. }
  177. if (res == ids.Length)
  178. return Success("删除成功");
  179. else if (res > 0 && res < ids.Length)
  180. return Error("部分删除失败,请查看后重新操作");
  181. else
  182. return Error("删除失败,请查看后重新操作");
  183. }
  184. else
  185. return Error("请选择要删除的记录");
  186. }
  187. #region 私有方法
  188. /// <summary>
  189. /// 根据名称查询是否存在此职位
  190. /// </summary>
  191. /// <returns></returns>
  192. private async Task<bool> GetExistByNameAsync(string name, int id)
  193. {
  194. long c = 0;
  195. Expression<Func<T_Sys_Position, bool>> eq = a => a.F_State == (int)EnumDelState.Enabled;
  196. eq = eq.And(b => b.F_Name.Equals(name));
  197. if (id > 0)
  198. eq = eq.And(b => b.F_Id != id);
  199. c = await _sys_positionRepository.GetCount(eq);
  200. return c > 0;
  201. }
  202. #endregion
  203. }
  204. }