package server import ( "fmt" "gitea.timerzz.com/kedaya_haitao/cn-coach-spider/spider" productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product" "github.com/gofiber/fiber/v3" "strconv" ) type SpiderSvc struct { ctl *spider.Controller shopType string } func NewSpiderSvc(ctl *spider.Controller, shopType string) *SpiderSvc { return &SpiderSvc{ ctl: ctl, shopType: shopType, } } func (s *SpiderSvc) RegistryRouter(r fiber.Router) { r.Get(fmt.Sprintf("spider/cn/%s/global/calculate", s.shopType), s.GetGlobalCalculate) r.Post(fmt.Sprintf("spider/cn/%s/global/calculate", s.shopType), s.UpsertGlobalCalculate) r.Delete(fmt.Sprintf("spider/cn/%s/global/calculate/u/:id", s.shopType), s.DelGlobalCalculate) r.Post(fmt.Sprintf("spider/cn/%s/calculate", s.shopType), s.UpsertCalculate) r.Delete(fmt.Sprintf("spider/cn/%s/calculate/u/:id", s.shopType), s.DelCalculate) } 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 }