neck_ring_log.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package model
  2. import (
  3. "time"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. )
  6. type NeckRingLog struct {
  7. Id int64 `json:"id"`
  8. Number string `json:"number"`
  9. CowId int64 `json:"cowId"`
  10. WearAt int64 `json:"wearAt"`
  11. UnbindAt int64 `json:"unbindAt"`
  12. Status pasturePb.NeckRingStatus_Kind `json:"status"`
  13. ErrorReason string `json:"errorReason"`
  14. OperationId int32 `json:"operationId"`
  15. OperationName string `json:"operationName"`
  16. CreatedAt int64 `json:"createdAt"`
  17. UpdatedAt int64 `json:"updatedAt"`
  18. }
  19. func (n *NeckRingLog) TableName() string {
  20. return "neck_ring_log"
  21. }
  22. func NewNeckRingLog(number string, cowId int64, currentUser *SystemUser) *NeckRingLog {
  23. return &NeckRingLog{
  24. Number: number,
  25. CowId: cowId,
  26. WearAt: time.Now().Unix(),
  27. Status: pasturePb.NeckRingStatus_Bind,
  28. OperationId: int32(currentUser.Id),
  29. OperationName: currentUser.Name,
  30. }
  31. }
  32. func NewNeckRingLogList(req []*pasturePb.NeckRingCreateItem, currentUser *SystemUser) []*NeckRingLog {
  33. res := make([]*NeckRingLog, len(req))
  34. for i, v := range req {
  35. res[i] = NewNeckRingLog(v.Number, int64(v.CowId), currentUser)
  36. }
  37. return res
  38. }
  39. type NeckRingLogSlice []*NeckRingLog
  40. func (n NeckRingLogSlice) ToPB(neckRingStatus map[pasturePb.NeckRingStatus_Kind]string) []*pasturePb.SearchNeckRingList {
  41. res := make([]*pasturePb.SearchNeckRingList, len(n))
  42. for i, v := range n {
  43. res[i] = &pasturePb.SearchNeckRingList{
  44. Id: int32(v.Id),
  45. Number: v.Number,
  46. CowId: int32(v.CowId),
  47. WearAtFormat: time.Unix(v.WearAt, 0).Format(LayoutDate2),
  48. WearDays: int32(time.Now().Sub(time.Unix(v.WearAt, 0)).Hours() / 24),
  49. Status: v.Status,
  50. StatusName: neckRingStatus[v.Status],
  51. ErrorReason: v.ErrorReason,
  52. }
  53. }
  54. return res
  55. }