cow.go 8.3 KB

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