123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package backend
- import (
- "context"
- "encoding/json"
- "fmt"
- "kpt-pasture/model"
- "kpt-pasture/util"
- "strconv"
- "strings"
- "time"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "go.uber.org/zap"
- "gitee.com/xuyiping_admin/pkg/xerr"
- )
- const (
- eidStart = 7
- eidEndLen = 64 + 7 - eidStart
- )
- // GEAMILK gea品牌实现
- type GEAMILK struct {
- store *StoreEntry
- }
- func (a *GEAMILK) SaveData(ctx context.Context, body *model.MilkHallBody) error {
- gEAMilkHallOriginalList := make([]*model.GEAMilkHallOriginal, 0)
- if err := json.Unmarshal(body.Content, &gEAMilkHallOriginalList); err != nil {
- return xerr.WithStack(err)
- }
- nowTime := time.Now()
- zaplog.Info("GEAMILK", zap.String("nowTime", nowTime.Format(model.LayoutTime)), zap.Any("body", body))
- pastureData := &model.PastureList{}
- if err := a.store.DB.Model(new(model.PastureList)).
- Where("farm_id = ?", body.FarmId).
- First(pastureData).Error; err != nil {
- return xerr.WithStack(err)
- }
- gEAOriginalList := make([]*model.MilkOriginal, 0)
- for _, gea := range gEAMilkHallOriginalList {
- text := gea.Vstr
- s1 := strings.TrimSpace(util.Substr(text, 0, 6))
- milkWeight, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 25, 4)), 10, 64)
- if len(s1) == 0 && milkWeight == 0 {
- continue
- }
- hour := strings.TrimSpace(util.Substr(text, 0, 2))
- minute := strings.TrimSpace(util.Substr(text, 2, 2))
- second := strings.TrimSpace(util.Substr(text, 4, 2))
- milkDate, attachTime := "", ""
- hour1 := strings.TrimSpace(util.Substr(text, 37, 2))
- minute1 := strings.TrimSpace(util.Substr(text, 39, 2))
- second1 := strings.TrimSpace(util.Substr(text, 41, 2))
- hour2 := strings.TrimSpace(util.Substr(text, 44, 2))
- minute2 := strings.TrimSpace(util.Substr(text, 46, 2))
- second2 := strings.TrimSpace(util.Substr(text, 49, 2))
- if hour == "00" {
- if hour1 == "23" {
- milkDate = nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
- } else {
- milkDate = nowTime.Format(model.LayoutDate2)
- }
- if hour2 == "23" {
- attachTime = nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
- } else {
- attachTime = nowTime.Format(model.LayoutTime)
- }
- }
- milkDate = fmt.Sprintf("%s %s:%s:%s", milkDate, hour1, minute1, second1)
- attachTime = fmt.Sprintf("%s %s:%s:%s", attachTime, hour2, minute2, second2)
- detachTime := fmt.Sprintf("%s %s:%s:%s", nowTime, hour, minute, second)
- earNumber := fmt.Sprintf("%s", strings.TrimSpace(util.Substr(text, 7, 6)))
- detacherAddress, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 20, 2)), 10, 64)
- duration, _ := strconv.ParseFloat(strings.TrimSpace(util.Substr(text, 31, 4)), 64)
- peakFlow, _ := strconv.ParseFloat(strings.TrimSpace(util.Substr(text, 52, 4)), 64)
- conductivity, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 57, 4)), 10, 64)
- timestampDiffSeconds := TimestampDiffSeconds(attachTime, detachTime)
- // 过滤无奶量且时间间隔小于30秒的记录
- if milkWeight == 0 && duration == 0 && timestampDiffSeconds < 30 {
- continue
- }
- eId := fmt.Sprintf("%s", strings.TrimSpace(util.Substr(text, eidStart, eidEndLen)))
- f0t15, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 90, 3)), 10, 64)
- f15t30, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 95, 3)), 10, 64)
- f30t60, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 100, 3)), 10, 64)
- f60t120, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 105, 3)), 10, 64)
- manualDetach, _ := strconv.ParseInt(strings.TrimSpace(util.Substr(text, 109, 1)), 10, 64)
- newGEAOriginal := model.NewGEAMilkOriginal(pastureData.Id, body.MilkHallNumber, detachTime, earNumber, milkDate, attachTime,
- milkWeight, detacherAddress, duration, peakFlow, int32(conductivity), eId, f0t15, f15t30, f30t60, f60t120, int8(manualDetach))
- gEAOriginalList = append(gEAOriginalList, newGEAOriginal)
- }
- if len(gEAOriginalList) > 0 {
- if err := a.store.DB.Model(new(model.MilkOriginal)).
- Create(gEAOriginalList).Error; err != nil {
- return xerr.WithStack(err)
- }
- }
- return nil
- }
- func TimestampDiffSeconds(startTime, endTime string) int64 {
- start, err := util.TimeParseLocal(model.LayoutTime, startTime)
- if err != nil {
- return 0
- }
- end, err := util.TimeParseLocal(model.LayoutTime, endTime)
- if err != nil {
- return 0
- }
- diff := end.Sub(start)
- return int64(diff.Seconds())
- }
|