|
@@ -3,7 +3,9 @@ package crontab
|
|
|
import (
|
|
|
"context"
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"kpt-pasture/model"
|
|
|
+ "kpt-pasture/module/backend"
|
|
|
"kpt-pasture/util"
|
|
|
"time"
|
|
|
|
|
@@ -28,7 +30,7 @@ func (e *Entry) UpdateCowInfo() error {
|
|
|
calvingAge := cow.GetCalvingAge()
|
|
|
pregnancyAge := cow.GetDaysPregnant()
|
|
|
admissionAge := cow.GetAdmissionAge()
|
|
|
- if err := e.DB.Where("id = ?", cow.Id).Updates(map[string]interface{}{
|
|
|
+ if err := e.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{
|
|
|
"day_age": dayAge,
|
|
|
"calving_at": calvingAge,
|
|
|
"pregnancy_age": pregnancyAge,
|
|
@@ -101,7 +103,7 @@ func (e *Entry) ImmunizationPlan() error {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- if err := e.GenerateCalendarByImmunization(cowList, plan.Name); err != nil {
|
|
|
+ if err := e.GenerateCalendarByImmunization(cowList, plan); err != nil {
|
|
|
zaplog.Error("crontab", zap.Any("GenerateWorkOrderCalendar", err), zap.Any("cowList", cowList))
|
|
|
}
|
|
|
}
|
|
@@ -115,34 +117,20 @@ func (e *Entry) SameTimePlan() error {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- cowSameTimeList := make([]*model.SameTimeCow, 0)
|
|
|
- if err := e.DB.Select("cow_id").Where("status = ?", pasturePb.IsShow_Ok).Find(&cowSameTimeList).Error; err != nil {
|
|
|
- if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- cowIds := make([]int64, 0)
|
|
|
- if len(cowSameTimeList) > 0 {
|
|
|
- for _, cowSameTime := range cowSameTimeList {
|
|
|
- cowIds = append(cowIds, cowSameTime.CowId)
|
|
|
- }
|
|
|
- }
|
|
|
+ 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 := e.DB.Select("id").
|
|
|
- Where("is_remove = ?", pasturePb.IsShow_Ok).
|
|
|
- Where("is_pregnant = ?", pasturePb.IsShow_No).
|
|
|
- Where("calving_age >= ?", plan.PostpartumDaysStart).
|
|
|
- Where("calving_age <= ?", plan.PostpartumDaysEnd)
|
|
|
- if len(cowIds) > 0 {
|
|
|
- pref.Where("id NOT IN ?", cowIds)
|
|
|
- }
|
|
|
|
|
|
- if err := pref.Find(&cowList); err != nil {
|
|
|
+ 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))
|
|
|
- continue
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
+
|
|
|
if len(cowList) <= 0 {
|
|
|
continue
|
|
|
}
|
|
@@ -154,14 +142,82 @@ func (e *Entry) SameTimePlan() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, name string) error {
|
|
|
- workOrderCalendarList := e.getWorkOrderCalendar(name)
|
|
|
+// 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(cowSameTimeList) <= 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ workOrderCalendarList := make([]*model.WorkOrderCalendar, 0)
|
|
|
+ calendarMap := backend.CalendarTypeMap()
|
|
|
+ workOrderCalendarList = append(workOrderCalendarList,
|
|
|
+ model.NewWorkOrderCalendar(
|
|
|
+ calendarMap[pasturePb.CalendarType_Immunisation],
|
|
|
+ pasturePb.CalendarType_Immunisation,
|
|
|
+ int32(len(cowSameTimeList)),
|
|
|
+ ))
|
|
|
+ if len(sameTime.CollateNodes) > 0 {
|
|
|
+ for _, collateNode := range sameTime.CollateNodes {
|
|
|
+ workOrderCalendarList = append(workOrderCalendarList, nil)
|
|
|
+ // todo 待实现待实现待实现
|
|
|
+ fmt.Println("==collateNode==", collateNode)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := e.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ // 创建新的牛只同期计划详情
|
|
|
+ if err := tx.Create(cowSameTimeList).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).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)
|
|
|
if len(workOrderCalendarList) > 0 {
|
|
|
// 过滤已经存在的牛只数据,避免重复生成工作单
|
|
|
calendarIds := make([]int64, 0)
|
|
|
if err := e.DB.Model(&model.WorkOrderCalendar{}).Select("id").
|
|
|
- Where("name = ?", name).
|
|
|
+ Where("name = ?", plan.Name).
|
|
|
Where("is_show = ?", pasturePb.IsShow_Ok).
|
|
|
Order("id DESC").
|
|
|
Limit(100). // todo 默认取100条数据
|
|
@@ -190,13 +246,18 @@ func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, name string
|
|
|
}
|
|
|
count := len(newCowList)
|
|
|
if err := e.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- newWorkOrderCalendar := model.NewWorkOrderCalendar(name, int32(count))
|
|
|
+ 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(name, newWorkOrderCalendar.Id, cow.Id))
|
|
|
+ newWorkOrderList = append(newWorkOrderList, model.NewWorkOrderList(plan.Name, newWorkOrderCalendar.Id, cow.Id))
|
|
|
}
|
|
|
if err := tx.Create(newWorkOrderList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -208,29 +269,6 @@ func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, name string
|
|
|
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 {
|
|
|
- cowSameTimeList = append(cowSameTimeList, &model.SameTimeCow{
|
|
|
- CowId: cow.Id,
|
|
|
- SameTimeId: sameTime.Id,
|
|
|
- Status: pasturePb.IsShow_Ok,
|
|
|
- StartAt: time.Now().Unix(),
|
|
|
- EndAt: 0,
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- if err := e.DB.Create(cowSameTimeList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
func (e *Entry) getWorkOrderCalendar(name string) []*model.WorkOrderCalendar {
|
|
|
res := make([]*model.WorkOrderCalendar, 0)
|
|
|
if err := e.DB.Where("name = ?", name).
|