using Newtonsoft.Json; using SqlSugar; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_Model.Models; namespace WIDESEAWCS_TaskInfoRepository { /// /// 机械手状态 SqlSugar 仓储实现 /// public class RobotStateRepository : RepositoryBase, IRobotStateRepository { public RobotStateRepository(IUnitOfWorkManage unitOfWork) : base(unitOfWork) { } public Dt_RobotState? GetByIp(string ipAddress) { return Db.Queryable() .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() .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(entity.RobotCraneJson); } if (!string.IsNullOrEmpty(entity.CurrentBatchBarcodes)) { state.CurrentBatchBarcodes = JsonConvert.DeserializeObject>(entity.CurrentBatchBarcodes) ?? new List(); } if (!string.IsNullOrEmpty(entity.CurrentTaskJson)) { state.CurrentTask = JsonConvert.DeserializeObject(entity.CurrentTaskJson); } if (!string.IsNullOrEmpty(entity.LastPickPositionsJson)) { state.LastPickPositions = JsonConvert.DeserializeObject(entity.LastPickPositionsJson); } if (!string.IsNullOrEmpty(entity.LastPutPositionsJson)) { state.LastPutPositions = JsonConvert.DeserializeObject(entity.LastPutPositionsJson); } if (!string.IsNullOrEmpty(entity.CellBarcodeJson)) { state.CellBarcode = JsonConvert.DeserializeObject>(entity.CellBarcodeJson) ?? new List(); } 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; } } }