123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- 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<Grp_GroupsTaskAssignment, Grp_GroupsTaskAssignment>
- {
- private readonly IMapper _mapper;
- public TaskAssignmentRepository(SqlSugarClient sqlSugar, IMapper mapper) :
- base(sqlSugar)
- {
- this._mapper = mapper;
- }
- public async Task<Result> GetTaskAssignmen()
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- try
- {
- //团组下拉框
- List<dynamic> _DelegationInfos = new List<dynamic>();
- List<Grp_DelegationInfo> grp_DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().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<dynamic> _SetData = new List<dynamic>();
- //List<Sys_SetData> sys_SetData = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.IsDel == 0 && a.Name != "其他款项" && a.STid == 16).ToList();
- List<Sys_SetData> sys_SetData = _sqlSugar.Queryable<Sys_SetData>().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<dynamic> _User = new List<dynamic>();
- List<Sys_Users> sys_User = _sqlSugar.Queryable<Sys_Users>().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<Result> GetTaskAssignmenOp(TaskAssignmenDto dto)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- try
- {
- List<int> id = new List<int>();
- List<Grp_GroupsTaskAssignment> _GroupsTaskAssignments = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DIId && a.CTId == dto.CTId && a.IsDel == 0).ToList();
- foreach (var Group in _GroupsTaskAssignments)
- {
- id.Add(Group.UId);
- }
- List<int> intersection = id.Intersect(dto.UIdStr).ToList();//交集
- List<int> del = id.Except(dto.UIdStr).ToList();//差集
- List<int> union = id.Union(dto.UIdStr).ToList();//并集
- for (int i = 0; i < del.Count; i++)
- {
- await _sqlSugar.Updateable<Grp_GroupsTaskAssignment>().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<Grp_GroupsTaskAssignment>(dto);
- AddData.UId = item;
- Grp_GroupsTaskAssignment addQuery = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().First(a => a.DIId == AddData.DIId && a.UId == AddData.UId && a.CTId == AddData.CTId);
- if (addQuery != null)
- {
- await _sqlSugar.Updateable<Grp_GroupsTaskAssignment>().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<Result> 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<Grp_GroupsTaskAssignment> GroupsTaskAssignment = await _sqlSugar.SqlQueryable<Grp_GroupsTaskAssignment>(sql).ToListAsync();
- Grp_DelegationInfo grp_DelegationInfo = new Grp_DelegationInfo();
- grp_DelegationInfo = _sqlSugar.Queryable<Grp_DelegationInfo>().First(a => a.Id == dto.DIId && a.IsDel == 0);
- if (GroupsTaskAssignment.Count != 0)
- {
- List<int> UserId = new List<int>();
- 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<Grp_GroupsTaskAssignment>(),
- UserStr = new List<int>(),
- DelegationInfo = grp_DelegationInfo
- };
- result = new Result()
- {
- Code = 0,
- Msg = "查询成功!",
- Data = data
- };
- }
- }
- catch (Exception)
- {
- result = new Result() { Code = -2, Msg = "未知错误" };
- throw;
- }
- return result;
- }
- /// <summary>
- /// 查询配置任务
- /// 条件:根据 任务用户Id查询
- /// </summary>
- /// <param name="UserTaskId"></param>
- /// <returns></returns>
- public async Task<Result> TaskAssignmenQueryByUserTaskId(int UserTaskId)
- {
- Result result = new Result() { Code = -2, Msg = "未知错误" };
- var data = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
- .Where(gta => gta.IsDel == 0 && gta.IsEnable == 1 && gta.CreateUserId == UserTaskId)
- .ToListAsync();
- result = new Result() { Code = 0, Msg = "查询成功!" };
- return result;
- }
- /// <summary>
- /// 团组任务分配
- /// 批量分配
- /// 查询团组
- /// </summary>
- /// <param name="UserTaskId"></param>
- /// <returns></returns>
- public async Task<JsonView> 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<Grp_DelegationInfo>()
- .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;
- }
- /// <summary>
- /// 团组任务分配
- /// 批量分配
- /// 查询团组
- /// </summary>
- /// <param name="UserTaskId"></param>
- /// <returns></returns>
- public async Task<JsonView> TaskAllocationBulkAdd(TaskAllocationBulkAddDto dto)
- {
- JsonView _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "其他错误" };
- var infos = await _sqlSugar.Queryable<Grp_GroupsTaskAssignment>()
- .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<Grp_GroupsTaskAssignment> _GroupsTaskAssignments = new List<Grp_GroupsTaskAssignment>();
- 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<Grp_GroupsTaskAssignment>(_GroupsTaskAssignments)
- .ExecuteCommandAsync();
- if (add < 1)
- {
- _view.Msg = "操作失败!";
- return _view;
- }
- }
- _view.Msg = $"操作成功!团组总计:{dto.GroupIds.Length};已操作团数:{addCount};未操作团数:{unAddCount}(该权限已存在)";
- _view.Code = StatusCodes.Status200OK;
- _view.Msg = "操作成功!";
- return _view;
- }
- }
- }
|