123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "kpt-pasture/util"
- "net/http"
- "strings"
- "time"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "go.uber.org/zap"
- "gitee.com/xuyiping_admin/pkg/xerr"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- )
- func (s *StoreEntry) List(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) {
- currentUser, err := s.GetCurrentSystemUser(ctx)
- if err != nil {
- return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
- }
- cowList := make([]*model.Cow, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.Cow)).
- Where("pasture_id = ?", currentUser.PastureId)
- if len(req.CowId) > 0 {
- cowIds := strings.Split(req.CowId, ",")
- pref.Where("id IN ?", cowIds)
- }
- if req.Id > 0 {
- pref.Where("id = ?", req.Id)
- }
- if req.PenId > 0 {
- pref.Where("pen_id = ?", req.PenId)
- }
- if req.CowType > 0 {
- pref.Where("cow_type = ?", req.CowType)
- }
- if req.BreedStatus > 0 {
- pref.Where("breed_status = ?", req.BreedStatus)
- }
- if req.CowKind > 0 {
- pref.Where("cow_kind = ?", req.CowKind)
- }
- if req.Sex > 0 {
- pref.Where("sex = ?", req.Sex)
- }
- if req.Lact > 0 {
- pref.Where("lact = ?", req.Lact)
- }
- if req.CowSource > 0 {
- pref.Where("source_id = ?", req.CowSource)
- }
- if req.EarNumber != "" {
- pref.Where("ear_number = ?", req.EarNumber)
- }
- if err = pref.Order("id desc").
- Count(&count).
- Limit(int(pagination.PageSize)).
- Offset(int(pagination.PageOffset)).
- Find(&cowList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- penMap := s.PenMap(ctx, currentUser.PastureId)
- cowTypeMap := s.CowTypeMap()
- breedStatusMap := s.CowBreedStatusMap()
- cowKindMap := s.CowKindMap()
- cowSourceMap := s.CowSourceMap()
- admissionStatusMap := s.AdmissionStatusMap()
- healthStatusMap := s.HealthStatusMap()
- return &pasturePb.SearchCowListResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.SearchCowData{
- List: model.CowSlice(cowList).ToPB(penMap, cowTypeMap, breedStatusMap,
- cowKindMap, cowSourceMap, admissionStatusMap, healthStatusMap),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) EventList(ctx context.Context, req *pasturePb.SearchCowEventListRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CowEventListResponse, error) {
- currentUser, err := s.GetCurrentSystemUser(ctx)
- if err != nil {
- return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
- }
- cow := &model.Cow{}
- if err = s.DB.Model(cow).
- Where("pasture_id = ?", currentUser.PastureId).
- Where("id = ?", req.CowId).First(cow).Error; err != nil {
- zaplog.Error("EventList", zap.Any("req", req), zap.Any("err", err), zap.Any("currentUser", currentUser))
- return nil, xerr.Customf("该牛不存在: %d", req.CowId)
- }
- eventCowLogList := make([]*model.EventCowLog, 0)
- eventCowLog := &model.EventCowLog{CowId: cow.Id}
- pref := s.DB.Table(eventCowLog.TableName()).
- Where("cow_id = ?", req.CowId)
- if req.Lact >= 0 {
- pref.Where("lact = ?", req.Lact)
- }
- if req.EventCategoryKind > 0 {
- pref.Where("event_category_id = ?", req.EventCategoryKind)
- }
- if err = pref.Order("id desc").
- Limit(int(pagination.PageSize)).
- Offset(int(pagination.PageOffset)).
- Find(&eventCowLogList).Error; err != nil {
- zaplog.Error("EventList", zap.Any("req", req), zap.Any("err", err), zap.Any("currentUser", currentUser))
- return nil, xerr.WithStack(err)
- }
- eventCategoryMap := s.EventCategoryMap()
- return &pasturePb.CowEventListResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.CowEventData{
- List: model.EventCowLogSlice(eventCowLogList).ToPB(eventCategoryMap),
- Total: int32(len(eventCowLogList)),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) BehaviorCurve(ctx context.Context, req *pasturePb.CowBehaviorCurveRequest) (*pasturePb.CowBehaviorCurveResponse, error) {
- currentUser, err := s.GetCurrentSystemUser(ctx)
- if err != nil {
- return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
- }
- cowInfo, err := s.GetCowInfoByCowId(ctx, currentUser.PastureId, int64(req.CowId))
- if err != nil {
- return nil, xerr.Customf("错误的牛只信息: %d", req.CowId)
- }
- nowTime := time.Now()
- nowDayZero := util.TimeParseLocalUnix(nowTime.Format(model.LayoutDate2))
- endDataTime := nowTime.Format(model.LayoutDate2)
- startDataTime := nowTime.AddDate(0, 0, -30).Format(model.LayoutDate2)
- // 行为曲线数据
- neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
- if err = s.DB.Table(new(model.NeckActiveHabit).TableName()).
- Where("neck_ring_number = ?", cowInfo.NeckRingNumber).
- Where("pasture_id = ?", currentUser.PastureId).
- Where("heat_date >= ?", startDataTime).
- Where("heat_date <= ?", endDataTime).
- Order("heat_date, frameid").
- Find(&neckActiveHabitList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- // 牛只事件列表
- eventLogList := make([]*model.EventCowLog, 0)
- eventLog := &model.EventCowLog{CowId: cowInfo.Id}
- if err = s.DB.Table(eventLog.TableName()).
- Where("cow_id = ?", cowInfo.Id).
- Where("pasture_id = ?", currentUser.PastureId).
- Where("event_at >= ?", nowDayZero-(30*86400)).
- Where("event_at <= ?", nowDayZero+86400).
- Order("event_at").
- Find(&eventLogList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- data := model.NeckActiveHabitSlice(neckActiveHabitList).ToPB(req.CurveName)
- for _, v := range eventLogList {
- eventAt := time.Unix(v.EventAt, 0)
- data.EventList[eventAt.Format(model.LayoutDate2)] = v.EventTypeName
- }
- // 发情数据
- estrusList := make([]*model.EventEstrus, 0)
- if err = s.DB.Table(new(model.EventEstrus).TableName()).
- Where("cow_id = ?", cowInfo.Id).
- Where("pasture_id = ?", currentUser.PastureId).
- Where("estrus_start_date >= ?", nowTime.Format(model.LayoutTime)).
- Where("estrus_start_date <= ?", nowTime.AddDate(0, 0, -30).Format(model.LayoutTime)).
- Order("estrus_start_date").
- Find(&estrusList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- for _, v := range estrusList {
- data.EstrusList = append(data.EstrusList, v.EstrusStartDate)
- }
- return &pasturePb.CowBehaviorCurveResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: data,
- }, nil
- }
|