Ver código fonte

sameTime: 日历同期数据更新

Yi 7 meses atrás
pai
commit
97001f257f

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240916154658-fd81c51d9378
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240918070654-f8e5007dfb14
 	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

+ 2 - 0
go.sum

@@ -86,6 +86,8 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240914085632-2dbb719d5eb9 h1:/irLE00X
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240914085632-2dbb719d5eb9/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240916154658-fd81c51d9378 h1:60W1C1tbo3bdQJ9WNmBhbPt9hgXZ5lf9Lb7/+6B1+hs=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240916154658-fd81c51d9378/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240918070654-f8e5007dfb14 h1:D8a3ImcphFPiJwaulcEJT6as1PZSumhQAG6gvoV9LZg=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240918070654-f8e5007dfb14/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=

+ 1 - 1
model/calendar.go

@@ -71,5 +71,5 @@ type CalendarData struct {
 	Page     int32       `json:"page"`
 	PageSize int32       `json:"pageSize"`
 	Header   interface{} `json:"header"`
-	List     interface{} `json:"data"`
+	List     interface{} `json:"list"`
 }

+ 14 - 5
model/cow.go

@@ -14,12 +14,13 @@ type Cow struct {
 	NeckRingNumber      string                     `json:"neckRingNumber"`
 	EarNumber           string                     `json:"earNumber"`
 	EarOldNumber        string                     `json:"earOldNumber"`
-	PenId               int64                      `json:"penId"`
+	PenId               int32                      `json:"penId"`
 	Lact                int32                      `json:"lact"`
 	DayAge              int32                      `json:"dayAge"`
 	CalvingAge          int64                      `json:"calvingAge"`
 	PregnancyAge        int64                      `json:"pregnancyAge"` // 怀孕天数 孕检结果有阳性更新,产犊后至0
 	AdmissionAge        int64                      `json:"admissionAge"`
+	AbortionAge         int64                      `json:"abortionAge"` // 流产天数
 	CowType             pasturePb.CowType_Kind     `json:"cowType"`
 	BreedStatus         pasturePb.BreedStatus_Kind `json:"breedStatus"`
 	CowKind             pasturePb.CowKind_Kind     `json:"cowKind"`
@@ -66,7 +67,7 @@ func (c CowSlice) ToPB(
 	for i, v := range c {
 		penName := ""
 		for _, pen := range penList {
-			if v.PenId != int64(pen.Id) {
+			if v.PenId != int32(pen.Id) {
 				continue
 			}
 			penName = pen.Name
@@ -76,7 +77,7 @@ func (c CowSlice) ToPB(
 			Sex:             v.Sex,
 			NeckRingNumber:  v.NeckRingNumber,
 			EarNumber:       v.EarNumber,
-			PenId:           int32(v.PenId),
+			PenId:           v.PenId,
 			PenName:         penName,
 			CowType:         int32(v.CowType),
 			Lact:            v.Lact,
@@ -103,7 +104,7 @@ func NewCow(req *pasturePb.EventEnterData) *Cow {
 	return &Cow{
 		Sex:                 req.Sex,
 		EarNumber:           req.EarNumber,
-		PenId:               int64(req.PenId),
+		PenId:               req.PenId,
 		Lact:                req.Lact,
 		CowType:             req.CowTypeId,
 		BreedStatus:         req.BreedStatusId,
@@ -126,7 +127,7 @@ func NewCalfCow(motherId, fatherId int64, calf *CalvingCalf) *Cow {
 	return &Cow{
 		Sex:         calf.Sex,
 		EarNumber:   calf.EarNumber,
-		PenId:       int64(calf.PenId),
+		PenId:       calf.PenId,
 		CowType:     pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
 		BreedStatus: pasturePb.BreedStatus_UnBreed,    // 未配
 		CowKind:     calf.CowKind,                     // 牛只品种
@@ -220,3 +221,11 @@ func (c *Cow) GetAverageDailyWeight() float64 {
 	}
 	return 0
 }
+
+func (c *Cow) GetAbortionAge() int32 {
+	if c.LastAbortionAt > 0 && c.IsRemove == pasturePb.IsShow_Ok {
+		return int32(math.Floor(float64(time.Now().Unix()-c.LastAbortionAt) / 86400))
+	}
+	return 0
+
+}

+ 47 - 34
model/same_time_cow_detail.go

@@ -7,8 +7,9 @@ import (
 type SameTimeCowDetail struct {
 	Id            int64                       `json:"id"`
 	CowId         int64                       `json:"cowId"`
+	PenId         int32                       `json:"penId"`
+	PenName       string                      `json:"penName"`
 	Lact          int32                       `json:"lact"`
-	PenId         int64                       `json:"penId"`
 	SameTimeId    int64                       `json:"sameTimeId"`
 	SameTimeType  pasturePb.SameTimeType_Kind `json:"sameTimeType"`
 	PlanDay       string                      `json:"planDay"`
@@ -47,48 +48,60 @@ func NewSameTimeCowDetailList(cowList []*Cow, sameTimeId int64, planTime string,
 }
 
 type SameTimeHeader struct {
-	Id          string `json:"id"`
-	CowId       string `json:"cowId"`
-	BreedStatus string `json:"breedStatus"`
-	PenName     string `json:"penName"`
-	Lact        string `json:"lact"`
-	CalvingAge  string `json:"calvingAge"`
-	AbortionAge string `json:"abortionAge"`
-	DayAge      string `json:"dayAge"`
-	Status      string `json:"status"`
-	ExecType    string `json:"execType"`
+	Id              string `json:"id"`
+	CowId           string `json:"cowId"`
+	CowType         string `json:"cowType"`
+	CowTypeName     string `json:"cowTypeName"`
+	BreedStatus     string `json:"breedStatus"`
+	BreedStatusName string `json:"breedStatusName"`
+	PenId           string `json:"penId"`
+	PenName         string `json:"penName"`
+	Lact            string `json:"lact"`
+	CalvingAge      string `json:"calvingAge"`
+	AbortionAge     string `json:"abortionAge"`
+	DayAge          string `json:"dayAge"`
+	Status          string `json:"status"`
 }
 
 type SameTimeBody struct {
-	Id          int64                      `json:"id"`
-	CowId       int64                      `json:"cowId"`
-	BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
-	PenName     string                     `json:"penName"`
-	Lact        int32                      `json:"lact"`
-	CalvingAge  int32                      `json:"calvingAge"`
-	AbortionAge int32                      `json:"abortionAge"`
-	DayAge      int32                      `json:"dayAge"`
-	Status      pasturePb.IsShow_Kind      `json:"status"`
-	ExecType    string                     `json:"execType"` // todo 执行类型定义成枚举???
+	Id              int64                      `json:"id"`
+	CowId           int64                      `json:"cowId"`
+	BreedStatus     pasturePb.BreedStatus_Kind `json:"breedStatus"`
+	BreedStatusName string                     `json:"breedStatusName"`
+	CowType         pasturePb.CowType_Kind     `json:"cowType"`
+	CowTypeName     string                     `json:"cowTypeName"`
+	PenId           int32                      `json:"penId"`
+	PenName         string                     `json:"penName"`
+	Lact            int32                      `json:"lact"`
+	CalvingAge      int32                      `json:"calvingAge"`
+	AbortionAge     int32                      `json:"abortionAge"`
+	DayAge          int32                      `json:"dayAge"`
+	Status          pasturePb.IsShow_Kind      `json:"status"`
 }
 
-type SameTimeCowDetailSlice []*SameTimeCowDetail
+type SameTimeBodySlice []*SameTimeBody
 
-// ToPB  todo SameTimeCowDetailSlice
-func (s SameTimeCowDetailSlice) ToPB() []*SameTimeBody {
+func (s SameTimeBodySlice) ToPB(
+	cowTypeMap map[pasturePb.CowType_Kind]string,
+	breedStatusMap map[pasturePb.BreedStatus_Kind]string,
+	penMap map[int32]*Pen,
+) []*SameTimeBody {
 	res := make([]*SameTimeBody, len(s))
 	for i, v := range s {
 		res[i] = &SameTimeBody{
-			Id:          v.Id,
-			CowId:       v.CowId,
-			BreedStatus: 0,
-			PenName:     "",
-			Lact:        v.Lact,
-			CalvingAge:  0,
-			AbortionAge: 0,
-			DayAge:      0,
-			Status:      v.Status,
-			ExecType:    "",
+			Id:              v.Id,
+			CowId:           v.CowId,
+			CowType:         v.CowType,
+			CowTypeName:     cowTypeMap[v.CowType],
+			BreedStatus:     v.BreedStatus,
+			BreedStatusName: breedStatusMap[v.BreedStatus],
+			PenName:         penMap[v.PenId].Name,
+			PenId:           v.PenId,
+			Lact:            v.Lact,
+			CalvingAge:      v.CalvingAge,
+			AbortionAge:     v.AbortionAge,
+			DayAge:          v.DayAge,
+			Status:          v.Status,
 		}
 	}
 	return res

+ 59 - 0
model/weaning_cow.go

@@ -0,0 +1,59 @@
+package model
+
+import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
+type WeaningCow struct {
+	Id            int64                 `json:"id"`
+	CowId         int64                 `json:"cowId"`
+	PlanDay       int64                 `json:"planDay"`
+	EndDay        int64                 `json:"endDay"`
+	RealityDay    int64                 `json:"realityDay"`
+	Status        pasturePb.IsShow_Kind `json:"status"`
+	BeforePenId   int32                 `json:"beforePenId"`
+	AfterPenId    int32                 `json:"afterPenId"`
+	Remarks       string                `json:"remarks"`
+	OperationId   int32                 `json:"operationId"`
+	OperationName string                `json:"operationName"`
+	CreatedAt     int64                 `json:"createdAt"`
+	UpdatedAt     int64                 `json:"updatedAt"`
+}
+
+func (w *WeaningCow) TableName() string {
+	return "weaning_cow"
+}
+
+func NewWeaningCow(cowId int64, planDay int64, penId int32) *WeaningCow {
+	return &WeaningCow{
+		CowId:       cowId,
+		PlanDay:     planDay,
+		EndDay:      planDay,
+		Status:      pasturePb.IsShow_No,
+		BeforePenId: penId,
+	}
+}
+
+func NewWeaningCowList(cowList []*Cow, planDay int64) []*WeaningCow {
+	var weaningList = make([]*WeaningCow, 0)
+	for _, cow := range cowList {
+		weaningList = append(weaningList, NewWeaningCow(cow.Id, planDay, cow.PenId))
+	}
+	return weaningList
+}
+
+type WeaningHeader struct {
+	Id      string `json:"id"`
+	CowId   string `json:"cowId"`
+	DayAge  string `json:"dayAge"`
+	PlanDay string `json:"planDay"`
+	Status  string `json:"status"`
+	PenName string `json:"penName"`
+}
+
+type WeaningBody struct {
+	Id      int64                 `json:"id"`
+	CowId   int64                 `json:"cowId"`
+	DayAge  int64                 `json:"dayAge"`
+	PlanDay string                `json:"planDay"`
+	Status  pasturePb.IsShow_Kind `json:"status"`
+	PenName string                `json:"penName"`
+}

+ 1 - 1
module/backend/analysis.go

@@ -46,7 +46,7 @@ func (s *StoreEntry) GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowt
 		currentWeight := float32(cow.CurrentWeight) / 100
 		penName := ""
 		for _, v := range penList {
-			if cow.PenId != v.Id {
+			if int64(cow.PenId) != v.Id {
 				continue
 			}
 			penName = v.Name

+ 81 - 34
module/backend/calendar.go

@@ -2,6 +2,7 @@ package backend
 
 import (
 	"context"
+	"fmt"
 	"kpt-pasture/model"
 	"net/http"
 	"time"
@@ -10,6 +11,8 @@ import (
 	"gitee.com/xuyiping_admin/pkg/xerr"
 )
 
+// CalendarList
+// /*func (s StoreEntry) CalendarToDoList(ctx context.Context) (*pasturePb.CalendarToDoListResponse, error) {
 func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.CalendarRequest) (*pasturePb.CalendarResponse, error) {
 	calendarList := make([]*model.Calendar, 0)
 	if err := s.DB.Model(&model.Calendar{}).
@@ -59,10 +62,8 @@ func (s *StoreEntry) getCalendarCowList(
 	switch calendarType {
 	case pasturePb.CalendarType_Immunisation: // 免疫
 		return s.ImmunisationCowList(ctx, startDate, pagination)
-	case pasturePb.CalendarType_PG: // 同期PG
-		return s.PGCowList(ctx, startDate, pagination)
-	case pasturePb.CalendarType_RnGH: // 同期RnGH
-		return s.RnGHCowList(ctx, startDate, pagination)
+	case pasturePb.CalendarType_PG, pasturePb.CalendarType_RnGH: // 同期
+		return s.SameTimeCowList(ctx, startDate, pagination, calendarType)
 	case pasturePb.CalendarType_Pregnancy_Check: // 孕检
 		return s.PregnancyCheckCowList(ctx, startDate, pagination)
 	case pasturePb.CalendarType_WorkOrder: // 工作单
@@ -97,12 +98,11 @@ func (s *StoreEntry) ImmunisationCowList(ctx context.Context, dateTime string, p
 			Page:     pagination.Page,
 			PageSize: pagination.PageSize,
 			Header: model.ImmunizationCalendarHeader{
-				Id:                   "id",
-				CowId:                "cowId",
-				PlanStartTime:        "planStartTime",
-				ImmunizationPlanId:   "immunizationPlanId",
-				ImmunizationPlanName: "immunizationPlanName",
-				Status:               "status",
+				Id:                   "编号",
+				CowId:                "牛号",
+				PlanStartTime:        "免疫开始时间",
+				ImmunizationPlanName: "免疫名称",
+				Status:               "状态",
 			},
 			List: model.ImmunizationPlanCowSlice(immunizationPlanCowList).ToPB(),
 		},
@@ -110,17 +110,33 @@ func (s *StoreEntry) ImmunisationCowList(ctx context.Context, dateTime string, p
 
 }
 
-func (s *StoreEntry) PGCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
-	sameTimeCowDetailList := make([]*model.SameTimeCowDetail, 0)
+func (s *StoreEntry) SameTimeCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel, calendarType pasturePb.CalendarType_Kind) (*model.CalendarResponse, error) {
+	sameTimeBodyList := make([]*model.SameTimeBody, 0)
 	count := int64(0)
-	if err := s.DB.Model(&model.SameTimeCowDetail{}).
-		Where("plan_start_time <= ?", dateTime).
-		Where("status = ?", pasturePb.IsShow_No).
-		Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
-		Find(&sameTimeCowDetailList).Error; err != nil {
-		return nil, xerr.WithStack(err)
+	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.SameTimeCowDetail).TableName())).
+		Select("a.id,a.cow_id,a.status,b.breed_status,b.cow_type,b.pen_id,b.day_age,b.calving_age,b.abortion_age").
+		Joins("left join cow as b on a.cow_id = b.id").
+		Where("b.is_remove = ?", pasturePb.IsShow_No).
+		Where("a.plan_day <= ?", dateTime).
+		Where("a.status = ?", pasturePb.IsShow_No)
+
+	if calendarType == pasturePb.CalendarType_PG {
+		pref.Where("a.same_time_type = ?", calendarType)
 	}
 
+	if calendarType == pasturePb.CalendarType_RnGH {
+		pref.Where("a.same_time_type = ?", calendarType)
+	}
+
+	if err := pref.Order("a.id desc").Count(&count).
+		Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&sameTimeBodyList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	cowTypeMap := s.CowTypeMap()
+	breedStatusMap := s.CowBreedStatusMap()
+	penMap := s.PenMap(ctx)
 	return &model.CalendarResponse{
 		Code:    http.StatusOK,
 		Message: "ok",
@@ -129,26 +145,22 @@ func (s *StoreEntry) PGCowList(ctx context.Context, dateTime string, pagination
 			Page:     pagination.Page,
 			PageSize: pagination.PageSize,
 			Header: model.SameTimeHeader{
-				Id:          "id",
-				CowId:       "cowId",
-				BreedStatus: "breedStatus",
-				PenName:     "penName",
-				Lact:        "lact",
-				CalvingAge:  "calvingAge",
-				AbortionAge: "abortionAge",
-				DayAge:      "dayAge",
-				Status:      "status",
-				ExecType:    "execType",
+				Id:              "编号",
+				CowId:           "牛号",
+				BreedStatusName: "繁殖状态",
+				CowTypeName:     "牛只类型",
+				PenName:         "栏舍",
+				Lact:            "胎次",
+				CalvingAge:      "产后天数",
+				AbortionAge:     "流产天数",
+				DayAge:          "日龄",
+				Status:          "状态",
 			},
-			List: model.SameTimeCowDetailSlice(sameTimeCowDetailList).ToPB(),
+			List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(cowTypeMap, breedStatusMap, penMap),
 		},
 	}, nil
 }
 
-func (s *StoreEntry) RnGHCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
-	return nil, nil
-}
-
 func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
 	return nil, nil
 }
@@ -158,7 +170,42 @@ func (s *StoreEntry) WorkOrderCowList(ctx context.Context, dateTime string, pagi
 }
 
 func (s *StoreEntry) WeaningCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
-	return nil, nil
+	weaningBodyList := make([]*model.WeaningBody, 0)
+	count := int64(0)
+	dateTime = dateTime + " 23:59:59" // todo 转换成时间戳
+
+	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.SameTimeCowDetail).TableName())).
+		Select("a.*,b.day_age").
+		Joins("left join cow as b on a.cow_id = b.id").
+		Where("b.is_remove = ?", pasturePb.IsShow_No).
+		Where("a.plan_day <= ?", dateTime).
+		Where("a.status = ?", pasturePb.IsShow_No)
+
+	if err := pref.Order("a.id desc").Count(&count).
+		Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&weaningBodyList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	return &model.CalendarResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &model.CalendarData{
+			Total:    int32(count),
+			Page:     pagination.Page,
+			PageSize: pagination.PageSize,
+			Header: model.WeaningHeader{
+				Id:      "编号",
+				CowId:   "牛号",
+				PenName: "栏舍",
+				PlanDay: "断奶日期",
+				DayAge:  "日龄",
+				Status:  "状态",
+			},
+			List: weaningBodyList,
+		},
+	}, nil
 }
 
 func (s *StoreEntry) TreatmentCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {

+ 1 - 1
module/backend/event_base.go

@@ -141,7 +141,7 @@ func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.Tra
 			return xerr.WithStack(err)
 		}
 		// 转去栏舍和当前栏舍相同,则不处理
-		if cow.PenId == int64(v.TransferInPenId) {
+		if cow.PenId == v.TransferInPenId {
 			continue
 		}
 		pen, err := s.GetPenById(ctx, int64(v.TransferInPenId))

+ 9 - 0
module/backend/sql.go

@@ -85,6 +85,15 @@ func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) {
 	return penList, nil
 }
 
+func (s *StoreEntry) PenMap(ctx context.Context) map[int32]*model.Pen {
+	penList, _ := s.GetPenList(ctx)
+	penMap := make(map[int32]*model.Pen)
+	for _, v := range penList {
+		penMap[int32(v.Id)] = v
+	}
+	return penMap
+}
+
 func (s *StoreEntry) GetCowList(ctx context.Context) ([]*model.Cow, error) {
 	cowList := make([]*model.Cow, 0)
 	if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {

+ 5 - 8
module/crontab/cow_cron.go

@@ -62,15 +62,12 @@ func (e *Entry) UpdateCowInfo() error {
 		e.CreateCrontabLog(UpdateCowInfo)
 	}()
 	for _, cow := range cowList {
-		dayAge := cow.GetDayAge()
-		calvingAge := cow.GetCalvingAge()
-		pregnancyAge := cow.GetDaysPregnant()
-		admissionAge := cow.GetAdmissionAge()
 		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,
-			"admission_age": admissionAge,
+			"day_age":       cow.GetDayAge(),
+			"calving_at":    cow.GetCalvingAge(),
+			"pregnancy_age": cow.GetDaysPregnant(),
+			"admission_age": cow.GetAdmissionAge(),
+			"abortion_age":  cow.GetAbortionAge(),
 		}).Error; err != nil {
 			zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
 		}