wanshenmean
5 小时以前 ad64840cc04dac2278ca02f22ddc02b1a218e9cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
        }
    }
}