wanshenmean
3 天以前 b690250002ee04f4309e6a90fd16fbfd9bd959e2
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Newtonsoft.Json;
using SqlSugar;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_TaskInfoRepository
{
    /// <summary>
    /// 机械手状态 SqlSugar 仓储实现
    /// </summary>
    public class RobotStateRepository : RepositoryBase<Dt_RobotState>, IRobotStateRepository
    {
        public RobotStateRepository(IUnitOfWorkManage unitOfWork) : base(unitOfWork)
        {
        }
 
        public Dt_RobotState? GetByIp(string ipAddress)
        {
            return Db.Queryable<Dt_RobotState>()
                .Where(x => x.IpAddress == ipAddress)
                .First();
        }
 
        public Dt_RobotState GetOrCreate(string ipAddress, RobotCraneDevice robotCrane)
        {
            var existing = GetByIp(ipAddress);
            if (existing != null)
            {
                return existing;
            }
 
            var newState = new Dt_RobotState
            {
                IpAddress = ipAddress,
                Version = DateTime.UtcNow.Ticks,
                RobotCraneJson = JsonConvert.SerializeObject(robotCrane),
                CreateDate = DateTime.Now,
                ModifyDate = DateTime.Now
            };
 
            Db.Insertable(newState).ExecuteCommand();
            return newState;
        }
 
        public bool TryUpdate(string ipAddress, Dt_RobotState newState, long expectedVersion)
        {
            newState.ModifyDate = DateTime.Now;
 
            // SqlSugar 的 Updateable(entity).Where(x => x.Version == param) 存在参数混淆问题:
            // 实体的 Version 已被设为 expectedVersion+1,.Where() 中 SqlSugar 可能使用实体的
            // Version 值(expectedVersion+1)而非参数值(expectedVersion),导致 WHERE 永远匹配不上。
            // 修复:将版本校验拆为独立查询,更新仅通过主键执行。
 
            // 步骤1:校验版本号是否与期望一致
            var currentVersion = Db.Queryable<Dt_RobotState>()
                .Where(x => x.IpAddress == ipAddress)
                .Select(x => x.Version)
                .First();
 
            if (currentVersion != expectedVersion)
            {
                return false;
            }
 
            // 步骤2:版本匹配,通过主键直接更新
            var affectedRows = Db.Updateable(newState).ExecuteCommand();
 
            return affectedRows > 0;
        }
 
        public RobotSocketState ToSocketState(Dt_RobotState entity)
        {
            var state = new RobotSocketState
            {
                IPAddress = entity.IpAddress,
                Version = entity.Version,
                IsEventSubscribed = entity.IsEventSubscribed,
                RobotRunMode = entity.RobotRunMode,
                RobotControlMode = entity.RobotControlMode,
                RobotArmObject = entity.RobotArmObject,
                Homed = entity.Homed,
                CurrentAction = entity.CurrentAction,
                OperStatus = entity.OperStatus,
                IsSplitPallet = entity.IsSplitPallet,
                IsGroupPallet = entity.IsGroupPallet,
                RobotTaskTotalNum = entity.RobotTaskTotalNum,
                IsInFakeBatteryMode = entity.IsInFakeBatteryMode,
                CurrentBatchIndex = entity.CurrentBatchIndex,
                ChangePalletPhase = entity.ChangePalletPhase,
                IsScanNG = entity.IsScanNG,
                BatteryArrived = entity.BatteryArrived,
                CurrentTaskNum = entity.CurrentTaskNum,
            };
 
            // 反序列化复杂 JSON 字段
            if (!string.IsNullOrEmpty(entity.RobotCraneJson))
            {
                state.RobotCrane = JsonConvert.DeserializeObject<RobotCraneDevice>(entity.RobotCraneJson);
            }
 
            if (!string.IsNullOrEmpty(entity.CurrentBatchBarcodes))
            {
                state.CurrentBatchBarcodes = JsonConvert.DeserializeObject<List<string>>(entity.CurrentBatchBarcodes) ?? new List<string>();
            }
 
            if (!string.IsNullOrEmpty(entity.CurrentTaskJson))
            {
                state.CurrentTask = JsonConvert.DeserializeObject<Dt_RobotTask>(entity.CurrentTaskJson);
            }
 
            if (!string.IsNullOrEmpty(entity.LastPickPositionsJson))
            {
                state.LastPickPositions = JsonConvert.DeserializeObject<int[]>(entity.LastPickPositionsJson);
            }
 
            if (!string.IsNullOrEmpty(entity.LastPutPositionsJson))
            {
                state.LastPutPositions = JsonConvert.DeserializeObject<int[]>(entity.LastPutPositionsJson);
            }
 
            if (!string.IsNullOrEmpty(entity.CellBarcodeJson))
            {
                state.CellBarcode = JsonConvert.DeserializeObject<List<string>>(entity.CellBarcodeJson) ?? new List<string>();
            }
 
            return state;
        }
 
        public Dt_RobotState ToEntity(RobotSocketState state)
        {
            var entity = new Dt_RobotState
            {
                IpAddress = state.IPAddress,
                IsEventSubscribed = state.IsEventSubscribed,
                RobotRunMode = state.RobotRunMode,
                RobotControlMode = state.RobotControlMode,
                RobotArmObject = state.RobotArmObject,
                Homed = state.Homed,
                CurrentAction = state.CurrentAction,
                OperStatus = state.OperStatus,
                IsSplitPallet = state.IsSplitPallet,
                IsGroupPallet = state.IsGroupPallet,
                RobotTaskTotalNum = state.RobotTaskTotalNum,
                IsInFakeBatteryMode = state.IsInFakeBatteryMode,
                CurrentBatchIndex = state.CurrentBatchIndex,
                ChangePalletPhase = state.ChangePalletPhase,
                IsScanNG = state.IsScanNG,
                BatteryArrived = state.BatteryArrived,
                CellBarcodeJson = state.CellBarcode.ToJson(),
                LastPickPositionsJson = state.LastPickPositions.ToJson(),
                CurrentTaskJson = state.CurrentTask.ToJson(),
                LastPutPositionsJson = state.LastPutPositions.ToJson(),
                CurrentBatchBarcodes = state.CurrentBatchBarcodes.ToJson(),
                CurrentTaskNum = state.CurrentTaskNum,
            };
 
            // 序列化复杂对象为 JSON
            if (state.RobotCrane != null)
            {
                entity.RobotCraneJson = JsonConvert.SerializeObject(state.RobotCrane);
            }
 
            //if (state.CurrentTask != null)
            //{
            //    entity.CurrentTaskJson = JsonConvert.SerializeObject(state.CurrentTask);
            //}
 
            //if (state.LastPickPositions != null)
            //{
            //    entity.LastPickPositionsJson = JsonConvert.SerializeObject(state.LastPickPositions);
            //}
 
            //if (state.LastPutPositions != null)
            //{
            //    entity.LastPutPositionsJson = JsonConvert.SerializeObject(state.LastPutPositions);
            //}
 
            //if (state.CellBarcode != null && state.CellBarcode.Count > 0)
            //{
            //    entity.CellBarcodeJson = JsonConvert.SerializeObject(state.CellBarcode);
            //}
 
            //if (state.CurrentBatchBarcodes != null && state.CurrentBatchBarcodes.Count > 0)
            //{
            //    entity.CurrentBatchBarcodes = JsonConvert.SerializeObject(state.CurrentBatchBarcodes);
            //}
 
            return entity;
        }
    }
}