neck_ring.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package model
  2. import (
  3. "kpt-pasture/store/kptstore"
  4. "time"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. type NeckRing struct {
  8. Id int64 `json:"id"`
  9. PastureId int64 `json:"pastureId"`
  10. NeckRingNumber string `json:"neckRingNumber"`
  11. CowId int64 `json:"cowId"`
  12. EarNumber string `json:"earNumber"`
  13. WearAt int64 `json:"wearAt"`
  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().Unix()
  27. }
  28. func NewNeckRing(pastureId int64, cowInfo *Cow, operationUser *SystemUser) *NeckRing {
  29. return &NeckRing{
  30. PastureId: pastureId,
  31. NeckRingNumber: cowInfo.NeckRingNumber,
  32. CowId: cowInfo.Id,
  33. EarNumber: cowInfo.EarNumber,
  34. WearAt: time.Now().Unix(),
  35. Status: pasturePb.NeckRingStatus_Bind,
  36. OperationId: int32(operationUser.Id),
  37. OperationName: operationUser.Name,
  38. }
  39. }
  40. type NeckRingSlice []*NeckRing
  41. func (n NeckRingSlice) ToPB(
  42. neckRingStatus map[pasturePb.NeckRingStatus_Kind]string,
  43. db *kptstore.DB,
  44. getCowPenInfoByCowId func(DB *kptstore.DB, cowId int64) *Pen,
  45. ) []*pasturePb.SearchNeckRingList {
  46. res := make([]*pasturePb.SearchNeckRingList, len(n))
  47. for i, v := range n {
  48. penId := int32(0)
  49. penName := ""
  50. if v.CowId > 0 {
  51. penInfo := getCowPenInfoByCowId(db, v.CowId)
  52. if penInfo != nil {
  53. penId = penInfo.Id
  54. penName = penInfo.Name
  55. }
  56. }
  57. wearAtFormat := ""
  58. wearDays := int32(0)
  59. if v.WearAt > 0 {
  60. wearAtFormat = time.Unix(v.WearAt, 0).Format(LayoutDate2)
  61. wearDays = int32(time.Now().Sub(time.Unix(v.WearAt, 0)).Hours() / 24)
  62. }
  63. res[i] = &pasturePb.SearchNeckRingList{
  64. Id: int32(v.Id),
  65. Number: v.NeckRingNumber,
  66. EarNumber: v.EarNumber,
  67. PenId: penId,
  68. PenName: penName,
  69. CowId: int32(v.CowId),
  70. WearAtFormat: wearAtFormat,
  71. WearDays: wearDays,
  72. Status: v.Status,
  73. StatusName: neckRingStatus[v.Status],
  74. ErrorReason: v.ErrorReason,
  75. }
  76. }
  77. return res
  78. }