203 lines
7.5 KiB
C#
203 lines
7.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using ZR.Model.Business;
|
||
using ZR.Model.Business.Dto;
|
||
using ZR.Service.Business.IBusinessService;
|
||
|
||
//创建时间:2025-09-21
|
||
namespace ZR.Admin.WebApi.Controllers.Business
|
||
{
|
||
/// <summary>
|
||
/// 光缆管理
|
||
/// </summary>
|
||
[Route("business/OdfCables")]
|
||
public class OdfCablesController : BaseController
|
||
{
|
||
/// <summary>
|
||
/// 光缆管理接口
|
||
/// </summary>
|
||
private readonly IOdfCablesService _OdfCablesService;
|
||
private readonly IOdfAuditLogsService _auditLogsService;
|
||
|
||
public OdfCablesController(
|
||
IOdfCablesService OdfCablesService,
|
||
IOdfAuditLogsService auditLogsService)
|
||
{
|
||
_OdfCablesService = OdfCablesService;
|
||
_auditLogsService = auditLogsService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询光缆列表
|
||
/// </summary>
|
||
/// <param name="parm"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("list")]
|
||
[ActionPermissionFilter(Permission = "odfcables:list")]
|
||
public IActionResult GetList([FromQuery] OdfCablesQueryDto parm)
|
||
{
|
||
var userDeptId = HttpContext.GetDeptId();
|
||
// 如果前端指定了公司 deptId,则只查该公司的光缆(需在用户可见范围内)
|
||
var targetDeptId = parm.DeptId ?? userDeptId;
|
||
var response = _OdfCablesService.GetList(parm, targetDeptId);
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 搜索光缆、故障和标石/杆号牌(限定公司范围)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpGet("search")]
|
||
[ActionPermissionFilter(Permission = "odfcables:query")]
|
||
public IActionResult Search([FromQuery] long deptId, [FromQuery] string keyword)
|
||
{
|
||
var response = _OdfCablesService.Search(deptId, keyword);
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增光缆
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ActionPermissionFilter(Permission = "odfcables:add")]
|
||
[Log(Title = "光缆管理", BusinessType = BusinessType.INSERT)]
|
||
public IActionResult Add([FromBody] OdfCables parm)
|
||
{
|
||
parm.CreatedAt = DateTime.Now;
|
||
parm.UpdatedAt = DateTime.Now;
|
||
var response = _OdfCablesService.Add(parm);
|
||
|
||
// 手动记录 INSERT 审计日志
|
||
try
|
||
{
|
||
var newRecord = _OdfCablesService.GetFirst(f => f.Id == parm.Id);
|
||
string newData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["Id"] = newRecord.Id,
|
||
["光缆名称"] = newRecord.CableName,
|
||
["部门ID"] = newRecord.DeptId,
|
||
["部门名称"] = newRecord.DeptName,
|
||
["创建时间"] = newRecord.CreatedAt,
|
||
["更新时间"] = newRecord.UpdatedAt,
|
||
});
|
||
_auditLogsService.AddLog(BuildAuditLog("odf_cables", parm.Id, "INSERT", null, newData));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
NLog.LogManager.GetCurrentClassLogger().Error(ex, "审计日志写入失败");
|
||
}
|
||
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改光缆
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPut]
|
||
[ActionPermissionFilter(Permission = "odfcables:edit")]
|
||
[Log(Title = "光缆管理", BusinessType = BusinessType.UPDATE)]
|
||
public IActionResult Update([FromBody] OdfCables parm)
|
||
{
|
||
// 查旧数据(仅记录 UPDATE 实际修改的字段)
|
||
var oldRecord = _OdfCablesService.GetFirst(f => f.Id == parm.Id);
|
||
string oldData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["光缆名称"] = oldRecord.CableName,
|
||
["部门ID"] = oldRecord.DeptId,
|
||
["部门名称"] = oldRecord.DeptName,
|
||
["更新时间"] = oldRecord.UpdatedAt,
|
||
});
|
||
|
||
parm.UpdatedAt = DateTime.Now;
|
||
var response = _OdfCablesService.Update(parm);
|
||
|
||
// 查新数据并记录 UPDATE 审计日志
|
||
try
|
||
{
|
||
var newRecord = _OdfCablesService.GetFirst(f => f.Id == parm.Id);
|
||
string newData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["光缆名称"] = newRecord.CableName,
|
||
["部门ID"] = newRecord.DeptId,
|
||
["部门名称"] = newRecord.DeptName,
|
||
["更新时间"] = newRecord.UpdatedAt,
|
||
});
|
||
_auditLogsService.AddLog(BuildAuditLog("odf_cables", parm.Id, "UPDATE", oldData, newData));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
NLog.LogManager.GetCurrentClassLogger().Error(ex, "审计日志写入失败");
|
||
}
|
||
|
||
return ToResponse(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询光缆详情
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{id}")]
|
||
[ActionPermissionFilter(Permission = "odfcables:query")]
|
||
public IActionResult GetDetail(int id)
|
||
{
|
||
var response = _OdfCablesService.GetDetail(id);
|
||
return SUCCESS(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除光缆
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost("delete/{id}")]
|
||
[ActionPermissionFilter(Permission = "odfcables:delete")]
|
||
[Log(Title = "光缆管理", BusinessType = BusinessType.DELETE)]
|
||
public IActionResult Delete(int id)
|
||
{
|
||
// 删除前记录 DELETE 审计日志
|
||
var oldRecord = _OdfCablesService.GetFirst(f => f.Id == id);
|
||
string oldData = BuildAuditJson(new Dictionary<string, object>
|
||
{
|
||
["Id"] = oldRecord.Id,
|
||
["光缆名称"] = oldRecord.CableName,
|
||
["部门ID"] = oldRecord.DeptId,
|
||
["部门名称"] = oldRecord.DeptName,
|
||
["创建时间"] = oldRecord.CreatedAt,
|
||
["更新时间"] = oldRecord.UpdatedAt,
|
||
});
|
||
try
|
||
{
|
||
_auditLogsService.AddLog(BuildAuditLog("odf_cables", id, "DELETE", oldData, null));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
NLog.LogManager.GetCurrentClassLogger().Error(ex, "审计日志写入失败");
|
||
}
|
||
|
||
var response = _OdfCablesService.Delete(id);
|
||
return ToResponse(response);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 导出光缆列表
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[Log(Title = "光缆管理", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
|
||
[HttpGet("export")]
|
||
[ActionPermissionFilter(Permission = "odfcables:export")]
|
||
public IActionResult Export([FromQuery] OdfCablesQueryDto parm)
|
||
{
|
||
var userDeptId = HttpContext.GetDeptId();
|
||
var targetDeptId = parm.DeptId ?? userDeptId;
|
||
var list = _OdfCablesService.ExportList(parm, targetDeptId);
|
||
if (list == null || list.Result == null || list.Result.Count <= 0)
|
||
{
|
||
return ToResponse(ResultCode.FAIL, "没有要导出的数据");
|
||
}
|
||
var result = ExportExcelMini(list.Result, "光缆列表", "光缆列表");
|
||
return ExportExcel(result.Item2, result.Item1);
|
||
}
|
||
}
|
||
}
|