refactor 优化命名和结构,注释
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using Dlhis.Basic.WorkGrids.Response;
|
||||
|
||||
namespace Dlhis.Basic.WorkGrids.Repositories;
|
||||
|
||||
public interface IWorkGridScheduleRepository : IScopeDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取用户在指定天数内最大连续排班信息
|
||||
/// </summary>
|
||||
/// <param name="userCodes">用户编码列表</param>
|
||||
/// <param name="days">天数</param>s
|
||||
/// <param name="date">日期</param>
|
||||
/// <returns>最大连续排班信息列表</returns>
|
||||
Task<List<WorkGridScheduleMaximumConsecutiveDto>> MaximumConsecutiveUserCodesAsync(List<string> userCodes, int days, DateOnly date);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Dlhis.Basic.WorkGrids.Response;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
|
||||
namespace Dlhis.Basic.WorkGrids.Repositories.Implements;
|
||||
|
||||
public class WorkGridScheduleRepository : IWorkGridScheduleRepository
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public WorkGridScheduleRepository(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<List<WorkGridScheduleMaximumConsecutiveDto>> MaximumConsecutiveUserCodesAsync(List<string> userCodes, int days, DateOnly date)
|
||||
{
|
||||
var userCodesStr = userCodes.Select(x => $"@param{x.Replace("-", "A_")}").Join(",");
|
||||
var parameters = userCodes.ToDictionary(x => $"param{x.Replace("-", "A_")}", x => x);
|
||||
|
||||
parameters.Add("dateBefore", date.AddDays(-1).ToString("yyyy-MM-dd"));
|
||||
parameters.Add("dateAfter", date.AddDays(1).ToString("yyyy-MM-dd"));
|
||||
parameters.Add("days", days.ToString());
|
||||
|
||||
/* 1.用户按日期排序,标记序号
|
||||
* 2.将日期减去序号,需要得出结果一致 == 数据连续 == 连续排班
|
||||
* 3.根据减去序号后的日期分组,统计连续排班的天数
|
||||
* 4.数据过滤,取当前排班的前后一天
|
||||
*
|
||||
* 根据以上结果,获取连续排班天数大于等于指定天数的用户
|
||||
*/
|
||||
var sql = $@"
|
||||
|
||||
WITH ranked_dates AS (SELECT UserCode,
|
||||
Date,
|
||||
ROW_NUMBER() OVER (PARTITION BY UserCode ORDER BY Date) AS rn
|
||||
FROM WorkGridSchedules
|
||||
where IsDeleted = 0 and UserCode in ({userCodesStr})
|
||||
GROUP BY UserCode, Date),
|
||||
grouped AS (SELECT UserCode,
|
||||
Date,
|
||||
DATE_SUB(Date, INTERVAL rn DAY) AS grp
|
||||
FROM ranked_dates),
|
||||
streaks AS (SELECT UserCode,
|
||||
grp,
|
||||
COUNT(*) AS streak_days,
|
||||
MIN(Date) AS start_date,
|
||||
MAX(Date) AS end_date
|
||||
FROM grouped
|
||||
GROUP BY UserCode, grp
|
||||
HAVING streak_days >= @days)
|
||||
SELECT UserCode, streak_days as Count, start_date as Start, end_date as End
|
||||
FROM streaks
|
||||
where (@dateBefore >= start_date
|
||||
and @dateBefore <= end_date)
|
||||
or (@dateAfter >= start_date
|
||||
and @dateAfter <= end_date)
|
||||
";
|
||||
|
||||
return await _dbContext.Client.Ado.SqlQueryAsync<WorkGridScheduleMaximumConsecutiveDto>(sql, parameters);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user