package crontab import ( "kpt-pasture/model" "go.uber.org/zap" "gitee.com/xuyiping_admin/pkg/logger/zaplog" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" ) func (e *Entry) FindPastureList() []*model.AppPastureList { res := make([]*model.AppPastureList, 0) if err := e.DB.Model(new(model.AppPastureList)). Where("is_show = ?", pasturePb.IsShow_Ok). Find(&res).Error; err != nil { zaplog.Error("FindPastureList error", zap.Any("err", err)) return res } return res } func (e *Entry) GetCowById(cowId int64) (*model.Cow, error) { cowInfo := &model.Cow{} if err := e.DB.Model(new(model.Cow)). Where("id = ?", cowId). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). First(cowInfo).Error; err != nil { return nil, err } return cowInfo, nil } func (e *Entry) GetPenMapList() (map[int32]*model.Pen, error) { penList := make([]*model.Pen, 0) if err := e.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil { return nil, xerr.WithStack(err) } penMap := make(map[int32]*model.Pen) for _, v := range penList { penMap[v.Id] = v } return penMap, nil } func (e *Entry) GetSystemConfigure(pastureId int64, name string) (*model.SystemConfigure, error) { res := &model.SystemConfigure{} if err := e.DB.Model(new(model.SystemConfigure)). Where("name = ?", name). Where("pasture_id = ?", pastureId). Where("is_show = ?", pasturePb.IsShow_Ok). First(res).Error; err != nil { return nil, xerr.WithStack(err) } return res, nil } // GetBeforeThreeDaysCowEstrus 获取值得时间之前三天内最大发情记录 func (e *Entry) GetBeforeThreeDaysCowEstrus(cowId int64, activeTime string) *model.EventEstrus { eventEstrus := &model.EventEstrus{} if err := e.DB.Model(new(model.EventEstrus)). Select("MAX(filter_high) as filter_high,cow_id,MAX(per_twenty_four_high) as per_twenty_four_high"). Select("MAX(IF(e16.result=1,3,e16.result)) AS result"). Where("cow_id = ?", cowId). Where("active_date >= ?", activeTime). First(eventEstrus).Error; err != nil { return eventEstrus } return eventEstrus } // GetTwoEstrus 判断最近50天内是否存在发情记录(发情等级>=2),如果18~25天@xadjust21,如果36~50天@xadjust42 func (e *Entry) GetTwoEstrus(cowId int64, startActiveTime, endActiveTime string) *CowEstrus { newCowEstrus := &CowEstrus{} if err := e.DB.Model(new(model.EventEstrus)). Select("cow_id,MAX(active_date) as active_date"). Where("cow_id = ?", cowId). Where("active_date BETWEEN ? AND ?", startActiveTime, endActiveTime). Where("level >= ?", pasturePb.EstrusLevel_Middle). First(newCowEstrus).Error; err != nil { return newCowEstrus } return newCowEstrus } func (e *Entry) FindCowInfoByNeckRingNumber(neckRingNumber string) *model.Cow { res := &model.Cow{} if err := e.DB.Model(new(model.Cow)). Where("neck_ring_number = ?", neckRingNumber).First(res).Error; err != nil { return nil } return res }