using System;
using System.Collections.Generic;
using CncService.Interface;
using CncModels.Dto;
using CncModels.Dto.Production;
using CncModels.Entity;
using CncModels.Constants;
using CncRepository.Interface;
namespace CncService.Impl
{
///
/// 产量管理实现
///
public class ProductionService : IProductionService
{
private readonly IDailyProductionRepository _dailyProductionRepository;
private readonly IProductionSegmentRepository _productionSegmentRepository;
private readonly IProductionAdjustmentRepository _productionAdjustmentRepository;
public ProductionService(
IDailyProductionRepository dailyProductionRepository,
IProductionSegmentRepository productionSegmentRepository,
IProductionAdjustmentRepository productionAdjustmentRepository)
{
_dailyProductionRepository = dailyProductionRepository ?? throw new ArgumentNullException(nameof(dailyProductionRepository));
_productionSegmentRepository = productionSegmentRepository ?? throw new ArgumentNullException(nameof(productionSegmentRepository));
_productionAdjustmentRepository = productionAdjustmentRepository ?? throw new ArgumentNullException(nameof(productionAdjustmentRepository));
}
///
public PagedResult GetList(ProductionQuery query)
{
if (query == null) throw new BusinessException(ErrorCode.BadRequest, "查询参数不能为空");
return _dailyProductionRepository.GetList(query);
}
///
public DailySummaryResponse GetSummary(DateTime? date, int? workshopId)
{
var targetDate = date ?? DateTime.Today;
var total = _dailyProductionRepository.GetTotalByDateRange(targetDate, targetDate, workshopId);
// 如果汇总表无数据,从产量分段实时计算
if (total == 0)
{
total = _productionSegmentRepository.GetTotalByDateRange(targetDate, targetDate, workshopId);
}
int machineCount = _productionSegmentRepository.GetActiveMachineCount(targetDate, workshopId);
decimal cuttingTime = _productionSegmentRepository.GetTotalCuttingTimeByDate(targetDate);
return new DailySummaryResponse
{
TotalQuantity = (int)total,
MachineCount = machineCount,
NormalCount = 0,
OfflineCount = 0,
totalCuttingTime = cuttingTime
};
}
///
public decimal GetTotalByDateRange(DateTime startDate, DateTime endDate, int? workshopId)
{
return _dailyProductionRepository.GetTotalByDateRange(startDate, endDate, workshopId);
}
///
public bool Adjust(ProductionAdjustRequest request)
{
if (request == null) throw new BusinessException(ErrorCode.BadRequest, "请求参数不能为空");
decimal newValue;
decimal.TryParse(request.NewValue, out newValue);
var entity = new ProductionAdjustment
{
TargetTable = request.TargetTable,
TargetId = request.TargetId,
FieldName = request.FieldName,
OldValue = null,
NewValue = newValue,
Reason = request.Reason,
OperatorIp = "",
CreatedAt = DateTime.Now
};
_productionAdjustmentRepository.Create(entity);
return true;
}
///
public List GetAdjustmentHistory(int recordId)
{
var records = _productionAdjustmentRepository.GetByTargetId(recordId);
var result = new List();
foreach (var r in records)
{
result.Add(new AdjustmentHistoryItem
{
Id = (int)r.Id,
OldValue = r.OldValue?.ToString() ?? "-",
NewValue = r.NewValue.ToString(),
Reason = r.Reason ?? "",
OperatorIp = r.OperatorIp ?? "",
CreatedAt = r.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")
});
}
return result;
}
}
}