package mqtt import ( "errors" "kpt-pasture/model" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) // NeckRingIsBind 脖环是否绑定 func (e *Entry) NeckRingIsBind(number string) bool { var count int64 = 0 if err := e.DB.Model(new(model.NeckRing)).Where("number = ?", number). Where("status != ?", pasturePb.NeckRingStatus_Unbind). Count(&count).Error; err != nil { return false } if count > 0 { return true } return false } 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) 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) IsExistNeckActiveHabit(neckRingNumber, heatDate string, frameId int32) (*model.NeckActiveHabit, int64) { count := int64(0) neckRingProcess := &model.NeckRingProcess{} if err := e.DB.Model(new(model.NeckRingProcess)). Where("neck_ring_number = ?", neckRingNumber). Where("active_date = ?", heatDate). Where("frameid = ?", frameId). Count(&count).Error; err != nil { return nil, 0 } res := &model.NeckActiveHabit{} if neckRingProcess != nil && neckRingProcess.HabitId > 0 { if err := e.DB.Model(new(model.NeckActiveHabit)). Where("id = ?", neckRingProcess.HabitId). First(res).Error; err != nil { return nil, 0 } } return res, count } // GetMinIdByHeatDate 获取最小的id 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 } 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) GetSystemConfigure2(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 }