285 lines
11 KiB
C#
285 lines
11 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using ZR.Model.Business.Dto;
|
||
using ZR.Model.Business;
|
||
using ZR.Service.Business.IBusinessService;
|
||
using ZR.Service.Business;
|
||
|
||
//创建时间:2025-08-05
|
||
namespace ZR.Admin.WebApi.Controllers.Business
|
||
{
|
||
/// <summary>
|
||
/// 框-信息
|
||
/// </summary>
|
||
[Route("business/OdfFrames")]
|
||
public class OdfFramesController : BaseController
|
||
{
|
||
/// <summary>
|
||
/// 框-信息接口
|
||
/// </summary>
|
||
private readonly IOdfFramesService _OdfFramesService;
|
||
|
||
/// <summary>
|
||
/// 端口
|
||
/// </summary>
|
||
private readonly IOdfPortsService _OdfPortsService;
|
||
/// <summary>
|
||
/// 机架列表接口
|
||
/// </summary>
|
||
private readonly IOdfRacksService _OdfRacksService;
|
||
private readonly IOdfRoomsService _odfRooms;
|
||
private readonly IOdfAuditLogsService _auditLogsService;
|
||
|
||
public OdfFramesController(IOdfRacksService OdfRacksService, IOdfRoomsService odfRooms,
|
||
IOdfFramesService odfFramesService,
|
||
IOdfPortsService odfPortsService,
|
||
IOdfAuditLogsService auditLogsService)
|
||
{
|
||
_OdfRacksService = OdfRacksService;
|
||
_odfRooms = odfRooms;
|
||
_OdfFramesService = odfFramesService;
|
||
_OdfPortsService = odfPortsService;
|
||
_auditLogsService = auditLogsService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询框-信息列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("list")]
|
||
[ActionPermissionFilter(Permission = "odfframes:list")]
|
||
public IActionResult QueryOdfFrames([FromQuery] OdfFramesQueryDto parm)
|
||
{
|
||
var response = _OdfFramesService.GetList(parm);
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 查询框-信息详情
|
||
/// </summary>
|
||
/// <param name="Id"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{Id}")]
|
||
[ActionPermissionFilter(Permission = "odfframes:query")]
|
||
public IActionResult GetOdfFrames(int Id)
|
||
{
|
||
var response = _OdfFramesService.GetInfo(Id);
|
||
|
||
var info = response.Adapt<OdfFramesDto>();
|
||
return SUCCESS(info);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加框-信息
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ActionPermissionFilter(Permission = "odfframes:add")]
|
||
[Log(Title = "框-信息", BusinessType = BusinessType.INSERT)]
|
||
public async Task<IActionResult> AddOdfFrames([FromBody] OdfFramesExpertDto parm)
|
||
{
|
||
var modal = parm.Adapt<OdfFrames>().ToCreate(HttpContext);
|
||
var rooms = _odfRooms.GetById(parm.RoomId);
|
||
if (rooms == null)
|
||
{
|
||
return ToResponse(ResultCode.FAIL, "机房不存在");
|
||
}
|
||
modal.DeptId = rooms.DeptId ?? 0;
|
||
modal.RackId = parm.RackId;
|
||
modal.PortsRow = parm.RowCount;
|
||
modal.PortsCol = parm.PortsCount;
|
||
modal.PortsCount = parm.RowCount * parm.PortsCount;
|
||
modal.UpdateAt = DateTime.Now;
|
||
modal.CreatedAt = DateTime.Now;
|
||
var response = _OdfFramesService.AddOdfFrames(modal);
|
||
|
||
// INSERT 审计日志(仅记录框)
|
||
try
|
||
{
|
||
var newRecord = _OdfFramesService.GetFirst(f => f.Id == response.Id);
|
||
string newData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["Id"] = newRecord.Id,
|
||
["机架ID"] = newRecord.RackId,
|
||
["名称"] = newRecord.PortsName,
|
||
["部门ID"] = newRecord.DeptId,
|
||
["序号"] = newRecord.SequenceNumber,
|
||
["端口数量"] = newRecord.PortsCount,
|
||
["列数量"] = newRecord.PortsCol,
|
||
["行数量"] = newRecord.PortsRow,
|
||
["创建时间"] = newRecord.CreatedAt,
|
||
["修改时间"] = newRecord.UpdateAt,
|
||
});
|
||
_auditLogsService.AddLog(BuildAuditLog("odf_frames", response.Id, "INSERT", null, newData));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
NLog.LogManager.GetCurrentClassLogger().Error(ex, "审计日志写入失败");
|
||
}
|
||
|
||
var roomId = rooms.Id;
|
||
var roomName = rooms.RoomName;
|
||
|
||
var ra = _OdfRacksService.GetById(modal.RackId);
|
||
//添加机框结束
|
||
if (parm.RowCount > 0)
|
||
{
|
||
//添加行
|
||
if (parm.PortsCount > 0)
|
||
{
|
||
int index = 0;
|
||
//添加端口
|
||
|
||
var frame = response;
|
||
List<OdfPorts> ports = new List<OdfPorts>();
|
||
for (int row = 0; row < parm.RowCount; row++)
|
||
{
|
||
for (int port = 0; port < parm.PortsCount; port++)
|
||
{
|
||
ports.Add(new OdfPorts()
|
||
{
|
||
CreatedAt = DateTime.Now,
|
||
DeptId = rooms.DeptId ?? 0,
|
||
DeptName = rooms.DeptName,
|
||
RackId = frame.RackId,
|
||
RackName = ra.RackName,
|
||
RoomId = roomId,
|
||
RoomName = roomName,
|
||
FrameId = frame.Id,
|
||
FrameName = frame.PortsName,
|
||
Name = $"{(row + 1)}-{(port + 1)}",
|
||
RowNumber = row + 1,
|
||
PortNumber = port + 1,
|
||
OpticalAttenuation = "",
|
||
HistoryRemarks = "",
|
||
Remarks = "",
|
||
Status = parm.DefaultStatus,
|
||
UpdatedAt = DateTime.Now,
|
||
});
|
||
}
|
||
}
|
||
await _OdfPortsService.AsInsertable(ports).ExecuteReturnEntityAsync(true);
|
||
//如果超过100个机框,则休眠一下,防止服务器死机
|
||
index++;
|
||
if (index > 100)
|
||
{
|
||
Thread.Sleep(50);
|
||
index = 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新框-信息
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPut]
|
||
[ActionPermissionFilter(Permission = "odfframes:edit")]
|
||
[Log(Title = "框-信息", BusinessType = BusinessType.UPDATE)]
|
||
public async Task<IActionResult> UpdateOdfFrames([FromBody] OdfFramesDto parm)
|
||
{
|
||
var modal = parm.Adapt<OdfFrames>().ToUpdate(HttpContext);
|
||
|
||
// 查旧数据
|
||
var oldRecord = _OdfFramesService.GetFirst(f => f.Id == parm.Id);
|
||
string oldData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["名称"] = oldRecord.PortsName,
|
||
["序号"] = oldRecord.SequenceNumber,
|
||
["端口数量"] = oldRecord.PortsCount,
|
||
["列数量"] = oldRecord.PortsCol,
|
||
["行数量"] = oldRecord.PortsRow,
|
||
["修改时间"] = oldRecord.UpdateAt,
|
||
});
|
||
|
||
var oldModel = _OdfFramesService.GetById(parm.Id);
|
||
var response = _OdfFramesService.UpdateOdfFrames(modal);
|
||
|
||
// UPDATE 审计日志(仅记录框)
|
||
try
|
||
{
|
||
var newRecord = _OdfFramesService.GetFirst(f => f.Id == parm.Id);
|
||
string newData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["名称"] = newRecord.PortsName,
|
||
["序号"] = newRecord.SequenceNumber,
|
||
["端口数量"] = newRecord.PortsCount,
|
||
["列数量"] = newRecord.PortsCol,
|
||
["行数量"] = newRecord.PortsRow,
|
||
["修改时间"] = newRecord.UpdateAt,
|
||
});
|
||
_auditLogsService.AddLog(BuildAuditLog("odf_frames", parm.Id, "UPDATE", oldData, newData));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
NLog.LogManager.GetCurrentClassLogger().Error(ex, "审计日志写入失败");
|
||
}
|
||
|
||
if (response > 0)
|
||
{
|
||
var rortsName = oldModel.PortsName;
|
||
var frameId = modal.Id;
|
||
var rackId = modal.RackId;
|
||
|
||
if (oldModel.PortsName != modal.PortsName)
|
||
{
|
||
// 最直接的转换
|
||
await _OdfPortsService.UpdateAsync(
|
||
it => it.FrameId == frameId && it.RackId == rackId, // WHERE条件
|
||
it => new OdfPorts // SET部分
|
||
{
|
||
FrameName = modal.PortsName,
|
||
}
|
||
);
|
||
}
|
||
}
|
||
return ToResponse(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除框-信息
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost("delete/{ids}")]
|
||
[ActionPermissionFilter(Permission = "odfframes:delete")]
|
||
[Log(Title = "框-信息", BusinessType = BusinessType.DELETE)]
|
||
public IActionResult DeleteOdfFrames([FromRoute] string ids)
|
||
{
|
||
var idArr = Tools.SplitAndConvert<int>(ids);
|
||
|
||
// 循环每条记录分别记录 DELETE 审计日志
|
||
foreach (var id in idArr)
|
||
{
|
||
var oldRecord = _OdfFramesService.GetFirst(f => f.Id == id);
|
||
string oldData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["Id"] = oldRecord.Id,
|
||
["机架ID"] = oldRecord.RackId,
|
||
["名称"] = oldRecord.PortsName,
|
||
["部门ID"] = oldRecord.DeptId,
|
||
["序号"] = oldRecord.SequenceNumber,
|
||
["端口数量"] = oldRecord.PortsCount,
|
||
["列数量"] = oldRecord.PortsCol,
|
||
["行数量"] = oldRecord.PortsRow,
|
||
["创建时间"] = oldRecord.CreatedAt,
|
||
["修改时间"] = oldRecord.UpdateAt,
|
||
});
|
||
try
|
||
{
|
||
_auditLogsService.AddLog(BuildAuditLog("odf_frames", id, "DELETE", oldData, null));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
NLog.LogManager.GetCurrentClassLogger().Error(ex, "审计日志写入失败");
|
||
}
|
||
}
|
||
|
||
return ToResponse(_OdfFramesService.Delete(idArr, "删除框-信息"));
|
||
}
|
||
|
||
}
|
||
} |