neck_ring.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package model
  2. import (
  3. "time"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. )
  6. type NeckRing struct {
  7. Id int64 `json:"id"`
  8. PastureId int64 `json:"pastureId"`
  9. NeckRingNumber string `json:"neckRingNumber"`
  10. CowId int64 `json:"cowId"`
  11. EarNumber string `json:"earNumber"`
  12. WearAt int64 `json:"wearAt"`
  13. IsBind pasturePb.NeckRingIsBind_Kind `json:"isBind"`
  14. Status pasturePb.NeckRingStatus_Kind `json:"status"`
  15. ErrorReason string `json:"errorReason"`
  16. OperationId int32 `json:"operationId"`
  17. OperationName string `json:"operationName"`
  18. CreatedAt int64 `json:"createdAt"`
  19. UpdatedAt int64 `json:"updatedAt"`
  20. }
  21. func (n *NeckRing) TableName() string {
  22. return "neck_ring"
  23. }
  24. func (n *NeckRing) EventBindUpdate(cowId int64) {
  25. n.CowId = cowId
  26. n.WearAt = time.Now().Local().Unix()
  27. }
  28. func (n *NeckRing) EventUnBindUpdate() {
  29. n.IsBind = pasturePb.NeckRingIsBind_Unbind
  30. n.CowId = 0
  31. n.WearAt = 0
  32. n.ErrorReason = ""
  33. n.EarNumber = ""
  34. n.OperationId = 0
  35. n.OperationName = ""
  36. }
  37. func NewNeckRing(pastureId int64, number string, cowInfo *Cow, operationUser *SystemUser) *NeckRing {
  38. return &NeckRing{
  39. PastureId: pastureId,
  40. NeckRingNumber: number,
  41. CowId: cowInfo.Id,
  42. EarNumber: cowInfo.EarNumber,
  43. WearAt: time.Now().Local().Unix(),
  44. IsBind: pasturePb.NeckRingIsBind_Bind,
  45. Status: pasturePb.NeckRingStatus_Normal,
  46. OperationId: int32(operationUser.Id),
  47. OperationName: operationUser.Name,
  48. }
  49. }
  50. type NeckRingSlice []*NeckRingModel
  51. func (n NeckRingSlice) ToPB(neckRingIsBind map[pasturePb.NeckRingIsBind_Kind]string, neckRingStatus map[pasturePb.NeckRingStatus_Kind]string) []*pasturePb.SearchNeckRingList {
  52. res := make([]*pasturePb.SearchNeckRingList, len(n))
  53. for i, v := range n {
  54. wearAtFormat := ""
  55. wearDays := int32(0)
  56. if v.WearAt > 0 {
  57. wearAtFormat = time.Unix(v.WearAt, 0).Format(LayoutDate2)
  58. wearDays = int32(time.Now().Local().Sub(time.Unix(v.WearAt, 0)).Hours() / 24)
  59. }
  60. res[i] = &pasturePb.SearchNeckRingList{
  61. Id: int32(v.Id),
  62. Number: v.NeckRingNumber,
  63. EarNumber: v.EarNumber,
  64. PenName: v.PenName,
  65. CowId: int32(v.CowId),
  66. WearAtFormat: wearAtFormat,
  67. WearDays: wearDays,
  68. IsBind: v.IsBind,
  69. IsBindName: neckRingIsBind[v.IsBind],
  70. ErrorReason: v.ErrorReason,
  71. Status: v.Status,
  72. StatusName: neckRingStatus[v.Status],
  73. }
  74. }
  75. return res
  76. }
  77. type NeckRingModel struct {
  78. Id int64 `json:"id"`
  79. PastureId int64 `json:"pastureId"`
  80. NeckRingNumber string `json:"neckRingNumber"`
  81. CowId int64 `json:"cowId"`
  82. PenName string `json:"penName"`
  83. EarNumber string `json:"earNumber"`
  84. WearAt int64 `json:"wearAt"`
  85. IsBind pasturePb.NeckRingIsBind_Kind `json:"isBind"`
  86. Status pasturePb.NeckRingStatus_Kind `json:"status"`
  87. ErrorReason string `json:"errorReason"`
  88. OperationId int32 `json:"operationId"`
  89. OperationName string `json:"operationName"`
  90. CreatedAt int64 `json:"createdAt"`
  91. UpdatedAt int64 `json:"updatedAt"`
  92. }