2024-05-14 21:18:24 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/coach-spider/product"
|
2024-06-15 21:10:37 +08:00
|
|
|
productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product"
|
2024-05-14 21:18:24 +08:00
|
|
|
"github.com/gofiber/fiber/v3"
|
2024-06-15 21:10:37 +08:00
|
|
|
"strconv"
|
2024-05-14 21:18:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SpiderSvc struct {
|
|
|
|
ctl *product.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSpiderSvc(ctl *product.Controller) *SpiderSvc {
|
|
|
|
return &SpiderSvc{
|
|
|
|
ctl: ctl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SpiderSvc) RegistryRouter(r fiber.Router) {
|
2024-06-15 21:10:37 +08:00
|
|
|
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)
|
2024-05-14 21:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-06-15 21:10:37 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|