using ATS.NonCustodial.Application.Base;
using ATS.NonCustodial.Application.Contracts.Interfaces.Business.AppCaseManagements.AppSupervisedPersons;
using ATS.NonCustodial.Application.Contracts.Interfaces.Business.AppCaseManagements.AppSupervisedPersons.Input;
using ATS.NonCustodial.Domain.Entities.Business.CaseManagements;
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.UnifiedResults;
using Microsoft.EntityFrameworkCore;
using Yitter.IdGenerator;
namespace ATS.NonCustodial.Application.Impl.Business.CaseManagements
{
///
/// 被监管人员管理服务
///
/// Author:mxg
/// CreatedTimed:2022-06-06 11:16 AM
///
/// 1、姓名可模糊搜索系统用户管理中用户类型为“被监管人”的数据,展示“姓名_手机号”,选择后可作为填写数据的默认值,用户还是可以在此基础上进行更改。
/// 2、预警类型等级根据案件类型来展示,在案件类型字典数据中取数据。
/// 3、打卡频次的数据可输入小数,保留一位小数。在被监管人在非休息时间内打卡时间间隔超过设置的间隔,则需要进行预警,提示XXX未打卡。
/// 4、隐私等级的时间是在监控大屏展示该人员的位置信息,是在设置的时间之前,如设置为120min,则大屏展示的该人员位置信息展示的是120分钟前的位置。
/// 5、被监管人对应的未结束的案件,只能是一个。提交案件创建时进行校验判定。
///
[DynamicApi(Area = "admin")]
public class AppSupervisedPersonService : AdminAppServiceBase, IAppSupervisedPersonService, IDynamicApi
{
#region Identity
private readonly IEfRepository _appSupervisedPersonRepository;
private readonly IEfRepository _appCaseManagementEfRepository;
private readonly IEfRepository _appauditrecordsRepository;
///
///
///
///
///
public AppSupervisedPersonService(IEfRepository appSupervisedPersonRepository,
IEfRepository appCaseManagementEfRepository, IEfRepository appauditrecords)
{
_appSupervisedPersonRepository = appSupervisedPersonRepository;
_appCaseManagementEfRepository = appCaseManagementEfRepository;
_appauditrecordsRepository = appauditrecords;
}
#endregion Identity
///
/// 批量审核 被监管人员账号绑定
///
///
///
public async Task BatchAuditSupervisedPersonBinding(BatchAuditSupervisedPersonBindingInput input)
{
if (!input.Ids.Any()) return ResultOutput.NotOk("参数不能为空!");
var allCaseIds = new List();
var dataList = await _appSupervisedPersonRepository
.Where(w => input.Ids.Contains(w.SupervisedPersonId) && w.ApprovalStatus != ApprovalStatusEnum.PassReview)
.ToListAsync();
dataList.ForEach(item =>
{
item!.ApprovalStatus = input.ApprovalStatus;
item.IsBound = input.ApprovalStatus == ApprovalStatusEnum.PassReview;
allCaseIds.Add(item.CaseId);
});
await _appSupervisedPersonRepository.UpdateAsync(dataList, UpdatingProps(w =>
w.ApprovalStatus,
w => w.IsBound));
#region 添加被监管人审核记录
var data = new List();
foreach (var item in dataList)
{
data.Add(new appauditrecords
{
Id = YitIdHelper.NextId(),
SupervisedPersonId = item.SupervisedPersonId,
recordstime = DateTime.Now,
CreatedTime = DateTime.Now,
});
}
if(data.Count>0)
await _appauditrecordsRepository.InsertAsync(data);
#endregion
//更改案件的状态
allCaseIds = allCaseIds.Distinct().ToList();
var spCaseList = await _appSupervisedPersonRepository.AsQueryable(false, true)
.Where(w => allCaseIds.Contains(w.CaseId))
.ToListAsync();
var spCaseGroup = spCaseList.GroupBy(w => w.CaseId).ToList();
/*
1、当案例已在的被监管人 只要有审核通过的。案件变更位已经行
2、如果说 我把这个审核通过的删除了,其余被监管人里没有审核通过的 则变更位待进行
3、如果只存在1个被监管人则不能被删除,且创建案件和编辑案件的时候 被监管人必须存在
*/
var caseProgress = new Dictionary();
foreach (var scg in spCaseGroup)
{
var existPass = await _appSupervisedPersonRepository.AnyAsync(w => w.CaseId == scg.Key && w.ApprovalStatus == ApprovalStatusEnum.PassReview);
switch (existPass)
{
//如果有任何审核通过的案件都将变为执行中
case true:
caseProgress.TryAdd(scg.Key, CaseProgressEnum.InExecution);
break;
case false:
caseProgress.TryAdd(scg.Key, CaseProgressEnum.Pending);
break;
}
}
//修改状态
foreach (var (key, value) in caseProgress)
{
await _appCaseManagementEfRepository.UpdateAsync(w => w.Id == key,
w => new AppCaseManagement()
{
CaseProgress = value,
CaseBeginTime = value == CaseProgressEnum.InExecution ? DateTime.Now : null
});
}
return ResultOutput.Ok();
}
}
}