wanshenmean
3 天以前 b690250002ee04f4309e6a90fd16fbfd9bd959e2
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoRepository/RobotStateRepository.cs
@@ -19,7 +19,7 @@
        public Dt_RobotState? GetByIp(string ipAddress)
        {
            return Db.Queryable<Dt_RobotState>()
                .Where(x => x.IPAddress == ipAddress)
                .Where(x => x.IpAddress == ipAddress)
                .First();
        }
@@ -33,7 +33,7 @@
            var newState = new Dt_RobotState
            {
                IPAddress = ipAddress,
                IpAddress = ipAddress,
                Version = DateTime.UtcNow.Ticks,
                RobotCraneJson = JsonConvert.SerializeObject(robotCrane),
                CreateDate = DateTime.Now,
@@ -48,10 +48,24 @@
        {
            newState.ModifyDate = DateTime.Now;
            // 乐观锁:WHERE IPAddress = @ip AND Version = @expectedVersion,版本匹配才更新
            var affectedRows = Db.Updateable<Dt_RobotState>(newState)
                .Where(x => x.IPAddress == ipAddress)
                .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;
        }
@@ -60,7 +74,7 @@
        {
            var state = new RobotSocketState
            {
                IPAddress = entity.IPAddress,
                IPAddress = entity.IpAddress,
                Version = entity.Version,
                IsEventSubscribed = entity.IsEventSubscribed,
                RobotRunMode = entity.RobotRunMode,
@@ -76,13 +90,19 @@
                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))
@@ -112,7 +132,7 @@
        {
            var entity = new Dt_RobotState
            {
                IPAddress = state.IPAddress,
                IpAddress = state.IPAddress,
                IsEventSubscribed = state.IsEventSubscribed,
                RobotRunMode = state.RobotRunMode,
                RobotControlMode = state.RobotControlMode,
@@ -132,6 +152,8 @@
                LastPickPositionsJson = state.LastPickPositions.ToJson(),
                CurrentTaskJson = state.CurrentTask.ToJson(),
                LastPutPositionsJson = state.LastPutPositions.ToJson(),
                CurrentBatchBarcodes = state.CurrentBatchBarcodes.ToJson(),
                CurrentTaskNum = state.CurrentTaskNum,
            };
            // 序列化复杂对象为 JSON
@@ -140,27 +162,32 @@
                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;
        }
    }
}
}