using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; using Dlhis.Entity.Sys.Entities; using Dlhis.Entity.Sys.Enums; using Dlhis.Entity.Visitor.Entity; using Dlhis.Entity.Visitor.Enums; using Dlhis.Jip.Api; using Dlhis.Jip.Api.Responses; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; using Mapster; using SqlSugar; namespace Dlhis.Basic.Organization.Services.Implements; public class SapBusinessTripService : ISapBusinessTripService { private readonly IJinkoJipApi _jinkoJipApi; private readonly IDbContext _dbContext; private readonly IGainfoPermissionService _gainfoPermissionService; private readonly ILogger _logger; public SapBusinessTripService(IJinkoJipApi jinkoJipApi, IDbContext dbContext, IGainfoPermissionService gainfoPermissionService, ILogger logger) { _jinkoJipApi = jinkoJipApi; _dbContext = dbContext; _gainfoPermissionService = gainfoPermissionService; _logger = logger; } /// public async Task HandlerBusinessTripsByFactoryAsync(string factory) { var today = DateTime.Today; var yesterday = today.AddDays(-1); try { var tripResponse = await _jinkoJipApi.GetBusinessTripInfosByFactoryAsync(factory, today, today); //先写入BusinessTripEntity表,保留原始数据 await PersistBusinessTripDataAsync(factory, tripResponse); //获取昨天和今天的出差数据 var tripInfos = await GetHandleBusinessTripDataAsync(factory, yesterday, today); //获取分组信息 var tripGroup = await GetTripGroupAsync(factory); var deviceCodes = await GetTripGroupUnderDeviceCodesAsync(tripGroup.Code); if (deviceCodes.Any()) { //排除当前记录存例外的人员 var manuallyUserCodes = await _dbContext.Queryable() .LeftJoin((x, ga) => x.UserNo == ga.No) .Where(x => x.TenantId == factory) .Select((x, ga) => ga.Code) .Distinct() .ToListAsync(); var userCodes = tripInfos.Select(x => x.Code).Distinct().Where(x => !manuallyUserCodes.Contains(x)).ToList(); var gainfos = await _dbContext.Queryable() .Where(x => x.Factory != factory) //排除本基地出差 .Where(x => userCodes.Contains(x.Code)) .Select(x => new { x.No, x.Code }) .ToListAsync(); foreach (var gainfo in gainfos) { try { var myTripInfos = tripInfos.Where(x => x.Code == gainfo.Code).ToList(); var yesterdayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == yesterday && x.EndDate == yesterday); //昨天出差数据 var todayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == today && x.EndDate == today); //今天出差数据 //都为空不处理 if (yesterdayTripInfo == null && todayTripInfo == null) continue; //都不为空则更新数据状态 if (yesterdayTripInfo != null && todayTripInfo != null) { todayTripInfo.Status = BusinessTripStatusEnumType.Generated; await _dbContext.UpdateAsync(todayTripInfo); //下发 await _gainfoPermissionService.ResetPermissionsAsync(gainfo.No, DeviceTaskEnumType.Add, null, deviceCodes, "出差昨天今天都有"); continue; } if (yesterdayTripInfo != null) { //todayTripInfo == null yesterdayTripInfo.Status = BusinessTripStatusEnumType.Deleted; await _dbContext.Updateable() .SetColumns(x => x.IsDeleted == true) .Where(x => x.AssignType == AssignTypeEnumType.User) .Where(x => x.AssignValue == gainfo.No) .Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group) .Where(x => x.UniqueValue == tripGroup.Code) .Where(x => x.Type == tripGroup.Type) .Where(x => x.TenantId == factory) .ExecuteCommandAsync(); await _dbContext.UpdateAsync(yesterdayTripInfo); await _gainfoPermissionService.ResetPermissionsAsync(gainfo.No, DeviceTaskEnumType.Delete, null, deviceCodes, "出差昨天有今天无"); } if (yesterdayTripInfo == null) { //todayTripInfo != null if (!await _dbContext.Queryable() .Where(x => x.AssignType == AssignTypeEnumType.User) .Where(x => x.AssignValue == gainfo.No) .Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group) .Where(x => x.UniqueValue == tripGroup.Code) .Where(x => x.Type == tripGroup.Type) .Where(x => x.TenantId == factory && x.IsDeleted == false) .AnyAsync()) { await _dbContext.InsertAsync(new DevicePermissionEntity { AssignType = AssignTypeEnumType.User, AssignValue = gainfo.No, UniqueType = DeviceAssignmentTypeEnumType.Group, UniqueValue = tripGroup.Code, Type = tripGroup.Type, TenantId = factory }); } todayTripInfo.Status = BusinessTripStatusEnumType.Generated; await _dbContext.UpdateAsync(todayTripInfo); await _gainfoPermissionService.ResetPermissionsAsync(gainfo.No, DeviceTaskEnumType.Add, null, deviceCodes, "出差昨天无今天有"); } } catch (Exception e) { _logger.LogError(e, "出差自动下发任务失败:{message}", e.Message); } } } } catch (Exception e) { _logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message); } } /// /// 持久化数据 /// private async Task PersistBusinessTripDataAsync(string factory, SapBusinessTripInfoResponse tripResponse) { foreach (var tripInfoItem in tripResponse.Items.Where(x => x.Destination.IsPresent())) { if (await _dbContext.Queryable().Where(x => x.Code == tripInfoItem.Code && x.BeginDate == tripInfoItem.BeginDate && x.EndDate == tripInfoItem.EndDate && x.Destination == factory).AnyAsync()) { continue; } var entity = tripInfoItem.Adapt(); entity.Status = BusinessTripStatusEnumType.Wait; await _dbContext.InsertAsync(entity); } } private async Task> GetHandleBusinessTripDataAsync(string factory, DateTime yesterday, DateTime today) { return await _dbContext.Queryable() .Where(x => SqlFunc.DateIsSame(x.BeginDate, yesterday) || SqlFunc.DateIsSame(x.BeginDate, today)) .Where(x => !SqlFunc.IsNullOrEmpty(x.Destination)) //必须有目的地 //.Where(x => !SqlFunc.IsNullOrEmpty(x.BeginDateTime) && !SqlFunc.IsNullOrEmpty(x.EndDateTime)) .Where(x => x.Destination == factory) .ToListAsync(); } private async Task GetTripGroupAsync(string factory) { return await _dbContext.Queryable() .Where(x => x.Type == DeviceTypeEnumType.Door) .Where(x => x.TenantId == factory) .Where(x => x.IsBusinessTrip) .Where(x => x.IsDeleted == false) .SingleAsync(); } private async Task> GetTripGroupUnderDeviceCodesAsync(Guid tripGroupCode) { return await _dbContext.Queryable() .Where(x => x.GroupCode == tripGroupCode) .Where(x => x.IsDeleted == false) .Select(x => x.DeviceCode) .ToListAsync(); } }