123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package model
- import (
- "fmt"
- "kpt-pasture/util"
- "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:"neckRingNumber"`
- EarNumber string `json:"earNumber"`
- EarOldNumber string `json:"earOldNumber"`
- PenId int32 `json:"penId"`
- Lact int32 `json:"lact"`
- DayAge int32 `json:"dayAge"`
- CalvingAge int64 `json:"calvingAge"`
- PregnancyAge int32 `json:"pregnancyAge"`
- AdmissionAge int32 `json:"admissionAge"`
- AbortionAge int64 `json:"abortionAge"`
- CowType pasturePb.CowType_Kind `json:"cowType"`
- BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
- CowKind pasturePb.CowKind_Kind `json:"cowKind"`
- BirthWeight int64 `json:"birthWeight"`
- CurrentWeight int64 `json:"currentWeight"`
- AdmissionWeight int64 `json:"admissionWeight"`
- SourceId pasturePb.CowSource_Kind `json:"sourceId"`
- FatherNumber string `json:"fatherNumber"`
- MotherNumber string `json:"motherNumber"`
- AdmissionStatus pasturePb.AdmissionStatus_Kind `json:"admissionStatus"`
- IsPregnant pasturePb.IsShow_Kind `json:"isPregnant"`
- HealthStatus pasturePb.HealthStatus_Kind `json:"healthStatus"`
- WeaningAt int64 `json:"weaningAt"`
- CalvingAt int64 `json:"calvingAt"`
- BirthAt int64 `json:"birthAti"`
- AdmissionAt int64 `json:"admissionAt"`
- FirstMatingAt int64 `json:"firstMatingAt"`
- LastEstrusAt int64 `json:"lastEstrusAt"`
- LastCalvingAt int64 `json:"lastCalvingAt"`
- LastMatingAt int64 `json:"lastMatingAt"`
- LastBullNumber string `json:"lastBullNumber"`
- LastPregnantCheckAt int64 `json:"lastPregnantCheckAt"`
- LastDryMilkAt int64 `json:"lastDryMilkAt"`
- LastSecondWeight int64 `json:"lastSecondWeight"`
- LastSecondWeightAt int64 `json:"lastSecondWeightAt"`
- LastAbortionAt int64 `json:"lastAbortionAt"`
- LastWeightAt int64 `json:"lastWeightAt"`
- CreatedAt int64 `json:"createdAt"`
- UpdatedAt int64 `json:"updatedAt"`
- }
- func (c *Cow) TableName() string {
- return "cow"
- }
- type CowSlice []*Cow
- func (c CowSlice) ToPB(
- penMap map[int32]*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 := ""
- if pen, ok := penMap[v.PenId]; ok {
- penName = pen.Name
- }
- res[i] = &pasturePb.SearchCowList{
- CowId: int32(v.Id),
- Sex: v.Sex,
- NeckRingNumber: v.NeckRingNumber,
- EarNumber: v.EarNumber,
- PenId: 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 (c CowSlice) ToPB2(penMap map[int32]*Pen, penWeightSlice PenWeightSlice) []*pasturePb.CowList {
- res := make([]*pasturePb.CowList, len(c))
- for i, v := range c {
- penName := ""
- if pen, ok := penMap[v.PenId]; ok {
- penName = pen.Name
- }
- penWeight := penWeightSlice.GetPenWeight(v.PenId)
- lastWeightDay := util.Ceil(float64(v.LastWeightAt-v.LastSecondWeightAt) / 86400)
- penAvgWeight := float32(0)
- previousStageDailyWeight := float32(0)
- cowPenAvgWeightDiffValue := float32(0)
- if penWeight != nil {
- penAvgWeight = float32(penWeight.AvgWeight) / 1000
- cowPenAvgWeightDiffValue = float32(v.CurrentWeight-int64(penWeight.AvgWeight)) / 1000
- if lastWeightDay > 0 {
- previousStageDailyWeight = float32(v.CurrentWeight-v.LastSecondWeight) / 1000 / float32(lastWeightDay)
- }
- }
- res[i] = &pasturePb.CowList{
- CowId: int32(v.Id),
- DayAge: v.DayAge,
- DailyWeightGain: float32(v.GetDayWeight()),
- AverageDailyWeightGain: float32(v.GetAverageDailyWeight()),
- EarNumber: v.EarNumber,
- PenName: penName,
- BirthAt: int32(v.BirthAt),
- BirthWeight: float32(v.BirthWeight) / 1000,
- CurrentWeight: float32(v.CurrentWeight) / 1000,
- LastWeightAt: int32(v.LastWeightAt),
- AdmissionAge: int32(v.AdmissionAge),
- AdmissionWeight: float32(v.AbortionAge) / 1000,
- PreviousStageDailyWeight: previousStageDailyWeight,
- PenAvgWeight: penAvgWeight,
- CowPenAvgWeightDiffValue: cowPenAvgWeightDiffValue,
- }
- }
- return res
- }
- func NewCow(req *pasturePb.EventEnterRequest) *Cow {
- var isPregnant = pasturePb.IsShow_No
- if req.BreedStatus == pasturePb.BreedStatus_Pregnant {
- isPregnant = pasturePb.IsShow_Ok
- }
- return &Cow{
- Sex: req.Sex,
- EarNumber: req.EarNumber,
- PenId: req.PenId,
- Lact: req.Lact,
- CowType: req.CowType,
- BreedStatus: req.BreedStatus,
- CowKind: req.CowKind,
- SourceId: req.CowSource,
- FatherNumber: req.FatherNumber,
- MotherNumber: req.MotherNumber,
- AdmissionStatus: pasturePb.AdmissionStatus_Admission,
- HealthStatus: pasturePb.HealthStatus_Health,
- IsPregnant: isPregnant,
- WeaningAt: int64(req.WeaningAt),
- BirthAt: int64(req.BirthAt),
- AdmissionWeight: int64(req.Weight * 1000),
- FirstMatingAt: int64(req.MatingAt),
- LastMatingAt: int64(req.MatingAt),
- LastPregnantCheckAt: int64(req.PregnancyCheckAt),
- }
- }
- func NewCalfCow(motherId int64, fatherNumber string, calf *CalvingCalf) *Cow {
- return &Cow{
- Sex: calf.Sex,
- EarNumber: calf.EarNumber,
- PenId: calf.PenId,
- CowType: pasturePb.CowType_Lactating_Calf,
- BreedStatus: pasturePb.BreedStatus_UnBreed,
- CowKind: calf.CowKind,
- BirthWeight: calf.BirthWeight,
- BirthAt: calf.BirthAt,
- SourceId: pasturePb.CowSource_Calving,
- FatherNumber: fatherNumber,
- MotherNumber: fmt.Sprintf("%d", motherId),
- AdmissionStatus: pasturePb.AdmissionStatus_Admission,
- 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
- }
- func (c *Cow) GetDayAge() int32 {
- if c.BirthAt <= 0 {
- return 0
- }
- return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
- }
- func (c *Cow) GetCalvingAge() int64 {
- if c.CalvingAt <= 0 {
- return 0
- }
- return int64(math.Floor(float64(time.Now().Unix()-c.CalvingAt) / 86400))
- }
- func (c *Cow) GetDaysPregnant() int32 {
- if c.BreedStatus == pasturePb.BreedStatus_Pregnant &&
- c.AdmissionStatus == pasturePb.AdmissionStatus_Admission &&
- c.IsPregnant == pasturePb.IsShow_Ok {
- return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
- }
- return 0
- }
- func (c *Cow) GetLactationDays() int32 {
- if c.BreedStatus == pasturePb.BreedStatus_Calving && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
- return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
- }
- return 0
- }
- func (c *Cow) GetAdmissionAge() int32 {
- if c.AdmissionAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
- return int32(math.Floor(float64(time.Now().Unix()-c.AdmissionAt) / 86400))
- }
- return 0
- }
- 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
- }
- dayWeight := math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
- return dayWeight / 1000
- }
- return 0
- }
- 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 / 1000
- }
- return 0
- }
- func (c *Cow) GetAbortionAge() int32 {
- if c.LastAbortionAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
- return int32(math.Floor(float64(time.Now().Unix()-c.LastAbortionAt) / 86400))
- }
- return 0
- }
- type CowWeightRange struct {
- WeightRange string `json:"weight_range"`
- Count int32 `json:"count"`
- }
- func (c CowSlice) WeightRangeToPB(penMap map[int32]*Pen) []*pasturePb.CowList {
- res := make([]*pasturePb.CowList, len(c))
- for i, v := range c {
- penName := ""
- if pen, ok := penMap[v.PenId]; ok {
- penName = pen.Name
- }
- res[i] = &pasturePb.CowList{
- CowId: int32(v.Id),
- DayAge: v.DayAge,
- DailyWeightGain: float32(v.GetDayWeight()),
- AverageDailyWeightGain: float32(v.GetAverageDailyWeight()),
- EarNumber: v.EarNumber,
- PenName: penName,
- BirthAt: int32(v.BirthAt),
- BirthWeight: float32(v.BirthWeight) / 1000,
- CurrentWeight: float32(v.CurrentWeight) / 1000,
- LastWeightAt: int32(v.LastWeightAt),
- }
- }
- return res
- }
|