material_manage_system/internal/service/material_log.go
2026-09-15 16:08:18 +08:00

66 lines
1.9 KiB
Go

package service
import (
"encoding/json"
"fmt"
"github.com/wmms/wmms/internal/model"
"github.com/wmms/wmms/internal/repository"
)
type MaterialLogService struct {
logRepo *repository.MaterialLogRepo
}
func NewMaterialLogService() *MaterialLogService {
return &MaterialLogService{
logRepo: repository.NewMaterialLogRepo(),
}
}
func (s *MaterialLogService) LogCreate(material *model.Material, operator string) error {
newValue, _ := json.Marshal(material)
log := &model.MaterialLog{
OperationType: "CREATE",
MaterialID: material.ID,
NewValue: string(newValue),
Operator: operator,
Description: fmt.Sprintf("新增物料:%s", material.Name),
}
return s.logRepo.Create(log)
}
func (s *MaterialLogService) LogUpdate(material *model.Material, oldMaterial *model.Material, operator string) error {
oldValue, _ := json.Marshal(oldMaterial)
newValue, _ := json.Marshal(material)
log := &model.MaterialLog{
OperationType: "UPDATE",
MaterialID: material.ID,
OldValue: string(oldValue),
NewValue: string(newValue),
Operator: operator,
Description: fmt.Sprintf("修改物料:%s", material.Name),
}
return s.logRepo.Create(log)
}
func (s *MaterialLogService) LogDelete(material *model.Material, operator string) error {
oldValue, _ := json.Marshal(material)
log := &model.MaterialLog{
OperationType: "DELETE",
MaterialID: material.ID,
OldValue: string(oldValue),
Operator: operator,
Description: fmt.Sprintf("删除物料:%s", material.Name),
}
return s.logRepo.Create(log)
}
func (s *MaterialLogService) List(operationType, materialId, startDate, endDate string, limit, offset int) ([]model.MaterialLog, int64, error) {
return s.logRepo.List(operationType, materialId, startDate, endDate, limit, offset)
}
func (s *MaterialLogService) GetByMaterialID(materialID int64) ([]model.MaterialLog, error) {
return s.logRepo.GetByMaterialID(materialID)
}