Browse Source

docker: update 4

Yi 6 months ago
parent
commit
869ec86e4c

+ 2 - 3
Dockerfile

@@ -10,7 +10,7 @@ RUN go env -w GO111MODULE=on && \
     go env -w CGO_ENABLED=0 && \
     go env -w GOARCH=amd64 && \
     go env -w GOOS=linux && \
-    go build -o ./bin/kptPasture -ldflags "-X kpt.kptyun.cn:3000/kpt-event/kpt-pasture/pod.appVersion=beef" main.go
+    go build -o ./kptPasture -ldflags "-X kpt.kptyun.cn:3000/kpt-event/kpt-pasture/pod.appVersion=beef" main.go
 
 
 FROM alpine:latest
@@ -20,9 +20,8 @@ owner="yiping.xu"
 
 WORKDIR /app/kpt-pasture
 
-
 COPY --from=0 /app/kpt-pasture/config/ /app/kpt-pasture/config/
-COPY --from=0  /app/kpt-pasture/bin/kptPasture /app/kpt-pasture/kptPasture
+COPY --from=0  /app/kpt-pasture/kptPasture /app/kpt-pasture/kptPasture
 
 EXPOSE 8090
 VOLUME ["/app/kpt-pasture/logger","/app/kpt-pasture/config","/app/kpt-pasture/files"]

+ 1 - 0
config/app.develop.yaml

@@ -28,6 +28,7 @@ side_work_setting:
       addr: '47.92.95.119:6389'
       db: 0
       pool_size: 10
+      password: "root"
     concurrency: 5
     queues:
       workflow: 20

+ 2 - 2
docker-compose.yml

@@ -12,8 +12,6 @@ services:
     environment:
       - APP_ENVIRONMENT=production
       - PASTURE_WORK_DIR=/app/kpt-pasture
-    ports:
-      - "8091:8090"
     command: [ "/app/kpt-pasture/kptPasture","crontab" ]
   kpt-pasture-http:
     privileged: true
@@ -25,6 +23,8 @@ services:
       - /etc/localtime:/etc/localtime
       - /data/docker-compose/kpt-pasture/config:/app/kpt-pasture/config
       - /data/docker-compose/kpt-pasture/files:/app/kpt-pasture/files
+    ports:
+      - "8091:8090"
     environment:
       - APP_ENVIRONMENT=production
       - PASTURE_WORK_DIR=/app/kpt-pasture

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240909083429-fbc0524f090a
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240910085752-595a1baf8c90
 	gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eko/gocache v1.1.0

+ 4 - 0
go.sum

@@ -52,6 +52,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240909070422-d8e24115eea0 h1:WlkXLe5N
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240909070422-d8e24115eea0/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240909083429-fbc0524f090a h1:Vz40n7q51ZKrI2P5BCT0eY1I2Du5ApRaaFkkzqDO1bg=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240909083429-fbc0524f090a/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240910084821-7a933688a3b5 h1:eLdcgCs/bIFhaykTcnBtdDwBJI+r1PqYmbF+d0+AqoU=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240910084821-7a933688a3b5/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240910085752-595a1baf8c90 h1:MoEtVVhW9LMk1FgiYYubNixK1H8hCNdkLMKdKjwETQE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240910085752-595a1baf8c90/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 2 - 2
model/calving_calf.go

@@ -10,7 +10,7 @@ type CalvingCalf struct {
 	EarNumber     string                 `json:"earNumber"`
 	Sex           pasturePb.Genders_Kind `json:"sex"`
 	CowKind       pasturePb.CowKind_Kind `json:"cowKind"`
-	BrithWeight   int64                  `json:"brithWeight"`
+	BirthWeight   int64                  `json:"birthWeight"`
 	IsLive        pasturePb.IsShow_Kind  `json:"isLive"`
 	IsAdoption    pasturePb.IsShow_Kind  `json:"isAdoption"`
 	PenId         int32                  `json:"penId"`
@@ -36,7 +36,7 @@ func NewEventCalvingCalf(motherId, calvingId int64, req *pasturePb.EventCalving)
 			EarNumber:     v.EarNumber,
 			CalvingId:     calvingId,
 			PenId:         v.PenId,
-			BrithWeight:   int64(v.Weight * 1000),
+			BirthWeight:   int64(v.Weight * 1000),
 			CurrentWeight: int64(v.Weight * 1000),
 			Sex:           v.Sex,
 			MotherId:      motherId,

+ 1 - 1
model/cow.go

@@ -127,7 +127,7 @@ func NewCalfCow(motherId, fatherId int64, calf *CalvingCalf) *Cow {
 		CowType:     pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
 		BreedStatus: pasturePb.BreedStatus_UnBreed,    // 未配
 		CowKind:     calf.CowKind,                     // 牛只品种
-		BirthWeight: calf.BrithWeight,
+		BirthWeight: calf.BirthWeight,
 		SourceId:    pasturePb.CowSource_Calving, // 产犊方式
 		FatherId:    fatherId,
 		MotherId:    motherId,

+ 22 - 0
model/crontab_log.go

@@ -0,0 +1,22 @@
+package model
+
+import "time"
+
+type CronLog struct {
+	Id        int64  `json:"id"`
+	Name      string `json:"name"`
+	Date      string `json:"date"`
+	CreatedAt int64  `json:"created_at"`
+	UpdatedAt int64  `json:"updated_at"`
+}
+
+func (c CronLog) TableName() string {
+	return "cron_log"
+}
+
+func NewCronLog(name string) *CronLog {
+	return &CronLog{
+		Name: name,
+		Date: time.Now().Format(LayoutDate2),
+	}
+}

+ 1 - 1
model/event_calving.go

@@ -62,7 +62,7 @@ func (e EventCalvingListSlice) ToPB(req []*CalvingCalf) []*pasturePb.LavingList
 			CalfItemList = append(CalfItemList, &pasturePb.CalfItem{
 				EarNumber:  v.EarNumber,
 				Sex:        v.Sex,
-				Weight:     float32(v.BrithWeight) / 1000,
+				Weight:     float32(v.BirthWeight) / 1000,
 				IsAdoption: v.IsAdoption,
 				IsLive:     v.IsLive,
 				CreatedAt:  int32(v.CreatedAt),

+ 11 - 9
model/same_time_cow.go

@@ -7,21 +7,23 @@ import (
 )
 
 type SameTimeCow struct {
-	Id         int64                 `json:"id"`
-	CowId      int64                 `json:"cowId"`
-	SameTimeId int64                 `json:"sameTimeId"`
-	Status     pasturePb.IsShow_Kind `json:"status"`
-	StartAt    int64                 `json:"startAt"`
-	EndAt      int64                 `json:"endAt"`
-	CreatedAt  int64                 `json:"createdAt"`
-	UpdatedAt  int64                 `json:"updatedAt"`
+	Id               int64                         `json:"id"`
+	CowId            int64                         `json:"cowId"`
+	SameTimeId       int64                         `json:"sameTimeId"`
+	Status           pasturePb.SameTimeStatus_Kind `json:"status"`
+	NextSameTimeType pasturePb.SameTimeType_Kind   `json:"nextSameTimeType"`
+	NextSameTimeAt   int64                         `json:"nextSameTimeAt"`
+	StartAt          int64                         `json:"startAt"`
+	EndAt            int64                         `json:"endAt"`
+	CreatedAt        int64                         `json:"createdAt"`
+	UpdatedAt        int64                         `json:"updatedAt"`
 }
 
 func NewSameTimeCow(cowId, sameTimeId int64) *SameTimeCow {
 	return &SameTimeCow{
 		CowId:      cowId,
 		SameTimeId: sameTimeId,
-		Status:     pasturePb.IsShow_Ok,
+		Status:     pasturePb.SameTimeStatus_No_Start,
 		StartAt:    time.Now().Unix(),
 	}
 }

+ 1 - 1
module/backend/event_breed.go

@@ -96,7 +96,7 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
 			return xerr.WithStack(err)
 		}
 
-		if err = tx.Model(new(model.Cow)).Where("id = ?", cow).Updates(map[string]interface{}{
+		if err = tx.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{
 			"calving_age":     0,
 			"lact":            cow.Lact + 1,
 			"breed_status":    pasturePb.BreedStatus_Calving,

+ 43 - 159
module/crontab/crontab.go → module/crontab/cow_cron.go

@@ -1,22 +1,15 @@
 package crontab
 
 import (
-	"context"
-	"encoding/json"
-	"errors"
 	"kpt-pasture/model"
 	"kpt-pasture/module/backend"
-	"kpt-pasture/util"
-	"time"
-
-	"gorm.io/gorm"
 
 	"gitee.com/xuyiping_admin/pkg/xerr"
+	"gorm.io/gorm"
 
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 	"go.uber.org/zap"
-
-	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 
 // UpdateCowInfo 牛只基本信息维护
@@ -25,6 +18,11 @@ func (e *Entry) UpdateCowInfo() error {
 	if err := e.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
 		return err
 	}
+
+	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
+		return nil
+	}
+
 	for _, cow := range cowList {
 		dayAge := cow.GetDayAge()
 		calvingAge := cow.GetCalvingAge()
@@ -42,30 +40,6 @@ func (e *Entry) UpdateCowInfo() error {
 	return nil
 }
 
-// GenerateAsynqWorkOrder 异步生成工作单
-func (e *Entry) GenerateAsynqWorkOrder() error {
-	workOrderList := make([]*model.WorkOrderMaster, 0)
-	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&workOrderList).Error; err != nil {
-		return err
-	}
-	for _, workOrder := range workOrderList {
-		timeUnix, err := util.ConvertParseLocalUnix(workOrder.ExecTime)
-		if timeUnix <= 0 || err != nil {
-			zaplog.Error("crontab", zap.Any("GenerateWorkOrder", err), zap.Any("execTime", workOrder.ExecTime))
-			continue
-		}
-		execTime := time.Now().Unix() - timeUnix
-
-		if _, err = e.AsynqClient.CtxEnqueue(
-			context.Background(),
-			model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second),
-		); err != nil {
-			zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
-		}
-	}
-	return nil
-}
-
 // ImmunizationPlan 免疫计划,生成工作单
 func (e *Entry) ImmunizationPlan() error {
 	planList := make([]*model.ImmunizationPlan, 0)
@@ -73,6 +47,10 @@ func (e *Entry) ImmunizationPlan() error {
 		return xerr.WithStack(err)
 	}
 
+	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
+		return nil
+	}
+
 	for _, plan := range planList {
 		cowList := make([]*model.Cow, 0)
 		pref := e.DB.Select("id").Where("is_remove = ?", pasturePb.IsShow_Ok).
@@ -110,125 +88,6 @@ func (e *Entry) ImmunizationPlan() error {
 	return nil
 }
 
-// SameTimePlan 同期计划,生成工作单
-func (e *Entry) SameTimePlan() error {
-	sameTimeList := make([]*model.SameTime, 0)
-	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
-		return xerr.WithStack(err)
-	}
-
-	pref := e.DB.Select("id").
-		Where("is_remove = ?", pasturePb.IsShow_Ok).
-		Where("is_pregnant = ?", pasturePb.IsShow_No)
-
-	for _, plan := range sameTimeList {
-		cowList := make([]*model.Cow, 0)
-		pref.Where("calving_age >= ?", plan.PostpartumDaysStart).
-			Where("calving_age <= ?", plan.PostpartumDaysEnd)
-		if err := pref.Find(&cowList).Error; err != nil {
-			zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", plan))
-			return xerr.WithStack(err)
-		}
-
-		if len(cowList) <= 0 {
-			continue
-		}
-		if err := e.GenerateCalendarBySameTimePlan(cowList, plan); err != nil {
-			zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cowList", cowList), zap.Any("plan", plan))
-			continue
-		}
-	}
-	return nil
-}
-
-// GenerateCalendarBySameTimePlan 生成同期计划工作单
-func (e *Entry) GenerateCalendarBySameTimePlan(cowList []*model.Cow, sameTime *model.SameTime) error {
-	if len(cowList) <= 0 {
-		return nil
-	}
-
-	cowSameTimeList := make([]*model.SameTimeCow, 0)
-	for _, cow := range cowList {
-		newCowSameTime, err := e.createNewCowSameTime(cow, sameTime)
-		if err != nil {
-			zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cow", cow))
-		}
-		cowSameTimeList = append(cowSameTimeList, newCowSameTime)
-	}
-
-	calendarName := backend.CalendarTypeMap()[pasturePb.CalendarType_Immunisation]
-	workOrderCalendarList := make([]*model.WorkOrderCalendar, 0)
-	if len(sameTime.CollateNodes) > 0 {
-		collateNodes := make([]*pasturePb.CollateNode, 0)
-		var _ = json.Unmarshal([]byte(sameTime.CollateNodes), &collateNodes)
-		nowTime := time.Now()
-		for i, collateNode := range collateNodes {
-			showDay := nowTime.Format(model.LayoutDate2)
-			if i > 0 {
-				showDay = nowTime.Add(time.Hour * 24 * time.Duration(collateNode.NextNodeDay)).Format(model.LayoutDate2)
-			}
-
-			histCount := e.GetTowardSameTimeCowSum(sameTime.Id, showDay)
-			workOrderCalendarList = append(workOrderCalendarList, &model.WorkOrderCalendar{
-				Name:         calendarName,
-				CalendarType: pasturePb.CalendarType_Immunisation,
-				Count:        int32(len(cowSameTimeList)) + int32(histCount),
-				ShowDay:      showDay,
-				IsShow:       pasturePb.IsShow_Ok,
-			})
-		}
-	}
-
-	if err := e.DB.Transaction(func(tx *gorm.DB) error {
-		// 创建新的牛只同期计划详情
-		if err := tx.Create(cowSameTimeList).Error; err != nil {
-			return xerr.WithStack(err)
-		}
-		for _, v := range workOrderCalendarList {
-			if err := tx.Model(&model.WorkOrderCalendar{}).Where(map[string]interface{}{
-				"calendar_type": v.CalendarType,
-				"show_day":      v.ShowDay,
-			}).Assign(map[string]interface{}{
-				"name":          v.Name,
-				"calendar_type": v.CalendarType,
-				"count":         v.Count,
-				"show_day":      v.ShowDay,
-				"is_show":       v.IsShow,
-			}).FirstOrCreate(&model.Pen{}).Error; err != nil {
-				return xerr.WithStack(err)
-			}
-		}
-
-		return nil
-	}); err != nil {
-		return xerr.WithStack(err)
-	}
-
-	return nil
-}
-
-func (e *Entry) createNewCowSameTime(cow *model.Cow, sameTime *model.SameTime) (*model.SameTimeCow, error) {
-	cowSameTime := &model.SameTimeCow{}
-	if err := e.DB.Where("cow_id = ?", cow.Id).
-		Where("lact = ?", cow.Lact).
-		Where("same_time_id = ?", sameTime.Id).
-		First(cowSameTime).Error; err != nil {
-		if errors.Is(err, gorm.ErrRecordNotFound) {
-			return &model.SameTimeCow{
-				CowId:      cow.Id,
-				SameTimeId: sameTime.Id,
-				Status:     pasturePb.IsShow_Ok,
-				StartAt:    time.Now().Unix(),
-				EndAt:      0,
-			}, nil
-		} else {
-			zaplog.Error("crontab", zap.Error(err), zap.Any("GenerateCalendarBySameTimePlan", "error"), zap.Any("cow", cow.Id), zap.Any("lact", cow.Lact))
-			return nil, xerr.WithStack(err)
-		}
-	}
-	return cowSameTime, nil
-}
-
 func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, plan *model.ImmunizationPlan) error {
 	workOrderCalendarList := e.getWorkOrderCalendar(plan.Name)
 	newCowList := make([]*model.Cow, 0)
@@ -301,12 +160,37 @@ func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, showDay string) int64
 	return res
 }
 
-func (e *Entry) getWorkOrderCalendar(name string) []*model.WorkOrderCalendar {
-	res := make([]*model.WorkOrderCalendar, 0)
-	if err := e.DB.Where("name = ?", name).
-		Where("is_show = ?", pasturePb.IsShow_Ok).
-		Find(&res).Error; err != nil {
-		zaplog.Error("getWorkOrderCalendar", zap.Any("err", err))
+// SameTimePlan 同期计划,生成工作单
+func (e *Entry) SameTimePlan() error {
+	sameTimeList := make([]*model.SameTime, 0)
+	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+
+	pref := e.DB.Select("id").
+		Where("is_remove = ?", pasturePb.IsShow_Ok).
+		Where("is_pregnant = ?", pasturePb.IsShow_No)
+
+	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
+		return nil
 	}
-	return res
+
+	for _, plan := range sameTimeList {
+		cowList := make([]*model.Cow, 0)
+		pref.Where("calving_age >= ?", plan.PostpartumDaysStart).
+			Where("calving_age <= ?", plan.PostpartumDaysEnd)
+		if err := pref.Find(&cowList).Error; err != nil {
+			zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", plan))
+			return xerr.WithStack(err)
+		}
+
+		if len(cowList) <= 0 {
+			continue
+		}
+		if err := e.GenerateCalendarBySameTimePlan(cowList, plan); err != nil {
+			zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cowList", cowList), zap.Any("plan", plan))
+			continue
+		}
+	}
+	return nil
 }

+ 1 - 1
module/crontab/interface.go

@@ -20,7 +20,6 @@ type Entry struct {
 	DB          *kptstore.DB
 	AsynqClient asynqsvc.Client
 	Redis       *redis.CacheStoreRedisEntry
-	//BackendEntry *backend.StoreEntry
 }
 
 func NewCrontab(entry Entry) Crontab {
@@ -32,4 +31,5 @@ type Crontab interface {
 	GenerateAsynqWorkOrder() error
 	ImmunizationPlan() error
 	SameTimePlan() error
+	PregnancyCheck() error
 }

+ 23 - 0
module/crontab/other.go

@@ -0,0 +1,23 @@
+package crontab
+
+import (
+	"errors"
+	"kpt-pasture/model"
+	"time"
+
+	"gorm.io/gorm"
+)
+
+// CreateCrontabLog 生成日志记录
+func (e *Entry) CreateCrontabLog(name string) bool {
+	currDateTime := time.Now().Format(model.LayoutDate2)
+	newCronLog := &model.CronLog{}
+	if err := e.DB.Where("name = ?", name).
+		Where("date = ?", currDateTime).First(newCronLog).Error; err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			e.DB.Create(model.NewCronLog(name))
+			return true
+		}
+	}
+	return false
+}

+ 148 - 0
module/crontab/work_cron.go

@@ -0,0 +1,148 @@
+package crontab
+
+import (
+	"context"
+	"encoding/json"
+	"errors"
+	"kpt-pasture/model"
+	"kpt-pasture/module/backend"
+	"kpt-pasture/util"
+	"time"
+
+	"gorm.io/gorm"
+
+	"gitee.com/xuyiping_admin/pkg/xerr"
+
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"go.uber.org/zap"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
+
+// GenerateAsynqWorkOrder 异步生成工作单
+func (e *Entry) GenerateAsynqWorkOrder() error {
+	workOrderList := make([]*model.WorkOrderMaster, 0)
+	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&workOrderList).Error; err != nil {
+		return err
+	}
+	for _, workOrder := range workOrderList {
+		timeUnix, err := util.ConvertParseLocalUnix(workOrder.ExecTime)
+		if timeUnix <= 0 || err != nil {
+			zaplog.Error("crontab", zap.Any("GenerateWorkOrder", err), zap.Any("execTime", workOrder.ExecTime))
+			continue
+		}
+		execTime := time.Now().Unix() - timeUnix
+
+		if _, err = e.AsynqClient.CtxEnqueue(
+			context.Background(),
+			model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second),
+		); err != nil {
+			zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
+		}
+	}
+	return nil
+}
+
+// GenerateCalendarBySameTimePlan 生成同期计划工作单
+func (e *Entry) GenerateCalendarBySameTimePlan(cowList []*model.Cow, sameTime *model.SameTime) error {
+	if len(cowList) <= 0 {
+		return nil
+	}
+
+	cowSameTimeList := make([]*model.SameTimeCow, 0)
+	for _, cow := range cowList {
+		newCowSameTime, err := e.createNewCowSameTime(cow, sameTime)
+		if err != nil {
+			zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cow", cow))
+		}
+		cowSameTimeList = append(cowSameTimeList, newCowSameTime)
+	}
+
+	calendarName := backend.CalendarTypeMap()[pasturePb.CalendarType_Immunisation]
+	workOrderCalendarList := make([]*model.WorkOrderCalendar, 0)
+	if len(sameTime.CollateNodes) > 0 {
+		collateNodes := make([]*pasturePb.CollateNode, 0)
+		var _ = json.Unmarshal([]byte(sameTime.CollateNodes), &collateNodes)
+		nowTime := time.Now()
+		for i, collateNode := range collateNodes {
+			showDay := nowTime.Format(model.LayoutDate2)
+			if i > 0 {
+				showDay = nowTime.Add(time.Hour * 24 * time.Duration(collateNode.NextNodeDay)).Format(model.LayoutDate2)
+			}
+
+			histCount := e.GetTowardSameTimeCowSum(sameTime.Id, showDay)
+			workOrderCalendarList = append(workOrderCalendarList, &model.WorkOrderCalendar{
+				Name:         calendarName,
+				CalendarType: pasturePb.CalendarType_Immunisation,
+				Count:        int32(len(cowSameTimeList)) + int32(histCount),
+				ShowDay:      showDay,
+				IsShow:       pasturePb.IsShow_Ok,
+			})
+		}
+	}
+
+	if err := e.DB.Transaction(func(tx *gorm.DB) error {
+		// 创建新的牛只同期计划详情
+		if err := tx.Create(cowSameTimeList).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+		for _, v := range workOrderCalendarList {
+			if err := tx.Model(&model.WorkOrderCalendar{}).Where(map[string]interface{}{
+				"calendar_type": v.CalendarType,
+				"show_day":      v.ShowDay,
+			}).Assign(map[string]interface{}{
+				"name":          v.Name,
+				"calendar_type": v.CalendarType,
+				"count":         v.Count,
+				"show_day":      v.ShowDay,
+				"is_show":       v.IsShow,
+			}).FirstOrCreate(&model.Pen{}).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+		}
+
+		return nil
+	}); err != nil {
+		return xerr.WithStack(err)
+	}
+
+	return nil
+}
+
+func (e *Entry) createNewCowSameTime(cow *model.Cow, sameTime *model.SameTime) (*model.SameTimeCow, error) {
+	cowSameTime := &model.SameTimeCow{}
+	if err := e.DB.Where("cow_id = ?", cow.Id).
+		Where("lact = ?", cow.Lact).
+		Where("same_time_id = ?", sameTime.Id).
+		First(cowSameTime).Error; err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return &model.SameTimeCow{
+				CowId:      cow.Id,
+				SameTimeId: sameTime.Id,
+				Status:     pasturePb.SameTimeStatus_No_Start,
+				StartAt:    time.Now().Unix(),
+				EndAt:      0,
+			}, nil
+		} else {
+			zaplog.Error("crontab", zap.Error(err), zap.Any("GenerateCalendarBySameTimePlan", "error"), zap.Any("cow", cow.Id), zap.Any("lact", cow.Lact))
+			return nil, xerr.WithStack(err)
+		}
+	}
+	return cowSameTime, nil
+}
+
+func (e *Entry) getWorkOrderCalendar(name string) []*model.WorkOrderCalendar {
+	res := make([]*model.WorkOrderCalendar, 0)
+	if err := e.DB.Where("name = ?", name).
+		Where("is_show = ?", pasturePb.IsShow_Ok).
+		Find(&res).Error; err != nil {
+		zaplog.Error("getWorkOrderCalendar", zap.Any("err", err))
+	}
+	return res
+}
+
+// PregnancyCheck 妊娠期检查
+func (e *Entry) PregnancyCheck() error {
+
+	return nil
+}

+ 0 - 0
module/crontab/crontab_test.go → module/crontab/work_cron_test.go


+ 4 - 4
service/asynqsvc/client.go

@@ -28,12 +28,12 @@ type ClientEntry struct {
 }
 
 func (c *ClientEntry) CtxEnqueue(ctx context.Context, task *asynq.Task, ops ...Option) (*asynq.TaskInfo, error) {
-	zaplog.Info("asynq CtxEnqueue", zap.Any("task", task), zap.Any("ops", ops))
 	taskInfo, err := c.Client.Enqueue(task, ops...)
 	if err != nil {
-		zaplog.Info("asynq CtxEnqueue failed Error ", zap.Any("err", err))
+		zaplog.Error("asynq CtxEnqueue failed Error ", zap.Any("err", err))
 		return taskInfo, xerr.WithStack(err)
-	} else {
-		return taskInfo, nil
 	}
+
+	zaplog.Info("asynq CtxEnqueue", zap.Any("task", task), zap.Any("ops", ops))
+	return taskInfo, nil
 }