Files
jinko.yikatong/Src/Dlhis.Basic/WorkGrids/Services/Implements/WorkGridScheduleService.cs
2025-03-05 12:38:58 +08:00

158 lines
6.1 KiB
C#

using Dlhis.Basic.WorkGrids.Request;
using Dlhis.Basic.WorkGrids.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Device.Entities;
using Dlhis.Jip.Api;
using Dlhis.Jip.Api.Requests;
using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.Basic.WorkGrids.Services.Implements;
public class WorkGridScheduleService : IWorkGridScheduleService
{
private readonly IDbContext _dbContext;
private readonly IJinkoJipApi _jinkoJipApi;
public WorkGridScheduleService(IDbContext dbContext, IJinkoJipApi jinkoJipApi)
{
_dbContext = dbContext;
_jinkoJipApi = jinkoJipApi;
}
public async Task SaveAsync(WorkGridScheduleSaveRequest request)
{
var exists = await _dbContext.Queryable<WorkGridScheduleEntity>()
.Where(x => x.WorkGridCode == request.WorkGridCode
&& x.PeriodType == request.PeriodType
&& x.Date == request.Date
&& request.UserCodes.Contains(x.UserCode)
)
.AnyAsync();
exists.ThrowIfTrue("当天已经存在排班人员");
var toInserts = request.UserCodes.Select(x => new WorkGridScheduleEntity
{
WorkGridCode = request.WorkGridCode,
Date = request.Date,
PeriodType = request.PeriodType,
UserCode = x
}).ToList();
await _dbContext.InsertAsync(toInserts);
}
/// <inheritdoc />
public async Task RemoveAsync(WorkGridScheduleSaveRequest request)
{
var data = await _dbContext.Queryable<WorkGridScheduleEntity>()
.Where(x => x.WorkGridCode == request.WorkGridCode)
.Where(x => x.Date == request.Date)
.Where(x => x.PeriodType == request.PeriodType)
.Where(x => request.UserCodes.Contains(x.UserCode))
.ToListAsync();
await _dbContext.DeleteReallyAsync(data);
}
/// <inheritdoc />
public async Task<List<WorkGridScheduleResponse>> GetListAsync(Guid workGridCode, DateOnly date, WorkGridTimePeriodEnumType? periodType)
{
return await _dbContext.Queryable<WorkGridScheduleEntity>()
.LeftJoin<GainfoEntity>((x, ga) => x.UserCode == ga.Code)
.Where(x => x.WorkGridCode == workGridCode && x.Date == date)
.WhereIF(periodType.HasValue, x => x.PeriodType == periodType)
.Select((x, ga) => new WorkGridScheduleResponse
{
Date = x.Date,
PeriodType = x.PeriodType,
UserCode = x.UserCode,
UserName = ga.Name
})
.ToListAsync();
}
/// <inheritdoc />
public async Task<IEnumerable<WorkGridScheduleSummaryResponse>> GetListSummaryAsync(Guid workGridCode, DateOnly start, DateOnly end)
{
var workGridSchedules = await _dbContext.Queryable<WorkGridScheduleEntity>()
.Where(x => x.WorkGridCode == workGridCode && x.Date >= start && x.Date <= end)
.ToListAsync();
return workGridSchedules.GroupBy(x => x.Date).Select(x =>
{
return new WorkGridScheduleSummaryResponse
{
Date = x.Key,
Details = x.GroupBy(p => p.PeriodType).Select(p => new WorkGridScheduleSummaryDetailResponse
{
PeriodType = p.Key,
Count = p.Count()
})
};
});
}
/// <inheritdoc />
public async Task<Pagination<WorkGridScheduleAttendancePageResponse>> GetAttendancePageAsync(WorkGridScheduleAttendancePageRequest request)
{
return await _dbContext.Queryable<DeviceUploadLogEntity>()
.LeftJoin<DeviceEntity>((log, d) => log.DeviceUniqueNo == d.UniqueNo)
.Where(log => log.UserUniqueNo == request.UserCode)
.WhereIF(request.DateTimePeriod.HasValue(), log => log.RaiseDateTime >= request.DateTimePeriod.Start
&& log.RaiseDateTime <= request.DateTimePeriod.End)
.OrderBy(log => log.RaiseDateTime)
.Select((log, d) => new WorkGridScheduleAttendancePageResponse
{
Id = log.Id,
OriginUserUniqueNo = log.UserUniqueNo,
DeviceUniqueNo = log.DeviceUniqueNo,
RaiseDateTime = log.RaiseDateTime,
CreateTime = log.CreateTime,
SerialNo = log.SerialNo,
DeployPlace = d.DeployPlace,
Sync = log.Sync,
Status = log.Status
})
.ToPageListAsync(request);
}
/// <inheritdoc />
public async Task ManuallyUploadAsync(List<long> logIds)
{
var requests = await _dbContext.Queryable<DeviceUploadLogEntity>()
.LeftJoin<DeviceEntity>((x, device) => x.DeviceUniqueNo == device.UniqueNo)
.Where(x => logIds.Contains(x.Id))
.Where(x => x.NeedUpload && x.Sync == false)
.Select((x, device) => new UploadAttendanceRequestItem
{
Id = x.Id,
RaiseDateTime = x.RaiseDateTime,
ZNOTE = device.DeployPlace,
ZWORKCODE = x.UserUniqueNo
})
.ToListAsync();
if (requests.Any())
{
foreach (var items in requests.Chunk(500))
{
var ids = items.Select(x => x.Id).ToList();
var r = await _jinkoJipApi.UploadAttendancesAsync(new UploadAttendanceRequest { Data = items.ToList() });
if (r.OLX == "0")
{
await _dbContext.Updateable<DeviceUploadLogEntity>()
.SetColumns(x => x.Sync == true)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => ids.Contains(x.Id))
.ExecuteCommandAsync();
}
}
}
}
}