Browse Source

work: calendar update

Yi 3 days ago
parent
commit
905d16b97f
7 changed files with 54 additions and 39 deletions
  1. 1 1
      go.mod
  2. 6 0
      go.sum
  3. 1 1
      http/handler/work/calendar.go
  4. 1 0
      model/calendar.go
  5. 3 3
      model/event_dry_milk.go
  6. 26 19
      module/backend/calendar.go
  7. 16 15
      module/backend/calendar_more.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250325071609-113a7f4c6da5
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250326020740-d3d1bc9a851f
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 6 - 0
go.sum

@@ -86,6 +86,12 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250325011618-727f0797ea27 h1:DruuERLr
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250325011618-727f0797ea27/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250325071609-113a7f4c6da5 h1:gOfJObe2DLEfFjO+8+atJ2mLWAo6HAkOnYQIFW4P7tk=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250325071609-113a7f4c6da5/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250325093632-db9c4586260e h1:d5OaTPXG1JP/2k11WBgj/pggSMrBYYT67R9pD/Z9la4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250325093632-db9c4586260e/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250326020249-4550f6f60bbe h1:gR2DdJByndC/DBKOIV0c6yA6PiPkMaL9BSRREGdl/tw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250326020249-4550f6f60bbe/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250326020740-d3d1bc9a851f h1:vQm+DMaNM40JJAkx/KUgfBbDYZxrLv4dAUbqS8k7Y68=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250326020740-d3d1bc9a851f/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/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 1 - 1
http/handler/work/calendar.go

@@ -80,7 +80,7 @@ func CalendarToDoList(c *gin.Context) {
 		apierr.ClassifiedAbort(c, err)
 		return
 	}
-	c.JSON(http.StatusOK, res)
+	ginutil.JSONResp(c, res)
 }
 
 func ImmunizationItems(c *gin.Context) {

+ 1 - 0
model/calendar.go

@@ -86,5 +86,6 @@ func (c CalendarSlice) ToPB() []*pasturePb.Calendar {
 
 type TodayCompletedData struct {
 	Count            int32
+	CalendarTypeKind pasturePb.CalendarType_Kind
 	CalendarTypeName string
 }

+ 3 - 3
model/event_dry_milk.go

@@ -17,7 +17,7 @@ type EventDryMilk struct {
 	PlanDay       int64                 `json:"planDay"`
 	RealityDay    int64                 `json:"realityDay"`
 	EndDay        int64                 `json:"endDay"`
-	IsShow        pasturePb.IsShow_Kind `json:"isShow"`
+	Status        pasturePb.IsShow_Kind `json:"status"`
 	Remarks       string                `json:"remarks"`
 	OperationId   int64                 `json:"operationId"`
 	OperationName string                `json:"operationName"`
@@ -35,7 +35,7 @@ func (e *EventDryMilk) EventDryMilkUpdate(cow *Cow, dryMilkAt int64, pen *Pen, o
 	e.Lact = cow.Lact
 	e.RealityDay = dryMilkAt
 	e.Remarks = remarks
-	e.IsShow = pasturePb.IsShow_Ok
+	e.Status = pasturePb.IsShow_Ok
 	e.OperationId = operation.Id
 	e.OperationName = operation.Name
 	e.MessageId = message.Id
@@ -52,7 +52,7 @@ func NewEventDryMilk(pastureId int64, cow *Cow, startDay, endDay string) *EventD
 		Lact:      cow.Lact,
 		PlanDay:   util.TimeParseLocalUnix(startDay),
 		EndDay:    util.TimeParseLocalUnix(endDay),
-		IsShow:    pasturePb.IsShow_No,
+		Status:    pasturePb.IsShow_No,
 	}
 }
 

+ 26 - 19
module/backend/calendar.go

@@ -21,20 +21,20 @@ func (s *StoreEntry) CalendarToDoList(ctx context.Context, req *pasturePb.Calend
 	whereSql := fmt.Sprintf(" AND pasture_id = %d AND end_day <= %d ", userModel.AppPasture.Id, util.TimeParseLocalEndUnix(time.Now().Format(model.LayoutDate2)))
 
 	calendarToDoList := make([]*pasturePb.CalendarToDoList, 0)
-	sql := `SELECT a.cow_id,b.pen_name,a.calendar_type_name,DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day,a.remaining_days,b.lact,b.ear_number FROM (
-		SELECT cow_id,plan_day,'免疫' as calendar_type_name,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_immunization_plan WHERE status = 2` + whereSql + `
+	sql := `SELECT a.cow_id,b.pen_name,a.calendar_type_name,a.calendar_type_kind as calendar_type,DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day,a.remaining_days,b.lact,b.ear_number FROM (
+		SELECT cow_id,plan_day,'免疫' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_immunization_plan WHERE status = 2` + whereSql + `
 		UNION ALL
-		SELECT cow_id,plan_day,'同期' as calendar_type_name,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_cow_same_time WHERE status = 2` + whereSql + `
+		SELECT cow_id,plan_day,'同期' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_cow_same_time WHERE status = 2` + whereSql + `
 		UNION ALL
-		SELECT cow_id,plan_day,'孕检' as calendar_type_name,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_pregnant_check WHERE status = 2` + whereSql + `
+		SELECT cow_id,plan_day,'孕检' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_pregnant_check WHERE status = 2` + whereSql + `
 		UNION ALL
-		SELECT cow_id,plan_day,'断奶' as calendar_type_name,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_weaning WHERE status = 2` + whereSql + `
+		SELECT cow_id,plan_day,'断奶' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_weaning WHERE status = 2` + whereSql + `
 		UNION ALL
-		SELECT cow_id,plan_day,'配种' as calendar_type_name,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_mating WHERE status = 2` + whereSql + `
+		SELECT cow_id,plan_day,'配种' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_mating WHERE status = 2` + whereSql + `
 		UNION ALL
-		SELECT cow_id,plan_day,'产犊' as calendar_type_name,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_calving WHERE status = 2` + whereSql + `
+		SELECT cow_id,plan_day,'产犊' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days FROM event_calving WHERE status = 2` + whereSql + `
 		UNION ALL
-		SELECT cow_id,disease_at as plan_day,'疾病' as calendar_type_name,0 AS remaining_days FROM event_cow_disease WHERE diagnosed_result IN (2,3) AND pasture_id = ` + fmt.Sprintf("%d", userModel.AppPasture.Id) + `
+		SELECT cow_id,disease_at as plan_day,'疾病' as calendar_type_name,1 as calendar_type_kind,0 AS remaining_days FROM event_cow_disease WHERE diagnosed_result IN (2,3) AND pasture_id = ` + fmt.Sprintf("%d", userModel.AppPasture.Id) + `
 	) as a JOIN cow b ON a.cow_id = b.id WHERE 1 = 1 `
 
 	completeSql := fmt.Sprintf("%s ORDER BY a.plan_day DESC", sql)
@@ -43,30 +43,37 @@ func (s *StoreEntry) CalendarToDoList(ctx context.Context, req *pasturePb.Calend
 	}
 
 	nowTime := time.Now().Format(model.LayoutDate2)
-	todayCompletedSql := `SELECT a.count as count,a.calendar_type_name as calendar_type_name FROM (
-		SELECT count('cow_id') as count,'免疫' as calendar_type_name FROM event_immunization_plan WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
+	todayCompletedSql := `SELECT a.count as count,a.calendar_type_name as calendar_type_name,a.calendar_type_kind as calendar_type_kind FROM (
+		SELECT count('cow_id') as count,'免疫' as calendar_type_name,1 as calendar_type_kind FROM event_immunization_plan WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
 		UNION ALL
-		SELECT count('cow_id') as count,'同期' as calendar_type_name FROM event_cow_same_time WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
+		SELECT count('cow_id') as count,'同期' as calendar_type_name,2 as calendar_type_kind FROM event_cow_same_time WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
 		UNION ALL
-		SELECT count('cow_id') as count,'孕检' as calendar_type_name FROM event_pregnant_check WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
+		SELECT count('cow_id') as count,'孕检' as calendar_type_name,4 as calendar_type_kind FROM event_pregnant_check WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
 		UNION ALL
-		SELECT count('cow_id') as count,'断奶' as calendar_type_name FROM event_weaning WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
+		SELECT count('cow_id') as count,'断奶' as calendar_type_name,6 as calendar_type_kind FROM event_weaning WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
 		UNION ALL
-		SELECT count('cow_id') as count,'配种' as calendar_type_name FROM event_mating WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
+		SELECT count('cow_id') as count,'配种' as calendar_type_name,8 as calendar_type_kind FROM event_mating WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
 		UNION ALL
-		SELECT count('cow_id') as count,'产犊' as calendar_type_name FROM event_calving WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
+		SELECT count('cow_id') as count,'产犊' as calendar_type_name,9 as calendar_type_kind FROM event_calving WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?
 		UNION ALL
-		SELECT count('cow_id') as count,'疾病' as calendar_type_name FROM event_cow_disease WHERE diagnosed_result = 4 AND DATE_FORMAT(FROM_UNIXTIME(curable_at), '%Y-%m-%d') = ?
+		SELECT count('cow_id') as count,'干奶' as calendar_type_name,10 as calendar_type_kind FROM event_dry_milk WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ?		
+		UNION ALL
+		SELECT count('cow_id') as count,'疾病' as calendar_type_name,7 as calendar_type_kind FROM event_cow_disease WHERE diagnosed_result = 4 AND DATE_FORMAT(FROM_UNIXTIME(curable_at), '%Y-%m-%d') = ?
 	) as a `
 
 	toDayCompletedList := make([]*model.TodayCompletedData, 0)
-	if err = s.DB.Raw(todayCompletedSql, nowTime, nowTime, nowTime, nowTime, nowTime, nowTime, nowTime).Find(&toDayCompletedList).Error; err != nil {
+	if err = s.DB.Raw(todayCompletedSql, nowTime, nowTime, nowTime, nowTime, nowTime, nowTime, nowTime, nowTime).
+		Find(&toDayCompletedList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
-	toDayCompletedCountMap := make(map[string]int32)
+	toDayCompletedCountMap := make(map[pasturePb.CalendarType_Kind]*pasturePb.ProgressList)
 	for _, v := range toDayCompletedList {
-		toDayCompletedCountMap[v.CalendarTypeName] = v.Count
+		toDayCompletedCountMap[v.CalendarTypeKind] = &pasturePb.ProgressList{
+			CalendarTypeKind: v.CalendarTypeKind,
+			CalendarName:     v.CalendarTypeName,
+			CompletedCount:   v.Count,
+		}
 	}
 
 	list, total := Paginate(calendarToDoList, req, pagination)

+ 16 - 15
module/backend/calendar_more.go

@@ -261,27 +261,28 @@ func Paginate(slice []*pasturePb.CalendarToDoList, req *pasturePb.CalendarToDoRe
 	return newSlice[start:end], total
 }
 
-func ProgressList(dataList []*pasturePb.CalendarToDoList, toDayCompletedCountMap map[string]int32) map[string]*pasturePb.ProgressList {
+func ProgressList(dataList []*pasturePb.CalendarToDoList, toDayCompletedCountMap map[pasturePb.CalendarType_Kind]*pasturePb.ProgressList) map[string]*pasturePb.ProgressList {
 	res := make(map[string]*pasturePb.ProgressList)
-	for cn, cc := range toDayCompletedCountMap {
-		res[cn] = &pasturePb.ProgressList{
-			CalendarName:   cn,
-			CompletedCount: cc,
-		}
+	dMap := map[pasturePb.CalendarType_Kind][]*pasturePb.CalendarToDoList{}
+	for _, v := range dataList {
+		dMap[v.CalendarType] = append(dMap[v.CalendarType], v)
+	}
 
-		for _, d := range dataList {
-			calendarName := CalendarTypeMap()[d.CalendarType]
-			if calendarName != cn {
-				continue
+	for kind, cc := range toDayCompletedCountMap {
+		if res[cc.CalendarName] == nil {
+			res[cc.CalendarName] = &pasturePb.ProgressList{
+				CalendarTypeKind: kind,
+				CalendarName:     cc.CalendarName,
+				Color:            model.CalendarTypeColorMap[kind],
+				CompletedCount:   cc.CompletedCount,
+				IncompleteTotal:  int32(len(dMap[kind])),
 			}
-			res[cn].CalendarTypeKind = d.CalendarType
-			res[cn].IncompleteTotal += 1
 		}
 
-		if res[cn].IncompleteTotal > 0 && res[cn].CompletedCount > 0 {
-			res[cn].Progress = strconv.FormatFloat(float64(res[cn].CompletedCount)/float64(res[cn].IncompleteTotal)*100, 'f', 2, 64)
+		if res[cc.CalendarName].IncompleteTotal > 0 && res[cc.CalendarName].CompletedCount > 0 {
+			res[cc.CalendarName].Progress = strconv.FormatFloat(float64(res[cc.CalendarName].CompletedCount)/float64(res[cc.CalendarName].IncompleteTotal)*100, 'f', 2, 64)
 		} else {
-			res[cn].Progress = "0%"
+			res[cc.CalendarName].Progress = "0%"
 		}
 	}