using AutoMapper; using MySqlX.XDevAPI.Relational; using NPOI.OpenXmlFormats.Shared; using OASystem.Domain; using OASystem.Domain.Dtos.Groups; using OASystem.Domain.Entities.Groups; using OASystem.Domain.Entities.Resource; using OASystem.Domain.ViewModels.Groups; using Org.BouncyCastle.Utilities.Collections; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OASystem.Infrastructure.Repositories.Groups { public class TaskAssignmentRepository : BaseRepository { private readonly IMapper _mapper; public TaskAssignmentRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar) { this._mapper = mapper; } public async Task GetTaskAssignmen() { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { //团组下拉框 List _DelegationInfos = new List(); List grp_DelegationInfos = _sqlSugar.Queryable().Where(a => a.IsDel == 0 && a.TourCode != "" && a.IsState == 0).OrderByDescending(s => s.Id).ToList(); foreach (var Item in grp_DelegationInfos) { var data = new { Id = Item.Id, Name = Item.TeamName }; _DelegationInfos.Add(data); } //任务下拉框 List _SetData = new List(); //List sys_SetData = _sqlSugar.Queryable().Where(a => a.IsDel == 0 && a.Name != "其他款项" && a.STid == 16).ToList(); List sys_SetData = _sqlSugar.Queryable().Where(a => a.IsDel == 0 && a.STid == 16).ToList(); foreach (var Item in sys_SetData) { var data = new { Id = Item.Id, Name = Item.Name }; _SetData.Add(data); } //人员安排列表 List _User = new List(); List sys_User = _sqlSugar.Queryable().Where(a => a.IsDel == 0 && a.Id != 4).ToList(); foreach (var Item in sys_User) { var data = new { Id = Item.Id, Name = Item.CnName }; _User.Add(data); } var dataList = new { DelegationInfos = _DelegationInfos, SetData = _SetData, User = _User }; result = new Result() { Code = 0, Msg = "成功", Data = dataList }; } catch (Exception) { result = new Result() { Code = -2, Msg = "未知错误" }; } return result; } public async Task GetTaskAssignmenOp(TaskAssignmenDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { List id = new List(); List _GroupsTaskAssignments = _sqlSugar.Queryable().Where(a => a.DIId == dto.DIId && a.CTId == dto.CTId && a.IsDel == 0).ToList(); foreach (var Group in _GroupsTaskAssignments) { id.Add(Group.UId); } List intersection = id.Intersect(dto.UIdStr).ToList();//交集 List del = id.Except(dto.UIdStr).ToList();//差集 List union = id.Union(dto.UIdStr).ToList();//并集 for (int i = 0; i < del.Count; i++) { await _sqlSugar.Updateable().Where(a => a.UId == del[i] && a.DIId == dto.DIId && a.CTId == dto.CTId).SetColumns(a => new Grp_GroupsTaskAssignment() { IsDel = 1, DeleteUserId = dto.CreateUserId, DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }).ExecuteCommandAsync(); } //去掉交集 foreach (var item in intersection) { union.Remove(item); } //去掉差集 foreach (var item in del) { union.Remove(item); } //需添加的 foreach (var item in union) { Grp_GroupsTaskAssignment AddData = _mapper.Map(dto); AddData.UId = item; Grp_GroupsTaskAssignment addQuery = _sqlSugar.Queryable().First(a => a.DIId == AddData.DIId && a.UId == AddData.UId && a.CTId == AddData.CTId); if (addQuery != null) { await _sqlSugar.Updateable().Where(a => a.Id == addQuery.Id).SetColumns(a => new Grp_GroupsTaskAssignment() { IsDel = 0, DeleteUserId = 0, CreateUserId = AddData.CreateUserId, CreateTime = DateTime.Now }).ExecuteCommandAsync(); } else { int AddId = await AddAsyncReturnId(AddData); } } result = new Result() { Code = 0, Msg = "保存成功!" }; } catch (Exception) { result = new Result() { Code = -2, Msg = "保存失败,未知错误" }; } return result; } public async Task TaskAssignmenQuery(TaskAssignmenQueryDto dto) { Result result = new Result() { Code = -2, Msg = "未知错误" }; try { string sqlWhere = string.Empty; if (dto.DIId != 0) { sqlWhere += string.Format(@" And DIId ={0}", dto.DIId); } else { return result = new Result() { Code = -1, Msg = "暂无数据" }; } if (dto.CTId != 0) { sqlWhere += string.Format(@" And CTId ={0}", dto.CTId); } else { return result = new Result() { Code = -1, Msg = "暂无数据" }; } sqlWhere += string.Format(@" And IsDel={0} ", 0); if (!string.IsNullOrEmpty(sqlWhere.Trim())) { Regex r = new Regex("And"); sqlWhere = r.Replace(sqlWhere, "Where", 1); } string sql = string.Format(@"select * from Grp_GroupsTaskAssignment {0}", sqlWhere); List GroupsTaskAssignment = await _sqlSugar.SqlQueryable(sql).ToListAsync(); Grp_DelegationInfo grp_DelegationInfo = new Grp_DelegationInfo(); grp_DelegationInfo = _sqlSugar.Queryable().First(a => a.Id == dto.DIId && a.IsDel == 0); if (GroupsTaskAssignment.Count != 0) { List UserId = new List(); foreach (var item in GroupsTaskAssignment) { UserId.Add(item.UId); } var data = new { TaskAssignment = GroupsTaskAssignment[0], UserStr = UserId, DelegationInfo = grp_DelegationInfo }; result = new Result() { Code = 0, Msg = "查询成功!", Data = data }; } else { var data = new { TaskAssignment = new List(), UserStr = new List(), DelegationInfo = grp_DelegationInfo }; result = new Result() { Code = 0, Msg = "查询成功!", Data = data }; } } catch (Exception) { result = new Result() { Code = -2, Msg = "未知错误" }; throw; } return result; } /// /// 查询配置任务 /// 条件:根据 任务用户Id查询 /// /// /// public async Task TaskAssignmenQueryByUserTaskId(int UserTaskId) { Result result = new Result() { Code = -2, Msg = "未知错误" }; var data = await _sqlSugar.Queryable() .Where(gta => gta.IsDel == 0 && gta.IsEnable == 1 && gta.CreateUserId == UserTaskId) .ToListAsync(); result = new Result() { Code = 0, Msg = "查询成功!" }; return result; } /// /// 团组任务分配 /// 批量分配 /// 查询团组 /// /// /// public async Task TaskAllocationGroupSelect(TaskAllocationGroupSelectDto dto) { JsonView _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "其他错误" }; DateTime beginDt, endDt; if (!DateTime.TryParse(dto.VisitBeginDt, out beginDt) || !DateTime.TryParse(dto.VisitEndDt, out endDt)) { _view.Msg = $"团组出访开始/结束日期格式不正确!"; return _view; } string beginStr = $"{beginDt.ToString("yyyy-MM-dd")} 00:00:00", endStr = $"{endDt.ToString("yyyy-MM-dd")} 23:59:59"; var data = await _sqlSugar.Queryable() .Where(it => it.IsDel == 0 && it.VisitDate >= beginDt && it.VisitDate <= endDt) .Select(it => new { it.Id, it.TeamName }) .ToListAsync(); _view.Code = StatusCodes.Status200OK; _view.Msg = "操作成功!"; _view.Data = data; _view.Count = data.Count; return _view; } /// /// 团组任务分配 /// 批量分配 /// 查询团组 /// /// /// public async Task TaskAllocationBulkAdd(TaskAllocationBulkAddDto dto) { JsonView _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "其他错误" }; var infos = await _sqlSugar.Queryable() .Where(x => x.IsDel == 0 && dto.PermissionTypeIds.Contains(x.CTId) && dto.UserIds.Contains(x.UId) && dto.GroupIds.Contains(x.DIId) ) .ToListAsync(); int addCount = 0, unAddCount = 0; List _GroupsTaskAssignments = new List(); foreach (var userId in dto.UserIds) { foreach (var permissionTypeId in dto.PermissionTypeIds) { foreach (var groupId in dto.GroupIds) { var info = infos.Find(x => x.UId == userId && x.CTId == permissionTypeId && x.DIId == groupId); if (info == null) { _GroupsTaskAssignments.Add(new Grp_GroupsTaskAssignment() { DIId = groupId, CTId = permissionTypeId, UId = userId, CreateUserId = dto.CurrUserId, CreateTime = DateTime.UtcNow, }); addCount++; } else unAddCount++; } } } if (_GroupsTaskAssignments.Count > 0) { var add = await _sqlSugar.Insertable(_GroupsTaskAssignments) .ExecuteCommandAsync(); if (add < 1) { _view.Msg = "操作失败!"; return _view; } } _view.Msg = $"操作成功!团组总计:{dto.GroupIds.Length};已操作团数:{addCount};未操作团数:{unAddCount}(该权限已存在)"; _view.Code = StatusCodes.Status200OK; _view.Msg = "操作成功!"; return _view; } } }