123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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 {
- 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
- }
- return res
- }
- // GetBeforeThreeDaysCowEstrus 获取值得时间之前三天内最大发情记录
- func (e *Entry) GetBeforeThreeDaysCowEstrus(cowId int64, activeTime string) *model.EventEstrus {
- eventEstrus := &model.EventEstrus{}
- if err := e.DB.Model(new(model.EventEstrus)).
- Select("MAX(max_high) as max_high,cow_id,MAX(day_high) as day_high").
- Select("MAX(IF(result=1,3,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(pastureId, 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("pasture_id = ?", pastureId).
- 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).
- Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
- First(res).Error; err != nil {
- return nil
- }
- return res
- }
- func (e *Entry) IsExistEventEstrus(pastureId, cowId int64) *model.EventEstrus {
- res := &model.EventEstrus{}
- if err := e.DB.Model(new(model.EventEstrus)).
- Where("cow_id = ?", cowId).
- Where("pasture_id = ?", pastureId).
- Where("expose_estrus_type = ?", pasturePb.ExposeEstrusType_Neck_Ring).
- Where("is_show = ?", pasturePb.IsShow_Ok).
- Order("id DESC").
- First(res).Error; err != nil {
- return nil
- }
- return res
- }
|