init 项目代码初始化
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
using Dlhis.Basic.BusinessShift.Request;
|
||||
using Dlhis.Basic.BusinessShift.Response;
|
||||
using Dlhis.Entity.Canteen.Entities;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.CustomClass;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
|
||||
namespace Dlhis.Basic.BusinessShift.Services.Implements;
|
||||
|
||||
public class BusinessPlaceShiftService : IBusinessPlaceShiftService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public BusinessPlaceShiftService(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<Pagination<BusinessPlaceShiftResponse>> GetPageAsync(BusinessPlaceShiftPageRequest request)
|
||||
{
|
||||
var page = await _dbContext.Queryable<CanteenBusinessShiftEntity>()
|
||||
.LeftJoin<CanteenBusinessPlaceEntity>((shift, place) => shift.BusinessPlaceCode == place.Code)
|
||||
.WhereIF(request.Search.IsPresent(), (shift, place) => shift.CustomCode.Contains(request.Search) || shift.Name.Contains(request.Search))
|
||||
.Select((shift, place) => new BusinessPlaceShiftResponse
|
||||
{
|
||||
Id = shift.Id,
|
||||
BusinessPlaceCode = place.Code,
|
||||
BusinessPlaceCustomCode = place.CustomCode,
|
||||
Code = shift.Code,
|
||||
CustomCode = shift.CustomCode,
|
||||
Name = shift.Name,
|
||||
Start = shift.Start,
|
||||
End = shift.End
|
||||
})
|
||||
.ToPageListAsync(request);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(BusinessPlaceShiftSaveRequest request)
|
||||
{
|
||||
await CheckExistsCustomCodeAsync(request);
|
||||
await DuplicateTimePeriodAsync(request);
|
||||
await CheckPlaceExistsAsync(request.BusinessPlaceCode);
|
||||
|
||||
if (!request.Id.HasValue)
|
||||
{
|
||||
var entity = request.Adapt<CanteenBusinessShiftEntity>();
|
||||
|
||||
entity.Code = Guid.NewGuid();
|
||||
entity.Start = request.TimePeriod.Start.Value;
|
||||
entity.End = request.TimePeriod.End.Value;
|
||||
|
||||
await _dbContext.InsertAsync(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
var entity = await _dbContext.Queryable<CanteenBusinessShiftEntity>().FirstAsync(x => x.Id == request.Id);
|
||||
entity.Adapt(request);
|
||||
|
||||
await _dbContext.UpdateAsync(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<BusinessPlaceShiftResponse>> GetShiftsByPlaceCustomCodeAsync(string placeCustomCode)
|
||||
{
|
||||
return await _dbContext.Queryable<CanteenBusinessShiftEntity>()
|
||||
.LeftJoin<CanteenBusinessPlaceEntity>((shift, place) => shift.BusinessPlaceCode == place.Code)
|
||||
.Where((shift, place) => place.CustomCode == placeCustomCode)
|
||||
.OrderBy((shift, place) => shift.Start)
|
||||
.Select((shift, place) => new BusinessPlaceShiftResponse
|
||||
{
|
||||
Id = shift.Id,
|
||||
BusinessPlaceCode = place.Code,
|
||||
BusinessPlaceCustomCode = place.CustomCode,
|
||||
Code = shift.Code,
|
||||
CustomCode = shift.CustomCode,
|
||||
Name = shift.Name,
|
||||
Start = shift.Start,
|
||||
End = shift.End
|
||||
})
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
//*********************************************** help methods ***************************************************//
|
||||
|
||||
|
||||
private async Task CheckPlaceExistsAsync(Guid code)
|
||||
{
|
||||
var existsPlace = await _dbContext.Queryable<CanteenBusinessPlaceEntity>().AnyAsync(x => x.Code == code);
|
||||
existsPlace.ThrowIfFalse("营业点不存在,请检查.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 时间段重合检查
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
private async Task DuplicateTimePeriodAsync(BusinessPlaceShiftSaveRequest request)
|
||||
{
|
||||
var periods = await _dbContext.Queryable<CanteenBusinessShiftEntity>()
|
||||
.Where(x => x.BusinessPlaceCode == request.BusinessPlaceCode)
|
||||
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
||||
.Select(x => new TimePeriod
|
||||
{
|
||||
Start = x.Start,
|
||||
End = x.End,
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
period.IsDuplicate(request.TimePeriod).ThrowIfTrue($"存在时间段重合[{period.Start}-{period.End}]");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检查重复手工号
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
private async Task CheckExistsCustomCodeAsync(BusinessPlaceShiftSaveRequest request)
|
||||
{
|
||||
var existsSameCustomCode = await _dbContext.Queryable<CanteenBusinessShiftEntity>()
|
||||
.Where(x => x.CustomCode == request.CustomCode)
|
||||
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
||||
.AnyAsync();
|
||||
|
||||
existsSameCustomCode.ThrowIfTrue($"自定义手工号[{request.CustomCode}]存在重复,请检查.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user