46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
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)
|
||
|
|
}
|