package ne_data_udm

import (
	"fmt"

	"be.ems/src/framework/cron"
	"be.ems/src/framework/logger"
	neModel "be.ems/src/modules/ne/model"
	neService "be.ems/src/modules/ne/service"
	neDataService "be.ems/src/modules/ne_data/service"
)

var NewProcessor = &NeDataUDM{
	udmAuthService: neDataService.NewUDMAuthUser,
	udmSubService:  neDataService.NewUDMSubUser,
	neInfoService:  neService.NewNeInfo,
	count:          0,
}

// NeDataUDM 网元配置文件定期备份
type NeDataUDM struct {
	udmAuthService *neDataService.UDMAuthUser // UDM鉴权信息
	udmSubService  *neDataService.UDMSubUser  // UDM签约信息
	neInfoService  *neService.NeInfo          // 网元信息服务
	count          int                        // 执行次数
}

func (s *NeDataUDM) Execute(data any) (any, error) {
	s.count++ // 执行次数加一
	options := data.(cron.JobData)
	sysJob := options.SysJob
	logger.Infof("重复:%v 任务ID:%d  执行次数:%d", options.Repeat, sysJob.JobId, s.count)
	// 返回结果，用于记录执行结果
	result := map[string]any{
		"count": s.count,
	}

	neList := s.neInfoService.Find(neModel.NeInfo{NeType: "UDM"}, false, false)
	for _, neInfo := range neList {
		result[fmt.Sprintf("AuthNumber_%s", neInfo.NeId)] = s.udmAuthService.ResetData(neInfo.NeId)
		result[fmt.Sprintf("SubNumber_%s", neInfo.NeId)] = s.udmSubService.ResetData(neInfo.NeId)
	}

	return result, nil
}
