|
@@ -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"
|
|
|
)
|
|
|
|
|
|
|
|
@@ -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
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-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
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-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
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-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))
|
|
|
+
|
|
|
+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
|
|
|
}
|