فهرست منبع

pasture: sameTime 同期策略优化

Yi 2 هفته پیش
والد
کامیت
97d549b9aa
6فایلهای تغییر یافته به همراه73 افزوده شده و 7 حذف شده
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 34 4
      model/same_time.go
  4. 1 1
      module/backend/prescription.go
  5. 19 1
      module/crontab/cow_cron.go
  6. 14 0
      util/util_test.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 go 1.17
 
 
 require (
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250401072057-a324cc76215b
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250403023152-3351c34ff8e2
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 4 - 0
go.sum

@@ -110,6 +110,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250401071526-0bfd7f4af6d3 h1:DlhNaXi3
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250401071526-0bfd7f4af6d3/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250401071526-0bfd7f4af6d3/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250401072057-a324cc76215b h1:kdALC/8+x5bYGQxBBSAq9FvsneuwfMSu0x6evwzJoP4=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250401072057-a324cc76215b h1:kdALC/8+x5bYGQxBBSAq9FvsneuwfMSu0x6evwzJoP4=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250401072057-a324cc76215b/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250401072057-a324cc76215b/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250403015930-728ac52191ed h1:jdfxjd7TdP8TLfOBaQxtA5IcnXI9/+FRuJ9hHPt1gM4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250403015930-728ac52191ed/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250403023152-3351c34ff8e2 h1:DxuqalLwzBemnGnXn3NK/WvtH3k1elUIVXEHRP2QSvA=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250403023152-3351c34ff8e2/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 34 - 4
model/same_time.go

@@ -2,6 +2,8 @@ package model
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
+	"sort"
+	"strings"
 
 
 	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 	"go.uber.org/zap"
 	"go.uber.org/zap"
@@ -9,11 +11,13 @@ import (
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 )
 
 
+const WeekTypeSeparator = "|"
+
 type SameTime struct {
 type SameTime struct {
 	Id                  int64                          `json:"id"`
 	Id                  int64                          `json:"id"`
 	PastureId           int64                          `json:"pastureId"`
 	PastureId           int64                          `json:"pastureId"`
 	Name                string                         `json:"name"`
 	Name                string                         `json:"name"`
-	WeekType            pasturePb.Week_Kind            `json:"weekType"`
+	WeekType            string                         `json:"weekType"`
 	CowType             pasturePb.SameTimeCowType_Kind `json:"cowType"`
 	CowType             pasturePb.SameTimeCowType_Kind `json:"cowType"`
 	IsShow              pasturePb.IsShow_Kind          `json:"isShow"`
 	IsShow              pasturePb.IsShow_Kind          `json:"isShow"`
 	PostpartumDaysStart int32                          `json:"postpartumDaysStart"`
 	PostpartumDaysStart int32                          `json:"postpartumDaysStart"`
@@ -34,10 +38,23 @@ func NewSameTime(pastureId int64, currentUser *SystemUser, req *pasturePb.Search
 	if len(req.CollateNodes) > 0 {
 	if len(req.CollateNodes) > 0 {
 		collateNodes, _ = json.Marshal(req.CollateNodes)
 		collateNodes, _ = json.Marshal(req.CollateNodes)
 	}
 	}
+	weekType := ""
+	if len(req.WeekType) > 0 {
+		sort.Slice(req.WeekType, func(i, j int) bool {
+			return req.WeekType[i] < req.WeekType[j]
+		})
+
+		for _, v := range req.WeekType {
+			weekType += v.String() + WeekTypeSeparator
+		}
+	}
+	if len(weekType) > 0 {
+		weekType = weekType[:len(weekType)-1]
+	}
 	return &SameTime{
 	return &SameTime{
 		PastureId:           pastureId,
 		PastureId:           pastureId,
 		Name:                req.Name,
 		Name:                req.Name,
-		WeekType:            req.WeekType,
+		WeekType:            weekType,
 		CowType:             req.CowType,
 		CowType:             req.CowType,
 		IsShow:              pasturePb.IsShow_Ok,
 		IsShow:              pasturePb.IsShow_Ok,
 		PostpartumDaysStart: req.PostpartumDaysStart,
 		PostpartumDaysStart: req.PostpartumDaysStart,
@@ -69,11 +86,24 @@ func (e SameTimeSlice) ToPB(
 		if err := json.Unmarshal([]byte(v.CollateNodes), &collectionNodes); err != nil {
 		if err := json.Unmarshal([]byte(v.CollateNodes), &collectionNodes); err != nil {
 			zaplog.Error("json.Unmarshal SameTimeSlice.ToPB error", zap.Any("err", err))
 			zaplog.Error("json.Unmarshal SameTimeSlice.ToPB error", zap.Any("err", err))
 		}
 		}
+
+		weekType := make([]pasturePb.Week_Kind, 0)
+		weekName := ""
+		for _, w := range strings.Split(v.WeekType, WeekTypeSeparator) {
+			wt := pasturePb.Week_Kind(pasturePb.Week_Kind_value[w])
+			weekType = append(weekType, wt)
+			weekName += weekMap[wt] + WeekTypeSeparator
+		}
+
+		if len(weekName) > 0 {
+			weekName = weekName[:len(weekName)-1]
+		}
+
 		res[i] = &pasturePb.SearchSameTimeList{
 		res[i] = &pasturePb.SearchSameTimeList{
 			Id:                  int32(v.Id),
 			Id:                  int32(v.Id),
 			Name:                v.Name,
 			Name:                v.Name,
-			WeekType:            v.WeekType,
-			WeekName:            weekMap[v.WeekType],
+			WeekType:            weekType,
+			WeekName:            weekName,
 			CowType:             v.CowType,
 			CowType:             v.CowType,
 			CowTypeName:         sameTimeCowTypeMap[v.CowType],
 			CowTypeName:         sameTimeCowTypeMap[v.CowType],
 			IsShow:              v.IsShow,
 			IsShow:              v.IsShow,

+ 1 - 1
module/backend/prescription.go

@@ -56,7 +56,7 @@ func (s *StoreEntry) SearchSameTimeList(ctx context.Context, req *pasturePb.Sear
 		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
 		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
 	}
 	}
 
 
-	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
+	if err = pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
 		Find(&semeTimeList).Error; err != nil {
 		Find(&semeTimeList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 		return nil, xerr.WithStack(err)
 	}
 	}

+ 19 - 1
module/crontab/cow_cron.go

@@ -6,6 +6,7 @@ import (
 	"kpt-pasture/model"
 	"kpt-pasture/model"
 	"kpt-pasture/module/backend"
 	"kpt-pasture/module/backend"
 	"kpt-pasture/util"
 	"kpt-pasture/util"
+	"strings"
 	"time"
 	"time"
 
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
@@ -237,7 +238,24 @@ func (e *Entry) SameTimePlan() error {
 
 
 	currWeek := time.Now().Weekday()
 	currWeek := time.Now().Weekday()
 	for _, sameTime := range sameTimeList {
 	for _, sameTime := range sameTimeList {
-		if time.Weekday(sameTime.WeekType) != currWeek {
+		if len(sameTime.WeekType) <= 0 {
+			continue
+		}
+
+		wts := strings.Split(sameTime.WeekType, model.WeekTypeSeparator)
+		if len(wts) <= 0 {
+			continue
+		}
+
+		var info bool
+		for _, wt := range wts {
+			if time.Weekday(int(pasturePb.Week_Kind_value[wt])) == currWeek {
+				info = true
+				break
+			}
+		}
+
+		if !info {
 			continue
 			continue
 		}
 		}
 
 

+ 14 - 0
util/util_test.go

@@ -2,11 +2,14 @@ package util
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"sort"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
 
 
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/assert"
 )
 )
 
 
@@ -531,4 +534,15 @@ func Test_demo(t *testing.T) {
 
 
 	initialTime, err := TimeParseLocal(LayoutTime, "")
 	initialTime, err := TimeParseLocal(LayoutTime, "")
 	fmt.Println(initialTime, err)
 	fmt.Println(initialTime, err)
+
+	type MyInt pasturePb.Week_Kind
+
+	nums := []MyInt{3, 1, 4, 5, 2, 6}
+
+	// 使用sort.Slice排序
+	sort.Slice(nums, func(i, j int) bool {
+		return nums[i] < nums[j]
+	})
+
+	fmt.Println(nums) // 输出: [1 1 2 3 4 5 6 9]
 }
 }