package resource

import (
	"encoding/json"
	"fmt"
	"nms_nbi/src/framework/cron"
	"nms_nbi/src/framework/logger"
	"nms_nbi/src/modules/nms_cxy/service"
)

var NewProcessor = &ResourceProcessor{
	count:           0,
	resourceService: service.NewResourceImpl,
}

// 资源上报 队列任务处理
type ResourceProcessor struct {
	// 执行次数
	count int
	// 资源数据处理服务
	resourceService service.IResource
}

func (s *ResourceProcessor) Execute(data any) (any, error) {
	logger.Infof("执行 %d 次", s.count)
	s.count++

	options := data.(cron.JobData)
	sysJob := options.SysJob
	logger.Infof("重复 %v 任务ID %s", options.Repeat, sysJob.JobID)

	// 读取参数值
	var params struct {
		NeTypes []string `json:"neTypes"`
	}
	err := json.Unmarshal([]byte(sysJob.TargetParams), &params)
	if err != nil {
		return nil, fmt.Errorf("json params 'interval' err: %v", err)
	}

	// 遍历网元
	for _, neType := range params.NeTypes {
		err = s.resourceService.ResourceeUploadOSS(neType)
		if err != nil {
			return nil, err
		}
	}

	// 返回结果，用于记录执行结果
	result := map[string]any{
		"repeat":  options.Repeat,
		"count":   s.count,
		"jobName": sysJob.JobName,
	}
	return result, nil
}
