Ver código fonte

crontab: update

Yi 6 meses atrás
pai
commit
c90e5fd156

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240912024219-df4ae8e00ef3
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240912072215-f7745ae5f7be
 	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

@@ -66,6 +66,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240912013108-b140d281ec27 h1:wzQovv4f
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240912013108-b140d281ec27/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240912024219-df4ae8e00ef3 h1:wYX+YcTbo+81TrtewBCWExxctetMvjN1sabiq3oPI6k=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240912024219-df4ae8e00ef3/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912053920-4574bdb15216 h1:NWizM8am/bDb3L+xZUlNbLrPIyz4hKe7qhBDoaKbbug=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912053920-4574bdb15216/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912072215-f7745ae5f7be h1:c8X60ELEPuwUbjNCQ/ensGzX8vwLa8Bxi2kjUjjp9RA=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912072215-f7745ae5f7be/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=

+ 0 - 2
http/handler/pasture/prescription.go

@@ -186,10 +186,8 @@ func CreatedOrUpdateImmunization(c *gin.Context) {
 
 	if err := valid.ValidateStruct(&req,
 		valid.Field(&req.Name, valid.Required),
-		valid.Field(&req.CowType, valid.Required),
 		valid.Field(&req.Conditions, valid.Required),
 		valid.Field(&req.Value, valid.Required),
-		valid.Field(&req.Value2, valid.Required),
 	); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
 		return

+ 35 - 0
http/handler/work/calendar.go

@@ -0,0 +1,35 @@
+package work
+
+import (
+	"kpt-pasture/http/middleware"
+	"net/http"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	"gitee.com/xuyiping_admin/pkg/apierr"
+	"gitee.com/xuyiping_admin/pkg/ginutil"
+	"gitee.com/xuyiping_admin/pkg/valid"
+	"github.com/gin-gonic/gin"
+)
+
+func CalendarList(c *gin.Context) {
+	var req pasturePb.WorkOrderCalendarRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.ShowStartDay, valid.Required),
+		valid.Field(&req.ShowEndDay, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.CalendarList(c, &req)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}

+ 3 - 0
http/route/work_order.go

@@ -17,5 +17,8 @@ func WorkOrderAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		workRoute.POST("/order/createOrUpdate", work.OrderCreateOrUpdate)
 		workRoute.PUT("/order/is_show/:id", work.OrderIsShow)
 		workRoute.GET("/user/order/list/:status", work.UserOrderList)
+
+		workRoute.POST("/calendar/list", work.CalendarList)
+
 	}
 }

+ 19 - 23
model/immunization_plan.go

@@ -1,23 +1,21 @@
 package model
 
 import (
-	"fmt"
-
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 
 type ImmunizationPlan struct {
-	Id            int64                                 `json:"id"`
-	Name          string                                `json:"name"`
-	CowType       pasturePb.CowType_Kind                `json:"cow_type"`
-	Conditions    pasturePb.ImmunizationConditions_Kind `json:"conditions"`
-	Value         int64                                 `json:"value"`
-	Value2        int64                                 `json:"value2"`
-	IsShow        pasturePb.IsShow_Kind                 `json:"is_show"`
-	OperationId   int64                                 `json:"operation_id"`
-	OperationName string                                `json:"operation_name"`
-	CreatedAt     int64                                 `json:"created_at"`
-	UpdatedAt     int64                                 `json:"updated_at"`
+	Id                 int64                                 `json:"id"`
+	Name               string                                `json:"name"`
+	CowType            pasturePb.CowType_Kind                `json:"cowType"`
+	Conditions         pasturePb.ImmunizationConditions_Kind `json:"conditions"`
+	Value              int64                                 `json:"value"`
+	IsShow             pasturePb.IsShow_Kind                 `json:"isShow"`
+	ImmunizationPlanId int64                                 `json:"immunizationPlanId"`
+	OperationId        int64                                 `json:"operationId"`
+	OperationName      string                                `json:"operationName"`
+	CreatedAt          int64                                 `json:"createdAt"`
+	UpdatedAt          int64                                 `json:"updatedAt"`
 }
 
 func (i *ImmunizationPlan) TableName() string {
@@ -26,14 +24,14 @@ func (i *ImmunizationPlan) TableName() string {
 
 func NewImmunizationPlan(systemUser *SystemUser, req *pasturePb.ImmunizationRequest) *ImmunizationPlan {
 	return &ImmunizationPlan{
-		Name:          req.Name,
-		CowType:       req.CowType,
-		Conditions:    req.Conditions,
-		Value:         int64(req.Value),
-		Value2:        int64(req.Value2),
-		IsShow:        req.IsShow,
-		OperationId:   systemUser.Id,
-		OperationName: systemUser.Name,
+		Name:               req.Name,
+		CowType:            req.CowType,
+		Conditions:         req.Conditions,
+		Value:              int64(req.Value),
+		ImmunizationPlanId: int64(req.ImmunizationPlanId),
+		IsShow:             req.IsShow,
+		OperationId:        systemUser.Id,
+		OperationName:      systemUser.Name,
 	}
 }
 
@@ -68,8 +66,6 @@ func (i ImmunizationPlanSlice) ToPB(cowTypeOptions []*pasturePb.ConfigOptionsLis
 			OperationName:  v.OperationName,
 			CreatedAt:      int32(v.CreatedAt),
 			UpdatedAt:      int32(v.UpdatedAt),
-			Value2:         int32(v.Value2),
-			DaysRange:      fmt.Sprintf("%d ~ %d", v.Value, v.Value2),
 		}
 	}
 	return res

+ 47 - 0
model/immunization_plan_cow.go

@@ -0,0 +1,47 @@
+package model
+
+import (
+	"time"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
+
+type ImmunizationPlanCow struct {
+	Id                   int64                 `json:"id"`
+	CowId                int64                 `json:"cowId"`
+	ImmunizationPlanId   int64                 `json:"immunizationPlanId"`
+	ImmunizationPlanName string                `json:"immunizationPlanName"`
+	PlanStartTime        string                `json:"planStartTime"`
+	RealityTime          string                `json:"realityTime"`
+	Status               pasturePb.IsShow_Kind `json:"status"`
+	OperationId          int64                 `json:"operationId"`
+	OperationName        string                `json:"operationName"`
+	DrugsId              int64                 `json:"drugsId"`
+	Unit                 pasturePb.Unit_Kind   `json:"unit"`
+	Usage                string                `json:"usage"`
+	Remarks              string                `json:"remarks"`
+	CreateAt             int64                 `json:"createAt"`
+	UpdateAt             int64                 `json:"updateAt"`
+}
+
+func (i *ImmunizationPlanCow) TableName() string {
+	return "immunization_plan_cow"
+}
+
+func NewImmunizationPlanCow(cowId int64, immunizationPlan *ImmunizationPlan) *ImmunizationPlanCow {
+	return &ImmunizationPlanCow{
+		CowId:                cowId,
+		ImmunizationPlanId:   immunizationPlan.Id,
+		ImmunizationPlanName: immunizationPlan.Name,
+		PlanStartTime:        time.Now().Format(LayoutDate2),
+		Status:               pasturePb.IsShow_No,
+	}
+}
+
+func NewImmunizationPlanCowList(cowList []*Cow, immunizationPlan *ImmunizationPlan) []*ImmunizationPlanCow {
+	immunizationPlanCowList := make([]*ImmunizationPlanCow, len(cowList))
+	for i, v := range cowList {
+		immunizationPlanCowList[i] = NewImmunizationPlanCow(v.Id, immunizationPlan)
+	}
+	return immunizationPlanCowList
+}

+ 18 - 0
model/work_order_calendar.go

@@ -13,6 +13,7 @@ type WorkOrderCalendar struct {
 	Count        int32                       `json:"count"`
 	ShowDay      string                      `json:"showDay"`
 	IsShow       pasturePb.IsShow_Kind       `json:"isShow"`
+	Backup       string                      `json:"backup"`
 	CreatedAt    int64                       `json:"createdAt"`
 	UpdatedAt    int64                       `json:"updatedAt"`
 }
@@ -30,3 +31,20 @@ func NewWorkOrderCalendar(name string, calendarType pasturePb.CalendarType_Kind,
 		IsShow:       pasturePb.IsShow_Ok,
 	}
 }
+
+type WorkOrderCalendarSlice []*WorkOrderCalendar
+
+func (w WorkOrderCalendarSlice) ToPB() []*pasturePb.WorkOrderCalendar {
+	res := make([]*pasturePb.WorkOrderCalendar, len(w))
+	for i, v := range w {
+		res[i] = &pasturePb.WorkOrderCalendar{
+			Id:           int32(v.Id),
+			Name:         v.Name,
+			CalendarType: v.CalendarType,
+			Count:        v.Count,
+			ShowDay:      v.ShowDay,
+			Backup:       v.Backup,
+		}
+	}
+	return res
+}

+ 5 - 1
module/backend/config_data.go

@@ -255,6 +255,10 @@ func (s *StoreEntry) ImmunizationConditionsEnumList() []*pasturePb.ConfigOptions
 		Value:    int32(pasturePb.ImmunizationConditions_Admission_Days),
 		Label:    "入场天数",
 		Disabled: true,
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.ImmunizationConditions_Other_Vaccine_After),
+		Label:    "基于其他疫苗之后",
+		Disabled: true,
 	})
 	return cowTypeList
 }
@@ -724,7 +728,7 @@ func CalendarTypeEnumList() []*pasturePb.ConfigOptionsList {
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
 		Value:    int32(pasturePb.CalendarType_Pregnancy_Check),
-		Label:    "孕检-初检",
+		Label:    "孕检",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
 		Value:    int32(pasturePb.CalendarType_WorkOrder),

+ 2 - 0
module/backend/interface.go

@@ -197,4 +197,6 @@ type WorkService interface {
 	OrderCreateOrUpdate(ctx context.Context, req *pasturePb.WorkOrderList) error
 	OrderIsShow(ctx context.Context, id int64) error
 	UserWorkOrderList(ctx context.Context, workOrderStatus pasturePb.WorkOrderStatus_Kind, pagination *pasturePb.PaginationModel) (*pasturePb.UserWorkOrderResponse, error)
+
+	CalendarList(ctx context.Context, req *pasturePb.WorkOrderCalendarRequest) (*pasturePb.WorkOrderCalendarResponse, error)
 }

+ 17 - 18
module/backend/prescription.go

@@ -362,34 +362,33 @@ func (s *StoreEntry) ImmunizationList(ctx context.Context, req *pasturePb.Immuni
 }
 
 func (s *StoreEntry) CreatedOrUpdateImmunization(ctx context.Context, req *pasturePb.ImmunizationRequest) error {
-	var immunization *model.ImmunizationPlan
-	var err error
+	var (
+		immunization *model.ImmunizationPlan
+		err          error
+		systemUser   *model.SystemUser
+	)
 	if req.Id > 0 {
 		immunization, err = s.GetImmunizationById(ctx, int64(req.Id))
 		if err != nil {
 			return xerr.WithStack(err)
 		}
-		immunization.Name = req.Name
-		immunization.CowType = req.CowType
-		immunization.Conditions = req.Conditions
-		immunization.Value = int64(req.Value)
-		immunization.Value2 = int64(req.Value2)
-		immunization.IsShow = req.IsShow
+		systemUser, _ = s.GetSystemUserById(ctx, immunization.OperationId)
 	} else {
-		systemUser, _ := s.GetCurrentSystemUser(ctx)
-		immunization = model.NewImmunizationPlan(systemUser, req)
+		systemUser, _ = s.GetCurrentSystemUser(ctx)
 	}
+
+	immunization = model.NewImmunizationPlan(systemUser, req)
 	if err = s.DB.Model(&model.ImmunizationPlan{}).Where(map[string]interface{}{
 		"id": req.Id,
 	}).Assign(map[string]interface{}{
-		"name":           immunization.Name,
-		"cow_type":       immunization.CowType,
-		"conditions":     immunization.Conditions,
-		"value":          immunization.Value,
-		"value2":         immunization.Value2,
-		"is_show":        immunization.IsShow,
-		"operation_id":   immunization.OperationId,
-		"operation_name": immunization.OperationName,
+		"name":                 immunization.Name,
+		"cow_type":             immunization.CowType,
+		"conditions":           immunization.Conditions,
+		"value":                immunization.Value,
+		"immunization_plan_id": immunization.ImmunizationPlanId,
+		"is_show":              immunization.IsShow,
+		"operation_id":         immunization.OperationId,
+		"operation_name":       immunization.OperationName,
 	}).FirstOrCreate(&model.ImmunizationPlan{}).Error; err != nil {
 		return xerr.WithStack(err)
 	}

+ 16 - 0
module/backend/work.go

@@ -285,3 +285,19 @@ func (s *StoreEntry) UserWorkOrderList(ctx context.Context, workOrderStatus past
 		},
 	}, nil
 }
+
+func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.WorkOrderCalendarRequest) (*pasturePb.WorkOrderCalendarResponse, error) {
+	calendarList := make([]*model.WorkOrderCalendar, 0)
+	if err := s.DB.Model(&model.WorkOrderCalendar{}).
+		Where("show_day >= ?", req.ShowStartDay).
+		Where("show_day <= ?", req.ShowEndDay).
+		Where("is_show = ?", pasturePb.IsShow_Ok).
+		Find(&calendarList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return &pasturePb.WorkOrderCalendarResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data:    model.WorkOrderCalendarSlice(calendarList).ToPB(),
+	}, nil
+}

+ 44 - 95
module/crontab/cow_cron.go

@@ -1,14 +1,13 @@
 package crontab
 
 import (
+	"fmt"
 	"kpt-pasture/model"
-	"kpt-pasture/module/backend"
-
-	"gitee.com/xuyiping_admin/pkg/xerr"
-	"gorm.io/gorm"
+	"time"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"gitee.com/xuyiping_admin/pkg/xerr"
 	"go.uber.org/zap"
 )
 
@@ -42,36 +41,51 @@ func (e *Entry) UpdateCowInfo() error {
 
 // ImmunizationPlan 免疫计划,生成工作单
 func (e *Entry) ImmunizationPlan() error {
+	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
+		return nil
+	}
+
 	planList := make([]*model.ImmunizationPlan, 0)
 	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&planList).Error; err != nil {
 		return xerr.WithStack(err)
 	}
 
-	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
-		return nil
-	}
+	var todayCount int32 = 0
+	defer func() {
+		e.CreatedWorkOrderCalendar(pasturePb.CalendarType_Immunisation, todayCount)
+	}()
 
 	for _, plan := range planList {
+		if plan == nil {
+			continue
+		}
 		cowList := make([]*model.Cow, 0)
-		pref := e.DB.Select("id").Where("is_remove = ?", pasturePb.IsShow_Ok).
-			Where("cow_type = ?", plan.CowType)
+		pref := e.DB.Table(fmt.Sprintf("%s as a", new(model.ImmunizationPlan).TableName())).
+			Select("a.*").
+			Where("a.is_remove = ?", pasturePb.IsShow_Ok)
+		if plan.CowType > 0 {
+			pref.Where("a.cow_type = ?", plan.CowType)
+		}
 
 		switch plan.Conditions {
 		case pasturePb.ImmunizationConditions_Days_Age:
-			pref.Where("day_age >= ?", plan.Value).
-				Where("day_age <= ?", plan.Value2)
+			pref.Where("a.day_age = ?", plan.Value)
 		case pasturePb.ImmunizationConditions_Days_After_Delivery:
-			pref.Where("calving_age >= ?", plan.Value).
-				Where("calving_age <= ?", plan.Value2)
+			pref.Where("a.calving_age = ?", plan.Value)
 		case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
-			pref.Where("pregnancy_age >= ?", plan.Value).
-				Where("pregnancy_age <= ?", plan.Value2).
-				Where("is_pregnant = ?", pasturePb.IsShow_Ok)
+			pref.Where("a.pregnancy_age = ?", plan.Value).
+				Where("a.is_pregnant = ?", pasturePb.IsShow_Ok)
 		case pasturePb.ImmunizationConditions_Month:
 			// todo 待实现月份
 		case pasturePb.ImmunizationConditions_Admission_Days:
-			pref.Where("admission_age >= ?", plan.Value).
-				Where("admission_age <= ?", plan.Value2)
+			pref.Where("a.admission_age = ?", plan.Value)
+		case pasturePb.ImmunizationConditions_Other_Vaccine_After:
+			if plan.ImmunizationPlanId > 0 {
+				pref.Joins("INNER JOIN immunization_plan_cow as b ON b.immunization_plan_id = ?", plan.ImmunizationPlanId).
+					Where("b.cow_id = a.id").
+					Where("DATE_ADD(b.reality_time, INTERVAL ? DAY) = ?", plan.Value, time.Now().Format(model.LayoutDate2)).
+					Where("b.status = ?", pasturePb.IsShow_Ok)
+			}
 		}
 
 		if err := pref.Find(&cowList).Error; err != nil {
@@ -80,88 +94,21 @@ func (e *Entry) ImmunizationPlan() error {
 		if len(cowList) <= 0 {
 			continue
 		}
-
-		if err := e.GenerateCalendarByImmunization(cowList, plan); err != nil {
-			zaplog.Error("crontab", zap.Any("GenerateWorkOrderCalendar", err), zap.Any("cowList", cowList))
+		todayCount += int32(len(cowList))
+		newImmunizationPlanCowList := model.NewImmunizationPlanCowList(cowList, plan)
+		if err := e.DB.Create(newImmunizationPlanCowList).Error; err != nil {
+			zaplog.Error("ImmunizationPlan", zap.Any("CreateImmunizationPlanCow", err), zap.Any("plan", plan), zap.Any("cowList", cowList))
 		}
 	}
 	return nil
 }
 
-func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, plan *model.ImmunizationPlan) error {
-	workOrderCalendarList := e.getWorkOrderCalendar(plan.Name)
-	newCowList := make([]*model.Cow, 0)
-	if len(workOrderCalendarList) > 0 {
-		// 过滤已经存在的牛只数据,避免重复生成工作单
-		calendarIds := make([]int64, 0)
-		if err := e.DB.Model(&model.WorkOrderCalendar{}).Select("id").
-			Where("name = ?", plan.Name).
-			Where("is_show = ?", pasturePb.IsShow_Ok).
-			Order("id DESC").
-			Limit(100). // todo 默认取100条数据
-			Find(&calendarIds).Error; err != nil {
-			return xerr.WithStack(err)
-		}
-
-		workOrderList := make([]*model.WorkOrderList, 0)
-		if err := e.DB.Where("calendar_id IN ?", calendarIds).
-			Where("is_show  = ?", pasturePb.IsShow_Ok).
-			Where("is_completion = ?", pasturePb.IsShow_No).
-			Find(&workOrderList).Error; err != nil {
-			return xerr.WithStack(err)
-		}
-
-		if len(workOrderList) > 0 {
-			for _, cow := range cowList {
-				for _, workOrder := range workOrderList {
-					if workOrder.CowId == cow.Id {
-						continue
-					}
-				}
-				newCowList = append(newCowList, cow)
-			}
-		}
-	}
-	count := len(newCowList)
-	if err := e.DB.Transaction(func(tx *gorm.DB) error {
-		calendarTypeMap := backend.CalendarTypeMap()
-		newWorkOrderCalendar := model.NewWorkOrderCalendar(
-			calendarTypeMap[pasturePb.CalendarType_Immunisation],
-			pasturePb.CalendarType_Immunisation,
-			int32(count),
-		)
-		if err := tx.Create(newWorkOrderCalendar).Error; err != nil {
-			return xerr.WithStack(err)
-		}
-		newWorkOrderList := make([]*model.WorkOrderList, 0)
-		for _, cow := range newCowList {
-			newWorkOrderList = append(newWorkOrderList, model.NewWorkOrderList(plan.Name, newWorkOrderCalendar.Id, cow.Id))
-		}
-		if err := tx.Create(newWorkOrderList).Error; err != nil {
-			return xerr.WithStack(err)
-		}
+// SameTimePlan 同期计划,生成工作单
+func (e *Entry) SameTimePlan() error {
+	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
 		return nil
-	}); err != nil {
-		return xerr.WithStack(err)
 	}
-	return nil
-}
-
-// GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
-func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, showDay string) int64 {
-	res := int64(0)
-	if err := e.DB.Model(&model.SameTimeCow{}).
-		Where("status = ?", pasturePb.IsShow_Ok).
-		Where("same_time_id = ?", sameTimeId).
-		Where("show_day = ?", showDay).
-		Count(&res).Error; err != nil {
-		zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
-	}
-	return res
-}
 
-// 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)
@@ -171,9 +118,10 @@ func (e *Entry) SameTimePlan() error {
 		Where("is_remove = ?", pasturePb.IsShow_Ok).
 		Where("is_pregnant = ?", pasturePb.IsShow_No)
 
-	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
-		return nil
-	}
+	var todayCount int32 = 0
+	defer func() {
+		e.CreatedWorkOrderCalendar(pasturePb.CalendarType_Immunisation, todayCount)
+	}()
 
 	for _, plan := range sameTimeList {
 		cowList := make([]*model.Cow, 0)
@@ -187,6 +135,7 @@ func (e *Entry) SameTimePlan() error {
 		if len(cowList) <= 0 {
 			continue
 		}
+		todayCount += int32(len(cowList))
 		if err := e.GenerateCalendarBySameTimePlan(cowList, plan); err != nil {
 			zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cowList", cowList), zap.Any("plan", plan))
 			continue

+ 15 - 0
module/crontab/other.go

@@ -3,8 +3,14 @@ package crontab
 import (
 	"errors"
 	"kpt-pasture/model"
+	"kpt-pasture/module/backend"
 	"time"
 
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"go.uber.org/zap"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
 	"gorm.io/gorm"
 )
 
@@ -21,3 +27,12 @@ func (e *Entry) CreateCrontabLog(name string) bool {
 	}
 	return false
 }
+
+// CreatedWorkOrderCalendar 创建当天工单日历记录
+func (e *Entry) CreatedWorkOrderCalendar(calendarType pasturePb.CalendarType_Kind, count int32) {
+	calendarTypeName := backend.CalendarTypeMap()[calendarType]
+	workOrderCalendar := model.NewWorkOrderCalendar(calendarTypeName, calendarType, count)
+	if err := e.DB.Model(&model.WorkOrderCalendar{}).Create(workOrderCalendar).Error; err != nil {
+		zaplog.Error("CreatedWorkOrderCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", workOrderCalendar))
+	}
+}

+ 16 - 4
module/crontab/work_cron.go

@@ -33,10 +33,9 @@ func (e *Entry) GenerateAsynqWorkOrder() error {
 		}
 		execTime := time.Now().Unix() - timeUnix
 
-		if _, err = e.AsynqClient.CtxEnqueue(
-			context.Background(),
-			model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second),
-		); err != nil {
+		task := model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second)
+		zaplog.Info("GenerateWorkOrder", zap.Any("workOrder", workOrder), zap.Any("task", task))
+		if _, err = e.AsynqClient.CtxEnqueue(context.Background(), task); err != nil {
 			zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
 		}
 	}
@@ -141,6 +140,19 @@ func (e *Entry) getWorkOrderCalendar(name string) []*model.WorkOrderCalendar {
 	return res
 }
 
+// GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
+func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, showDay string) int64 {
+	res := int64(0)
+	if err := e.DB.Model(&model.SameTimeCow{}).
+		Where("status = ?", pasturePb.IsShow_Ok).
+		Where("same_time_id = ?", sameTimeId).
+		Where("show_day = ?", showDay).
+		Count(&res).Error; err != nil {
+		zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
+	}
+	return res
+}
+
 // PregnancyCheck 妊娠期检查
 func (e *Entry) PregnancyCheck() error {