TaskAssignmentRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using AutoMapper;
  2. using MySqlX.XDevAPI.Relational;
  3. using NPOI.OpenXmlFormats.Shared;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Dtos.Groups;
  6. using OASystem.Domain.Entities.Groups;
  7. using OASystem.Domain.Entities.Resource;
  8. using OASystem.Domain.ViewModels.Groups;
  9. using Org.BouncyCastle.Utilities.Collections;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace OASystem.Infrastructure.Repositories.Groups
  16. {
  17. public class TaskAssignmentRepository : BaseRepository<Grp_GroupsTaskAssignment, Grp_GroupsTaskAssignment>
  18. {
  19. private readonly IMapper _mapper;
  20. public TaskAssignmentRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  21. base(sqlSugar)
  22. {
  23. this._mapper = mapper;
  24. }
  25. public async Task<Result> GetTaskAssignmen()
  26. {
  27. Result result = new Result() { Code = -2, Msg = "未知错误" };
  28. try
  29. {
  30. //团组下拉框
  31. List<dynamic> _DelegationInfos = new List<dynamic>();
  32. List<Grp_DelegationInfo> grp_DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().Where(a => a.IsDel == 0 && a.TourCode != "" && a.IsState == 0).OrderByDescending(s => s.Id).ToList();
  33. foreach (var Item in grp_DelegationInfos)
  34. {
  35. var data = new
  36. {
  37. Id = Item.Id,
  38. Name = Item.TeamName
  39. };
  40. _DelegationInfos.Add(data);
  41. }
  42. //任务下拉框
  43. List<dynamic> _SetData = new List<dynamic>();
  44. //List<Sys_SetData> sys_SetData = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.IsDel == 0 && a.Name != "其他款项" && a.STid == 16).ToList();
  45. List<Sys_SetData> sys_SetData = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.IsDel == 0 && a.STid == 16).ToList();
  46. foreach (var Item in sys_SetData)
  47. {
  48. var data = new
  49. {
  50. Id = Item.Id,
  51. Name = Item.Name
  52. };
  53. _SetData.Add(data);
  54. }
  55. //人员安排列表
  56. List<dynamic> _User = new List<dynamic>();
  57. List<Sys_Users> sys_User = _sqlSugar.Queryable<Sys_Users>().Where(a => a.IsDel == 0 && a.Id != 4).ToList();
  58. foreach (var Item in sys_User)
  59. {
  60. var data = new
  61. {
  62. Id = Item.Id,
  63. Name = Item.CnName
  64. };
  65. _User.Add(data);
  66. }
  67. var dataList = new
  68. {
  69. DelegationInfos = _DelegationInfos,
  70. SetData = _SetData,
  71. User = _User
  72. };
  73. result = new Result() { Code = 0, Msg = "成功", Data = dataList };
  74. }
  75. catch (Exception)
  76. {
  77. result = new Result() { Code = -2, Msg = "未知错误" };
  78. }
  79. return result;
  80. }
  81. public async Task<Result> GetTaskAssignmenOp(TaskAssignmenDto dto)
  82. {
  83. Result result = new Result() { Code = -2, Msg = "未知错误" };
  84. try
  85. {
  86. List<int> id = new List<int>();
  87. List<Grp_GroupsTaskAssignment> _GroupsTaskAssignments = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DIId && a.CTId == dto.CTId && a.IsDel == 0).ToList();
  88. foreach (var Group in _GroupsTaskAssignments)
  89. {
  90. id.Add(Group.UId);
  91. }
  92. List<int> intersection = id.Intersect(dto.UIdStr).ToList();//交集
  93. List<int> del = id.Except(dto.UIdStr).ToList();//差集
  94. List<int> union = id.Union(dto.UIdStr).ToList();//并集
  95. for (int i = 0; i < del.Count; i++)
  96. {
  97. await _sqlSugar.Updateable<Grp_GroupsTaskAssignment>().Where(a => a.UId == del[i] && a.DIId == dto.DIId && a.CTId == dto.CTId).SetColumns(a => new Grp_GroupsTaskAssignment()
  98. {
  99. IsDel = 1,
  100. DeleteUserId = dto.CreateUserId,
  101. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  102. }).ExecuteCommandAsync();
  103. }
  104. //去掉交集
  105. foreach (var item in intersection)
  106. {
  107. union.Remove(item);
  108. }
  109. //去掉差集
  110. foreach (var item in del)
  111. {
  112. union.Remove(item);
  113. }
  114. //需添加的
  115. foreach (var item in union)
  116. {
  117. Grp_GroupsTaskAssignment AddData = _mapper.Map<Grp_GroupsTaskAssignment>(dto);
  118. AddData.UId = item;
  119. Grp_GroupsTaskAssignment addQuery = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().First(a => a.DIId == AddData.DIId && a.UId == AddData.UId && a.CTId == AddData.CTId);
  120. if (addQuery != null)
  121. {
  122. await _sqlSugar.Updateable<Grp_GroupsTaskAssignment>().Where(a => a.Id == addQuery.Id).SetColumns(a => new Grp_GroupsTaskAssignment()
  123. {
  124. IsDel = 0,
  125. DeleteUserId = 0,
  126. CreateUserId = AddData.CreateUserId,
  127. CreateTime = DateTime.Now
  128. }).ExecuteCommandAsync();
  129. }
  130. else
  131. {
  132. int AddId = await AddAsyncReturnId(AddData);
  133. }
  134. }
  135. result = new Result() { Code = 0, Msg = "保存成功!" };
  136. }
  137. catch (Exception)
  138. {
  139. result = new Result() { Code = -2, Msg = "保存失败,未知错误" };
  140. }
  141. return result;
  142. }
  143. public async Task<Result> TaskAssignmenQuery(TaskAssignmenQueryDto dto)
  144. {
  145. Result result = new Result() { Code = -2, Msg = "未知错误" };
  146. try
  147. {
  148. string sqlWhere = string.Empty;
  149. if (dto.DIId != 0)
  150. {
  151. sqlWhere += string.Format(@" And DIId ={0}", dto.DIId);
  152. }
  153. else
  154. {
  155. return result = new Result() { Code = -1, Msg = "暂无数据" };
  156. }
  157. if (dto.CTId != 0)
  158. {
  159. sqlWhere += string.Format(@" And CTId ={0}", dto.CTId);
  160. }
  161. else
  162. {
  163. return result = new Result() { Code = -1, Msg = "暂无数据" };
  164. }
  165. sqlWhere += string.Format(@" And IsDel={0} ", 0);
  166. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  167. {
  168. Regex r = new Regex("And");
  169. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  170. }
  171. string sql = string.Format(@"select * from Grp_GroupsTaskAssignment {0}", sqlWhere);
  172. List<Grp_GroupsTaskAssignment> GroupsTaskAssignment = await _sqlSugar.SqlQueryable<Grp_GroupsTaskAssignment>(sql).ToListAsync();
  173. Grp_DelegationInfo grp_DelegationInfo = new Grp_DelegationInfo();
  174. grp_DelegationInfo = _sqlSugar.Queryable<Grp_DelegationInfo>().First(a => a.Id == dto.DIId && a.IsDel == 0);
  175. if (GroupsTaskAssignment.Count != 0)
  176. {
  177. List<int> UserId = new List<int>();
  178. foreach (var item in GroupsTaskAssignment)
  179. {
  180. UserId.Add(item.UId);
  181. }
  182. var data = new
  183. {
  184. TaskAssignment = GroupsTaskAssignment[0],
  185. UserStr = UserId,
  186. DelegationInfo = grp_DelegationInfo
  187. };
  188. result = new Result()
  189. {
  190. Code = 0,
  191. Msg = "查询成功!",
  192. Data = data
  193. };
  194. }
  195. else
  196. {
  197. var data = new
  198. {
  199. TaskAssignment = new List<Grp_GroupsTaskAssignment>(),
  200. UserStr = new List<int>(),
  201. DelegationInfo = grp_DelegationInfo
  202. };
  203. result = new Result()
  204. {
  205. Code = 0,
  206. Msg = "查询成功!",
  207. Data = data
  208. };
  209. }
  210. }
  211. catch (Exception)
  212. {
  213. result = new Result() { Code = -2, Msg = "未知错误" };
  214. throw;
  215. }
  216. return result;
  217. }
  218. /// <summary>
  219. /// 查询配置任务
  220. /// 条件:根据 任务用户Id查询
  221. /// </summary>
  222. /// <param name="UserTaskId"></param>
  223. /// <returns></returns>
  224. public async Task<Result> TaskAssignmenQueryByUserTaskId(int UserTaskId)
  225. {
  226. Result result = new Result() { Code = -2, Msg = "未知错误" };
  227. var data = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
  228. .Where(gta => gta.IsDel == 0 && gta.IsEnable == 1 && gta.CreateUserId == UserTaskId)
  229. .ToListAsync();
  230. result = new Result() { Code = 0, Msg = "查询成功!" };
  231. return result;
  232. }
  233. /// <summary>
  234. /// 团组任务分配
  235. /// 批量分配
  236. /// 查询团组
  237. /// </summary>
  238. /// <param name="UserTaskId"></param>
  239. /// <returns></returns>
  240. public async Task<JsonView> TaskAllocationGroupSelect(TaskAllocationGroupSelectDto dto)
  241. {
  242. JsonView _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "其他错误" };
  243. DateTime beginDt, endDt;
  244. if (!DateTime.TryParse(dto.VisitBeginDt, out beginDt) || !DateTime.TryParse(dto.VisitEndDt, out endDt))
  245. {
  246. _view.Msg = $"团组出访开始/结束日期格式不正确!";
  247. return _view;
  248. }
  249. string beginStr = $"{beginDt.ToString("yyyy-MM-dd")} 00:00:00",
  250. endStr = $"{endDt.ToString("yyyy-MM-dd")} 23:59:59";
  251. var data = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  252. .Where(it => it.IsDel == 0 && it.VisitDate >= beginDt && it.VisitDate <= endDt)
  253. .Select(it => new
  254. {
  255. it.Id,
  256. it.TeamName
  257. })
  258. .ToListAsync();
  259. _view.Code = StatusCodes.Status200OK;
  260. _view.Msg = "操作成功!";
  261. _view.Data = data;
  262. _view.Count = data.Count;
  263. return _view;
  264. }
  265. /// <summary>
  266. /// 团组任务分配
  267. /// 批量分配
  268. /// 查询团组
  269. /// </summary>
  270. /// <param name="UserTaskId"></param>
  271. /// <returns></returns>
  272. public async Task<JsonView> TaskAllocationBulkAdd(TaskAllocationBulkAddDto dto)
  273. {
  274. JsonView _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "其他错误" };
  275. var infos = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
  276. .Where(x => x.IsDel == 0 &&
  277. dto.PermissionTypeIds.Contains(x.CTId) &&
  278. dto.UserIds.Contains(x.UId) &&
  279. dto.GroupIds.Contains(x.DIId)
  280. )
  281. .ToListAsync();
  282. int addCount = 0,
  283. unAddCount = 0;
  284. List<Grp_GroupsTaskAssignment> _GroupsTaskAssignments = new List<Grp_GroupsTaskAssignment>();
  285. foreach (var userId in dto.UserIds)
  286. {
  287. foreach (var permissionTypeId in dto.PermissionTypeIds)
  288. {
  289. foreach (var groupId in dto.GroupIds)
  290. {
  291. var info = infos.Find(x => x.UId == userId && x.CTId == permissionTypeId && x.DIId == groupId);
  292. if (info == null)
  293. {
  294. _GroupsTaskAssignments.Add(new Grp_GroupsTaskAssignment()
  295. {
  296. DIId = groupId,
  297. CTId = permissionTypeId,
  298. UId = userId,
  299. CreateUserId = dto.CurrUserId,
  300. CreateTime = DateTime.UtcNow,
  301. });
  302. addCount++;
  303. }
  304. else unAddCount++;
  305. }
  306. }
  307. }
  308. if (_GroupsTaskAssignments.Count > 0)
  309. {
  310. var add = await _sqlSugar.Insertable<Grp_GroupsTaskAssignment>(_GroupsTaskAssignments)
  311. .ExecuteCommandAsync();
  312. if (add < 1)
  313. {
  314. _view.Msg = "操作失败!";
  315. return _view;
  316. }
  317. }
  318. _view.Msg = $"操作成功!团组总计:{dto.GroupIds.Length};已操作团数:{addCount};未操作团数:{unAddCount}(该权限已存在)";
  319. _view.Code = StatusCodes.Status200OK;
  320. _view.Msg = "操作成功!";
  321. return _view;
  322. }
  323. }
  324. }