Files
jinko.yikatong/Src/Dlhis.Basic/WorkGrids/Services/Implements/WorkGridService.cs
2025-07-11 23:07:13 +08:00

283 lines
12 KiB
C#

using Dlhis.Basic.Common.ApiLog.Services;
using Dlhis.Basic.WorkGrids.Repositories;
using Dlhis.Basic.WorkGrids.Request;
using Dlhis.Basic.WorkGrids.Response;
using Dlhis.Basic.WorkGrids.Validations;
using Dlhis.Constants;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Common.Enums;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
namespace Dlhis.Basic.WorkGrids.Services.Implements;
public class WorkGridService : IWorkGridService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly IWorkGridValidateService _workGridValidateService;
private readonly IWorkGridRepository _workGridRepository;
private readonly IWorkGridScheduleRepository _workGridScheduleRepository;
private readonly IOperateLog _operateLog;
public WorkGridService(IDbContext dbContext,
ICurrentUser currentUser,
IWorkGridValidateService workGridValidateService,
IWorkGridRepository workGridRepository,
IWorkGridScheduleRepository workGridScheduleRepository,
IOperateLog operateLog)
{
_dbContext = dbContext;
_currentUser = currentUser;
_workGridValidateService = workGridValidateService;
_workGridRepository = workGridRepository;
_workGridScheduleRepository = workGridScheduleRepository;
_operateLog = operateLog;
}
public async Task<Pagination<WorkGridGainfoPageResponse>> GetUserSelectionPageAsync(WorkGridGainfoPageRequest request)
{
var pagination = await _workGridRepository.GetUserSelectionPageAsync(request);
var userCodeList = pagination.Data.Select(x => x.Code).ToList();
if (userCodeList.Any())
{
var maximumConsecutiveUserCodeList = await _workGridScheduleRepository.MaximumConsecutiveUserCodesAsync(userCodeList, 1, DateOnly.FromDateTime(request.Date));
foreach (var item in pagination.Data)
{
item.MaximumConsecutiveSchedules = maximumConsecutiveUserCodeList.FirstOrDefault(x => x.UserCode == item.Code);
}
}
return pagination;
}
public async Task SaveAsync(List<WorkGridSaveRequest> requests)
{
await _workGridValidateService.ValidateSaveAsync(requests);
var insertGrids = new List<string>();
var updateGrids = new List<string>();
foreach (var request in requests)
{
if (!request.Id.HasValue)
{
var exists = await _dbContext.Queryable<WorkGridEntity>()
.Where(x => x.LeaderUserCode == request.LeaderUserCode)
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
.AnyAsync();
exists.ThrowIfTrue($"{request.LeaderUserCode}人员存在管理中的网格,无法添加");
var entity = request.Adapt<WorkGridEntity>();
entity.Code = Guid.NewGuid();
entity.LeaderUserCode = request.LeaderUserCode;
await _dbContext.InsertAsync(entity);
insertGrids.Add($"{entity.Name}({entity.LeaderUserCode}),最大用户数:{entity.MaxUserTipCount}");
}
else
{
var entity = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.Id == request.Id).SingleAsync();
entity.ThrowIfNull("网格不存在");
if (request.LeaderUserCode != entity.LeaderUserCode)
{
await ChangeAsync(entity.LeaderUserCode, request.LeaderUserCode);
}
updateGrids.Add($"{entity.Name}->{request.Name}," +
$"排班用户数{entity.MaxUserTipCount}->{request.MaxUserTipCount}," +
$"连续排班天数:{entity.MaxConsecutiveTipCount}->{request.MaxConsecutiveTipCount}," +
$"最大排班限制:{entity.MaxConsecutiveTipCount}->{request.MaxConsecutiveTipCount}," +
$"别名:{entity.Alias}->{request.Alias}," +
$"编制人数{entity.Count}->{request.Count}");
request.Adapt(entity);
entity.LeaderUserCode = request.LeaderUserCode;
await _dbContext.UpdateAsync(entity);
}
}
await _operateLog.LogAsync(OperateLogType.WorkGrid, "创建网格:" + insertGrids.Join("\r\n"));
await _operateLog.LogAsync(OperateLogType.WorkGrid, "修改网格:" + updateGrids.Join("\r\n"));
}
public async Task<Pagination<WorkGridPageResponse>> GetPageAsync(WorkGridPageRequest request)
{
var pagination = await _workGridRepository.GetPageAsync(request);
if (request.EnableShowDateScheduleDetails)
{
var workGridCodes = pagination.Data.Select(wg => wg.Code).ToList();
if (workGridCodes.Any())
{
var date = DateOnly.FromDateTime(request.Date);
foreach (var workGridCode in workGridCodes)
{
var scheduleSummaryResponses = await _workGridScheduleRepository.GetListSummaryAsync(workGridCode, date, date);
pagination.Data.First(x => x.Code == workGridCode).DateScheduleSummary = scheduleSummaryResponses.FirstOrDefault();
}
}
}
return pagination;
}
public async Task ChangeAsync(string from, string to)
{
if (await _dbContext.Queryable<WorkGridEntity>().Where(x => x.LeaderUserCode == to).AnyAsync())
{
true.ThrowIfTrue($"{to}已存在网格,不可操作");
}
var workGrid = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.LeaderUserCode == from).SingleAsync();
workGrid.ThrowIfNull($"{from}管理的网格不存在");
var beforeGainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == from).SingleAsync();
beforeGainfo ??= await _dbContext.Queryable<GainfoLeaveEntity>().Where(x => x.Code == from).SingleAsync();
beforeGainfo.ThrowIfNull($"{from}人员不存在");
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == to).SingleAsync();
gainfo.ThrowIfNull($"{to}人员不存在");
(gainfo.Factory != beforeGainfo.Factory).ThrowIfTrue($"必须选择相同厂区({beforeGainfo.Factory})人员管理({gainfo.Factory}错误)");
workGrid.LeaderUserCode = to;
await _dbContext.UpdateAsync(workGrid);
await _operateLog.LogAsync(OperateLogType.WorkGrid, $"网格({workGrid.Name})修改负责人{from}->{to}");
}
public async Task DismissAsync(Guid workGridCode)
{
var workGrid = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.Code == workGridCode).SingleAsync();
workGrid.ThrowIfNull("工作网格不存在");
var existsUser = await _dbContext.Queryable<WorkGridUserEntity>().Where(x => x.WorkGridCode == workGridCode).AnyAsync();
existsUser.ThrowIfTrue("工作网格下存在人员,请先移除人员");
await _dbContext.DeleteAsync(workGrid);
await _operateLog.LogAsync(OperateLogType.WorkGrid, $"解散网格({workGrid.Name})");
}
public async Task<WorkGridUserPageResponse> GetMyWorkGridAsync(WorkGridPageRequest request)
{
var currentUserCode = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode);
var myWorkGrid = await _dbContext.Queryable<WorkGridEntity>()
.WhereIF(request.WorkGridCode.HasValue == false, x => x.LeaderUserCode == currentUserCode)
.WhereIF(request.WorkGridCode.HasValue, x => x.Code == request.WorkGridCode)
.Select(x => new WorkGridUserPageResponse
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
Remark = x.Remark,
MaxUserTipCount = x.MaxUserTipCount,
MaxConsecutiveTipCount = x.MaxConsecutiveTipCount,
MaxConsecutiveLimitCount = x.MaxConsecutiveLimitCount,
Alias = x.Alias,
Count = x.Count
})
.SingleAsync();
if (myWorkGrid != null)
{
myWorkGrid.UserCount = await _dbContext.Queryable<WorkGridUserEntity>().Where(x => x.WorkGridCode == myWorkGrid.Code).CountAsync();
myWorkGrid.UserInfos = await _dbContext.Queryable<WorkGridUserEntity>()
.LeftJoin<GainfoEntity>((x, ga) => x.UserCode == ga.Code)
.Where((x, ga) => x.WorkGridCode == myWorkGrid.Code)
.WhereIF(request.Search.IsPresent(), (x, ga) => x.UserCode == request.Search || ga.Name.Contains(request.Search))
.Select((x, ga) => new WorkGridUserPageDetailResponse
{
Code = x.UserCode,
Name = ga.Name
}).ToListAsync();
var userCodes = myWorkGrid.UserInfos.Select(x => x.Code).ToList();
if (userCodes.Any())
{
var maximumConsecutiveUserCodeList = await _workGridScheduleRepository.MaximumConsecutiveUserCodesAsync(userCodes, 1, DateOnly.FromDateTime(request.Date));
foreach (var item in myWorkGrid.UserInfos)
{
item.MaximumConsecutiveSchedules = maximumConsecutiveUserCodeList.FirstOrDefault(x => x.UserCode == item.Code);
}
}
}
return myWorkGrid;
}
public async Task AssignAsync(WorkGridUserAssignRequest request)
{
var currentFactory = _currentUser.GetStringTenantId();
var role = _currentUser.GetRole();
var specific = (role is "admin" or "tenant-admin" or "WGZGLY") && request.WorkGridCode.HasValue;
var currentUserCode = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode);
var myWorkGrid = await _dbContext.Queryable<WorkGridEntity>()
.WhereIF(specific, x => x.Code == request.WorkGridCode)
.WhereIF(specific == false, x => x.LeaderUserCode == currentUserCode)
.SingleAsync();
myWorkGrid.ThrowIfNull("工作网格尚未配置,请联系管理员");
var existsAssigned = await _dbContext.Queryable<WorkGridUserEntity>()
.Where(x => request.UserCodes.Contains(x.UserCode))
.AnyAsync();
existsAssigned.ThrowIfTrue("存在已经分配人员,操作失败!");
//检查配置的人员
var count = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.Factory == currentFactory)
.Where(x => request.UserCodes.Contains(x.Code))
.CountAsync();
(count != request.UserCodes.Count).ThrowIfTrue("存在非当前厂区人员,请检查");
var dataToInserts = request.UserCodes.Select(x => new WorkGridUserEntity
{
WorkGridCode = myWorkGrid.Code,
UserCode = x
}).ToList();
await _dbContext.InsertAsync(dataToInserts);
await _operateLog.LogAsync(OperateLogType.WorkGrid, $"分配人员到网格({myWorkGrid.Name}),人员:{dataToInserts.Select(x => x.UserCode).Join(",")}");
}
public async Task RemoveAsync(WorkGridUserRemoveRequest request)
{
var role = _currentUser.GetRole();
var specific = (role is "admin" or "tenant-admin" or "WGZGLY") && request.WorkGridCode.HasValue;
var currentUserCode = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode);
var myWorkGrid = await _dbContext.Queryable<WorkGridEntity>()
.WhereIF(specific, x => x.Code == request.WorkGridCode)
.WhereIF(specific == false, x => x.LeaderUserCode == currentUserCode)
.SingleAsync();
var workGridUsers = await _dbContext.Queryable<WorkGridUserEntity>()
.Where(x => x.WorkGridCode == myWorkGrid.Code)
.Where(x => request.UserCodes.Contains(x.UserCode))
.ToListAsync();
var date = DateOnly.FromDateTime(DateTime.Today);
var workGridSchedules = await _dbContext.Queryable<WorkGridScheduleEntity>()
.Where(x => x.Date > date)
.Where(x => x.WorkGridCode == myWorkGrid.Code)
.Where(x => request.UserCodes.Contains(x.UserCode))
.ToListAsync();
await _dbContext.DeleteAsync(workGridUsers);
await _dbContext.DeleteAsync(workGridSchedules);
await _operateLog.LogAsync(OperateLogType.WorkGrid, $"从网格({myWorkGrid.Name})移除人员:{request.UserCodes.Join(",")}");
}
}