This commit is contained in:
“hsc”
2026-08-28 15:20:58 +08:00
16 changed files with 423 additions and 85 deletions
@@ -1,9 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Model;
using Model.Dto.Asset;
using Model.Entity.Asset;
using Model.Mapper;
using Service.Interface;
using SqlSugar;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace IOT_API.Controllers.Asset
@@ -11,16 +14,21 @@ namespace IOT_API.Controllers.Asset
/// <summary>
/// 资产管理 - 设备台账控制器
/// </summary>
[Route("api/Equipment")] //资产管理模块下的设备管理接口
[ApiController]
[Route("api/asset/equipment")]
[Route("api/asset/equipment")] //资产管理模块下的设备管理接口
public class EquipmentController : ControllerBase
{
private readonly IEquipmentService _equipmentService;
private readonly IEquipmentAttachmentService _attachmentService;
private readonly IWebHostEnvironment _webHostEnvironment;
public EquipmentController(IEquipmentService equipmentService)
public EquipmentController(IEquipmentService equipmentService,
IEquipmentAttachmentService attachmentService,
IWebHostEnvironment webHostEnvironment)
{
_equipmentService = equipmentService;
_attachmentService = attachmentService;
_webHostEnvironment = webHostEnvironment;
}
/// <summary>
@@ -32,22 +40,27 @@ namespace IOT_API.Controllers.Asset
/// <param name="categoryId">设备分类Id0表示不过滤)</param>
/// <param name="status">设备状态(不传表示不过滤)</param>
[HttpGet("list")]
public async Task<Result<List<EquipmentEntity>>> GetList(int pageIndex = 1, int pageSize = 10,
public async Task<Result<List<EquipmentDto>>> GetList(int pageIndex = 1, int pageSize = 10,
string? keyword = null, long categoryId = 0, EquipmentStatusEnum? status = null)
{
RefAsync<int> total = 0;
var result = await _equipmentService.GetPagedAsync(pageIndex, pageSize, total, keyword, categoryId, status);
Response.Headers["X-Total-Count"] = total.Value.ToString();
return result;
return result.IsSuccess
? Result<List<EquipmentDto>>.Success(result.Data.ToDtoList())
: Result<List<EquipmentDto>>.Error(result.Msg);
}
/// <summary>
/// 查询全部设备(不分页,供下拉选择等场景使用)
/// </summary>
[HttpGet("all")]
public async Task<Result<List<EquipmentEntity>>> GetAll()
public async Task<Result<List<EquipmentDto>>> GetAll()
{
return await _equipmentService.GetAllAsync();
var result = await _equipmentService.GetAllAsync();
return result.IsSuccess
? Result<List<EquipmentDto>>.Success(result.Data.ToDtoList())
: Result<List<EquipmentDto>>.Error(result.Msg);
}
/// <summary>
@@ -55,29 +68,33 @@ namespace IOT_API.Controllers.Asset
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpGet("{id}")]
public async Task<Result<EquipmentEntity>> GetById(long id)
public async Task<Result<EquipmentDto>> GetById(long id)
{
return await _equipmentService.GetByIdAsync(id);
var res = await _equipmentService.GetByIdAsync(id);
return res.IsSuccess
? Result<EquipmentDto>.Success(res.Data.ToDto())
: Result<EquipmentDto>.Error(res.Msg);
}
/// <summary>
/// 新增设备
/// </summary>
/// <param name="entity">设备实体</param>
/// <param name="dto">设备 DTOIsDel/CreateTime 等服务端字段不接受入参)</param>
[HttpPost]
public async Task<Result<bool>> Add([FromBody] EquipmentEntity entity)
public async Task<Result<bool>> Add([FromBody] EquipmentDto dto)
{
return await _equipmentService.InsertAsync(entity);
return await _equipmentService.InsertAsync(dto.ToEntity());
}
/// <summary>
/// 修改设备
/// </summary>
/// <param name="id">设备主键 Id</param>
/// <param name="entity">设备实体</param>
/// <param name="id">设备主键 Id(以路由参数为准,忽略请求体中的 Id)</param>
/// <param name="dto">设备 DTOIsDel/CreateTime 等服务端字段不接受入参)</param>
[HttpPut("{id}")]
public async Task<Result<bool>> Update(long id, [FromBody] EquipmentEntity entity)
public async Task<Result<bool>> Update(long id, [FromBody] EquipmentDto dto)
{
var entity = dto.ToEntity();
entity.Id = id;
return await _equipmentService.UpdateAsync(entity);
}
@@ -96,11 +113,11 @@ namespace IOT_API.Controllers.Asset
/// 变更设备状态(同步记录状态变更历史)
/// </summary>
/// <param name="id">设备主键 Id</param>
/// <param name="request">状态变更请求</param>
/// <param name="dto">状态变更请求(目标状态/操作人/备注)</param>
[HttpPost("{id}/status")]
public async Task<Result<bool>> ChangeStatus(long id, [FromBody] ChangeStatusRequest request)
public async Task<Result<bool>> ChangeStatus(long id, [FromBody] EquipmentStatusChangeDto dto)
{
return await _equipmentService.ChangeStatusAsync(id, request.Status, request.Operator, request.Remark);
return await _equipmentService.ChangeStatusAsync(id, dto.Status, dto.Operator, dto.Remark);
}
/// <summary>
@@ -108,30 +125,65 @@ namespace IOT_API.Controllers.Asset
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpGet("{id}/status-records")]
public async Task<Result<List<EquipmentStatusRecordEntity>>> GetStatusRecords(long id)
public async Task<Result<List<EquipmentStatusRecordDto>>> GetStatusRecords(long id)
{
return await _equipmentService.GetStatusRecordsAsync(id);
var res = await _equipmentService.GetStatusRecordsAsync(id);
return res.IsSuccess
? Result<List<EquipmentStatusRecordDto>>.Success(res.Data.ToDtoList())
: Result<List<EquipmentStatusRecordDto>>.Error(res.Msg);
}
/// <summary>
/// 查询设备附件列表(资产附件管理)
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpGet("{id}/attachments")]
public async Task<Result<List<EquipmentAttachmentDto>>> GetAttachments(long id)
{
var res = await _attachmentService.GetByEquipmentAsync(id);
return res.IsSuccess
? Result<List<EquipmentAttachmentDto>>.Success(res.Data.ToDtoList())
: Result<List<EquipmentAttachmentDto>>.Error(res.Msg);
}
/// <summary>
/// 上传设备附件(multipart/form-data,文件字段名 file
/// </summary>
/// <param name="id">设备主键 Id</param>
/// <param name="file">上传的文件</param>
/// <param name="fileType">文件类型(1使用说明书/2合格证明/3校准证书/4保养卡/5合同扫描件/6验收照片/99其他)</param>
/// <param name="uploader">上传人</param>
/// <param name="remark">备注</param>
[HttpPost("{id}/attachments")]
[RequestSizeLimit(50 * 1024 * 1024)]
public async Task<Result<EquipmentAttachmentDto>> UploadAttachment(long id, IFormFile file,
[FromForm] AttachmentTypeEnum fileType = AttachmentTypeEnum.Other,
[FromForm] string? uploader = null, [FromForm] string? remark = null)
{
if (file == null || file.Length == 0)
{
return Result<EquipmentAttachmentDto>.Error("请选择要上传的文件");
}
// wwwroot 不存在时退回运行目录下的 wwwroot(上传时会自动创建)
string webRootPath = _webHostEnvironment.WebRootPath
?? Path.Combine(_webHostEnvironment.ContentRootPath, "wwwroot");
using var stream = file.OpenReadStream();
var res = await _attachmentService.UploadAsync(id, stream, file.FileName, file.Length, fileType, uploader, remark, webRootPath);
return res.IsSuccess
? Result<EquipmentAttachmentDto>.Success(res.Data.ToDto())
: Result<EquipmentAttachmentDto>.Error(res.Msg);
}
/// <summary>
/// 删除设备附件
/// </summary>
/// <param name="attachmentId">附件主键 Id</param>
[HttpDelete("attachments/{attachmentId}")]
public async Task<Result<bool>> DeleteAttachment(long attachmentId)
{
return await _attachmentService.DeleteAsync(attachmentId);
}
}
/// <summary>
/// 状态变更请求体
/// </summary>
public class ChangeStatusRequest
{
/// <summary>
/// 目标状态
/// </summary>
public EquipmentStatusEnum Status { get; set; }
/// <summary>
/// 操作人
/// </summary>
public string? Operator { get; set; }
/// <summary>
/// 变更原因/备注
/// </summary>
public string? Remark { get; set; }
}
}
+4
View File
@@ -17,4 +17,8 @@
<ProjectReference Include="..\Service\Service.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Converters\" />
</ItemGroup>
</Project>
+4
View File
@@ -42,6 +42,7 @@ namespace WebAPI
options.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
// 自动注册业务服务(Service.Interface -> Service.Implement
builder.Services.AddBusinessServices();
@@ -67,6 +68,9 @@ namespace WebAPI
app.UseAuthorization();
// 静态文件:设备附件上传后通过 /uploads/... 访问(存储于 wwwroot
app.UseStaticFiles();
app.MapControllers();
app.Run();