This commit is contained in:
Hsy
2025-02-20 17:32:53 +08:00
commit 144c1788f7
57 changed files with 2827 additions and 0 deletions

View File

@ -0,0 +1,145 @@
package cmd
import (
"context"
"database/sql"
"fmt"
"hk/model/config_api"
"hk/model/goods_info"
"hk/model/goods_status"
_73 "hk/pkg/util/172"
"hk/pkg/util/strUtil"
"strconv"
"time"
)
func configApiList() {
configApiInfo, err := svcCtx.ConfigApiModel.FindList(context.Background())
if err != nil {
fmt.Println(err)
}
if configApiInfo == nil {
return
}
for _, item := range configApiInfo {
switch item.Type {
case "0":
getGoodsStatus(item)
updateGoodsStatus(item)
}
}
}
func configApiListv1() {
configApiInfo, err := svcCtx.ConfigApiModel.FindList(context.Background())
if err != nil {
fmt.Println(err)
}
if configApiInfo == nil {
return
}
for _, item := range configApiInfo {
switch item.Type {
case "0":
addGoodsInfo(item)
}
}
}
func addGoodsInfo(configApi config_api.ConfigApi) {
ids, err := svcCtx.GoodsStatusModel.FindNotIdOne(context.Background(), configApi.Id)
if err != nil || ids <= 0 {
return
}
req := _73.GoodsReq{
ProductID: strconv.FormatInt(ids, 10),
BaseParams: _73.BaseParams{
UserID: strUtil.ValidString(configApi.Key),
UserSign: strUtil.ValidString(configApi.Secret),
},
}
result, err := _73.GetProductV2(req)
if err != nil {
return
}
if result.Code != 0 || len(result.Data) <= 0 {
return
}
data := result.Data[0]
status := "0"
if data.Flag {
status = "1"
}
goodsStatus := goods_info.GoodsInfo{
Name: data.ProductName,
Type: "0",
MainPic: data.MainPic,
LittlePicture: data.LittlePicture,
NetAddr: data.NetAddr,
Area: data.Area,
DisableArea: data.DisableArea,
DisableAge: "",
DisableContract: "",
Notes: sql.NullString{},
ApiId: strUtil.Int64ToNullInt64(configApi.Id),
ApiProductId: strUtil.Int64ToNullInt64(data.ProductID),
NumberSel: strconv.Itoa(data.NumberSel),
Status: status,
CreateTime: time.Now(),
UpdateTime: time.Now(),
Remarks: sql.NullString{},
}
svcCtx.GoodsInfoModel.Insert(context.Background(), &goodsStatus)
}
func updateGoodsStatus(item config_api.ConfigApi) {
ids, err := svcCtx.GoodsStatusModel.FindUpList(context.Background(), item.Id)
if err != nil {
return
}
if len(ids) <= 0 {
return
}
svcCtx.GoodsStatusModel.UpdateDownByIds(context.Background(), ids)
}
func getGoodsStatus(configApi config_api.ConfigApi) {
req := _73.GoodsReq{
ProductID: "",
BaseParams: _73.BaseParams{
UserID: strUtil.ValidString(configApi.Key),
UserSign: strUtil.ValidString(configApi.Secret),
},
}
result, err := _73.GetProduct(req)
if err != nil {
return
}
if result.Code == 0 {
var goodsStatusList []goods_status.GoodsStatus
if len(result.Data) > 0 {
err = svcCtx.GoodsStatusModel.DeleteByApiId(context.Background(), configApi.Id)
if err != nil {
return
}
}
for _, item := range result.Data {
var status int64 = 0
if item.Flag == "上架中" {
status = 1
}
goodsStatus := goods_status.GoodsStatus{
ApiId: strUtil.Int64ToNullInt64(configApi.Id),
ApiProductId: strUtil.Int64ToNullInt64(item.ProductID),
Status: strUtil.Int64ToNullInt64(status),
}
svcCtx.GoodsStatusModel.Insert(context.Background(), &goodsStatus)
goodsStatusList = append(goodsStatusList, goodsStatus)
}
}
}

View File

@ -0,0 +1,46 @@
package cronx
// 每5秒
func Every5s() string {
return "*/5 * * * * *"
}
// 每10秒
func Every10s() string {
return "*/10 * * * * *"
}
// 每分钟
func EveryMinute() string {
return "0 */1 * * * *"
}
// 每五分钟
func EveryFiveMinute() string {
return "0 */5 * * * *"
}
// 每十分钟
func EveryTenMinute() string {
return "0 */10 * * * *"
}
// 每半小时
func EveryHalfHour() string {
return "0 0,30 * * * *"
}
// EveryHour 每小时
func EveryHour() string {
return "0 0 * * * *"
}
// 每几分钟执行 1,2,3,26
func Hourly(m string) string {
return "0 " + m + " * * * *"
}
// 每天几点执行
func Daily(h string) string {
return "0 0 " + h + " * * *"
}

View File

@ -0,0 +1,20 @@
package cmd
import (
"fmt"
"github.com/robfig/cron/v3"
"hk/service/api/internal/svc"
)
var svcCtx *svc.ServiceContext
func Execute(ctx *svc.ServiceContext) {
svcCtx = ctx
c := cron.New(cron.WithSeconds())
ScheduleRun(c)
fmt.Println("定时任务启动...")
go c.Start()
defer c.Stop()
//select {}
}

View File

@ -0,0 +1,11 @@
package cmd
import (
"github.com/robfig/cron/v3"
"hk/service/api/internal/cmd/cronx"
)
func ScheduleRun(c *cron.Cron) {
c.AddFunc(cronx.EveryHour(), configApiList)
c.AddFunc(cronx.EveryTenMinute(), configApiListv1)
}