| | |
| | | using Newtonsoft.Json; |
| | | using SqlSugar; |
| | | using WIDESEAWCS_Core.BaseRepository; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_ITaskInfoRepository; |
| | | using WIDESEAWCS_Model.Models; |
| | | |
| | |
| | | public Dt_RobotState? GetByIp(string ipAddress) |
| | | { |
| | | return Db.Queryable<Dt_RobotState>() |
| | | .Where(x => x.IPAddress == ipAddress) |
| | | .Where(x => x.IpAddress == ipAddress) |
| | | .First(); |
| | | } |
| | | |
| | |
| | | |
| | | var newState = new Dt_RobotState |
| | | { |
| | | IPAddress = ipAddress, |
| | | IpAddress = ipAddress, |
| | | Version = DateTime.UtcNow.Ticks, |
| | | RobotCraneJson = JsonConvert.SerializeObject(robotCrane), |
| | | CreateDate = DateTime.Now, |
| | |
| | | { |
| | | newState.ModifyDate = DateTime.Now; |
| | | |
| | | // 乐观锁:WHERE IPAddress = @ip AND Version = @expectedVersion,版本匹配才更新 |
| | | var affectedRows = Db.Updateable<Dt_RobotState>(newState) |
| | | .Where(x => x.IPAddress == ipAddress && x.Version == expectedVersion) |
| | | .ExecuteCommand(); |
| | | // 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; |
| | | } |
| | |
| | | { |
| | | var state = new RobotSocketState |
| | | { |
| | | IPAddress = entity.IPAddress, |
| | | IPAddress = entity.IpAddress, |
| | | Version = entity.Version, |
| | | IsEventSubscribed = entity.IsEventSubscribed, |
| | | RobotRunMode = entity.RobotRunMode, |
| | |
| | | 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)) |
| | |
| | | { |
| | | var entity = new Dt_RobotState |
| | | { |
| | | IPAddress = state.IPAddress, |
| | | IpAddress = state.IPAddress, |
| | | IsEventSubscribed = state.IsEventSubscribed, |
| | | RobotRunMode = state.RobotRunMode, |
| | | RobotControlMode = state.RobotControlMode, |
| | |
| | | CurrentBatchIndex = state.CurrentBatchIndex, |
| | | ChangePalletPhase = state.ChangePalletPhase, |
| | | IsScanNG = state.IsScanNG, |
| | | BatteryArrived = state.BatteryArrived |
| | | 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 |
| | |
| | | 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; |
| | | } |
| | | } |
| | | } |
| | | } |