package crontab import ( "fmt" "kpt-pasture/config" "kpt-pasture/model" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" ) func (e *Entry) NeckRingMergeData() error { cfg := config.Options() limit := cfg.NeckRingLimit if limit <= 0 { limit = 10000 } neckRingList := make([]*model.NeckRingOriginal, 0) if err := e.DB.Model(new(model.NeckRingOriginal)). Where("h1.is_show = ?", pasturePb.IsShow_No). Find(&neckRingList).Limit(int(limit)).Error; err != nil { return xerr.WithStack(err) } if len(neckRingList) <= 0 { return nil } neckRingIds := make([]int64, len(neckRingList)) // 更新已处理过的id defer func() { if len(neckRingIds) > 0 { e.DB.Model(new(model.NeckRingOriginal)). Where("id IN ?", neckRingIds). Update("is_show", pasturePb.IsShow_Ok) } }() originalMapData := make(map[string]*model.NeckRingOriginal) // 合并成2个小时的 for i, v := range neckRingList { if v.ActiveDate == "" { continue } neckRingIds[i] = v.Id if _, ok := originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)]; ok { if v.ActiveDateType == pasturePb.ActiveTimeType_Twenty_Minutes { originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Remain += v.Remain originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Intake += v.Intake originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Inactive += v.Inactive originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Gasp += v.Gasp originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Other += v.Other originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Active += v.Active } if v.ActiveDateType == pasturePb.ActiveTimeType_Two_Hours { originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Remain *= 6 originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Intake *= 6 originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Inactive *= 6 originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Gasp *= 6 originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Other *= 6 originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)].Active *= 6 } } else { originalMapData[fmt.Sprintf("%s-%s", v.Imei, v.ActiveDate)] = v } } return nil }