wanshenmean
4 天以前 b690250002ee04f4309e6a90fd16fbfd9bd959e2
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoRepository/RobotStateRepository.cs
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using SqlSugar;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.UnitOfWork;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_Model.Models;
@@ -10,21 +10,16 @@
    /// <summary>
    /// 机械手状态 SqlSugar 仓储实现
    /// </summary>
    public class RobotStateRepository : IUnitOfWork, IRobotStateRepository
    public class RobotStateRepository : RepositoryBase<Dt_RobotState>, IRobotStateRepository
    {
        private readonly IUnitOfWorkManage _unitOfWork;
        private readonly SqlSugarClient _db;
        public RobotStateRepository(IUnitOfWorkManage unitOfWork)
        public RobotStateRepository(IUnitOfWorkManage unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _db = unitOfWork.GetDbClient();
        }
        public Dt_RobotState? GetByIp(string ipAddress)
        {
            return _db.Queryable<Dt_RobotState>()
                .Where(x => x.IPAddress == ipAddress)
            return Db.Queryable<Dt_RobotState>()
                .Where(x => x.IpAddress == ipAddress)
                .First();
        }
@@ -38,24 +33,39 @@
            var newState = new Dt_RobotState
            {
                IPAddress = ipAddress,
                IpAddress = ipAddress,
                Version = DateTime.UtcNow.Ticks,
                RobotCraneJson = JsonConvert.SerializeObject(robotCrane),
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now
                CreateDate = DateTime.Now,
                ModifyDate = DateTime.Now
            };
            _db.Insertable(newState).ExecuteCommand();
            Db.Insertable(newState).ExecuteCommand();
            return newState;
        }
        public bool TryUpdate(string ipAddress, Dt_RobotState newState, byte[] expectedRowVersion)
        public bool TryUpdate(string ipAddress, Dt_RobotState newState, long expectedVersion)
        {
            newState.UpdateTime = DateTime.Now;
            newState.ModifyDate = DateTime.Now;
            var affectedRows = _db.Updateable<Dt_RobotState>(newState)
                .Where(x => x.IPAddress == ipAddress)
                .WhereRowVersion(x => x.RowVersion, expectedRowVersion)
                .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;
        }
@@ -64,8 +74,8 @@
        {
            var state = new RobotSocketState
            {
                IPAddress = entity.IPAddress,
                Version = BitConverter.ToInt64(entity.RowVersion.Length >= 8 ? entity.RowVersion.Take(8).ToArray() : new byte[8], 0),
                IPAddress = entity.IpAddress,
                Version = entity.Version,
                IsEventSubscribed = entity.IsEventSubscribed,
                RobotRunMode = entity.RobotRunMode,
                RobotControlMode = entity.RobotControlMode,
@@ -80,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))
@@ -116,7 +132,7 @@
        {
            var entity = new Dt_RobotState
            {
                IPAddress = state.IPAddress,
                IpAddress = state.IPAddress,
                IsEventSubscribed = state.IsEventSubscribed,
                RobotRunMode = state.RobotRunMode,
                RobotControlMode = state.RobotControlMode,
@@ -131,7 +147,13 @@
                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
@@ -140,35 +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;
        }
        public SqlSugarClient GetDbClient() => _db;
        public void BeginTran() => _unitOfWork.BeginTran();
        public void CommitTran() => _unitOfWork.CommitTran();
        public void RollbackTran() => _unitOfWork.RollbackTran();
    }
}
}