package backend import ( "context" "fmt" "kpt-pasture/model" "kpt-pasture/util" "strconv" "strings" "time" "gitee.com/xuyiping_admin/pkg/xerr" ) const ( eidEndLen = 8 eidStart = 63 + 7 - eidEndLen ) // GEAMILK gea品牌实现 type GEAMILK struct { store *StoreEntry body *model.MilkHallBody } func (a *GEAMILK) SaveData(ctx context.Context) error { if len(a.body.Content) <= 0 { return nil } pastureData := &model.AppPastureList{} if err := a.store.DB.Model(new(model.AppPastureList)). Where("farm_id = ?", a.body.FarmId). First(pastureData).Error; err != nil { return xerr.WithStack(err) } nowTime := time.Now() gEAOriginalList := make([]*model.MilkOriginal, 0) for _, v := range a.body.Content { content, ok := v.(map[string]interface{}) if !ok { continue } t, ok := content["vstr"] if !ok { continue } text := t.(string) s1 := strings.TrimSpace(util.Substr(text, 0, 6)) milkWeight, _ := strconv.ParseFloat(strings.TrimSpace(util.Substr(text, 25, 4)), 64) fmt.Println("milkWeight:", milkWeight, strings.TrimSpace(util.Substr(text, 25, 4))) 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" && hour1 == "23" { milkDate = nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2) } else { milkDate = nowTime.Format(model.LayoutDate2) } if hour == "00" && hour2 == "23" { attachTime = nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2) } else { attachTime = nowTime.Format(model.LayoutDate2) } recognitionTime := fmt.Sprintf("%s %02s:%02s:%02s", nowTime.Format(model.LayoutDate2), hour1, minute1, second1) attachTime = fmt.Sprintf("%s %02s:%02s:%02s", attachTime, hour2, minute2, second2) detachTime := fmt.Sprintf("%s %02s:%02s:%02s", nowTime.Format(model.LayoutDate2), hour, minute, second) earNumber := fmt.Sprintf("%s", strings.TrimSpace(util.Substr(text, 7, 6))) detachAddress, _ := 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, a.body.Brand, a.body.MilkHallNumber, detachTime, earNumber, milkDate, attachTime, recognitionTime, milkWeight, detachAddress, 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()) }