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/logger/zaplog" "go.uber.org/zap" ) // FindPastureAllCow 查询所有牧场牛只总数 func (e *Entry) FindPastureAllCow() map[int64]int32 { pastureList := e.FindPastureList() res := make(map[int64]int32) for _, pasture := range pastureList { var count int64 if err := e.DB.Model(&model.Cow{}). Where("pasture_id = ?", pasture.Id). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). Count(&count).Error; err != nil { zaplog.Error("FindAllCow", zap.Any("pasture_id", pasture.Id), zap.Any("err", err)) } res[pasture.Id] = int32(count) } return res } func (e *Entry) UpdatePastureIndicators(pastureId int64, indicatorsDetails *model.IndicatorsDetails, date, value string) { where := &model.IndicatorsData{ PastureId: pastureId, Date: date, Kind: indicatorsDetails.Kind, } data := &model.IndicatorsData{ PastureId: pastureId, CategoryType: indicatorsDetails.CategoryType, CategoryName: indicatorsDetails.CategoryName, Date: date, Kind: indicatorsDetails.Kind, Value: value, } var existData model.IndicatorsData if err := e.DB.Model(new(model.IndicatorsData)). Where(where).First(&existData).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { if err = e.DB.Model(new(model.IndicatorsData)).Create(data).Error; err != nil { zaplog.Error("UpdatePastureIndicators", zap.Any("Create", err)) } } else { zaplog.Error("UpdatePastureIndicators", zap.Any("Find", err)) return } } if err := e.DB.Model(new(model.IndicatorsData)). Where("id = ?", existData.Id). Update("value", value).Error; err != nil { zaplog.Error("UpdatePastureIndicators", zap.Any("Update", err)) } }