147 lines
3.2 KiB
Go
147 lines
3.2 KiB
Go
package service
|
|
|
|
import (
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/web"
|
|
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
|
|
"gitea.timerzz.com/kedaya_haitao/common/structs/utils"
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/henrylee2cn/ameda"
|
|
)
|
|
|
|
type Seller struct {
|
|
storage *storage.Storage
|
|
}
|
|
|
|
func NewSeller(storage *storage.Storage) *Seller {
|
|
return &Seller{
|
|
storage: storage,
|
|
}
|
|
}
|
|
|
|
func (s *Seller) Registry(r fiber.Router) {
|
|
api := r.Group("/api/v2")
|
|
api.Get("sellers", s.ListSellers)
|
|
api.Get("sellers/find", s.FindSellers)
|
|
api.Get("sellers/:id", s.GetSeller)
|
|
api.Post("sellers", s.CreateSeller)
|
|
api.Put("sellers", s.UpdateSeller)
|
|
api.Delete("sellers/:id", s.DeleteSeller)
|
|
// 获取状态字典
|
|
api.Get("sellers/dict/status", s.DictStatus)
|
|
}
|
|
|
|
func (s *Seller) ListSellers(c fiber.Ctx) error {
|
|
query := storage.NewPageListQuery(&storage.ListSellerQuery{})
|
|
if err := c.Bind().Query(query); err != nil {
|
|
return err
|
|
}
|
|
|
|
sellers, total, err := s.storage.Seller().List(*query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(web.NewResponse(web.NewListResponse(total, sellers)))
|
|
}
|
|
|
|
func (s *Seller) CreateSeller(c fiber.Ctx) error {
|
|
var seller v2.Seller
|
|
if err := c.Bind().JSON(&seller); err != nil {
|
|
return err
|
|
}
|
|
if err := s.storage.Seller().Create(seller); err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
func (s *Seller) UpdateSeller(c fiber.Ctx) error {
|
|
var seller v2.Seller
|
|
if err := c.Bind().JSON(&seller); err != nil {
|
|
return err
|
|
}
|
|
if err := s.storage.Seller().Update(seller); err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
func (s *Seller) GetSeller(c fiber.Ctx) error {
|
|
sid := c.Params("id")
|
|
if sid == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
id, err := ameda.Atoi(sid)
|
|
if err != nil {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
seller, err := s.storage.Seller().Get(uint(id))
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
func (s *Seller) DeleteSeller(c fiber.Ctx) error {
|
|
sid := c.Params("id")
|
|
if sid == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
id, err := ameda.Atoi(sid)
|
|
if err != nil {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
seller, err := s.storage.Seller().Delete(uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(seller))
|
|
}
|
|
|
|
// 返回所有符合条件的销售商
|
|
func (s *Seller) FindSellers(c fiber.Ctx) error {
|
|
var query storage.ListSellerQuery
|
|
if err := c.Bind().Query(&query); err != nil {
|
|
return err
|
|
}
|
|
sellers, err := s.storage.Seller().Find(&query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.JSON(web.NewResponse(sellers))
|
|
}
|
|
|
|
// 返回状态字典
|
|
func (s *Seller) DictStatus(c fiber.Ctx) error {
|
|
dicts := []utils.Dict{
|
|
{
|
|
Key: "seller_status_normal",
|
|
Value: v2.SellerStatus_Normal,
|
|
Title: "正常",
|
|
Color: "#87d068",
|
|
},
|
|
{
|
|
Key: "seller_status_pulling",
|
|
Value: v2.SellerStatus_Pulling,
|
|
Title: "抓取中",
|
|
Color: "#108ee9",
|
|
},
|
|
{
|
|
Key: "seller_status_calculate",
|
|
Value: v2.SellerStatus_Calculating,
|
|
Title: "计算中",
|
|
Color: "#108ee9",
|
|
},
|
|
{
|
|
Key: "seller_status_error",
|
|
Value: v2.SellerStatus_Error,
|
|
Title: "出错",
|
|
Color: "red",
|
|
},
|
|
}
|
|
return c.JSON(web.NewResponse(dicts))
|
|
}
|