neck_ring.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 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. PenName string `json:"penName" gorm:"-"`
  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, number string, cowInfo *Cow, operationUser *SystemUser) *NeckRing {
  29. return &NeckRing{
  30. PastureId: pastureId,
  31. NeckRingNumber: number,
  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(neckRingStatus map[pasturePb.NeckRingStatus_Kind]string) []*pasturePb.SearchNeckRingList {
  42. res := make([]*pasturePb.SearchNeckRingList, len(n))
  43. for i, v := range n {
  44. wearAtFormat := ""
  45. wearDays := int32(0)
  46. if v.WearAt > 0 {
  47. wearAtFormat = time.Unix(v.WearAt, 0).Format(LayoutDate2)
  48. wearDays = int32(time.Now().Sub(time.Unix(v.WearAt, 0)).Hours() / 24)
  49. }
  50. res[i] = &pasturePb.SearchNeckRingList{
  51. Id: int32(v.Id),
  52. Number: v.NeckRingNumber,
  53. EarNumber: v.EarNumber,
  54. PenName: v.PenName,
  55. CowId: int32(v.CowId),
  56. WearAtFormat: wearAtFormat,
  57. WearDays: wearDays,
  58. Status: v.Status,
  59. StatusName: neckRingStatus[v.Status],
  60. ErrorReason: v.ErrorReason,
  61. }
  62. }
  63. return res
  64. }