indicators_data.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package model
  2. import (
  3. "fmt"
  4. "sort"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. const (
  8. AllCow = "all_cow"
  9. AdultCow = "adult_cow"
  10. CalvingInterval = "calving_interval"
  11. OutputNumber = "output_number"
  12. InputNumber = "input_number"
  13. SalesVolume = "sales_volume"
  14. CalvingNumber = "calving_number"
  15. AdultAbortionRate = "adult_abortion_rate"
  16. YouthAbortionRate = "youth_abortion_rate"
  17. AllDieNumber = "all_die_number"
  18. DiseaseNumber = "disease_number"
  19. CureNumber = "cure_number"
  20. OutNumber = "out_number"
  21. CalfDieNumber = "calf_die_number"
  22. LactationCow = "lactation_cow"
  23. DryMilkCow = "dry_milk_cow"
  24. ReserveCow = "reserve_cow"
  25. )
  26. type IndicatorsData struct {
  27. Id int64 `json:"id"`
  28. PastureId int64 `json:"pastureId"`
  29. CategoryType pasturePb.IndicatorType_Kind `json:"categoryType"`
  30. CategoryName string `json:"categoryName"`
  31. Date string `json:"date"`
  32. Kind string `json:"kind"`
  33. Value string `json:"value"`
  34. CreatedAt int64 `json:"createdAt"`
  35. UpdatedAt int64 `json:"updatedAt"`
  36. }
  37. func (d *IndicatorsData) TableName() string {
  38. return "indicators_data"
  39. }
  40. type IndicatorsDataSlice []*IndicatorsData
  41. func (i IndicatorsDataSlice) ToPB(header, kinds []string, detailsMap map[string]*IndicatorsDetails) *IndicatorComparison {
  42. res := &IndicatorComparison{
  43. Headers: header,
  44. List: make([]map[string]string, 0),
  45. }
  46. if len(header) <= 0 || len(detailsMap) <= 0 || len(kinds) <= 0 || len(i) <= 0 {
  47. return res
  48. }
  49. sort.Strings(res.Headers) // 确保日期顺序一致
  50. indicatorsDataMap := make(map[string]*IndicatorsData)
  51. for _, v := range i {
  52. indicatorsDataMap[fmt.Sprintf("%s-%s", v.Kind, v.Date)] = v
  53. }
  54. dataMap := make(map[string]map[string]string)
  55. for _, kind := range kinds {
  56. if dataMap[kind] == nil {
  57. dataMap[kind] = make(map[string]string)
  58. }
  59. details := detailsMap[kind]
  60. dataMap[kind] = map[string]string{
  61. "name": details.Name,
  62. "kind": details.Kind,
  63. "unit": details.Unit,
  64. "categoryType": fmt.Sprintf("%d", details.CategoryType),
  65. "categoryName": details.CategoryName,
  66. "zh": details.Zh,
  67. }
  68. for _, v := range header {
  69. pref := fmt.Sprintf("%s-%s", kind, v)
  70. indicators, ok := indicatorsDataMap[pref]
  71. if ok {
  72. dataMap[kind][v] = indicators.Value
  73. } else {
  74. dataMap[kind][v] = ""
  75. }
  76. }
  77. }
  78. for _, data := range dataMap {
  79. res.List = append(res.List, data)
  80. }
  81. return res
  82. }
  83. type IndicatorsComparisonResponse struct {
  84. Code int32 `json:"code"`
  85. Msg string `json:"msg"`
  86. Data *IndicatorComparison `json:"data"`
  87. }
  88. type IndicatorComparison struct {
  89. Headers []string `json:"headers"`
  90. List []map[string]string `json:"list"`
  91. }