feat 人员任务重发
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
using Dlhis.Basic.BusinessPlace.Response;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Basic.Device.Services;
|
||||
using Dlhis.Basic.Organization.Request;
|
||||
using Dlhis.Basic.Organization.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Canteen.Entities;
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Basic.Organization.Services.Implements;
|
||||
|
||||
public class GainfoPermissionService : IGainfoPermissionService
|
||||
{
|
||||
private readonly IGainfoStore _gainfoStore;
|
||||
private readonly IDevicePermissionService _devicePermissionService;
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public GainfoPermissionService(IGainfoStore gainfoStore, IDevicePermissionService devicePermissionService, IDbContext dbContext)
|
||||
{
|
||||
_gainfoStore = gainfoStore;
|
||||
_devicePermissionService = devicePermissionService;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<Pagination<BusinessPlaceResponse>> GetPlaceByGainfoAsync(DeviceAssRequest request)
|
||||
{
|
||||
var gainfo = await _gainfoStore.Queryable()
|
||||
.Where(x => x.No == request.UserNo)
|
||||
.FirstAsync();
|
||||
|
||||
if (gainfo == null)
|
||||
{
|
||||
return new Pagination<BusinessPlaceResponse>();
|
||||
}
|
||||
|
||||
//用户所有deviceCode权限
|
||||
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(gainfo.No);
|
||||
|
||||
return await _dbContext.Queryable<DeviceEntity>()
|
||||
.LeftJoin<DeviceRegistryEntity>((x, dr) => x.UniqueNo == dr.DeviceUniqueNo)
|
||||
.LeftJoin<BusinessPlaceEntity>((x, dr, place) => dr.BusinessPlaceCode == place.Code)
|
||||
.LeftJoin<BusinessPlaceShiftRelationshipEntity>((x, dr, place, ps) => x.Code == ps.PlaceCode)
|
||||
.LeftJoin<BusinessShiftEntity>((x, dr, place, ps, s) => s.Code == ps.ShiftCode)
|
||||
.Where(x => x.Type == DeviceTypeEnumType.Meal)
|
||||
.Where(x => deviceCodes.Contains(x.Code))
|
||||
.Where((x, dr) => !string.IsNullOrEmpty(dr.BusinessPlaceCustomCode))
|
||||
.Select((x, dr, place, ps, s) => new BusinessPlaceResponse
|
||||
{
|
||||
Id = place.Id,
|
||||
Code = place.Code,
|
||||
Status = place.Status,
|
||||
Name = place.Name,
|
||||
Catalog = place.Catalog,
|
||||
CustomCode = place.CustomCode,
|
||||
Source = place.Source,
|
||||
|
||||
ShiftCode = s.Code,
|
||||
ShiftName = s.Name
|
||||
})
|
||||
.Distinct()
|
||||
.ToPageListAsync(request);
|
||||
}
|
||||
|
||||
public async Task<List<GetDevicesUnderGroupByUserNoResponse>> GetDevicesUnderGroupByUserNoAsync(string userNo, Guid deviceGroupCode)
|
||||
{
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||
var currentGroup = deviceGroups.Where(x => x.Code == deviceGroupCode).FirstOrDefault();
|
||||
if (currentGroup == null)
|
||||
{
|
||||
return new List<GetDevicesUnderGroupByUserNoResponse>();
|
||||
}
|
||||
|
||||
var groups = RecursionGetNext(currentGroup, deviceGroups).ToList();
|
||||
|
||||
//用户所有deviceCode权限
|
||||
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(userNo);
|
||||
|
||||
var lst = new List<GetDevicesUnderGroupByUserNoResponse>();
|
||||
foreach (var groupcode in groups)
|
||||
{
|
||||
var nos = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||
.LeftJoin<DeviceEntity>((x, d) => x.DeviceCode == d.Code)
|
||||
.Where(x => groupcode == x.GroupCode)
|
||||
.Select((x, d) => d.UniqueNo)
|
||||
.ToListAsync();
|
||||
|
||||
var page = await _dbContext.Queryable<DeviceEntity>()
|
||||
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
|
||||
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
|
||||
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
|
||||
.Where(d => nos.Contains(d.UniqueNo))
|
||||
.Select((d, dt, dr, bp) => new DevicePageResponse
|
||||
{
|
||||
FuncsStr = d.Funcs.ToString(),
|
||||
Brand = dt.Brand,
|
||||
Type = d.Type,
|
||||
BusinessPlaceCode = bp.Code,
|
||||
BusinessPlaceName = bp.Name,
|
||||
LastHeartbeatTime = dr.LastHeartbeatTime,
|
||||
UniqueNo = d.UniqueNo,
|
||||
Supplier = d.Supplier,
|
||||
DeployPlace = d.DeployPlace,
|
||||
DeployPosition = d.DeployPosition,
|
||||
DeployDirection = d.DeployDirection,
|
||||
TemplateCode = dt.Code,
|
||||
ConnectionInfo = d.ConnectionInfo,
|
||||
Enable = d.Enable,
|
||||
})
|
||||
.ToListAsync();
|
||||
var deviceUniqueNos = page.Select(x => x.UniqueNo).ToList();
|
||||
|
||||
var balanceInfos = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
|
||||
.Select(x => new
|
||||
{
|
||||
x.DeviceUniqueNo,
|
||||
Amount = SqlFunc.AggregateSum(x.Amount),
|
||||
UserCount = SqlFunc.AggregateDistinctCount(x.UserCode)
|
||||
})
|
||||
.GroupBy(x => x.DeviceUniqueNo)
|
||||
.ToListAsync();
|
||||
|
||||
page.ForEach(device =>
|
||||
{
|
||||
var info = balanceInfos.FirstOrDefault(x => x.DeviceUniqueNo == device.UniqueNo);
|
||||
if (info != null)
|
||||
{
|
||||
device.Amount = info.Amount;
|
||||
device.UserCount = info.UserCount;
|
||||
}
|
||||
});
|
||||
|
||||
var res = new GetDevicesUnderGroupByUserNoResponse
|
||||
{
|
||||
GroupCode = groupcode,
|
||||
Devices = page
|
||||
};
|
||||
lst.Add(res);
|
||||
}
|
||||
return lst;
|
||||
}
|
||||
|
||||
public async Task<Pagination<DevicePageResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request)
|
||||
{
|
||||
var ids = await _devicePermissionService.GetAssignDeviceCodesAsync(request.UserNo);
|
||||
|
||||
var page = await _dbContext.Queryable<DeviceEntity>()
|
||||
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
|
||||
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
|
||||
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
|
||||
.WhereIF(request.DeviceType.HasValue, d => d.Type == request.DeviceType)
|
||||
.Where(d => ids.Contains(d.Code))
|
||||
.Select((d, dt, dr, bp) => new DevicePageResponse
|
||||
{
|
||||
FuncsStr = d.Funcs.ToString(),
|
||||
Brand = dt.Brand,
|
||||
Type = d.Type,
|
||||
BusinessPlaceCode = bp.Code,
|
||||
BusinessPlaceName = bp.Name,
|
||||
LastHeartbeatTime = dr.LastHeartbeatTime
|
||||
}, true)
|
||||
.ToPageListAsync(request);
|
||||
|
||||
var deviceUniqueNos = page.Data.Select(x => x.UniqueNo).ToList();
|
||||
|
||||
var balanceInfos = await _dbContext.Queryable<BalanceRecordEntity>().Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
|
||||
.Select(x => new
|
||||
{
|
||||
x.DeviceUniqueNo,
|
||||
Amount = SqlFunc.AggregateSum(x.Amount),
|
||||
UserCount = SqlFunc.AggregateDistinctCount(x.UserCode)
|
||||
})
|
||||
.GroupBy(x => x.DeviceUniqueNo)
|
||||
.ToListAsync();
|
||||
|
||||
page.Data.ForEach(device =>
|
||||
{
|
||||
var info = balanceInfos.FirstOrDefault(x => x.DeviceUniqueNo == device.UniqueNo);
|
||||
if (info != null)
|
||||
{
|
||||
device.Amount = info.Amount;
|
||||
device.UserCount = info.UserCount;
|
||||
}
|
||||
});
|
||||
|
||||
return page;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public async Task ResetPermissionsAsync(GainfoPermissionResetRequest request)
|
||||
{
|
||||
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.No == request.UserNo).SingleAsync();
|
||||
gainfo.ThrowIfNull("档案数据不存在");
|
||||
|
||||
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(request.UserNo);
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceCodes.Contains(x.Code)).ToListAsync();
|
||||
|
||||
var tasks = devices.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
CancellationId = 0,
|
||||
Version = 0,
|
||||
DeviceUniqueNo = x.UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
UserNo = request.UserNo,
|
||||
Type = DeviceTaskEnumType.Add,
|
||||
TenantId = gainfo.Factory,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await _dbContext.InsertAsync(tasks);
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<DeviceGroupTreeResponse>> GetAssignGroupAsync(string userNo)
|
||||
{
|
||||
var groups = await _devicePermissionService.GetAssignmentsAsync(DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, userNo);
|
||||
return await _dbContext.Queryable<DeviceGroupEntity>()
|
||||
.Where(x => groups.Contains(x.Code))
|
||||
.Select<DeviceGroupTreeResponse>()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
private static IEnumerable<Guid> RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups)
|
||||
{
|
||||
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
|
||||
foreach (var item in next)
|
||||
{
|
||||
RecursionGetNext(item, deviceGroups);
|
||||
}
|
||||
|
||||
yield return current.Code;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user