cow.go 7.5 KB

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