using Infrastructure.Attribute;
using ZR.Model;
using ZR.Model.Business;
using ZR.Model.Business.Dto;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;
namespace ZR.Service.Business
{
///
/// 光缆管理Service业务层处理
///
[AppService(ServiceType = typeof(IOdfCablesService), ServiceLifetime = LifeTime.Transient)]
public class OdfCablesService : BaseService, IOdfCablesService
{
///
/// 按 DeptId 过滤光缆列表(支持分页和 CableName 模糊查询)
///
///
///
public PagedInfo GetList(OdfCablesQueryDto parm)
{
var predicate = Expressionable.Create();
predicate = predicate.AndIF(parm.DeptId != null, it => it.DeptId == parm.DeptId);
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.CableName), it => it.CableName.Contains(parm.CableName));
var response = Queryable()
.Where(predicate.ToExpression())
.OrderByDescending(it => it.CreatedAt)
.ToPage(parm);
return response;
}
///
/// 在指定公司范围内搜索光缆和故障
///
///
///
///
public object Search(long deptId, string keyword)
{
// 搜索光缆:按 DeptId 过滤,CableName LIKE keyword
var cables = Queryable()
.Where(c => c.DeptId == deptId)
.WhereIF(!string.IsNullOrEmpty(keyword), c => c.CableName.Contains(keyword))
.Select(c => new { c.Id, c.CableName })
.ToList();
// 搜索故障:联查 odf_cables 按 DeptId 过滤,对 FaultReason/Mileage/Location LIKE keyword
var faults = Context.Queryable()
.LeftJoin((f, c) => f.CableId == c.Id)
.Where((f, c) => c.DeptId == deptId)
.WhereIF(!string.IsNullOrEmpty(keyword), (f, c) =>
f.FaultReason.Contains(keyword) ||
f.Mileage.Contains(keyword) ||
f.Location.Contains(keyword))
.OrderByDescending((f, c) => f.FaultTime)
.Select((f, c) => new
{
f.Id,
f.FaultTime,
f.FaultReason,
f.Mileage,
CableName = c.CableName
})
.ToList();
return new { cables, faults };
}
///
/// 新增光缆
///
///
///
public OdfCables Add(OdfCables model)
{
model.CreatedAt = DateTime.Now;
model.UpdatedAt = DateTime.Now;
return Insertable(model).ExecuteReturnEntity();
}
///
/// 修改光缆
///
///
///
public int Update(OdfCables model)
{
model.UpdatedAt = DateTime.Now;
return base.Update(model, true);
}
///
/// 删除光缆
///
///
///
public int Delete(int id)
{
return base.Delete(id);
}
///
/// 获取光缆详情
///
///
///
public OdfCables GetDetail(int id)
{
return GetFirst(x => x.Id == id);
}
///
/// 导出光缆列表
///
///
///
public PagedInfo ExportList(OdfCablesQueryDto parm)
{
parm.PageNum = 1;
parm.PageSize = 100000;
return GetList(parm);
}
}
}