using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_TaskInfoRepository
{
///
/// 假电芯位置仓储实现
///
public class FakeBatteryPositionRepository : RepositoryBase, IFakeBatteryPositionRepository
{
public FakeBatteryPositionRepository(IUnitOfWorkManage unitOfWorkManage) : base(unitOfWorkManage)
{
}
///
public List GetNextAvailable(int count)
{
// 按行和列升序查询所有点位
var allPositions = Db.Queryable()
.OrderBy(x => x.Row)
.OrderBy(x => x.Col)
.ToList();
// 按行分组,在每行内查找连续可用的点位
var rows = allPositions.GroupBy(p => p.Row).OrderBy(g => g.Key);
foreach (var rowGroup in rows)
{
var rowPositions = rowGroup.OrderBy(p => p.Col).ToList();
// 在这一行内查找连续的count个未使用点位
for (int i = 0; i <= rowPositions.Count - count; i++)
{
var candidate = rowPositions.Skip(i).Take(count).ToList();
// 检查这count个点位是否都是连续的列(Col连续)且未使用
bool allAvailable = candidate.All(p => !p.IsUsed);
bool allConsecutive = true;
for (int j = 1; j < candidate.Count; j++)
{
if (candidate[j].Col != candidate[j - 1].Col + 1)
{
allConsecutive = false;
break;
}
}
if (allAvailable && allConsecutive)
{
return candidate.Select(p => p.PositionIndex).ToList();
}
}
}
// 没有找到连续的空点位,返回空列表
return new List();
}
///
public int ResetAll()
{
return Db.Updateable()
.SetColumns(x => x.IsUsed, false)
.ExecuteCommand();
}
///
public bool MarkAsUsed(List positions)
{
if (positions == null || positions.Count == 0)
return true;
return Db.Updateable()
.SetColumns(x => x.IsUsed, true)
.Where(x => positions.Contains(x.PositionIndex))
.ExecuteCommand() > 0;
}
///
public int? GetPositionIndex(int row, int col)
{
var entity = Db.Queryable()
.Where(x => x.Row == row && x.Col == col)
.Select(x => new { x.PositionIndex })
.First();
return entity?.PositionIndex;
}
}
}