neck_ring.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. WearAt int64 `json:"wearAt"`
  13. Status pasturePb.NeckRingStatus_Kind `json:"status"`
  14. ErrorReason string `json:"errorReason"`
  15. OperationId int32 `json:"operationId"`
  16. OperationName string `json:"operationName"`
  17. CreatedAt int64 `json:"createdAt"`
  18. UpdatedAt int64 `json:"updatedAt"`
  19. }
  20. func (n *NeckRing) TableName() string {
  21. return "neck_ring"
  22. }
  23. func (n *NeckRing) EventBindUpdate(cowId int64) {
  24. n.CowId = cowId
  25. n.WearAt = time.Now().Unix()
  26. }
  27. func NewNeckRing(pastureId int64, neckRingNumber string, cowId int64, operationUser *SystemUser) *NeckRing {
  28. return &NeckRing{
  29. PastureId: pastureId,
  30. NeckRingNumber: neckRingNumber,
  31. CowId: cowId,
  32. WearAt: time.Now().Unix(),
  33. Status: pasturePb.NeckRingStatus_Bind,
  34. OperationId: int32(operationUser.Id),
  35. OperationName: operationUser.Name,
  36. }
  37. }
  38. type NeckRingSlice []*NeckRing
  39. func (n NeckRingSlice) ToPB(
  40. neckRingStatus map[pasturePb.NeckRingStatus_Kind]string,
  41. db *kptstore.DB,
  42. getCowPenInfoByCowId func(DB *kptstore.DB, cowId int64) *Pen,
  43. ) []*pasturePb.SearchNeckRingList {
  44. res := make([]*pasturePb.SearchNeckRingList, len(n))
  45. for i, v := range n {
  46. penId := int32(0)
  47. penName := ""
  48. if v.CowId > 0 {
  49. penInfo := getCowPenInfoByCowId(db, v.CowId)
  50. if penInfo != nil {
  51. penId = penInfo.Id
  52. penName = penInfo.Name
  53. }
  54. }
  55. wearAtFormat := ""
  56. wearDays := int32(0)
  57. if v.WearAt > 0 {
  58. wearAtFormat = time.Unix(v.WearAt, 0).Format(LayoutDate2)
  59. wearDays = int32(time.Now().Sub(time.Unix(v.WearAt, 0)).Hours() / 24)
  60. }
  61. res[i] = &pasturePb.SearchNeckRingList{
  62. Id: int32(v.Id),
  63. Number: v.NeckRingNumber,
  64. PenId: penId,
  65. PenName: penName,
  66. CowId: int32(v.CowId),
  67. WearAtFormat: wearAtFormat,
  68. WearDays: wearDays,
  69. Status: v.Status,
  70. StatusName: neckRingStatus[v.Status],
  71. ErrorReason: v.ErrorReason,
  72. }
  73. }
  74. return res
  75. }