milk_gea.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. )
  12. const (
  13. eidEndLen = 8
  14. eidStart = 63 + 7 - eidEndLen
  15. )
  16. // GEAMILK gea品牌实现
  17. type GEAMILK struct {
  18. store *StoreEntry
  19. body *model.MilkHallBody
  20. }
  21. func (a *GEAMILK) SaveData(ctx context.Context) error {
  22. if len(a.body.Content) <= 0 {
  23. return nil
  24. }
  25. pastureData := &model.AppPastureList{}
  26. if err := a.store.DB.Model(new(model.AppPastureList)).
  27. Where("farm_id = ?", a.body.FarmId).
  28. First(pastureData).Error; err != nil {
  29. return xerr.WithStack(err)
  30. }
  31. nowTime := time.Now()
  32. gEAOriginalList := make([]*model.MilkOriginal, 0)
  33. for _, v := range a.body.Content {
  34. content, ok := v.(map[string]interface{})
  35. if !ok {
  36. continue
  37. }
  38. t, ok := content["vstr"]
  39. if !ok {
  40. continue
  41. }
  42. text := t.(string)
  43. s1 := strings.TrimSpace(util.Substr(text, 0, 6))
  44. milkWeight, _ := strconv.ParseFloat(strings.TrimSpace(util.Substr(text, 25, 4)), 64)
  45. fmt.Println("milkWeight:", milkWeight, strings.TrimSpace(util.Substr(text, 25, 4)))
  46. if len(s1) == 0 && milkWeight == 0 {
  47. continue
  48. }
  49. hour := strings.TrimSpace(util.Substr(text, 0, 2))
  50. minute := strings.TrimSpace(util.Substr(text, 2, 2))
  51. second := strings.TrimSpace(util.Substr(text, 4, 2))
  52. milkDate, attachTime := "", ""
  53. hour1 := strings.TrimSpace(util.Substr(text, 37, 2))
  54. minute1 := strings.TrimSpace(util.Substr(text, 39, 2))
  55. second1 := strings.TrimSpace(util.Substr(text, 41, 2))
  56. hour2 := strings.TrimSpace(util.Substr(text, 44, 2))
  57. minute2 := strings.TrimSpace(util.Substr(text, 46, 2))
  58. second2 := strings.TrimSpace(util.Substr(text, 49, 2))
  59. if hour == "00" && hour1 == "23" {
  60. milkDate = nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  61. } else {
  62. milkDate = nowTime.Format(model.LayoutDate2)
  63. }
  64. if hour == "00" && hour2 == "23" {
  65. attachTime = nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  66. } else {
  67. attachTime = nowTime.Format(model.LayoutDate2)
  68. }
  69. recognitionTime := fmt.Sprintf("%s %02s:%02s:%02s", nowTime.Format(model.LayoutDate2), hour1, minute1, second1)
  70. attachTime = fmt.Sprintf("%s %02s:%02s:%02s", attachTime, hour2, minute2, second2)
  71. detachTime := fmt.Sprintf("%s %02s:%02s:%02s", nowTime.Format(model.LayoutDate2), hour, minute, second)
  72. earNumber := fmt.Sprintf("%s", strings.TrimSpace(util.Substr(text, 7, 6)))
  73. detachAddress, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 20, 2)), 10, 64)
  74. duration, _ := strconv.ParseFloat(strings.TrimSpace(util.Substr(text, 31, 4)), 64)
  75. peakFlow, _ := strconv.ParseFloat(strings.TrimSpace(util.Substr(text, 52, 4)), 64)
  76. conductivity, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 57, 4)), 10, 64)
  77. timestampDiffSeconds := TimestampDiffSeconds(attachTime, detachTime)
  78. // 过滤无奶量且时间间隔小于30秒的记录
  79. if milkWeight == 0 && duration == 0 && timestampDiffSeconds < 30 {
  80. continue
  81. }
  82. eId := fmt.Sprintf("%s", strings.TrimSpace(util.Substr(text, eidStart, eidEndLen)))
  83. f0t15, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 90, 3)), 10, 64)
  84. f15t30, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 95, 3)), 10, 64)
  85. f30t60, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 100, 3)), 10, 64)
  86. f60t120, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 105, 3)), 10, 64)
  87. manualDetach, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 109, 1)), 10, 64)
  88. newGEAOriginal := model.NewGEAMilkOriginal(
  89. pastureData.Id, a.body.Brand, a.body.MilkHallNumber, detachTime, earNumber, milkDate, attachTime, recognitionTime, milkWeight,
  90. detachAddress, duration, peakFlow, int32(conductivity), eId, f0t15, f15t30, f30t60, f60t120, int8(manualDetach))
  91. gEAOriginalList = append(gEAOriginalList, newGEAOriginal)
  92. }
  93. if len(gEAOriginalList) > 0 {
  94. if err := a.store.DB.Model(new(model.MilkOriginal)).
  95. Create(gEAOriginalList).Error; err != nil {
  96. return xerr.WithStack(err)
  97. }
  98. }
  99. return nil
  100. }
  101. func TimestampDiffSeconds(startTime, endTime string) int64 {
  102. start, err := util.TimeParseLocal(model.LayoutTime, startTime)
  103. if err != nil {
  104. return 0
  105. }
  106. end, err := util.TimeParseLocal(model.LayoutTime, endTime)
  107. if err != nil {
  108. return 0
  109. }
  110. diff := end.Sub(start)
  111. return int64(diff.Seconds())
  112. }