154 lines
5.7 KiB
C#
154 lines
5.7 KiB
C#
using Dlhis.Basic.WorkGrids.Request;
|
|
using Dlhis.Basic.WorkGrids.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Basic.WorkGrids.Services.Implements;
|
|
|
|
public class WorkGridService : IWorkGridService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
public WorkGridService(IDbContext dbContext, ICurrentUser currentUser)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task SaveAsync(List<WorkGridSaveRequest> requests)
|
|
{
|
|
foreach (var request in requests)
|
|
{
|
|
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}已存在管理的网格");
|
|
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<WorkGridEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.Id == request.Id).SingleAsync();
|
|
request.Adapt(entity);
|
|
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Pagination<WorkGridPageResponse>> GetPageAsync(WorkGridPageRequest request)
|
|
{
|
|
return await _dbContext.Queryable<WorkGridEntity>()
|
|
.LeftJoin<GainfoEntity>((x, ga) => x.LeaderUserCode == ga.Code)
|
|
.Select((x, ga) => new WorkGridPageResponse
|
|
{
|
|
Id = x.Id,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
LeaderUserCode = x.LeaderUserCode,
|
|
UserName = ga.Name,
|
|
Remark = x.Remark
|
|
})
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task ChangeAsync(string from, string to)
|
|
{
|
|
var workGrid = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.LeaderUserCode == from).SingleAsync();
|
|
workGrid.ThrowIfNull("网格不存在");
|
|
|
|
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == to).SingleAsync();
|
|
gainfo.ThrowIfNull($"{to}人员不存在");
|
|
|
|
workGrid.LeaderUserCode = to;
|
|
await _dbContext.UpdateAsync(workGrid);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<WorkGridUserPageResponse> GetMyWorkGridAsync(WorkGridPageRequest request)
|
|
{
|
|
var currentUserCode = _currentUser.GetUserClaim("jinko-code");
|
|
var myWorkGrid = await _dbContext.Queryable<WorkGridEntity>()
|
|
.Where(x => x.LeaderUserCode == currentUserCode)
|
|
.Select(x => new WorkGridUserPageResponse
|
|
{
|
|
Id = x.Id,
|
|
Code = x.Code,
|
|
Name = x.Name,
|
|
Remark = x.Remark,
|
|
UserInfos = SqlFunc.Subqueryable<WorkGridUserEntity>()
|
|
.LeftJoin<GainfoEntity>((p, ga) => p.UserCode == ga.Code)
|
|
.Where((p, ga) => p.WorkGridCode == x.Code)
|
|
.ToList((p, ga) => new WorkGridUserPageDetailResponse
|
|
{
|
|
Code = p.UserCode,
|
|
Name = ga.Name
|
|
})
|
|
})
|
|
.SingleAsync();
|
|
|
|
return myWorkGrid;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task AssignAsync(WorkGridUserAssignRequest request)
|
|
{
|
|
var currentFactory = _currentUser.GetStringTenantId();
|
|
var currentUserCode = _currentUser.GetUserClaim("jinko-code");
|
|
|
|
var myWorkGrid = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.LeaderUserCode == currentUserCode).SingleAsync();
|
|
myWorkGrid.ThrowIfNull("工作网格尚未配置,请联系管理员");
|
|
|
|
//检查配置的人员
|
|
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 existsAssigned = await _dbContext.Queryable<WorkGridUserEntity>().Where(x => x.WorkGridCode == myWorkGrid.Code)
|
|
.Where(x => request.UserCodes.Contains(x.UserCode))
|
|
.AnyAsync();
|
|
existsAssigned.ThrowIfTrue("存在已经分配人员,请勿重复分配");
|
|
|
|
var dataToInserts = request.UserCodes.Select(x => new WorkGridUserEntity
|
|
{
|
|
WorkGridCode = myWorkGrid.Code,
|
|
UserCode = x
|
|
}).ToList();
|
|
|
|
await _dbContext.InsertAsync(dataToInserts);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task RemoveAsync(WorkGridUserRemoveRequest request)
|
|
{
|
|
var currentUserCode = _currentUser.GetUserClaim("jinko-code");
|
|
var myWorkGrid = await _dbContext.Queryable<WorkGridEntity>().Where(x => x.LeaderUserCode == currentUserCode).SingleAsync();
|
|
myWorkGrid.ThrowIfNull("工作网格尚未配置,请联系管理员");
|
|
|
|
var workGridUsers = await _dbContext.Queryable<WorkGridUserEntity>().Where(x => x.WorkGridCode == myWorkGrid.Code)
|
|
.Where(x => request.UserCodes.Contains(x.UserCode))
|
|
.ToListAsync();
|
|
|
|
await _dbContext.DeleteReallyAsync(workGridUsers);
|
|
}
|
|
} |