144 lines
3.3 KiB
Go
144 lines
3.3 KiB
Go
package service
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
|
|
"github.com/wmms/wmms/internal/model"
|
|
"github.com/wmms/wmms/internal/repository"
|
|
)
|
|
|
|
type MaterialService struct {
|
|
repo *repository.MaterialRepo
|
|
logSvc *MaterialLogService
|
|
}
|
|
|
|
func NewMaterialService() *MaterialService {
|
|
return &MaterialService{
|
|
repo: repository.NewMaterialRepo(),
|
|
logSvc: NewMaterialLogService(),
|
|
}
|
|
}
|
|
|
|
func (s *MaterialService) Create(req *model.CreateMaterialRequest, operator string) (*model.Material, error) {
|
|
unit := req.Unit
|
|
if unit == "" {
|
|
unit = "pcs"
|
|
}
|
|
|
|
var materialImage *string
|
|
if req.MaterialImage != "" {
|
|
materialImage = &req.MaterialImage
|
|
}
|
|
|
|
m := &model.Material{
|
|
MaterialCode: req.MaterialCode,
|
|
Name: req.Name,
|
|
MaterialImage: materialImage,
|
|
Spec: req.Spec,
|
|
Category: req.Category,
|
|
Unit: unit,
|
|
WarningStock: req.WarningStock,
|
|
}
|
|
|
|
err := s.repo.Create(m)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 记录日志
|
|
if err := s.logSvc.LogCreate(m, operator); err != nil {
|
|
log.Printf("Warning: failed to create material log: %v", err)
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (s *MaterialService) GetByID(id int64) (*model.Material, error) {
|
|
m := &model.Material{}
|
|
err := s.repo.GetByID(id, m)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (s *MaterialService) List(search, category string, limit, offset int) ([]model.Material, int64, error) {
|
|
return s.repo.List(search, category, limit, offset)
|
|
}
|
|
|
|
func (s *MaterialService) Update(id int64, req *model.UpdateMaterialRequest, operator string) (*model.Material, error) {
|
|
// 获取修改前的数据
|
|
oldMaterial, err := s.GetByID(id)
|
|
if err != nil {
|
|
log.Printf("Warning: failed to get old material for logging: %v", err)
|
|
}
|
|
|
|
err = s.repo.Update(id, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 获取修改后的数据
|
|
newMaterial, err := s.GetByID(id)
|
|
if err != nil {
|
|
log.Printf("Warning: failed to get new material for logging: %v", err)
|
|
}
|
|
|
|
// 记录日志
|
|
if err := s.logSvc.LogUpdate(newMaterial, oldMaterial, operator); err != nil {
|
|
log.Printf("Warning: failed to create material log: %v", err)
|
|
}
|
|
|
|
return newMaterial, nil
|
|
}
|
|
|
|
func (s *MaterialService) Delete(id int64, operator string) error {
|
|
// 获取删除前的数据
|
|
material, _ := s.GetByID(id)
|
|
|
|
err := s.repo.Delete(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 记录日志
|
|
if material != nil {
|
|
if err := s.logSvc.LogDelete(material, operator); err != nil {
|
|
log.Printf("Warning: failed to create material log: %v", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *MaterialService) GetStock(id int64) (int, error) {
|
|
return s.repo.GetStock(id)
|
|
}
|
|
|
|
func (s *MaterialService) UpdateStock(id int64, delta int) error {
|
|
return s.repo.UpdateStock(id, delta)
|
|
}
|
|
|
|
func (s *MaterialService) Exists(id int64) (bool, error) {
|
|
m := &model.Material{}
|
|
err := s.repo.GetByID(id, m)
|
|
if err == sql.ErrNoRows {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// GetStockForUpdateTx gets stock with row lock within a transaction
|
|
func (s *MaterialService) GetStockForUpdateTx(tx *sql.Tx, id int64) (int, error) {
|
|
return s.repo.GetStockForUpdateTx(tx, id)
|
|
}
|
|
|
|
// UpdateStockTx updates stock within a transaction
|
|
func (s *MaterialService) UpdateStockTx(tx *sql.Tx, id int64, delta int) error {
|
|
return s.repo.UpdateStockTx(tx, id, delta)
|
|
}
|