|
using Admin.NET.Core.Service;
|
|
namespace WCS.Application;
|
|
/// <summary>
|
/// 设备信息服务
|
/// </summary>
|
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
|
public class WcsDeviceService : IDynamicApiController, ITransient
|
{
|
private readonly SqlSugarRepository<WcsDevice> _wcsDeviceRep;
|
private readonly SysCacheService _sysCacheService;
|
public WcsDeviceService(SqlSugarRepository<WcsDevice> wcsDeviceRep, SysCacheService sysCacheService)
|
{
|
_wcsDeviceRep = wcsDeviceRep;
|
_sysCacheService = sysCacheService;
|
}
|
|
/// <summary>
|
/// 分页查询设备信息
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpPost]
|
[ApiDescriptionSettings(Name = "Page")]
|
[DisplayName("分页查询设备信息")]
|
public async Task<SqlSugarPagedList<WcsDeviceOutput>> Page(PageWcsDeviceInput input)
|
{
|
if (input.Field.IsNullOrEmpty())
|
{
|
input.Field = "u.Id";
|
input.Order = "desc";
|
}
|
input.SearchKey = input.SearchKey?.Trim();
|
var query = _wcsDeviceRep.AsQueryable()
|
.WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
|
u.Text.Contains(input.SearchKey)
|
)
|
.WhereIF(input.PlcId > 0, u => u.PlcId == input.PlcId)
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Text), u => u.Text.Contains(input.Text.Trim()))
|
//处理外键和TreeSelector相关字段的连接
|
.LeftJoin<WcsPlc>((u, plcid) => u.PlcId == plcid.Id)
|
.Select((u, plcid) => new WcsDeviceOutput
|
{
|
Id = u.Id,
|
PlcId = u.PlcId,
|
DeviceType = (DeviceTypeEnum)u.DeviceType,
|
PlcIdIP = plcid.IP,
|
Level = (DeviceLevelEnum)u.Level,
|
DbNumber = u.DbNumber,
|
StationNum = u.StationNum,
|
PlcPos = u.PlcPos,
|
WcsPos = u.WcsPos,
|
PosType = u.PosType,
|
LedIP = u.LedIP,
|
Text = u.Text,
|
CreateTime = u.CreateTime,
|
UpdateTime = u.UpdateTime,
|
CreateUserId = u.CreateUserId,
|
CreateUserName = u.CreateUserName,
|
UpdateUserId = u.UpdateUserId,
|
UpdateUserName = u.UpdateUserName,
|
CreateOrgId = u.CreateOrgId,
|
CreateOrgName = u.CreateOrgName,
|
IsDelete = u.IsDelete,
|
});
|
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
|
}
|
|
/// <summary>
|
/// 增加设备信息
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpPost]
|
[ApiDescriptionSettings(Name = "Add")]
|
[DisplayName("增加设备信息")]
|
public async Task<long> Add(AddWcsDeviceInput input)
|
{
|
var entity = input.Adapt<WcsDevice>();
|
await _wcsDeviceRep.InsertAsync(entity);
|
return entity.Id;
|
}
|
|
/// <summary>
|
/// 删除设备信息
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpPost]
|
[ApiDescriptionSettings(Name = "Delete")]
|
[DisplayName("删除设备信息")]
|
public async Task Delete(DeleteWcsDeviceInput input)
|
{
|
var entity = await _wcsDeviceRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
await _wcsDeviceRep.FakeDeleteAsync(entity); //假删除
|
//await _wcsDeviceRep.DeleteAsync(entity); //真删除
|
}
|
|
/// <summary>
|
/// 更新设备信息
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpPost]
|
[ApiDescriptionSettings(Name = "Update")]
|
[DisplayName("更新设备信息")]
|
public async Task Update(UpdateWcsDeviceInput input)
|
{
|
var entity = input.Adapt<WcsDevice>();
|
await _wcsDeviceRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
}
|
|
/// <summary>
|
/// 获取设备信息
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpGet]
|
[ApiDescriptionSettings(Name = "Detail")]
|
[DisplayName("获取设备信息")]
|
public async Task<WcsDevice> Detail([FromQuery] QueryByIdWcsDeviceInput input)
|
{
|
return await _wcsDeviceRep.GetFirstAsync(u => u.Id == input.Id);
|
}
|
|
/// <summary>
|
/// 获取PlcId列表
|
/// </summary>
|
/// <returns></returns>
|
[ApiDescriptionSettings(Name = "WcsPlcPlcIdDropdown"), HttpGet]
|
[DisplayName("获取PlcId列表")]
|
public async Task<dynamic> WcsPlcPlcIdDropdown()
|
{
|
return await _wcsDeviceRep.Context.Queryable<WcsPlc>()
|
.Select(u => new
|
{
|
Label = u.Text,
|
Value = u.Id
|
}
|
).ToListAsync();
|
}
|
|
/// <summary>
|
/// 生成点位
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpPost]
|
[ApiDescriptionSettings(Name = "GeneratePos")]
|
[DisplayName("生成点位")]
|
public async Task GeneratePos(GeneratePosInput input)
|
{
|
var modDevice = await _wcsDeviceRep.GetByIdAsync(input.Id);
|
|
var listPosition = new List<WcsPosition>();
|
listPosition.Add(new WcsPosition()
|
{
|
DeviceId = modDevice.Id,
|
StationNum = modDevice.StationNum,
|
PlcPos = input.Pos.ToString(),
|
PosType = PLCDataTypeEnum.String,
|
Text = "TaskNo"
|
});
|
listPosition.Add(new WcsPosition()
|
{
|
DeviceId = modDevice.Id,
|
StationNum = modDevice.StationNum,
|
PlcPos = (input.Pos + 4).ToString(),
|
PosType = PLCDataTypeEnum.UShort,
|
Text = "TaskType"
|
});
|
listPosition.Add(new WcsPosition()
|
{
|
DeviceId = modDevice.Id,
|
StationNum = modDevice.StationNum,
|
PlcPos = (input.Pos + 6).ToString(),
|
PosType = PLCDataTypeEnum.UShort,
|
Text = "StartLocatNo"
|
});
|
listPosition.Add(new WcsPosition()
|
{
|
DeviceId = modDevice.Id,
|
StationNum = modDevice.StationNum,
|
PlcPos = (input.Pos + 8).ToString(),
|
PosType = PLCDataTypeEnum.UShort,
|
Text = "EndLocatNo"
|
});
|
|
await _wcsDeviceRep.Context.Insertable(listPosition).ExecuteCommandAsync();
|
}
|
|
/// <summary>
|
/// 获取设备信息列表
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
[HttpGet]
|
[ApiDescriptionSettings(Name = "List")]
|
[DisplayName("获取设备信息列表")]
|
public async Task<List<WcsDeviceOutput>> List([FromQuery] PageWcsDeviceInput input)
|
{
|
var list = await _wcsDeviceRep.AsQueryable()
|
.LeftJoin<WcsPlc>((a, b) => a.PlcId == b.Id)
|
.Select<WcsDeviceOutput>((a, b) => new WcsDeviceOutput() { Type = b.Type }, true)
|
.ToListAsync();
|
//获取跺机的状态
|
foreach (var modDevice in list)
|
{
|
if (_sysCacheService.ExistKey("PlcConn" + modDevice.PlcId))
|
{
|
var cachePlc = _sysCacheService.Get<WcsPlc>("PlcConn" + modDevice.PlcId);
|
modDevice.Status = cachePlc.IsConn;
|
}
|
else
|
{
|
modDevice.Status = false;
|
}
|
}
|
|
return list;
|
}
|
}
|