using Newtonsoft.Json; using SqlSugar; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.UnitOfWork; using WIDESEAWCS_ITaskInfoRepository; using WIDESEAWCS_Model.Models; namespace WIDESEAWCS_TaskInfoRepository { /// /// 机械手状态 SqlSugar 仓储实现 /// public class RobotStateRepository : IUnitOfWork, IRobotStateRepository { private readonly IUnitOfWorkManage _unitOfWork; private readonly SqlSugarClient _db; public RobotStateRepository(IUnitOfWorkManage unitOfWork) { _unitOfWork = unitOfWork; _db = unitOfWork.GetDbClient(); } 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, RobotCraneJson = JsonConvert.SerializeObject(robotCrane), CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; _db.Insertable(newState).ExecuteCommand(); return newState; } public bool TryUpdate(string ipAddress, Dt_RobotState newState, byte[] expectedRowVersion) { newState.UpdateTime = DateTime.Now; var affectedRows = _db.Updateable(newState) .Where(x => x.IPAddress == ipAddress) .WhereRowVersion(x => x.RowVersion, expectedRowVersion) .ExecuteCommand(); return affectedRows > 0; } public RobotSocketState ToSocketState(Dt_RobotState entity) { var state = new RobotSocketState { IPAddress = entity.IPAddress, Version = BitConverter.ToInt64(entity.RowVersion.Length >= 8 ? entity.RowVersion.Take(8).ToArray() : new byte[8], 0), 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 }; // 反序列化复杂 JSON 字段 if (!string.IsNullOrEmpty(entity.RobotCraneJson)) { state.RobotCrane = JsonConvert.DeserializeObject(entity.RobotCraneJson); } 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 }; // 序列化复杂对象为 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); } return entity; } public SqlSugarClient GetDbClient() => _db; public void BeginTran() => _unitOfWork.BeginTran(); public void CommitTran() => _unitOfWork.CommitTran(); public void RollbackTran() => _unitOfWork.RollbackTran(); } }