cow.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "kpt-pasture/util"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. "go.uber.org/zap"
  11. "gitee.com/xuyiping_admin/pkg/xerr"
  12. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  13. )
  14. func (s *StoreEntry) List(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) {
  15. currentUser, err := s.GetCurrentSystemUser(ctx)
  16. if err != nil {
  17. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  18. }
  19. cowList := make([]*model.Cow, 0)
  20. var count int64 = 0
  21. pref := s.DB.Model(new(model.Cow)).
  22. Where("pasture_id = ?", currentUser.PastureId)
  23. if len(req.CowId) > 0 {
  24. cowIds := strings.Split(req.CowId, ",")
  25. pref.Where("id IN ?", cowIds)
  26. }
  27. if req.Id > 0 {
  28. pref.Where("id = ?", req.Id)
  29. }
  30. if req.PenId > 0 {
  31. pref.Where("pen_id = ?", req.PenId)
  32. }
  33. if req.CowType > 0 {
  34. pref.Where("cow_type = ?", req.CowType)
  35. }
  36. if req.BreedStatus > 0 {
  37. pref.Where("breed_status = ?", req.BreedStatus)
  38. }
  39. if req.CowKind > 0 {
  40. pref.Where("cow_kind = ?", req.CowKind)
  41. }
  42. if req.Sex > 0 {
  43. pref.Where("sex = ?", req.Sex)
  44. }
  45. if req.Lact > 0 {
  46. pref.Where("lact = ?", req.Lact)
  47. }
  48. if req.CowSource > 0 {
  49. pref.Where("source_id = ?", req.CowSource)
  50. }
  51. if req.EarNumber != "" {
  52. pref.Where("ear_number = ?", req.EarNumber)
  53. }
  54. if err = pref.Order("id desc").
  55. Count(&count).
  56. Limit(int(pagination.PageSize)).
  57. Offset(int(pagination.PageOffset)).
  58. Find(&cowList).Error; err != nil {
  59. return nil, xerr.WithStack(err)
  60. }
  61. penMap := s.PenMap(ctx, currentUser.PastureId)
  62. cowTypeMap := s.CowTypeMap()
  63. breedStatusMap := s.CowBreedStatusMap()
  64. cowKindMap := s.CowKindMap()
  65. cowSourceMap := s.CowSourceMap()
  66. admissionStatusMap := s.AdmissionStatusMap()
  67. healthStatusMap := s.HealthStatusMap()
  68. return &pasturePb.SearchCowListResponse{
  69. Code: http.StatusOK,
  70. Message: "ok",
  71. Data: &pasturePb.SearchCowData{
  72. List: model.CowSlice(cowList).ToPB(penMap, cowTypeMap, breedStatusMap,
  73. cowKindMap, cowSourceMap, admissionStatusMap, healthStatusMap),
  74. Total: int32(count),
  75. PageSize: pagination.PageSize,
  76. Page: pagination.Page,
  77. },
  78. }, nil
  79. }
  80. func (s *StoreEntry) EventList(ctx context.Context, req *pasturePb.SearchCowEventListRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CowEventListResponse, error) {
  81. currentUser, err := s.GetCurrentSystemUser(ctx)
  82. if err != nil {
  83. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  84. }
  85. cow := &model.Cow{}
  86. if err = s.DB.Model(cow).
  87. Where("pasture_id = ?", currentUser.PastureId).
  88. Where("id = ?", req.CowId).First(cow).Error; err != nil {
  89. zaplog.Error("EventList", zap.Any("req", req), zap.Any("err", err), zap.Any("currentUser", currentUser))
  90. return nil, xerr.Customf("该牛不存在: %d", req.CowId)
  91. }
  92. eventCowLogList := make([]*model.EventCowLog, 0)
  93. eventCowLog := &model.EventCowLog{CowId: cow.Id}
  94. pref := s.DB.Table(eventCowLog.TableName()).
  95. Where("cow_id = ?", req.CowId)
  96. if req.Lact >= 0 {
  97. pref.Where("lact = ?", req.Lact)
  98. }
  99. if req.EventCategoryKind > 0 {
  100. pref.Where("event_category_id = ?", req.EventCategoryKind)
  101. }
  102. if err = pref.Order("id desc").
  103. Limit(int(pagination.PageSize)).
  104. Offset(int(pagination.PageOffset)).
  105. Find(&eventCowLogList).Error; err != nil {
  106. zaplog.Error("EventList", zap.Any("req", req), zap.Any("err", err), zap.Any("currentUser", currentUser))
  107. return nil, xerr.WithStack(err)
  108. }
  109. eventCategoryMap := s.EventCategoryMap()
  110. return &pasturePb.CowEventListResponse{
  111. Code: http.StatusOK,
  112. Message: "ok",
  113. Data: &pasturePb.CowEventData{
  114. List: model.EventCowLogSlice(eventCowLogList).ToPB(eventCategoryMap),
  115. Total: int32(len(eventCowLogList)),
  116. PageSize: pagination.PageSize,
  117. Page: pagination.Page,
  118. },
  119. }, nil
  120. }
  121. func (s *StoreEntry) BehaviorCurve(ctx context.Context, req *pasturePb.CowBehaviorCurveRequest) (*pasturePb.CowBehaviorCurveResponse, error) {
  122. currentUser, err := s.GetCurrentSystemUser(ctx)
  123. if err != nil {
  124. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  125. }
  126. cowInfo, err := s.GetCowInfoByCowId(ctx, currentUser.PastureId, int64(req.CowId))
  127. if err != nil {
  128. return nil, xerr.Customf("错误的牛只信息: %d", req.CowId)
  129. }
  130. nowTime := time.Now()
  131. nowDayZero := util.TimeParseLocalUnix(nowTime.Format(model.LayoutDate2))
  132. endDataTime := nowTime.Format(model.LayoutDate2)
  133. startDataTime := nowTime.AddDate(0, 0, -30).Format(model.LayoutDate2)
  134. // 行为曲线数据
  135. neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  136. if err = s.DB.Table(new(model.NeckActiveHabit).TableName()).
  137. Where("neck_ring_number = ?", cowInfo.NeckRingNumber).
  138. Where("pasture_id = ?", currentUser.PastureId).
  139. Where("heat_date >= ?", startDataTime).
  140. Where("heat_date <= ?", endDataTime).
  141. Order("heat_date, frameid").
  142. Find(&neckActiveHabitList).Error; err != nil {
  143. return nil, xerr.WithStack(err)
  144. }
  145. // 牛只事件列表
  146. eventLogList := make([]*model.EventCowLog, 0)
  147. eventLog := &model.EventCowLog{CowId: cowInfo.Id}
  148. if err = s.DB.Table(eventLog.TableName()).
  149. Where("cow_id = ?", cowInfo.Id).
  150. Where("pasture_id = ?", currentUser.PastureId).
  151. Where("event_at >= ?", nowDayZero-(30*86400)).
  152. Where("event_at <= ?", nowDayZero+86400).
  153. Order("event_at").
  154. Find(&eventLogList).Error; err != nil {
  155. return nil, xerr.WithStack(err)
  156. }
  157. data := model.NeckActiveHabitSlice(neckActiveHabitList).ToPB(req.CurveName)
  158. for _, v := range eventLogList {
  159. eventAt := time.Unix(v.EventAt, 0)
  160. data.EventList[eventAt.Format(model.LayoutDate2)] = v.EventTypeName
  161. }
  162. // 发情数据
  163. estrusList := make([]*model.EventEstrus, 0)
  164. if err = s.DB.Table(new(model.EventEstrus).TableName()).
  165. Where("cow_id = ?", cowInfo.Id).
  166. Where("pasture_id = ?", currentUser.PastureId).
  167. Where("estrus_start_date >= ?", nowTime.Format(model.LayoutTime)).
  168. Where("estrus_start_date <= ?", nowTime.AddDate(0, 0, -30).Format(model.LayoutTime)).
  169. Order("estrus_start_date").
  170. Find(&estrusList).Error; err != nil {
  171. return nil, xerr.WithStack(err)
  172. }
  173. for _, v := range estrusList {
  174. data.EstrusList = append(data.EstrusList, v.EstrusStartDate)
  175. }
  176. return &pasturePb.CowBehaviorCurveResponse{
  177. Code: http.StatusOK,
  178. Message: "ok",
  179. Data: data,
  180. }, nil
  181. }