操控平台后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.5 KiB

// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using System.ComponentModel;
using Admin.NET.Bodk.Core;
using Admin.NET.Bodk.Device.Entities;
using Admin.NET.Bodk.Device.Models;
using Admin.NET.Core;
using Admin.NET.Core.Service;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NewLife.Caching;
namespace Admin.NET.Bodk.Device;
[ApiDescriptionSettings(Groups = new[] { "Bodk Groups" }, Name = "Device", Description = "设备服务")]
public class DeviceService(
UserManager userManager,
SysOrgService sysOrgService,
SysUserService sysUserService,
SqlSugarRepository<DeviceEntity> repository,
ICache cache)
: ServiceBase(userManager, sysOrgService, sysUserService), ITransient
{
[ApiDescriptionSettings(Name = "Page"), HttpPost]
[DisplayName("获取设备分页列表")]
public async Task<SqlSugarPagedList<DeviceOutput>> Page(DeviceQueryInput queryInput)
{
var queryable = await GetDataOutOfOrg(repository.AsQueryable());
return await queryable.WhereIF(!string.IsNullOrWhiteSpace(queryInput.SerialNumber),
u => queryInput.SerialNumber != null && u.SerialNumber.Contains(queryInput.SerialNumber))
.WhereIF(!string.IsNullOrWhiteSpace(queryInput.Name),
u => queryInput.Name != null && u.Name.Contains(queryInput.Name))
.WhereIF(!string.IsNullOrWhiteSpace(queryInput.Name),
u => queryInput.Name != null && u.Name.Contains(queryInput.Name))
.WhereIF(queryInput.Id is not null, u => u.Id == queryInput.Id)
.WhereIF(queryInput.DeviceType is not null, u => u.DeviceType == queryInput.DeviceType)
.Select(u => u.Adapt<DeviceOutput>())
.Mapper(u => u.Online = cache.Get<bool>($"bodk:device:{u.SerialNumber}:summary:online"))
.WhereIF(queryInput.Online is not null, u => u.Online == queryInput.Online)
.ToPagedListAsync(queryInput.Page, queryInput.PageSize);
}
[ApiDescriptionSettings(Name = "Add"), HttpPost]
[DisplayName("新增设备")]
public async Task<long> AddDevice(AddDeviceInput input)
{
var isExist = await repository.AsQueryable().AnyAsync(u => u.SerialNumber == input.SerialNumber);
if (isExist) throw Oops.Oh(ErrorCodeEnum.Device1000);
var entity = input.Adapt<DeviceEntity>();
entity.OrgId = UserManager.OrgId;
return await repository.InsertReturnBigIdentityAsync(entity);
}
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新设备")]
public async Task UpdateDevice(UpdateDeviceInput input)
{
var isExist = await repository.AsQueryable().AnyAsync(u => u.Id == input.Id);
if (!isExist) throw Oops.Oh("要更新的设备不存在");
var entity = await repository.GetByIdAsync(input.Id);
entity.DeviceType = input.DeviceType ?? entity.DeviceType;
entity.Name = input.Name ?? entity.Name;
entity.OrgId = input.OrgId ?? entity.OrgId;
await repository.UpdateAsync(entity);
}
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
[DisplayName("删除设备")]
public async Task DeleteDevice(long id)
{
var isExist = await repository.AsQueryable().AnyAsync(u => u.Id == id);
if (!isExist) throw Oops.Oh("要删除的设备不存在");
await repository.DeleteByIdAsync(id);
}
}