package handler import ( "GtDataStore/app/cmd/organization/internal/logic/job" "GtDataStore/app/cmd/organization/internal/svc" "GtDataStore/app/model" "context" "fmt" "github.com/zeromicro/go-zero/core/logx" "strconv" "time" ) func DeviceUf(ctx context.Context, task *job.Task, technologyName string) error { // 1. 查询所有的设备 devices, err := findDeviceUf(task.Job.SvcCtx, int64(task.Id)) if err != nil { return err } // 2. 获得点位信息 //var wg sync.WaitGroup //wg.Add(len(devices)) //for _, device := range devices { // device := device // go func() { // defer wg.Done() // if err := device.Items.FillCurrentValue(); err != nil { // logx.Errorf("DeviceUf device.Items.FillCurrentValue error: %s", err.Error()) // } // }() //} // //wg.Wait() // 3. 转换为存储对象 workings, err := transDeviceUfData(task.Job.SvcCtx, devices) if err != nil { return err } // 4. 批量入库 if _, err := task.Job.SvcCtx.WorkingUf.MultiInsert(context.Background(), workings); err != nil { logx.Errorf("DeviceUf task.Job.SvcCtx.WorkingUf.MultiInsert error: %s", err.Error()) } return nil } func findDeviceUf(svcCtx *svc.ServiceContext, projectId int64) ([]model.DcDeviceBind, error) { if data := deviceBindCacheTable.GetCache(DEVICE_UF); data != nil { return data, nil } if devices, err := svcCtx.DeviceBind.FindByProjectIdDeviceType(context.Background(), projectId, DEVICE_UF); err != nil { logx.Infof("findDeviceUf not found devices") return nil, err } else { deviceBindCacheTable.SetCache(DEVICE_UF, devices, time.Now().Add(300*time.Second)) return devices, nil } } func transDeviceUfData(svcCtx *svc.ServiceContext, datas []model.DcDeviceBind) ([]model.DcWorkingUf, error) { ts := make([]model.DcWorkingUf, len(datas)) for i, data := range datas { var filterCycleAdjust int64 = 0 key := fmt.Sprintf("events:adjust:%d:%s:%s:%s", data.ProjectId, data.DeviceCode, "filter_cycle", "") if adjustString, err := svcCtx.Cache.Get(key); err == nil && adjustString != "" { filterCycleAdjust, _ = strconv.ParseInt(adjustString, 10, 64) } ts[i] = model.DcWorkingUf{ ProjectId: data.ProjectId, DeviceCode: data.DeviceCode, WaterTemperature: data.Items.GetItemFloat64Value("water_temperature"), FeedFlow: data.Items.GetItemFloat64Value("feed_flow"), ConFlow: data.Items.GetItemFloat64Value("con_flow"), ProductFlow: data.Items.GetItemFloat64Value("product_flow"), FeedPressure: data.Items.GetItemFloat64Value("feed_pressure"), ConPressure: data.Items.GetItemFloat64Value("con_pressure"), ProductPressure: data.Items.GetItemFloat64Value("product_pressure"), Tmp: data.Items.GetItemFloat64Value("tmp"), Flux: data.Items.GetItemFloat64Value("flux"), Permeability: data.Items.GetItemFloat64Value("permeability"), FeedWqTurbidity: data.Items.GetItemFloat64Value("feed_wq_turbidity"), FeedWqPh: data.Items.GetItemInt64Value("feed_wq_ph"), ProductWqPh: data.Items.GetItemInt64Value("product_wq_ph"), FeedWqAl: data.Items.GetItemFloat64Value("feed_wq_al"), ProductWqAl: data.Items.GetItemFloat64Value("product_wq_al"), FeedWqFe: data.Items.GetItemFloat64Value("feed_wq_fe"), ProductWqFe: data.Items.GetItemFloat64Value("product_wq_fe"), FeedWqMn: data.Items.GetItemFloat64Value("feed_wq_mn"), ProductWqMn: data.Items.GetItemFloat64Value("product_wq_mn"), FeedWqSio2: data.Items.GetItemFloat64Value("feed_wq_sio2"), ProductWqSio2: data.Items.GetItemFloat64Value("product_wq_sio2"), FeedWqCod: data.Items.GetItemFloat64Value("feed_wq_cod"), ProductWqCod: data.Items.GetItemFloat64Value("product_wq_cod"), FeedWqP: data.Items.GetItemFloat64Value("feed_wq_p"), ProductWqP: data.Items.GetItemFloat64Value("product_wq_p"), Step: data.Items.GetItemInt64Value("step"), FilterTime: data.Items.GetItemFloat64Value("filter_time"), FilterCycle: data.Items.GetItemInt64Value("filter_cycle") + filterCycleAdjust, CTime: time.Now(), } } return ts, nil }