123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package crontab
- import (
- "errors"
- "kpt-pasture/model"
- "gorm.io/gorm"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- "gitee.com/xuyiping_admin/pkg/xerr"
- )
- 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) GetCowInfoByImei(imei string) *model.Cow {
- res := &model.Cow{}
- if err := e.DB.Model(new(model.Cow)).Where("neck_ring_number = ?", imei).First(res).Error; err != nil {
- return nil
- }
- return res
- }
- func (e *Entry) IsExistNeckActiveHabit(neckRingNumber string, frameId int32) int64 {
- count := int64(0)
- if err := e.DB.Model(new(model.NeckActiveHabit)).
- Where("neck_ring_number = ? and frameid = ?", neckRingNumber, frameId).Count(&count).Error; err != nil {
- return 0
- }
- return count
- }
- func (e *Entry) GetNeckActiveHabit(neckRingNumber string, frameId int32) *model.NeckActiveHabit {
- res := &model.NeckActiveHabit{}
- if err := e.DB.Model(new(model.NeckActiveHabit)).
- Where("neck_ring_number = ? and frameid = ?", neckRingNumber, frameId).
- First(res).Error; err != nil {
- return nil
- }
- return res
- }
- func (e *Entry) GetSystemConfigure(name string) *model.SystemConfigure {
- res := &model.SystemConfigure{}
- if err := e.DB.Model(new(model.SystemConfigure)).
- Where("name = ?", name).
- Where("is_show = ?", pasturePb.IsShow_Ok).
- First(res).Error; err != nil {
- return nil
- }
- return res
- }
- func (e *Entry) GetMinIdByHeatDate(heatDate string, defaultId int64) (int64, error) {
- xMinId := struct {
- Id int64
- }{}
- if err := e.DB.Model(new(model.NeckActiveHabit)).
- Select("MIN(id) as id").
- //Where("heat_date = ?", minHeatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)).
- Where("heat_date >= ?", heatDate).
- First(&xMinId).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- xMinId.Id = defaultId
- } else {
- return 0, xerr.WithStack(err)
- }
- }
- return xMinId.Id, 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
- }
|