设备管理模块Entity添加和controller

This commit is contained in:
2026-08-28 10:40:28 +08:00
parent 9b852e5c95
commit 3e3341dd19
11 changed files with 994 additions and 8 deletions
@@ -0,0 +1,141 @@
using Microsoft.AspNetCore.Mvc;
using Model;
using Model.Entity.Asset;
using ORM;
using Service.Implement;
using Service.Interface;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace IOT_API.Controllers.Asset
{
/// <summary>
/// 资产管理 - 设备台账控制器
/// </summary>
[Route("api/Equipment")] //资产管理模块下的设备管理接口
[ApiController]
public class EquipmentController : ControllerBase
{
private readonly IEquipmentService _equipmentService;
public EquipmentController()
{
// 项目未注册 DI 容器,SqlSugarContext.DbContext 为静态单例,直接实例化服务
_equipmentService = new EquipmentService(
new SqlSugarRepository<EquipmentEntity>(),
new SqlSugarRepository<EquipmentStatusRecordEntity>());
}
/// <summary>
/// 设备列表(分页查询,支持关键字/分类/状态筛选)
/// </summary>
/// <param name="pageIndex">页码(从1开始,默认1</param>
/// <param name="pageSize">每页数量(默认10</param>
/// <param name="keyword">关键字(模糊匹配设备编号/名称)</param>
/// <param name="categoryId">设备分类Id0表示不过滤)</param>
/// <param name="status">设备状态(不传表示不过滤)</param>
[HttpGet("list")]
public async Task<Result<List<EquipmentEntity>>> 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;
}
/// <summary>
/// 查询全部设备(不分页,供下拉选择等场景使用)
/// </summary>
[HttpGet("all")]
public async Task<Result<List<EquipmentEntity>>> GetAll()
{
return await _equipmentService.GetAllAsync();
}
/// <summary>
/// 设备详情
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpGet("{id}")]
public async Task<Result<EquipmentEntity>> GetById(long id)
{
return await _equipmentService.GetByIdAsync(id);
}
/// <summary>
/// 新增设备
/// </summary>
/// <param name="entity">设备实体</param>
[HttpPost]
public async Task<Result<bool>> Add([FromBody] EquipmentEntity entity)
{
return await _equipmentService.InsertAsync(entity);
}
/// <summary>
/// 修改设备
/// </summary>
/// <param name="id">设备主键 Id</param>
/// <param name="entity">设备实体</param>
[HttpPut("{id}")]
public async Task<Result<bool>> Update(long id, [FromBody] EquipmentEntity entity)
{
entity.Id = id;
return await _equipmentService.UpdateAsync(entity);
}
/// <summary>
/// 删除设备(软删除)
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpDelete("{id}")]
public async Task<Result<bool>> Delete(long id)
{
return await _equipmentService.DeleteEquipmentAsync(id);
}
/// <summary>
/// 变更设备状态(同步记录状态变更历史)
/// </summary>
/// <param name="id">设备主键 Id</param>
/// <param name="request">状态变更请求</param>
[HttpPost("{id}/status")]
public async Task<Result<bool>> ChangeStatus(long id, [FromBody] ChangeStatusRequest request)
{
return await _equipmentService.ChangeStatusAsync(id, request.Status, request.Operator, request.Remark);
}
/// <summary>
/// 查询设备状态变更历史记录
/// </summary>
/// <param name="id">设备主键 Id</param>
[HttpGet("{id}/status-records")]
public async Task<Result<List<EquipmentStatusRecordEntity>>> GetStatusRecords(long id)
{
return await _equipmentService.GetStatusRecordsAsync(id);
}
}
/// <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; }
}
}
+29 -7
View File
@@ -1,4 +1,5 @@
using Common;
using ORM;
using System.Text.Json;
namespace WebAPI
@@ -7,33 +8,54 @@ namespace WebAPI
{
public static void Main(string[] args)
{
// ======= 崩溃转储:未处理异常时自动生成 MiniDump =======
// ======= תδ쳣ʱԶ MiniDump =======
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
//记录dump文件
//¼dumpļ
Exception ex = e.ExceptionObject as Exception;
MiniDump.TryDump($"dumps\\Error_{DateTime.Now:yyyy-MM-dd HH-mm-ss-ms}.dmp", MiniDump.Option.WithFullMemory, ex);
};
// ======= 崩溃转储结束 =======
// ======= ת =======
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// ======= 核心配置开始 =======
// ======= ÿʼ =======
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString;
// 3. 属性命名策略保持原样(不强制转为驼峰)
// 3. ԱԭǿתΪշ壩
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
// ======= 核心配置结束 =======
// ======= ý =======
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// ======= Database init start =======
// Npgsql 时间兼容开关:允许向 PostgreSQL 写入本地时间 DateTimeDateTime.Now
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
// 从 appsettings.json 读取数据库配置(PostgreSQL
var dbConfig = builder.Configuration.GetSection("Database");
DatabaseConfig.InitPostgreSql(
dbConfig["Server"] ?? "localhost",
int.TryParse(dbConfig["Port"], out var port) ? port : 5432,
dbConfig["Database"] ?? "iot",
dbConfig["Uid"] ?? "postgres",
dbConfig["Pwd"] ?? "");
// 数据库不存在则创建,并检查连接(租户雪花机器码可按需设置)
DatabaseConfig.SetTenant(10001);
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
// CodeFirst 自动建表:扫描 Model.dll 中所有以 Entity 结尾的类建表/补列
SqlSugarContext.InitDatabase();
// ======= Database init end =======
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -43,7 +65,7 @@ namespace WebAPI
app.UseSwaggerUI();
}
// 注意:如果上位机只支持 http 请求,现场部署时你可能需要根据实际情况注释掉下面这行 Https 重定向
// עλֻ֧ http ֳʱҪʵע͵ Https ض
app.UseHttpsRedirection();
app.UseAuthorization();
+8 -1
View File
@@ -5,5 +5,12 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Database": {
"Server": "localhost",
"Port": 5432,
"Database": "iot",
"Uid": "postgres",
"Pwd": "postgres"
}
}