TaskAssignmentRepository.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using AutoMapper;
  2. using MySqlX.XDevAPI.Relational;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.Resource;
  7. using OASystem.Domain.ViewModels.Groups;
  8. using Org.BouncyCastle.Utilities.Collections;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace OASystem.Infrastructure.Repositories.Groups
  15. {
  16. public class TaskAssignmentRepository : BaseRepository<Grp_GroupsTaskAssignment, Grp_GroupsTaskAssignment>
  17. {
  18. private readonly IMapper _mapper;
  19. public TaskAssignmentRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  20. base(sqlSugar)
  21. {
  22. this._mapper = mapper;
  23. }
  24. public async Task<Result> GetTaskAssignmen()
  25. {
  26. Result result = new Result() { Code = -2, Msg = "未知错误" };
  27. try
  28. {
  29. //团组下拉框
  30. List<dynamic> _DelegationInfos = new List<dynamic>();
  31. List<Grp_DelegationInfo> grp_DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().Where(a => a.IsDel == 0 && a.TourCode != "" && a.IsState == 0).ToList();
  32. foreach (var Item in grp_DelegationInfos)
  33. {
  34. var data = new
  35. {
  36. Id = Item.Id,
  37. Name = Item.TeamName
  38. };
  39. _DelegationInfos.Add(data);
  40. }
  41. //任务下拉框
  42. List<dynamic> _SetData = new List<dynamic>();
  43. List<Sys_SetData> sys_SetData = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.IsDel == 0 && a.Name != "其他款项" && a.STid == 16).ToList();
  44. foreach (var Item in sys_SetData)
  45. {
  46. var data = new
  47. {
  48. Id = Item.Id,
  49. Name = Item.Name
  50. };
  51. _SetData.Add(data);
  52. }
  53. //人员安排列表
  54. List<dynamic> _User = new List<dynamic>();
  55. List<Sys_Users> sys_User = _sqlSugar.Queryable<Sys_Users>().Where(a => a.IsDel == 0 && a.Id != 4).ToList();
  56. foreach (var Item in sys_User)
  57. {
  58. var data = new
  59. {
  60. Id = Item.Id,
  61. Name = Item.CnName
  62. };
  63. _User.Add(data);
  64. }
  65. var dataList = new
  66. {
  67. DelegationInfos = _DelegationInfos,
  68. SetData = _SetData,
  69. User = _User
  70. };
  71. result = new Result() { Code = 0, Msg = "成功", Data = dataList };
  72. }
  73. catch (Exception)
  74. {
  75. result = new Result() { Code = -2, Msg = "未知错误" };
  76. }
  77. return result;
  78. }
  79. public async Task<Result> GetTaskAssignmenOp(TaskAssignmenDto dto)
  80. {
  81. Result result = new Result() { Code = -2, Msg = "未知错误" };
  82. try
  83. {
  84. List<int> id = new List<int>();
  85. List<Grp_GroupsTaskAssignment> _GroupsTaskAssignments = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DIId && a.CTId == dto.CTId && a.IsDel == 0).ToList();
  86. foreach (var Group in _GroupsTaskAssignments)
  87. {
  88. id.Add(Group.UId);
  89. }
  90. List<int> intersection = id.Intersect(dto.UIdStr).ToList();//交集
  91. List<int> del = id.Except(dto.UIdStr).ToList();//差集
  92. List<int> union = id.Union(dto.UIdStr).ToList();//并集
  93. for (int i = 0; i < del.Count; i++)
  94. {
  95. await _sqlSugar.Updateable<Grp_GroupsTaskAssignment>().Where(a => a.UId == del[i] && a.DIId == dto.DIId && a.CTId == dto.CTId).SetColumns(a => new Grp_GroupsTaskAssignment()
  96. {
  97. IsDel = 1,
  98. DeleteUserId = dto.CreateUserId,
  99. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  100. }).ExecuteCommandAsync();
  101. }
  102. //去掉交集
  103. foreach (var item in intersection)
  104. {
  105. union.Remove(item);
  106. }
  107. //去掉差集
  108. foreach (var item in del)
  109. {
  110. union.Remove(item);
  111. }
  112. //需添加的
  113. foreach (var item in union)
  114. {
  115. Grp_GroupsTaskAssignment AddData = _mapper.Map<Grp_GroupsTaskAssignment>(dto);
  116. AddData.UId = item;
  117. Grp_GroupsTaskAssignment addQuery = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().First(a => a.DIId == AddData.DIId && a.UId == AddData.UId && a.CTId == AddData.CTId);
  118. if (addQuery != null)
  119. {
  120. await _sqlSugar.Updateable<Grp_GroupsTaskAssignment>().Where(a => a.Id == addQuery.Id).SetColumns(a => new Grp_GroupsTaskAssignment()
  121. {
  122. IsDel = 0,
  123. DeleteUserId = 0,
  124. CreateUserId = AddData.CreateUserId,
  125. CreateTime = DateTime.Now
  126. }).ExecuteCommandAsync();
  127. }
  128. else
  129. {
  130. int AddId = await AddAsyncReturnId(AddData);
  131. }
  132. }
  133. result = new Result() { Code = 0, Msg = "保存成功!" };
  134. }
  135. catch (Exception)
  136. {
  137. result = new Result() { Code = -2, Msg = "保存失败,未知错误" };
  138. }
  139. return result;
  140. }
  141. public async Task<Result> TaskAssignmenQuery(TaskAssignmenQueryDto dto)
  142. {
  143. Result result = new Result() { Code = -2, Msg = "未知错误" };
  144. try
  145. {
  146. string sqlWhere = string.Empty;
  147. if (dto.DIId != 0)
  148. {
  149. sqlWhere += string.Format(@" And DIId ={0}", dto.DIId);
  150. }
  151. else
  152. {
  153. return result = new Result() { Code = -1, Msg = "暂无数据" };
  154. }
  155. if (dto.CTId != 0)
  156. {
  157. sqlWhere += string.Format(@" And CTId ={0}", dto.CTId);
  158. }
  159. sqlWhere += string.Format(@" And IsDel={0} ", 0);
  160. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  161. {
  162. Regex r = new Regex("And");
  163. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  164. }
  165. string sql = string.Format(@"select * from Grp_GroupsTaskAssignment {0}", sqlWhere);
  166. List<Grp_GroupsTaskAssignment> GroupsTaskAssignment = await _sqlSugar.SqlQueryable<Grp_GroupsTaskAssignment>(sql).ToListAsync();
  167. Grp_DelegationInfo grp_DelegationInfo = new Grp_DelegationInfo();
  168. grp_DelegationInfo = _sqlSugar.Queryable<Grp_DelegationInfo>().First(a => a.Id == dto.DIId && a.IsDel == 0);
  169. if (GroupsTaskAssignment.Count != 0)
  170. {
  171. List<int> UserId = new List<int>();
  172. foreach (var item in GroupsTaskAssignment)
  173. {
  174. UserId.Add(item.UId);
  175. }
  176. var data = new
  177. {
  178. TaskAssignment = GroupsTaskAssignment[0],
  179. UserStr = UserId,
  180. DelegationInfo = grp_DelegationInfo
  181. };
  182. result = new Result()
  183. {
  184. Code = 0,
  185. Msg = "查询成功!",
  186. Data = data
  187. };
  188. }
  189. else
  190. {
  191. var data = new
  192. {
  193. TaskAssignment = new List<Grp_GroupsTaskAssignment>(),
  194. UserStr = new List<int>(),
  195. DelegationInfo = grp_DelegationInfo
  196. };
  197. result = new Result()
  198. {
  199. Code = 0,
  200. Msg = "查询成功!",
  201. Data = data
  202. };
  203. }
  204. }
  205. catch (Exception)
  206. {
  207. result = new Result() { Code = -2, Msg = "未知错误" };
  208. throw;
  209. }
  210. return result;
  211. }
  212. /// <summary>
  213. /// 查询配置任务
  214. /// 条件:根据 任务用户Id查询
  215. /// </summary>
  216. /// <param name="UserTaskId"></param>
  217. /// <returns></returns>
  218. public async Task<Result> TaskAssignmenQueryByUserTaskId(int UserTaskId)
  219. {
  220. Result result = new Result() { Code = -2, Msg = "未知错误" };
  221. var data = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
  222. .Where(gta => gta.IsDel == 0 && gta.IsEnable == 1 && gta.CreateUserId == UserTaskId)
  223. .ToListAsync();
  224. result = new Result() { Code = 0, Msg = "查询成功!" };
  225. return result;
  226. }
  227. }
  228. }