event_sale_cow.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package model
  2. import (
  3. "fmt"
  4. "time"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. type EventSaleCow struct {
  8. Id int64 `json:"id"`
  9. BatchNumber string `json:"batchNumber"`
  10. PastureId int64 `json:"pastureId"`
  11. SaleId int64 `json:"saleId"`
  12. CowId int64 `json:"cowId"`
  13. CowType pasturePb.CowType_Kind `json:"cowType"`
  14. CowKind pasturePb.CowKind_Kind `json:"cowKind"`
  15. EarNumber string `json:"earNumber"`
  16. PenId int32 `json:"penId"`
  17. PenName string `json:"penName"`
  18. DayAge int32 `json:"dayAge"`
  19. Lact int32 `json:"lact"`
  20. PregnancyAge int32 `json:"pregnancyAge"`
  21. BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
  22. LactationAge int32 `json:"lactationAge"`
  23. CalvingAge int32 `json:"calvingAge"`
  24. AdmissionAge int32 `json:"admissionAge"`
  25. CreatedAt int64 `json:"createdAt"`
  26. UpdatedAt int64 `json:"updatedAt"`
  27. }
  28. func (e *EventSaleCow) TableName() string {
  29. return "event_sale_cow"
  30. }
  31. func NewEventSaleCow(pastureId int64, saleId, saleAt int64, cowInfo *Cow) *EventSaleCow {
  32. return &EventSaleCow{
  33. PastureId: pastureId,
  34. BatchNumber: cowInfo.BatchNumber,
  35. SaleId: saleId,
  36. CowId: cowInfo.Id,
  37. Lact: cowInfo.Lact,
  38. EarNumber: cowInfo.EarNumber,
  39. PenId: cowInfo.PenId,
  40. PenName: cowInfo.PenName,
  41. PregnancyAge: cowInfo.PregnancyAge,
  42. BreedStatus: cowInfo.BreedStatus,
  43. LactationAge: cowInfo.LactationAge,
  44. AdmissionAge: cowInfo.AdmissionAge,
  45. DayAge: cowInfo.GetEventDayAge(saleAt),
  46. CowType: cowInfo.CowType,
  47. CowKind: cowInfo.CowKind,
  48. }
  49. }
  50. func NewEventSaleCowList(pastureId int64, eventSale *EventSale, cowList []*Cow) []*EventSaleCow {
  51. res := make([]*EventSaleCow, 0)
  52. for _, cow := range cowList {
  53. res = append(res, NewEventSaleCow(pastureId, eventSale.Id, eventSale.SaleAt, cow))
  54. }
  55. return res
  56. }
  57. type EventSaleCowSlice []*EventSaleCow
  58. func (e EventSaleCowSlice) ToPB(
  59. eventSaleMap map[int64]*EventSale,
  60. cowKindMap map[pasturePb.CowKind_Kind]string,
  61. cowMap map[int64]*Cow,
  62. sourceMap map[pasturePb.CowSource_Kind]string,
  63. ) []*pasturePb.AlreadySalesReport {
  64. res := make([]*pasturePb.AlreadySalesReport, 0)
  65. for _, v := range e {
  66. cowKindName, saleAtFormat, dealerName := "", "", ""
  67. if name, ok := cowKindMap[v.CowKind]; ok {
  68. cowKindName = name
  69. }
  70. salePrice, saleTotalAmount, saleAllWeight, saleAvgWeight := float32(0), float32(0), float32(0), float32(0)
  71. if eventSale, ok := eventSaleMap[v.SaleId]; ok {
  72. if eventSale.SaleAt > 0 {
  73. saleAtFormat = time.Unix(eventSale.SaleAt, 0).Local().Format(LayoutDate2)
  74. }
  75. saleTotalAmount = float32(eventSale.SaleAllAmount)
  76. dealerName = eventSale.DealerName
  77. }
  78. feedCycle, cowSourceName := "", ""
  79. if cowInfo, ok := cowMap[v.CowId]; ok {
  80. admissionAtFormat := ""
  81. if cowInfo.AdmissionAt > 0 {
  82. feedCycle = time.Unix(cowInfo.AdmissionAt, 0).Local().Format(LayoutDate2)
  83. }
  84. feedCycle = fmt.Sprintf("%s-%s", admissionAtFormat, saleAtFormat)
  85. if name, ok := sourceMap[cowInfo.SourceKind]; ok {
  86. cowSourceName = name
  87. }
  88. }
  89. res = append(res, &pasturePb.AlreadySalesReport{
  90. CowId: int32(v.CowId),
  91. EarNumber: v.EarNumber,
  92. BatchNumber: v.BatchNumber,
  93. CowKindName: cowKindName,
  94. PenName: v.PenName,
  95. SaleAtFormat: saleAtFormat,
  96. SalePrice: salePrice,
  97. SaleTotalAmount: saleTotalAmount,
  98. SaleAvgWeight: saleAvgWeight,
  99. SaleAllWeight: saleAllWeight,
  100. AdmissionAge: v.AdmissionAge,
  101. CowSourceName: cowSourceName,
  102. EnterWeight: 0,
  103. EnterPrice: 0,
  104. GrowthWeight: 0,
  105. AllFeedCost: 0,
  106. DayAvgFeedCost: 0,
  107. AllMedicalCharge: 0,
  108. OtherCost: 0,
  109. DealerName: dealerName,
  110. ProfitAndLoss: 0,
  111. FeedCycle: feedCycle,
  112. })
  113. }
  114. return res
  115. }