package model import ( "fmt" "math" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) type Cow struct { Id int64 `json:"id"` Sex pasturePb.Genders_Kind `json:"sex"` NeckRingNumber string `json:"neck_ring_number"` EarNumber string `json:"ear_number"` EarOldNumber string `json:"ear_old_number"` PenId int64 `json:"pen_id"` Lact int32 `json:"lact"` DayAge int32 `json:"day_age"` CalvingAge int64 `json:"calving_age"` PregnancyAge int64 `json:"pregnancy_age"` // 怀孕天数 孕检结果有阳性更新,产犊后至0 AdmissionAge int64 `json:"admission_age"` CowType pasturePb.CowType_Kind `json:"cow_type"` BreedStatus pasturePb.BreedStatus_Kind `json:"breed_status"` CowKind pasturePb.CowKind_Kind `json:"cow_kind"` BirthWeight int64 `json:"birth_weight"` CurrentWeight int64 `json:"current_weight"` SourceId pasturePb.CowSource_Kind `json:"source_id"` FatherId int64 `json:"father_id"` MotherId int64 `json:"mother_id"` IsRemove pasturePb.IsShow_Kind `json:"is_remove"` IsPregnant pasturePb.IsShow_Kind `json:"is_pregnant"` WeaningAt int64 `json:"weaning_at"` CalvingAt int64 `json:"calving_at"` BirthAt int64 `json:"birth_at"` AdmissionAt int64 `json:"admission_at"` FirstMatingAt int64 `json:"first_mating_at"` LastEstrusAt int64 `json:"last_estrus_at"` LastCalvingAt int64 `json:"last_calving_at"` LastMatingAt int64 `json:"last_mating_at"` LastBullId int64 `json:"last_bull_id"` LastPregnantCheckAt int64 `json:"last_pregnant_check_at"` LastDryMilkAt int64 `json:"last_dry_milk_at"` LastSecondWeight int64 `json:"last_second_weight"` LastSecondWeightAt int64 `json:"last_second_weight_at"` LastWeightAt int64 `json:"last_weight_at"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (c *Cow) TableName() string { return "cow" } type CowSlice []*Cow func (c CowSlice) ToPB( penList []*Pen, cowTypeMap map[pasturePb.CowType_Kind]string, breedStatusMap map[pasturePb.BreedStatus_Kind]string, cowKindMap map[pasturePb.CowKind_Kind]string, ) []*pasturePb.SearchCowList { res := make([]*pasturePb.SearchCowList, len(c)) for i, v := range c { penName := "" for _, pen := range penList { if v.PenId != int64(pen.Id) { continue } penName = pen.Name } res[i] = &pasturePb.SearchCowList{ CowId: int32(v.Id), Sex: v.Sex, NeckRingNumber: v.NeckRingNumber, EarNumber: v.EarNumber, PenId: int32(v.PenId), PenName: penName, CowType: int32(v.CowType), Lact: v.Lact, CowTypeName: cowTypeMap[v.CowType], BreedStatus: v.BreedStatus, BreedStatusName: breedStatusMap[v.BreedStatus], CowKind: v.CowKind, CowKindName: cowKindMap[v.CowKind], BirthAt: int32(v.BirthAt), BirthWeight: int32(v.BirthWeight) / 1000, CurrentWeight: int32(v.CurrentWeight) / 1000, LastWeightAt: int32(v.LastWeightAt), } } return res } func NewCow(req *pasturePb.EventEnterData) *Cow { var isPregnant = pasturePb.IsShow_No if req.BreedStatusId == pasturePb.BreedStatus_Pregnant { isPregnant = pasturePb.IsShow_Ok } return &Cow{ Sex: req.Sex, EarNumber: req.EarNumber, PenId: int64(req.PenId), Lact: req.Lact, CowType: req.CowTypeId, BreedStatus: req.BreedStatusId, CowKind: req.CowKindId, SourceId: req.CowSourceId, FatherId: int64(req.FatherId), MotherId: int64(req.MotherId), IsRemove: pasturePb.IsShow_Ok, IsPregnant: isPregnant, WeaningAt: int64(req.WeaningAt), BirthAt: int64(req.BirthAt), FirstMatingAt: int64(req.MatingAt), LastMatingAt: int64(req.MatingAt), LastPregnantCheckAt: int64(req.PregnancyCheckAt), } } func NewCalfCow(motherId, fatherId int64, calf *CalvingCalf) *Cow { return &Cow{ Sex: calf.Sex, EarNumber: calf.EarNumber, PenId: int64(calf.PenId), CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛 BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配 CowKind: calf.CowKind, // 牛只品种 BirthWeight: calf.BrithWeight, SourceId: pasturePb.CowSource_Calving, // 产犊方式 FatherId: fatherId, MotherId: motherId, IsRemove: pasturePb.IsShow_Ok, IsPregnant: pasturePb.IsShow_No, } } type BarCowStruct struct { Number int32 `json:"number"` TypeId pasturePb.CowType_Kind `json:"type_id"` } type BarCowStructSlice []*BarCowStruct func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct { var pb []*pasturePb.BarCowStruct for _, v := range b { name := fmt.Sprintf("%s", cowTypeMap[v.TypeId]) pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number}) } return pb } // GetDayAge 日龄 func (c *Cow) GetDayAge() int32 { if c.BirthAt <= 0 { return 0 } return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400)) } // GetCalvingAge 产后天数 func (c *Cow) GetCalvingAge() int64 { if c.CalvingAt <= 0 { return 0 } return int64(math.Floor(float64(time.Now().Unix()-c.CalvingAt) / 86400)) } // GetDaysPregnant 怀孕天数 func (c *Cow) GetDaysPregnant() int32 { if c.BreedStatus == pasturePb.BreedStatus_Pregnant && c.IsRemove == pasturePb.IsShow_No && c.IsPregnant == pasturePb.IsShow_Ok { return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400)) } return 0 } // GetLactationDays 泌乳天数 func (c *Cow) GetLactationDays() int32 { if c.BreedStatus == pasturePb.BreedStatus_Calving && c.IsRemove == pasturePb.IsShow_Ok { return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400)) } return 0 } // GetAdmissionAge 入场天数 func (c *Cow) GetAdmissionAge() int32 { if c.AdmissionAt > 0 && c.IsRemove == pasturePb.IsShow_Ok { return int32(math.Floor(float64(time.Now().Unix()-c.AdmissionAt) / 86400)) } return 0 } // GetDayWeight 日增重 func (c *Cow) GetDayWeight() float64 { if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt { days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400)) if days <= 0 { return 0 } return math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days)) } return 0 } // GetAverageDailyWeight 平均日增重 func (c *Cow) GetAverageDailyWeight() float64 { if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt { days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400)) if days <= 0 { return 0 } dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days)) return dailyWeight } return 0 }