wanshenmean
11 小时以前 f423e1277f91427f0a767bd1224c1260dcb73086
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,20 +10,15 @@
    /// <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>()
            return Db.Queryable<Dt_RobotState>()
                .Where(x => x.IPAddress == ipAddress)
                .First();
        }
@@ -39,22 +34,23 @@
            var newState = new Dt_RobotState
            {
                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 IPAddress = @ip AND Version = @expectedVersion,版本匹配才更新
            var affectedRows = Db.Updateable<Dt_RobotState>(newState)
                .Where(x => x.IPAddress == ipAddress)
                .WhereRowVersion(x => x.RowVersion, expectedRowVersion)
                .ExecuteCommand();
            return affectedRows > 0;
@@ -65,7 +61,7 @@
            var state = new RobotSocketState
            {
                IPAddress = entity.IPAddress,
                Version = BitConverter.ToInt64(entity.RowVersion.Length >= 8 ? entity.RowVersion.Take(8).ToArray() : new byte[8], 0),
                Version = entity.Version,
                IsEventSubscribed = entity.IsEventSubscribed,
                RobotRunMode = entity.RobotRunMode,
                RobotControlMode = entity.RobotControlMode,
@@ -131,7 +127,11 @@
                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(),
            };
            // 序列化复杂对象为 JSON
@@ -162,13 +162,5 @@
            return entity;
        }
        public SqlSugarClient GetDbClient() => _db;
        public void BeginTran() => _unitOfWork.BeginTran();
        public void CommitTran() => _unitOfWork.CommitTran();
        public void RollbackTran() => _unitOfWork.RollbackTran();
    }
}