event_cow_log.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package model
  2. import (
  3. "fmt"
  4. "time"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. const ShardTableNumber = 6
  8. type EventCowLog struct {
  9. Id int64 `json:"id"`
  10. PastureId int64 `json:"pastureId"`
  11. CowId int64 `json:"cowId"`
  12. DayAge int32 `json:"dayAge"`
  13. Lact int32 `json:"lact"`
  14. PenId int32 `json:"penId"`
  15. PenName string `json:"penName"`
  16. CowType pasturePb.CowType_Kind `json:"cowType"`
  17. CowTypeName string `json:"cowTypeName"`
  18. EventCategoryKind pasturePb.EventCategory_Kind `json:"eventCategoryId"`
  19. EventType pasturePb.EventType_Kind `json:"eventType"`
  20. EventTypeName string `json:"eventTypeName"`
  21. EventDescription string `json:"eventDescription"`
  22. OperationId int64 `json:"operationId"`
  23. OperationName string `json:"operationName"`
  24. EventAt int64 `json:"eventAt"`
  25. Remarks string `json:"remarks"`
  26. CreatedAt int64 `json:"createdAt"`
  27. UpdatedAt int64 `json:"updatedAt"`
  28. }
  29. func (e *EventCowLog) UnShardTableName() string {
  30. return "event_cow_log"
  31. }
  32. // TableName 表名
  33. func (e *EventCowLog) TableName() string {
  34. return fmt.Sprintf("%s_%04d", e.UnShardTableName(), e.CowId%ShardTableNumber)
  35. }
  36. type EventCowLogModel struct {
  37. Cow *Cow
  38. EventType pasturePb.EventType_Kind
  39. OperationUser *SystemUser
  40. CurrentUser *SystemUser
  41. EventAt int64
  42. ExposeEstrusType pasturePb.ExposeEstrusType_Kind
  43. EventCategoryKind pasturePb.EventCategory_Kind
  44. PenName string
  45. Remarks string
  46. CowTypeName string
  47. EventTypeName string
  48. Description string
  49. }
  50. func NewEventCowLog(req *EventCowLogModel) *EventCowLog {
  51. operationId := int64(0)
  52. operationName := ""
  53. if req.OperationUser != nil && req.OperationUser.Id > 0 {
  54. operationId = req.OperationUser.Id
  55. operationName = req.OperationUser.Name
  56. }
  57. return &EventCowLog{
  58. PastureId: req.Cow.PastureId,
  59. CowId: req.Cow.Id,
  60. DayAge: req.Cow.GetDayAge(),
  61. Lact: req.Cow.Lact,
  62. PenId: req.Cow.PenId,
  63. PenName: req.PenName,
  64. CowType: req.Cow.CowType,
  65. CowTypeName: req.CowTypeName,
  66. EventCategoryKind: req.EventCategoryKind,
  67. EventType: req.EventType,
  68. EventTypeName: req.EventTypeName,
  69. EventDescription: req.Description,
  70. OperationId: operationId,
  71. OperationName: operationName,
  72. EventAt: req.EventAt,
  73. Remarks: req.Remarks,
  74. }
  75. }
  76. type EventCowLogSlice []*EventCowLog
  77. func (e EventCowLogSlice) ToPB(eventCategoryMap map[pasturePb.EventCategory_Kind]string) []*pasturePb.CowEvent {
  78. res := make([]*pasturePb.CowEvent, len(e))
  79. for i, v := range e {
  80. eventAtFormat := ""
  81. if v.EventAt > 0 {
  82. eventAtFormat = time.Unix(v.EventAt, 0).Format(LayoutDate2)
  83. }
  84. res[i] = &pasturePb.CowEvent{
  85. Id: int32(v.Id),
  86. PastureId: int32(v.PastureId),
  87. CowId: int32(v.CowId),
  88. Lact: v.Lact,
  89. DayAge: v.DayAge,
  90. PenName: v.PenName,
  91. CowTypeName: v.CowTypeName,
  92. EventTypeName: v.EventTypeName,
  93. EventDescription: v.EventDescription,
  94. OperationName: v.OperationName,
  95. Remarks: v.Remarks,
  96. EventAtFormat: eventAtFormat,
  97. EventCategoryKind: v.EventCategoryKind,
  98. EventCategoryName: eventCategoryMap[v.EventCategoryKind],
  99. }
  100. }
  101. return res
  102. }