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
{
///
/// 光缆管理
///
[Route("business/OdfCables")]
public class OdfCablesController : BaseController
{
///
/// 光缆管理接口
///
private readonly IOdfCablesService _OdfCablesService;
private readonly IOdfAuditLogsService _auditLogsService;
public OdfCablesController(
IOdfCablesService OdfCablesService,
IOdfAuditLogsService auditLogsService)
{
_OdfCablesService = OdfCablesService;
_auditLogsService = auditLogsService;
}
///
/// 查询光缆列表
///
///
///
[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);
}
///
/// 搜索光缆、故障和标石/杆号牌(限定公司范围)
///
///
[HttpGet("search")]
[ActionPermissionFilter(Permission = "odfcables:query")]
public IActionResult Search([FromQuery] long deptId, [FromQuery] string keyword)
{
var response = _OdfCablesService.Search(deptId, keyword);
return SUCCESS(response);
}
///
/// 新增光缆
///
///
[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
{
["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);
}
///
/// 修改光缆
///
///
[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
{
["光缆名称"] = 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
{
["光缆名称"] = 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);
}
///
/// 查询光缆详情
///
///
///
[HttpGet("{id}")]
[ActionPermissionFilter(Permission = "odfcables:query")]
public IActionResult GetDetail(int id)
{
var response = _OdfCablesService.GetDetail(id);
return SUCCESS(response);
}
///
/// 删除光缆
///
///
[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
{
["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);
}
///
/// 导出光缆列表
///
///
[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);
}
}
}