using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_ITaskInfoRepository;
|
using WIDESEAWCS_Model.Models;
|
|
namespace WIDESEAWCS_TaskInfoRepository
|
{
|
/// <summary>
|
/// 假电芯位置仓储实现
|
/// </summary>
|
public class FakeBatteryPositionRepository : RepositoryBase<Dt_FakeBatteryPosition>, IFakeBatteryPositionRepository
|
{
|
public FakeBatteryPositionRepository(IUnitOfWorkManage unitOfWorkManage) : base(unitOfWorkManage)
|
{
|
}
|
|
/// <inheritdoc/>
|
public List<int> GetNextAvailable(int count)
|
{
|
// 按行和列升序查询所有点位
|
var allPositions = Db.Queryable<Dt_FakeBatteryPosition>()
|
.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<int>();
|
}
|
|
/// <inheritdoc/>
|
public int ResetAll()
|
{
|
return Db.Updateable<Dt_FakeBatteryPosition>()
|
.SetColumns(x => x.IsUsed, false)
|
.ExecuteCommand();
|
}
|
|
/// <inheritdoc/>
|
public bool MarkAsUsed(List<int> positions)
|
{
|
if (positions == null || positions.Count == 0)
|
return true;
|
|
return Db.Updateable<Dt_FakeBatteryPosition>()
|
.SetColumns(x => x.IsUsed, true)
|
.Where(x => positions.Contains(x.PositionIndex))
|
.ExecuteCommand() > 0;
|
}
|
|
/// <inheritdoc/>
|
public int? GetPositionIndex(int row, int col)
|
{
|
var entity = Db.Queryable<Dt_FakeBatteryPosition>()
|
.Where(x => x.Row == row && x.Col == col)
|
.Select(x => new { x.PositionIndex })
|
.First();
|
return entity?.PositionIndex;
|
}
|
}
|
}
|