Files
bj_power/bj_power_mes/internal/logic/product.go
T

46 lines
1.3 KiB
Go
Raw Normal View History

2026-08-28 15:06:01 +08:00
package logic
import (
"context"
"errors"
"bj_power_mes/ent"
"bj_power_mes/ent/producttype"
)
type ProductTypeReq struct {
Id int `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Category string `json:"category"`
Remark string `json:"remark"`
IsActive bool `json:"isActive"`
}
func (s *Service) CreateProductType(ctx context.Context, req ProductTypeReq) error {
active := req.IsActive
if !req.IsActive {
active = true
}
return s.ctx.EntClient.ProductType.Create().
SetName(req.Name).SetCode(req.Code).SetCategory(req.Category).
SetRemark(req.Remark).SetIsActive(active).Exec(ctx)
}
func (s *Service) UpdateProductType(ctx context.Context, req ProductTypeReq) error {
if req.Id == 0 {
return errors.New("缺少 id")
}
return s.ctx.EntClient.ProductType.UpdateOneID(req.Id).
SetName(req.Name).SetCode(req.Code).SetCategory(req.Category).
SetRemark(req.Remark).SetIsActive(req.IsActive).Exec(ctx)
}
func (s *Service) DeleteProductType(ctx context.Context, id int) error {
return s.ctx.EntClient.ProductType.DeleteOneID(id).Exec(ctx)
}
func (s *Service) ListProductTypes(ctx context.Context) ([]*ent.ProductType, error) {
return s.ctx.EntClient.ProductType.Query().
Order(ent.Desc(producttype.FieldID)).All(ctx)
}