You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haoliang-net/src/CncService/Impl/CollectAddressService.cs

202 lines
7.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Linq;
using System.Collections.Generic;
using CncModels.Dto;
using CncModels.Dto.CollectAddress;
using CncRepository.Interface;
using CncService.Interface;
using CncService;
using CncModels.Entity;
namespace CncService.Impl
{
/// <summary>
/// 采集地址实现
/// </summary>
public class CollectAddressService : ICollectAddressService
{
private readonly ICollectAddressRepository _collectAddressRepository;
private readonly IMachineRepository _machineRepository;
private readonly IBrandRepository _brandRepository;
private readonly IWorkshopRepository _workshopRepository;
private readonly ICollectRawRepository _collectRawRepository;
public CollectAddressService(ICollectAddressRepository collectAddressRepository,
IMachineRepository machineRepository,
IBrandRepository brandRepository,
IWorkshopRepository workshopRepository,
ICollectRawRepository collectRawRepository)
{
_collectAddressRepository = collectAddressRepository;
_machineRepository = machineRepository;
_brandRepository = brandRepository;
_workshopRepository = workshopRepository;
_collectRawRepository = collectRawRepository;
}
public PagedResult<CollectAddressListItem> GetList(CollectAddressQuery query)
{
return _collectAddressRepository.GetList(query);
}
public CollectAddressDetailResponse GetById(int id)
{
var detail = _collectAddressRepository.GetById(id);
if (detail == null) throw new BusinessException(CncModels.Constants.ErrorCode.NotFound, "采集地址不存在");
string brandName = null;
var brand = _brandRepository.GetById(detail.BrandId);
if (brand != null) brandName = brand.BrandName;
// 直接映射到输出 DTO
return new CollectAddressDetailResponse
{
Id = detail.Id,
Name = detail.Name,
Url = detail.Url,
BrandId = detail.BrandId,
BrandName = brandName,
CollectInterval = detail.CollectInterval,
IsEnabled = detail.IsEnabled == 1
};
}
public int Create(CreateCollectAddressRequest request)
{
if (request == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.BadRequest, "请求参数错误");
if (_brandRepository.GetById(request.BrandId) == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.NotFound, "品牌不存在");
var entity = new CollectAddress
{
Name = request.Name,
Url = request.Url,
BrandId = request.BrandId,
CollectInterval = request.CollectInterval,
IsEnabled = 1,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
var id = _collectAddressRepository.Create(entity);
// 绑定机床
if (request.MachineIds != null)
{
foreach (var mid in request.MachineIds)
{
_machineRepository.SetCollectAddress(mid, id);
}
}
return id;
}
public bool Update(int id, UpdateCollectAddressRequest request)
{
var address = _collectAddressRepository.GetById(id);
if (address == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.NotFound, "采集地址不存在");
if (request == null) throw new CncService.BusinessException(CncModels.Constants.ErrorCode.BadRequest, "请求参数错误");
address.Name = request.Name ?? address.Name;
address.Url = request.Url ?? address.Url;
address.BrandId = (request.BrandId != 0) ? request.BrandId : address.BrandId;
address.CollectInterval = (request.CollectInterval != 0) ? request.CollectInterval : address.CollectInterval;
address.UpdatedAt = DateTime.Now;
_collectAddressRepository.Update(address);
// 全量替换机床关联
if (request.MachineIds != null)
{
// 先清除该地址下所有机床的关联
var currentMachines = _machineRepository.GetEnabledByAddressId(id);
foreach (var m in currentMachines)
{
_machineRepository.SetCollectAddress(m.Id, null);
}
// 再绑定新选中的机床
foreach (var mid in request.MachineIds)
{
_machineRepository.SetCollectAddress(mid, id);
}
}
return true;
}
public bool Delete(int id)
{
// 检查关联机床数量
if (_collectAddressRepository.GetMachineCount(id) > 0) return false;
return _collectAddressRepository.Delete(id);
}
public bool ToggleEnabled(int id)
{
return _collectAddressRepository.ToggleEnabled(id);
}
/// <inheritdoc/>
public List<CollectAddressMachineItem> GetMachines(int collectAddressId)
{
var machines = _machineRepository.GetEnabledByAddressId(collectAddressId);
var result = new List<CollectAddressMachineItem>();
foreach (var m in machines)
{
string workshopName = null;
if (m.WorkshopId > 0)
{
var workshop = _workshopRepository.GetById(m.WorkshopId);
if (workshop != null) workshopName = workshop.Name;
}
result.Add(new CollectAddressMachineItem
{
MachineId = m.Id,
MachineName = m.Name ?? m.DeviceCode,
DeviceCode = m.DeviceCode,
WorkshopName = workshopName,
IsOnline = m.IsOnline == 1,
ProgramName = m.LastProgramName
});
}
return result;
}
/// <inheritdoc/>
public List<CollectAddressRecordItem> GetCollectRecords(int collectAddressId)
{
var paged = _collectRawRepository.GetByAddressId(collectAddressId, 1, 20);
var result = new List<CollectAddressRecordItem>();
foreach (var r in paged.Items)
{
result.Add(new CollectAddressRecordItem
{
RequestTime = r.RequestTime.ToString("yyyy-MM-dd HH:mm:ss"),
Duration = r.ResponseDuration ?? 0,
IsSuccess = r.IsSuccess == 1,
MachineCount = 0, // 采集服务运行时从JSON解析得到
MachineName = ""
});
}
return result;
}
/// <inheritdoc/>
public CollectAddressRawJsonResponse GetRawJson(int collectAddressId, string recordId = null)
{
// 优先按 recordId请求时间查找否则取最新一条
CollectRaw raw = null;
if (!string.IsNullOrEmpty(recordId))
{
var paged = _collectRawRepository.GetByAddressId(collectAddressId, 1, 100);
DateTime dt;
if (DateTime.TryParse(recordId, out dt))
{
raw = paged.Items.FirstOrDefault(r => r.RequestTime.ToString("yyyy-MM-dd HH:mm:ss") == recordId);
}
}
if (raw == null)
{
raw = _collectRawRepository.GetLatestByAddressId(collectAddressId);
}
return new CollectAddressRawJsonResponse
{
RawJson = raw?.RawJson ?? "[]"
};
}
}
}