using ATS.NonCustodial.Application.Base; using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.AppDictionaries; using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.User; using ATS.NonCustodial.Application.Contracts.Interfaces.Business.AppCaseManagements.AppCaseManagement.Output; using ATS.NonCustodial.Application.Contracts.Interfaces.Business.AppEarlyWarnings.Output; using ATS.NonCustodial.Application.Contracts.Interfaces.Business.ViolationStatistics; using ATS.NonCustodial.Application.Contracts.Interfaces.Business.ViolationStatistics.Input; using ATS.NonCustodial.Application.Contracts.Interfaces.Business.ViolationStatistics.Output; using ATS.NonCustodial.Domain.Entities.Admins; using ATS.NonCustodial.Domain.Entities.Business; using ATS.NonCustodial.Domain.Entities.Business.CaseManagements; using ATS.NonCustodial.Domain.Entities.Business.EarlyWarning; using ATS.NonCustodial.Domain.Shared.AggRootEntities.Dtos; using ATS.NonCustodial.Domain.Shared.Enums; using ATS.NonCustodial.Domain.Shared.OrmRepositories.Basic.EfCore; using ATS.NonCustodial.DynamicApi; using ATS.NonCustodial.DynamicApi.Attributes; using ATS.NonCustodial.Shared.Common.Auth; using ATS.NonCustodial.Shared.Common.Dtos; using ATS.NonCustodial.Shared.Common.UnifiedResults; using ATS.NonCustodial.Shared.Extensions; using ATS.NonCustodial.Shared.Extensions.Collection; using ATS.NonCustodial.Shared.Helpers; using AutoMapper.QueryableExtensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using StackExchange.Profiling.Internal; using static System.Runtime.InteropServices.JavaScript.JSType; namespace ATS.NonCustodial.Application.Impl.Business { /// /// 违规统计服务管理 /// /// Author:mxg /// CreatedTimed:2022-06-06 05:35 PM [DynamicApi(Area = "admin")] public class AppViolationStatisticsService : AdminCommonService, IAppViolationStatisticsService, IDynamicApi { #region Identity private readonly IEfRepository _appViolationStatisticsRepository; private readonly IAppDictionaryService _appDictionaryService; private readonly IEfRepository _appEarlyWarningRepository; private readonly IUserService _userService; private readonly IEfRepository _appSupervisedPersonRepository; private readonly IEfRepository _appSupervisorRepository; private readonly IEfRepository _userRepository; /// /// /// /// /// /// /// public AppViolationStatisticsService(IEfRepository userRepository, IEfRepository appSupervisorRepository, IEfRepository appViolationStatisticsRepository, IAppDictionaryService appDictionaryService, IEfRepository appEarlyWarningRepository, IUserService userService, IEfRepository asprl, IEfRepository appCaseManagementRepository, IEfRepository appCaseSupervisedPersonRepository, IEfRepository appCaseSupervisorRepository ) : base(appCaseManagementRepository, appCaseSupervisorRepository, appCaseSupervisedPersonRepository, userService, appDictionaryService, asprl) { _appSupervisorRepository = appSupervisorRepository; _appViolationStatisticsRepository = appViolationStatisticsRepository; _appDictionaryService = appDictionaryService; _appEarlyWarningRepository = appEarlyWarningRepository; _userService = userService; _appSupervisedPersonRepository = appCaseSupervisedPersonRepository; _userRepository = userRepository; } #endregion Identity /// /// 根据被监管人员违规明细Id分页查询 /// /// /// [HttpPost] public async Task GetPageAsync(AppViolationStatisticsGetPageInput input) { var data = await _appEarlyWarningRepository.AsQueryable(false, true) .Where(w => w.CaseId == input.Id && w.SupervisedPersonId == input.SupervisedPersonId) .OrderByDescending(r => r.CreatedTime) .ProjectTo(Mapper.ConfigurationProvider) .PagedAsync(input) .ConfigureAwait(false); return ResultOutput.Ok(data); } /// /// 违规记录统计 /// /// [HttpPost] public async Task ViolationStatisticsPageAsync(ViolationStatisticsPageInput input) { //获取当前用户权限下的案件ids var limits = User.limits; var selectLimits = await _appCaseSupervisorRepository.AsQueryable(false, true) .Where(w => limits.Contains(w.UnitId.ToString())) .ToListAsync(); var caseIdList = selectLimits.Select(w => w.CaseId).Distinct().ToList(); //根据当前登录人查看其手里的案件被监管人 var spList = await base.GetCurrentUserCaseListAsync(); //获取案件信息 var casebaselist = await spList.Where(q => q.AppCaseManagement != null && caseIdList.Contains(q.AppCaseManagement.Id)).Select(q => q.AppCaseManagement).ToListAsync(); //获取案件id var casebase = await spList .Where(q => q.AppCaseManagement != null && caseIdList.Contains(q.AppCaseManagement.Id)) .WhereIf(input.CaseProgress != null, a => a.AppCaseManagement.CaseProgress == input.CaseProgress).Select(q => q.AppCaseManagement.Id).ToListAsync(); var spIds = await spList.Where(q => q.AppCaseSupervisedPerson != null && casebase.Contains(q.AppCaseSupervisedPerson.CaseId)).Select(w => w.AppCaseSupervisedPerson!.SupervisedPersonId).ToListAsync(); //查询记录 var query = await _appEarlyWarningRepository .AsQueryable(false, true).Where(q => spIds.Contains(q.SupervisedPersonId)) .WhereIf(input.CaseId != default, a => a.CaseId == input.CaseId) .WhereIf(input.SupervisedPersonId != default, w => w.SupervisedPersonId == input.SupervisedPersonId) .WhereIf(input.SupervisedPersonName.HasValue(), w => w.SupervisedPersonName.Contains(input.SupervisedPersonName)) .WhereIf(input.TimeSearch?.BeginTime != null && input.TimeSearch?.EndTime != null, q => q.CreatedTime >= input.TimeSearch.BeginTime && q.CreatedTime <= $"{input.TimeSearch.EndTime.Value.ToString("yyyy-MM-dd")} 23:59:59".ToDateTime()) .ToListAsync(); //分组 var dataGroup = query .GroupBy(w => new { w.CaseId, w.SupervisedPersonId, w.CaseName, w.SupervisedPersonName }); var pageData = dataGroup.Skip((input.PageIndex - 1) * input.PageSize) .Take(input.PageSize); //处理身份证字段 var userList = await _userService.GetAllByConditionAsync(new BatchIdsInput() { Ids = pageData.Select(w => w.Key.SupervisedPersonId).ToList() }); var dataList = pageData.Select(data => new ViolationStatisticsListDto() { CaseId = data.Key.CaseId, SupervisedPersonId = data.Key.SupervisedPersonId, CaseName = casebaselist.Where(q => q.Id == data.Key.CaseId).First()?.Name, //data.Key.CaseName, SupervisedPersonName = data.Key.SupervisedPersonName, CaseProgress = casebaselist.Where(q => q.Id == data.Key.CaseId).First()?.CaseProgress, Violations = data.Count(), IdCard = userList.FirstOrDefault(w => w.Id == data.Key.SupervisedPersonId)?.IdCard }).ToList(); var pageResult = new PagedList() { TotalCount = dataGroup.Count(), Data = dataList }; return ResultOutput.Ok(pageResult); } /// /// 违规记录统计导出 /// /// [HttpPost] public async Task ViolationStatisticsExportAsync(ViolationStatisticsPageInput input) { //获取当前用户权限下的案件ids var limits = User.limits; var selectLimits = await _appCaseSupervisorRepository.AsQueryable(false, true) .Where(w => limits.Contains(w.UnitId.ToString())) .ToListAsync(); var caseIdList = selectLimits.Select(w => w.CaseId).Distinct().ToList(); //根据当前登录人查看其手里的案件被监管人 var spList = await base.GetCurrentUserCaseListAsync(); //获取案件信息 var casebaselist = await spList.Where(q => q.AppCaseManagement != null && caseIdList.Contains(q.AppCaseManagement.Id)).Select(q => q.AppCaseManagement).ToListAsync(); //获取案件id var casebase = await spList .Where(q => q.AppCaseManagement != null && caseIdList.Contains(q.AppCaseManagement.Id)) .WhereIf(input.CaseProgress != null, a => a.AppCaseManagement.CaseProgress == input.CaseProgress).Select(q => q.AppCaseManagement.Id).ToListAsync(); var spIds = await spList.Where(q => q.AppCaseSupervisedPerson != null && casebase.Contains(q.AppCaseSupervisedPerson.CaseId)).Select(w => w.AppCaseSupervisedPerson!.SupervisedPersonId).ToListAsync(); //查询记录 var query = await _appEarlyWarningRepository .AsQueryable(false, true).Where(q => spIds.Contains(q.SupervisedPersonId)) .WhereIf(input.CaseId != default, a => a.CaseId == input.CaseId) .WhereIf(input.SupervisedPersonId != default, w => w.SupervisedPersonId == input.SupervisedPersonId) .WhereIf(input.SupervisedPersonName.HasValue(), w => w.SupervisedPersonName.Contains(input.SupervisedPersonName)) .WhereIf(input.TimeSearch?.BeginTime != null && input.TimeSearch?.EndTime != null, q => q.CreatedTime >= input.TimeSearch.BeginTime && q.CreatedTime <= $"{input.TimeSearch.EndTime.Value.ToString("yyyy-MM-dd")} 23:59:59".ToDateTime()) .ToListAsync(); //分组 var dataGroup = query .GroupBy(w => new { w.CaseId, w.SupervisedPersonId, w.CaseName, w.SupervisedPersonName }); var pageData = dataGroup.Skip((input.PageIndex - 1) * input.PageSize) .Take(input.PageSize); //处理身份证字段 var userList = await _userService.GetAllByConditionAsync(new BatchIdsInput() { Ids = pageData.Select(w => w.Key.SupervisedPersonId).ToList() }); //(0:待执行 1:侦查阶段 2:已结束,3:审查起诉阶段,4:审理阶段) var dataList = pageData.Select(data => new ViolationStatisticsExportDto() { CaseId = data.Key.CaseId, SupervisedPersonId = data.Key.SupervisedPersonId, CaseName = casebaselist.Where(q => q.Id == data.Key.CaseId).First()?.Name, //data.Key.CaseName, SupervisedPersonName = data.Key.SupervisedPersonName, CaseProgress = casebaselist.Where(q => q.Id == data.Key.CaseId).First()?.CaseProgress.ToDescription(), Violations = data.Count(), IdCard = userList.FirstOrDefault(w => w.Id == data.Key.SupervisedPersonId)?.IdCard }).ToList(); var memorySystem = ExcelHelper.ToExcel(dataList); return new FileController().File(memorySystem.ToArray(), "application/ms-excel", DateTime.Now.ToString("f") + ".xlsx"); } /// /// 违规人数分页查询 /// /// [HttpPost] public async Task AppEarlyWarningPageAsync(ViolationStatisticsPageInput input) { //根据当前登录人查看其手里的案件被监管人 var spList = await _appCaseManagementRepository.AsQueryable(false, true).Where(q => q.IsDeleted == false).ToListAsync(); //获取案件id var casebase = spList.Select(q => q.Id); //查询记录 var query = await _appEarlyWarningRepository .AsQueryable(false, true).Where(q => casebase.Contains(q.CaseId) && !string.IsNullOrEmpty(q.Title)) .WhereIf(input.CaseId != default, a => a.CaseId == input.CaseId) .WhereIf(input.Wglx != null, a => a.EarlyWarningTypeId == input.Wglx) .WhereIf(input.SupervisedPersonId != default, w => w.SupervisedPersonId == input.SupervisedPersonId) .WhereIf(input.TimeSearch?.BeginTime != null && input.TimeSearch?.EndTime != null, q => q.CreatedTime >= input.TimeSearch.BeginTime && q.CreatedTime <= $"{input.TimeSearch.EndTime.Value.ToString("yyyy-MM-dd")} 23:59:59".ToDateTime()) .OrderByDescending(q => q.CreatedTime) .ToListAsync(); //分组 var dataGroup = query.Distinct((a, b) => a.SupervisedPersonId == b.SupervisedPersonId && a.Title == b.Title); var pageData = dataGroup.Skip((input.PageIndex - 1) * input.PageSize) .Take(input.PageSize); //处理身份证字段 var userList = await _userService.GetAllByConditionAsync(new BatchIdsInput() { Ids = pageData.Select(w => w.SupervisedPersonId).ToList() }); var dataList = pageData.Select(data => new ViolationStatisticsListDto() { SupervisedPersonId = data.SupervisedPersonId, CaseName = spList.FirstOrDefault(q => q.Id == data.CaseId)?.Name, CaseProgress = spList.FirstOrDefault(q => q.Id == data.CaseId)?.CaseProgress, //data.Key.CaseName, SupervisedPersonName = data.SupervisedPersonName, IdCard = userList.FirstOrDefault(w => w.Id == data.SupervisedPersonId)?.IdCard, Wgtime = data?.CreatedTime, Wglx = data?.Title, }).ToList(); var pageResult = new PagedList() { TotalCount = dataGroup.Count(), Data = dataList }; return ResultOutput.Ok(pageResult); } /// /// 违规人数统计 /// /// [HttpPost] public async Task ViolationStatisticsAsync(ViolationStatisticsPageInput input) { var caseIds = await _appCaseManagementRepository.AsQueryable(false, true).Where(q => q.IsDeleted == false).Select(q => q.Id).ToListAsync(); //查询记录 var list = await _appEarlyWarningRepository .AsQueryable(false, true) .WhereIf(input.TimeSearch.BeginTime != null && input.TimeSearch.EndTime != null, q => q.CreatedTime >= input.TimeSearch.BeginTime && q.CreatedTime <= $"{input.TimeSearch.EndTime.Value.ToString("yyyy-MM-dd")} 23:59:59".ToDateTime()) .WhereIf(input.Wglx != null, q => q.EarlyWarningTypeId == input.Wglx) .Where(q => caseIds.Contains(q.CaseId)).ToListAsync(); var geruplist = list.Where(q => !string.IsNullOrEmpty(q.Title)).GroupBy(q => q.Title).Select(q => new { name = q.Key, value = q.Distinct((a, b) => a.SupervisedPersonId == b.SupervisedPersonId).Count() }); var count = geruplist.Sum(q => q.value); return ResultOutput.Ok(new { count = count, data = geruplist, list = list.Distinct((a, b) => a.SupervisedPersonId == b.SupervisedPersonId && a.Title == b.Title).ToList() }); } /// /// 未打卡统计 /// /// [HttpPost] public async Task NotClockedStatisticsPageAsync(NotClockedInput input) { //获取当前用户权限下的案件ids var limits = User.limits; var selectLimits = await _appCaseSupervisorRepository.AsQueryable(false, true) .Where(w => limits.Contains(w.UnitId.ToString())) .ToListAsync(); var caseIdList = selectLimits.Select(w => w.CaseId).Distinct().ToList(); var caseList = await _appCaseManagementRepository.AsQueryable(false, true) .Where(w => caseIdList.Contains(w.Id)) .WhereIf(input.CaseId.HasValue, w => w.Id == input.CaseId) .ToListAsync(); var applicationType = await base.GetDictionariesOutput("early_warning_type", "notClocked"); List notClockList = new List(); foreach (var item in caseList) { if (item == null) continue; var caseType= await _appDictionaryService.GetDicByDicId(item.CaseTypeId); NotClockListDto notClockListDto = new NotClockListDto(); notClockListDto.Bmsah = item.Bmsah; notClockListDto.CaseId = item.Id; notClockListDto.CaseName = item.Name; notClockListDto.CreatedTime = item.CreatedTime; notClockListDto.CaseTypeName = caseType.Name; var unitList = await (from c in _appSupervisorRepository.AsQueryable(false, true) join u in _userRepository.AsQueryable(false, true) on c.SupervisorId equals u.Id where c.CaseId == item.Id select u.Unitname).FirstOrDefaultAsync(); notClockListDto.SupervisionUnit = unitList; //根据案件找到被监管 var supervisedPerson = await _appSupervisedPersonRepository .AsQueryable(false, true) .Where(w => w.CaseId == item.Id) .ToListAsync(); foreach (var person in supervisedPerson) { notClockListDto.SupervisedPersonId = person.SupervisedPersonId; notClockListDto.SupervisedPersonName = person.SupervisedPersonName; var earlyList = await _appEarlyWarningRepository .AsQueryable(false, true) .Where(w => w.EarlyWarningTypeId == applicationType.Id && w.SupervisedPersonId == person.SupervisedPersonId && w.CaseId == item.Id) .WhereIf(input.EndCreatedTime.HasValue, w => w.CreatedTime <= input.EndCreatedTime) .WhereIf(input.StartCreatedTime.HasValue, w => w.CreatedTime >= input.StartCreatedTime) .ToListAsync(); notClockListDto.NotClockedCount= earlyList.Count; notClockList.Add(notClockListDto); } } var pageData = notClockList.Skip((input.PageIndex - 1) * input.PageSize) .Take(input.PageSize).ToList(); var pageResult = new PagedList() { TotalCount = notClockList.Count(), Data = pageData }; return ResultOutput.Ok(pageResult); } /// /// 越界统计 /// /// [HttpPost] public async Task LeaveAreaStatisticsPageAsync(NotClockedInput input) { //获取当前用户权限下的案件ids var limits = User.limits; var selectLimits = await _appCaseSupervisorRepository.AsQueryable(false, true) .Where(w => limits.Contains(w.UnitId.ToString())) .ToListAsync(); var caseIdList = selectLimits.Select(w => w.CaseId).Distinct().ToList(); var caseList = await _appCaseManagementRepository.AsQueryable(false, true) .Where(w => caseIdList.Contains(w.Id)) .WhereIf(input.CaseId.HasValue, w => w.Id == input.CaseId) .ToListAsync(); var applicationType = await base.GetDictionariesOutput("early_warning_type", "LeaveArea"); List notClockList = new List(); foreach (var item in caseList) { if (item == null) continue; var caseType = await _appDictionaryService.GetDicByDicId(item.CaseTypeId); ViolationListDto notClockListDto = new ViolationListDto(); notClockListDto.Bmsah = item.Bmsah; notClockListDto.CaseId = item.Id; notClockListDto.CaseName = item.Name; notClockListDto.CreatedTime = item.CreatedTime; notClockListDto.CaseTypeName = caseType.Name; var unitList =await (from c in _appSupervisorRepository.AsQueryable(false, true) join u in _userRepository.AsQueryable(false, true) on c.SupervisorId equals u.Id where c.CaseId == item.Id select u.Unitname).FirstOrDefaultAsync(); notClockListDto.SupervisionUnit = unitList; //根据案件找到被监管 var supervisedPerson = await _appSupervisedPersonRepository .AsQueryable(false, true) .Where(w => w.CaseId == item.Id) .ToListAsync(); foreach (var person in supervisedPerson) { notClockListDto.SupervisedPersonId = person.SupervisedPersonId; notClockListDto.SupervisedPersonName = person.SupervisedPersonName; var earlyList = await _appEarlyWarningRepository .AsQueryable(false, true) .Where(w => w.EarlyWarningTypeId == applicationType.Id && w.SupervisedPersonId == person.SupervisedPersonId && w.CaseId == item.Id) .WhereIf(input.EndCreatedTime.HasValue, w => w.CreatedTime <= input.EndCreatedTime) .WhereIf(input.StartCreatedTime.HasValue, w => w.CreatedTime >= input.StartCreatedTime) .ToListAsync(); notClockListDto.ViolationCount = earlyList.Count; notClockList.Add(notClockListDto); } } var pageData = notClockList.Skip((input.PageIndex - 1) * input.PageSize) .Take(input.PageSize).ToList(); var pageResult = new PagedList() { TotalCount = notClockList.Count(), Data = pageData }; return ResultOutput.Ok(pageResult); } /// /// 未打卡统计导出 /// /// /// public async Task NotClockedStatisticsExportAsync(NotClockedInput input) { //获取当前用户权限下的案件ids var limits = User.limits; var selectLimits = await _appCaseSupervisorRepository.AsQueryable(false, true) .Where(w => limits.Contains(w.UnitId.ToString())) .ToListAsync(); var caseIdList = selectLimits.Select(w => w.CaseId).Distinct().ToList(); var caseList = await _appCaseManagementRepository.AsQueryable(false, true) .Where(w => caseIdList.Contains(w.Id)) .WhereIf(input.CaseId.HasValue, w => w.Id == input.CaseId) .ToListAsync(); var applicationType = await base.GetDictionariesOutput("early_warning_type", "notClocked"); List notClockList = new List(); foreach (var item in caseList) { if (item == null) continue; var caseType = await _appDictionaryService.GetDicByDicId(item.CaseTypeId); NotClockExportDto notClockListDto = new NotClockExportDto(); notClockListDto.Bmsah = item.Bmsah; notClockListDto.CaseId = item.Id; notClockListDto.CaseName = item.Name; notClockListDto.CreatedTime = item.CreatedTime; notClockListDto.CaseTypeName = caseType.Name; notClockListDto.SupervisionUnit = ""; //根据案件找到被监管 var supervisedPerson = await _appSupervisedPersonRepository .AsQueryable(false, true) .Where(w => w.CaseId == item.Id) .ToListAsync(); foreach (var person in supervisedPerson) { notClockListDto.SupervisedPersonId = person.SupervisedPersonId; notClockListDto.SupervisedPersonName = person.SupervisedPersonName; var earlyList = await _appEarlyWarningRepository .AsQueryable(false, true) .Where(w => w.EarlyWarningTypeId== applicationType.Id && w.SupervisedPersonId == person.SupervisedPersonId && w.CaseId == item.Id) .WhereIf(input.EndCreatedTime.HasValue, w => w.CreatedTime <= input.EndCreatedTime) .WhereIf(input.StartCreatedTime.HasValue, w => w.CreatedTime >= input.StartCreatedTime) .ToListAsync(); notClockListDto.NotClockedCount = earlyList.Count; notClockList.Add(notClockListDto); } } var memorySystem = ExcelHelper.ToExcel(notClockList); return new FileController().File(memorySystem.ToArray(), "application/ms-excel", DateTime.Now.ToString("f") + ".xlsx"); } /// /// 添加违规记录 /// /// /// public async Task AddAsync(AppViolationStatisticsAddInput input) => await base.AddAsync(input, _appViolationStatisticsRepository); } public class FileController : ControllerBase { } }