475 lines
17 KiB
Go
475 lines
17 KiB
Go
package service
|
|
|
|
import (
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
|
)
|
|
|
|
// MonitorService 定义了监控相关的业务逻辑服务接口
|
|
type MonitorService interface {
|
|
ListSensorData(req *dto.ListSensorDataRequest) (*dto.ListSensorDataResponse, error)
|
|
ListDeviceCommandLogs(req *dto.ListDeviceCommandLogRequest) (*dto.ListDeviceCommandLogResponse, error)
|
|
ListPlanExecutionLogs(req *dto.ListPlanExecutionLogRequest) (*dto.ListPlanExecutionLogResponse, error)
|
|
ListTaskExecutionLogs(req *dto.ListTaskExecutionLogRequest) (*dto.ListTaskExecutionLogResponse, error)
|
|
ListPendingCollections(req *dto.ListPendingCollectionRequest) (*dto.ListPendingCollectionResponse, error)
|
|
ListUserActionLogs(req *dto.ListUserActionLogRequest) (*dto.ListUserActionLogResponse, error)
|
|
ListRawMaterialPurchases(req *dto.ListRawMaterialPurchaseRequest) (*dto.ListRawMaterialPurchaseResponse, error)
|
|
ListRawMaterialStockLogs(req *dto.ListRawMaterialStockLogRequest) (*dto.ListRawMaterialStockLogResponse, error)
|
|
ListFeedUsageRecords(req *dto.ListFeedUsageRecordRequest) (*dto.ListFeedUsageRecordResponse, error)
|
|
ListMedicationLogs(req *dto.ListMedicationLogRequest) (*dto.ListMedicationLogResponse, error)
|
|
ListPigBatchLogs(req *dto.ListPigBatchLogRequest) (*dto.ListPigBatchLogResponse, error)
|
|
ListWeighingBatches(req *dto.ListWeighingBatchRequest) (*dto.ListWeighingBatchResponse, error)
|
|
ListWeighingRecords(req *dto.ListWeighingRecordRequest) (*dto.ListWeighingRecordResponse, error)
|
|
ListPigTransferLogs(req *dto.ListPigTransferLogRequest) (*dto.ListPigTransferLogResponse, error)
|
|
ListPigSickLogs(req *dto.ListPigSickLogRequest) (*dto.ListPigSickLogResponse, error)
|
|
ListPigPurchases(req *dto.ListPigPurchaseRequest) (*dto.ListPigPurchaseResponse, error)
|
|
ListPigSales(req *dto.ListPigSaleRequest) (*dto.ListPigSaleResponse, error)
|
|
ListNotifications(req *dto.ListNotificationRequest) (*dto.ListNotificationResponse, error)
|
|
}
|
|
|
|
// monitorService 是 MonitorService 接口的具体实现
|
|
type monitorService struct {
|
|
sensorDataRepo repository.SensorDataRepository
|
|
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
|
executionLogRepo repository.ExecutionLogRepository
|
|
planRepository repository.PlanRepository
|
|
pendingCollectionRepo repository.PendingCollectionRepository
|
|
userActionLogRepo repository.UserActionLogRepository
|
|
rawMaterialRepo repository.RawMaterialRepository
|
|
medicationRepo repository.MedicationLogRepository
|
|
pigBatchRepo repository.PigBatchRepository
|
|
pigBatchLogRepo repository.PigBatchLogRepository
|
|
pigTransferLogRepo repository.PigTransferLogRepository
|
|
pigSickLogRepo repository.PigSickLogRepository
|
|
pigTradeRepo repository.PigTradeRepository
|
|
notificationRepo repository.NotificationRepository
|
|
}
|
|
|
|
// NewMonitorService 创建一个新的 MonitorService 实例
|
|
func NewMonitorService(
|
|
sensorDataRepo repository.SensorDataRepository,
|
|
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
|
executionLogRepo repository.ExecutionLogRepository,
|
|
planRepository repository.PlanRepository,
|
|
pendingCollectionRepo repository.PendingCollectionRepository,
|
|
userActionLogRepo repository.UserActionLogRepository,
|
|
rawMaterialRepo repository.RawMaterialRepository,
|
|
medicationRepo repository.MedicationLogRepository,
|
|
pigBatchRepo repository.PigBatchRepository,
|
|
pigBatchLogRepo repository.PigBatchLogRepository,
|
|
pigTransferLogRepo repository.PigTransferLogRepository,
|
|
pigSickLogRepo repository.PigSickLogRepository,
|
|
pigTradeRepo repository.PigTradeRepository,
|
|
notificationRepo repository.NotificationRepository,
|
|
) MonitorService {
|
|
return &monitorService{
|
|
sensorDataRepo: sensorDataRepo,
|
|
deviceCommandLogRepo: deviceCommandLogRepo,
|
|
executionLogRepo: executionLogRepo,
|
|
planRepository: planRepository,
|
|
pendingCollectionRepo: pendingCollectionRepo,
|
|
userActionLogRepo: userActionLogRepo,
|
|
rawMaterialRepo: rawMaterialRepo,
|
|
medicationRepo: medicationRepo,
|
|
pigBatchRepo: pigBatchRepo,
|
|
pigBatchLogRepo: pigBatchLogRepo,
|
|
pigTransferLogRepo: pigTransferLogRepo,
|
|
pigSickLogRepo: pigSickLogRepo,
|
|
pigTradeRepo: pigTradeRepo,
|
|
notificationRepo: notificationRepo,
|
|
}
|
|
}
|
|
|
|
// ListSensorData 负责处理查询传感器数据列表的业务逻辑
|
|
func (s *monitorService) ListSensorData(req *dto.ListSensorDataRequest) (*dto.ListSensorDataResponse, error) {
|
|
opts := repository.SensorDataListOptions{
|
|
DeviceID: req.DeviceID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.SensorType != nil {
|
|
sensorType := models.SensorType(*req.SensorType)
|
|
opts.SensorType = &sensorType
|
|
}
|
|
|
|
data, total, err := s.sensorDataRepo.List(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListSensorDataResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListDeviceCommandLogs 负责处理查询设备命令日志列表的业务逻辑
|
|
func (s *monitorService) ListDeviceCommandLogs(req *dto.ListDeviceCommandLogRequest) (*dto.ListDeviceCommandLogResponse, error) {
|
|
opts := repository.DeviceCommandLogListOptions{
|
|
DeviceID: req.DeviceID,
|
|
ReceivedSuccess: req.ReceivedSuccess,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.deviceCommandLogRepo.List(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListDeviceCommandLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPlanExecutionLogs 负责处理查询计划执行日志列表的业务逻辑
|
|
func (s *monitorService) ListPlanExecutionLogs(req *dto.ListPlanExecutionLogRequest) (*dto.ListPlanExecutionLogResponse, error) {
|
|
opts := repository.PlanExecutionLogListOptions{
|
|
PlanID: req.PlanID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.Status != nil {
|
|
status := models.ExecutionStatus(*req.Status)
|
|
opts.Status = &status
|
|
}
|
|
|
|
planLogs, total, err := s.executionLogRepo.ListPlanExecutionLogs(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
planIds := make([]uint, 0, len(planLogs))
|
|
for _, datum := range planLogs {
|
|
has := false
|
|
for _, id := range planIds {
|
|
if id == datum.PlanID {
|
|
has = true
|
|
break
|
|
}
|
|
}
|
|
if !has {
|
|
planIds = append(planIds, datum.PlanID)
|
|
}
|
|
}
|
|
plans, err := s.planRepository.GetPlansByIDs(planIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dto.NewListPlanExecutionLogResponse(planLogs, plans, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListTaskExecutionLogs 负责处理查询任务执行日志列表的业务逻辑
|
|
func (s *monitorService) ListTaskExecutionLogs(req *dto.ListTaskExecutionLogRequest) (*dto.ListTaskExecutionLogResponse, error) {
|
|
opts := repository.TaskExecutionLogListOptions{
|
|
PlanExecutionLogID: req.PlanExecutionLogID,
|
|
TaskID: req.TaskID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.Status != nil {
|
|
status := models.ExecutionStatus(*req.Status)
|
|
opts.Status = &status
|
|
}
|
|
|
|
data, total, err := s.executionLogRepo.ListTaskExecutionLogs(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListTaskExecutionLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPendingCollections 负责处理查询待采集请求列表的业务逻辑
|
|
func (s *monitorService) ListPendingCollections(req *dto.ListPendingCollectionRequest) (*dto.ListPendingCollectionResponse, error) {
|
|
opts := repository.PendingCollectionListOptions{
|
|
DeviceID: req.DeviceID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.Status != nil {
|
|
status := models.PendingCollectionStatus(*req.Status)
|
|
opts.Status = &status
|
|
}
|
|
|
|
data, total, err := s.pendingCollectionRepo.List(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListPendingCollectionResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListUserActionLogs 负责处理查询用户操作日志列表的业务逻辑
|
|
func (s *monitorService) ListUserActionLogs(req *dto.ListUserActionLogRequest) (*dto.ListUserActionLogResponse, error) {
|
|
opts := repository.UserActionLogListOptions{
|
|
UserID: req.UserID,
|
|
Username: req.Username,
|
|
ActionType: req.ActionType,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.Status != nil {
|
|
status := models.AuditStatus(*req.Status)
|
|
opts.Status = &status
|
|
}
|
|
|
|
data, total, err := s.userActionLogRepo.List(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListUserActionLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListRawMaterialPurchases 负责处理查询原料采购记录列表的业务逻辑
|
|
func (s *monitorService) ListRawMaterialPurchases(req *dto.ListRawMaterialPurchaseRequest) (*dto.ListRawMaterialPurchaseResponse, error) {
|
|
opts := repository.RawMaterialPurchaseListOptions{
|
|
RawMaterialID: req.RawMaterialID,
|
|
Supplier: req.Supplier,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.rawMaterialRepo.ListRawMaterialPurchases(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListRawMaterialPurchaseResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListRawMaterialStockLogs 负责处理查询原料库存日志列表的业务逻辑
|
|
func (s *monitorService) ListRawMaterialStockLogs(req *dto.ListRawMaterialStockLogRequest) (*dto.ListRawMaterialStockLogResponse, error) {
|
|
opts := repository.RawMaterialStockLogListOptions{
|
|
RawMaterialID: req.RawMaterialID,
|
|
SourceID: req.SourceID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.SourceType != nil {
|
|
sourceType := models.StockLogSourceType(*req.SourceType)
|
|
opts.SourceType = &sourceType
|
|
}
|
|
|
|
data, total, err := s.rawMaterialRepo.ListRawMaterialStockLogs(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListRawMaterialStockLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListFeedUsageRecords 负责处理查询饲料使用记录列表的业务逻辑
|
|
func (s *monitorService) ListFeedUsageRecords(req *dto.ListFeedUsageRecordRequest) (*dto.ListFeedUsageRecordResponse, error) {
|
|
opts := repository.FeedUsageRecordListOptions{
|
|
PenID: req.PenID,
|
|
FeedFormulaID: req.FeedFormulaID,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.rawMaterialRepo.ListFeedUsageRecords(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListFeedUsageRecordResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListMedicationLogs 负责处理查询用药记录列表的业务逻辑
|
|
func (s *monitorService) ListMedicationLogs(req *dto.ListMedicationLogRequest) (*dto.ListMedicationLogResponse, error) {
|
|
opts := repository.MedicationLogListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
MedicationID: req.MedicationID,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.Reason != nil {
|
|
reason := models.MedicationReasonType(*req.Reason)
|
|
opts.Reason = &reason
|
|
}
|
|
|
|
data, total, err := s.medicationRepo.ListMedicationLogs(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListMedicationLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPigBatchLogs 负责处理查询猪批次日志列表的业务逻辑
|
|
func (s *monitorService) ListPigBatchLogs(req *dto.ListPigBatchLogRequest) (*dto.ListPigBatchLogResponse, error) {
|
|
opts := repository.PigBatchLogListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.ChangeType != nil {
|
|
changeType := models.LogChangeType(*req.ChangeType)
|
|
opts.ChangeType = &changeType
|
|
}
|
|
|
|
data, total, err := s.pigBatchLogRepo.List(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListPigBatchLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListWeighingBatches 负责处理查询批次称重记录列表的业务逻辑
|
|
func (s *monitorService) ListWeighingBatches(req *dto.ListWeighingBatchRequest) (*dto.ListWeighingBatchResponse, error) {
|
|
opts := repository.WeighingBatchListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.pigBatchRepo.ListWeighingBatches(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListWeighingBatchResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListWeighingRecords 负责处理查询单次称重记录列表的业务逻辑
|
|
func (s *monitorService) ListWeighingRecords(req *dto.ListWeighingRecordRequest) (*dto.ListWeighingRecordResponse, error) {
|
|
opts := repository.WeighingRecordListOptions{
|
|
WeighingBatchID: req.WeighingBatchID,
|
|
PenID: req.PenID,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.pigBatchRepo.ListWeighingRecords(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListWeighingRecordResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPigTransferLogs 负责处理查询猪只迁移日志列表的业务逻辑
|
|
func (s *monitorService) ListPigTransferLogs(req *dto.ListPigTransferLogRequest) (*dto.ListPigTransferLogResponse, error) {
|
|
opts := repository.PigTransferLogListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
PenID: req.PenID,
|
|
OperatorID: req.OperatorID,
|
|
CorrelationID: req.CorrelationID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.TransferType != nil {
|
|
transferType := models.PigTransferType(*req.TransferType)
|
|
opts.TransferType = &transferType
|
|
}
|
|
|
|
data, total, err := s.pigTransferLogRepo.ListPigTransferLogs(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListPigTransferLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPigSickLogs 负责处理查询病猪日志列表的业务逻辑
|
|
func (s *monitorService) ListPigSickLogs(req *dto.ListPigSickLogRequest) (*dto.ListPigSickLogResponse, error) {
|
|
opts := repository.PigSickLogListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
PenID: req.PenID,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
if req.Reason != nil {
|
|
reason := models.PigBatchSickPigReasonType(*req.Reason)
|
|
opts.Reason = &reason
|
|
}
|
|
if req.TreatmentLocation != nil {
|
|
treatmentLocation := models.PigBatchSickPigTreatmentLocation(*req.TreatmentLocation)
|
|
opts.TreatmentLocation = &treatmentLocation
|
|
}
|
|
|
|
data, total, err := s.pigSickLogRepo.ListPigSickLogs(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListPigSickLogResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPigPurchases 负责处理查询猪只采购记录列表的业务逻辑
|
|
func (s *monitorService) ListPigPurchases(req *dto.ListPigPurchaseRequest) (*dto.ListPigPurchaseResponse, error) {
|
|
opts := repository.PigPurchaseListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
Supplier: req.Supplier,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.pigTradeRepo.ListPigPurchases(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListPigPurchaseResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListPigSales 负责处理查询猪只销售记录列表的业务逻辑
|
|
func (s *monitorService) ListPigSales(req *dto.ListPigSaleRequest) (*dto.ListPigSaleResponse, error) {
|
|
opts := repository.PigSaleListOptions{
|
|
PigBatchID: req.PigBatchID,
|
|
Buyer: req.Buyer,
|
|
OperatorID: req.OperatorID,
|
|
OrderBy: req.OrderBy,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
}
|
|
|
|
data, total, err := s.pigTradeRepo.ListPigSales(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListPigSaleResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|
|
|
|
// ListNotifications 负责处理查询通知列表的业务逻辑
|
|
func (s *monitorService) ListNotifications(req *dto.ListNotificationRequest) (*dto.ListNotificationResponse, error) {
|
|
opts := repository.NotificationListOptions{
|
|
UserID: req.UserID,
|
|
NotifierType: req.NotifierType,
|
|
Level: req.Level,
|
|
StartTime: req.StartTime,
|
|
EndTime: req.EndTime,
|
|
OrderBy: req.OrderBy,
|
|
Status: req.Status,
|
|
}
|
|
|
|
data, total, err := s.notificationRepo.List(opts, req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dto.NewListNotificationResponse(data, total, req.Page, req.PageSize), nil
|
|
}
|