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

DeptTeamController.cs 10KB

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