milk_gea.go 4.2 KB

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