| | |
| | | { |
| | | newState.ModifyDate = DateTime.Now; |
| | | |
| | | // 乐观锁:WHERE IPAddress = @ip AND Version = @expectedVersion,版本匹配才更新 |
| | | var affectedRows = Db.Updateable<Dt_RobotState>(newState) |
| | | // 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) |
| | | .ExecuteCommand(); |
| | | .Select(x => x.Version) |
| | | .First(); |
| | | |
| | | if (currentVersion != expectedVersion) |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | // 步骤2:版本匹配,通过主键直接更新 |
| | | var affectedRows = Db.Updateable(newState).ExecuteCommand(); |
| | | |
| | | return affectedRows > 0; |
| | | } |
| | |
| | | CurrentBatchIndex = entity.CurrentBatchIndex, |
| | | ChangePalletPhase = entity.ChangePalletPhase, |
| | | IsScanNG = entity.IsScanNG, |
| | | BatteryArrived = entity.BatteryArrived |
| | | 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)) |
| | |
| | | LastPickPositionsJson = state.LastPickPositions.ToJson(), |
| | | CurrentTaskJson = state.CurrentTask.ToJson(), |
| | | LastPutPositionsJson = state.LastPutPositions.ToJson(), |
| | | CurrentBatchBarcodes = state.CurrentBatchBarcodes.ToJson(), |
| | | CurrentTaskNum = state.CurrentTaskNum, |
| | | }; |
| | | |
| | | // 序列化复杂对象为 JSON |
| | |
| | | entity.RobotCraneJson = JsonConvert.SerializeObject(state.RobotCrane); |
| | | } |
| | | |
| | | if (state.CurrentTask != null) |
| | | { |
| | | entity.CurrentTaskJson = JsonConvert.SerializeObject(state.CurrentTask); |
| | | } |
| | | //if (state.CurrentTask != null) |
| | | //{ |
| | | // entity.CurrentTaskJson = JsonConvert.SerializeObject(state.CurrentTask); |
| | | //} |
| | | |
| | | if (state.LastPickPositions != null) |
| | | { |
| | | entity.LastPickPositionsJson = JsonConvert.SerializeObject(state.LastPickPositions); |
| | | } |
| | | //if (state.LastPickPositions != null) |
| | | //{ |
| | | // entity.LastPickPositionsJson = JsonConvert.SerializeObject(state.LastPickPositions); |
| | | //} |
| | | |
| | | if (state.LastPutPositions != null) |
| | | { |
| | | entity.LastPutPositionsJson = JsonConvert.SerializeObject(state.LastPutPositions); |
| | | } |
| | | //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.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; |
| | | } |
| | | } |
| | | } |
| | | } |