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

DeptTeamController.cs 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 DeptTeamController : BaseController
  19. {
  20. private readonly ISys_DeptTeamRepository _sys_deptteamRepository;
  21. public DeptTeamController(ISys_DeptTeamRepository sys_deptteamRepository)
  22. {
  23. _sys_deptteamRepository = sys_deptteamRepository;
  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 deptid = 0, 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. if (deptid > 0)
  49. {
  50. conModels.Add(new ConditionalModel() { FieldName = "F_DeptId", ConditionalType = ConditionalType.Equal, FieldValue = deptid.ToString() });
  51. }
  52. #endregion
  53. int recordCount = 0;
  54. var list = await _sys_deptteamRepository.GetListViewByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
  55. return Success("成功", list);
  56. }
  57. [Authorize]
  58. [HttpGet("getlistdrop")]
  59. public async Task<IActionResult> GetListDrop(string key,int deptid=0)
  60. {
  61. List<IConditionalModel> conModels = new List<IConditionalModel>();
  62. #region 条件筛选
  63. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  64. if (!string.IsNullOrEmpty(key))
  65. {
  66. conModels.Add(new ConditionalCollections()
  67. {
  68. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  69. {
  70. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = key }),
  71. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Des", ConditionalType = ConditionalType.Like, FieldValue = key })
  72. }
  73. });
  74. }
  75. if (deptid > 0)
  76. {
  77. conModels.Add(new ConditionalModel() { FieldName = "F_DeptId", ConditionalType = ConditionalType.Equal, FieldValue = deptid.ToString() });
  78. }
  79. #endregion
  80. var list = await _sys_deptteamRepository.GetListALL(conModels, "F_CreateOn desc");
  81. var result = list.Select(x => new {
  82. x.F_Id,
  83. x.F_DeptId,
  84. x.F_Name,
  85. x.F_Des
  86. });
  87. return Success("获取全部数据成功", result);
  88. }
  89. /// <summary>
  90. /// 获取实体
  91. /// </summary>
  92. [Authorize]
  93. [HttpGet("getdetails")]
  94. public async Task<IActionResult> GetDetails(int id)
  95. {
  96. if (id > 0)
  97. {
  98. var dModel = await _sys_deptteamRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  99. if (dModel != null)
  100. {
  101. return Success("获取成功", dModel);
  102. }
  103. else
  104. {
  105. return Error("获取失败");
  106. }
  107. }
  108. else
  109. {
  110. return Error("获取参数失败");
  111. }
  112. }
  113. /// <summary>
  114. /// 添加
  115. /// </summary>
  116. [Authorize]
  117. [HttpPost("add")]
  118. public async Task<IActionResult> Add(string name, string des,int deptid=0)
  119. {
  120. #region 验证
  121. if (deptid <= 0)
  122. return Error("请选择部门");
  123. if (string.IsNullOrWhiteSpace(name))
  124. return Error("团队名称不能为空");
  125. if (await GetExistByNameAsync(name,deptid, 0))
  126. return Error("团队名称不能重复,请重新输入!");
  127. #endregion
  128. T_Sys_DeptTeam clmodel = new T_Sys_DeptTeam();
  129. clmodel.F_DeptId = deptid;
  130. clmodel.F_Name = name;
  131. clmodel.F_Des = des;
  132. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  133. clmodel.F_CreateOn = DateTime.Now;
  134. clmodel.F_State = (int)EnumDelState.Enabled;
  135. var res = await _sys_deptteamRepository.Add(clmodel);
  136. if (res > 0)
  137. return Success("保存成功");
  138. else
  139. {
  140. return Error("保存失败");
  141. }
  142. }
  143. /// <summary>
  144. /// 修改
  145. /// </summary>
  146. [Authorize]
  147. [HttpPost("update")]
  148. public async Task<IActionResult> Edit(string name, string des, int id = 0, int deptid = 0)
  149. {
  150. #region
  151. if (id <= 0)
  152. return Error("请选择要编辑的");
  153. if (deptid <= 0)
  154. return Error("请选择部门");
  155. if (string.IsNullOrWhiteSpace(name))
  156. return Error("团队名称不能为空");
  157. if (await GetExistByNameAsync(name, deptid, id))
  158. return Error("团队名称不能重复,请重新输入!");
  159. #endregion
  160. var clmodel = await _sys_deptteamRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  161. if (clmodel == null)
  162. return Error("信息获取失败");
  163. clmodel.F_DeptId = deptid;
  164. clmodel.F_Name = name;
  165. clmodel.F_Des = des;
  166. clmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  167. clmodel.F_LastModifyOn = DateTime.Now;
  168. var res = await _sys_deptteamRepository.Update(clmodel);
  169. if (res)
  170. return Success("保存成功");
  171. else
  172. {
  173. return Error("保存失败");
  174. }
  175. }
  176. [Authorize]
  177. [HttpPost("delete")]
  178. public async Task<IActionResult> Remove(int[] ids)
  179. {
  180. //使用逻辑删除
  181. //物理删除的数据无法恢复
  182. var res = 0;
  183. if (ids != null && ids.Length > 0)
  184. {
  185. foreach (var item in ids)
  186. {
  187. var ml = await _sys_deptteamRepository.GetSingle(x => x.F_Id == item);
  188. ml.F_State = (int)EnumDelState.Delete;
  189. if (await _sys_deptteamRepository.Update(ml))
  190. res += 1;
  191. }
  192. if (res == ids.Length)
  193. return Success("删除成功");
  194. else if (res > 0 && res < ids.Length)
  195. return Error("部分删除失败,请查看后重新操作");
  196. else
  197. return Error("删除失败,请查看后重新操作");
  198. }
  199. else
  200. return Error("请选择要删除的记录");
  201. }
  202. #region 私有方法
  203. /// <summary>
  204. /// 根据名称查询是否存在此职位
  205. /// </summary>
  206. /// <returns></returns>
  207. private async Task<bool> GetExistByNameAsync(string name,int deptid, int id)
  208. {
  209. long c = 0;
  210. Expression<Func<T_Sys_DeptTeam, bool>> eq = a => a.F_State == (int)EnumDelState.Enabled;
  211. eq = eq.And(b => b.F_Name.Equals(name));
  212. eq = eq.And(b => b.F_DeptId.Equals(deptid));
  213. if (id > 0)
  214. eq = eq.And(b => b.F_Id != id);
  215. c = await _sys_deptteamRepository.GetCount(eq);
  216. return c > 0;
  217. }
  218. #endregion
  219. }
  220. }