us-coach-spider/server/spider.go
timerzz 1e416c2f2f
Some checks failed
Build image / build (push) Failing after 59s
feat 支持自定义优惠
2024-06-15 21:10:37 +08:00

97 lines
2.3 KiB
Go

package server
import (
"gitea.timerzz.com/kedaya_haitao/coach-spider/product"
productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product"
"github.com/gofiber/fiber/v3"
"strconv"
)
type SpiderSvc struct {
ctl *product.Controller
}
func NewSpiderSvc(ctl *product.Controller) *SpiderSvc {
return &SpiderSvc{
ctl: ctl,
}
}
func (s *SpiderSvc) RegistryRouter(r fiber.Router) {
r.Get("spider/us/coach-outlet/cfg", s.GetSpiderCfg)
r.Post("spider/us/coach-outlet/cfg", s.SetSpiderCfg)
r.Get("spider/us/coach-outlet/global/calculate", s.GetGlobalCalculate)
r.Post("spider/us/coach-outlet/global/calculate", s.UpsertGlobalCalculate)
r.Delete("spider/us/coach-outlet/global/calculate/u/:id", s.DelGlobalCalculate)
r.Post("spider/us/coach-outlet/calculate", s.UpsertCalculate)
r.Delete("spider/us/coach-outlet/calculate/u/:id", s.DelCalculate)
}
func (s *SpiderSvc) GetSpiderCfg(ctx fiber.Ctx) error {
opt := s.ctl.GetOption()
return ctx.JSON(&opt)
}
func (s *SpiderSvc) SetSpiderCfg(ctx fiber.Ctx) error {
var opt product.Option
if err := ctx.Bind().JSON(&opt); err != nil {
return err
}
s.ctl.SaveOption(opt)
return nil
}
func (s *SpiderSvc) GetGlobalCalculate(ctx fiber.Ctx) error {
cals, err := s.ctl.GetGlobalCalculateProcess()
if err != nil {
return err
}
return ctx.JSON(cals)
}
func (s *SpiderSvc) DelGlobalCalculate(ctx fiber.Ctx) error {
idStr := ctx.Params("id")
if idStr == "" {
return fiber.ErrBadRequest
}
id, _ := strconv.Atoi(idStr)
if err := s.ctl.DeleteGlobalCalculateProcess(uint(id)); err != nil {
return err
}
return nil
}
func (s *SpiderSvc) UpsertGlobalCalculate(ctx fiber.Ctx) error {
var cs []productv1.CalculateProcess
if err := ctx.Bind().JSON(&cs); err != nil {
return err
}
if err := s.ctl.SaveGlobalCalculateProcess(cs); err != nil {
return err
}
return nil
}
func (s *SpiderSvc) DelCalculate(ctx fiber.Ctx) error {
idStr := ctx.Params("id")
if idStr == "" {
return fiber.ErrBadRequest
}
id, _ := strconv.Atoi(idStr)
if err := s.ctl.DeleteCalculateProcess(uint(id)); err != nil {
return err
}
return nil
}
func (s *SpiderSvc) UpsertCalculate(ctx fiber.Ctx) error {
var cs []productv1.CalculateProcess
if err := ctx.Bind().JSON(&cs); err != nil {
return err
}
if err := s.ctl.SaveCalculateProcess(cs); err != nil {
return err
}
return nil
}