package delete_ne_config_backup

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"time"

	"be.ems/src/framework/cron"
	"be.ems/src/framework/database/db"
	"be.ems/src/framework/logger"
	"be.ems/src/framework/utils/date"
	neModel "be.ems/src/modules/ne/model"
	neService "be.ems/src/modules/ne/service"
)

var NewProcessor = &DeleteNeConfigBackupProcessor{
	neInfoService: neService.NewNeInfo,
	count:         0,
}

// DeleteNeConfigBackupProcessor 网元配置文件定期备份
type DeleteNeConfigBackupProcessor struct {
	neInfoService *neService.NeInfo // 网元信息服务
	count         int               // 执行次数
}

func (s *DeleteNeConfigBackupProcessor) 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,
	}

	// 读取参数值
	var params struct {
		StoreDays int `json:"storeDays"` // 保留天数
		StoreNum  int `json:"storeNum"`  // 保留数量，默认保留7
	}
	err := json.Unmarshal([]byte(sysJob.TargetParams), &params)
	if err != nil {
		return nil, fmt.Errorf("json params err: %v", err)
	}
	if params.StoreDays < 0 {
		return nil, fmt.Errorf("params storeDays less than 0  ")
	}
	if params.StoreNum <= 0 {
		params.StoreNum = 7
	}

	neList := s.neInfoService.Find(neModel.NeInfo{}, false, false)
	for _, neInfo := range neList {
		neTypeAndId := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
		tx := db.DB("").Model(&neModel.NeConfigBackup{})
		tx = tx.Where("ne_type = ? and ne_id = ?", neInfo.NeType, neInfo.NeId)

		// 查询数量为0直接返回
		var total int64 = 0
		if err := tx.Count(&total).Error; err != nil {
			result[neTypeAndId] = err.Error()
			continue
		}
		if total <= int64(params.StoreNum) {
			result[neTypeAndId] = "less than storeNum"
			continue
		}

		// 查询最后记录数据
		var lastCreateTime int64 = 0
		lastTx := tx.Select("create_time").Order("create_time DESC").Limit(1)
		if err := lastTx.Find(&lastCreateTime).Error; err != nil {
			result[neTypeAndId] = err.Error()
			continue
		}
		if lastCreateTime <= 1e12 {
			result[neTypeAndId] = "no data"
			continue
		}

		// 计算删除时间
		lastTime := time.UnixMilli(lastCreateTime)
		ltTime := lastTime.AddDate(0, 0, -params.StoreDays)

		// 删除小于最后时间的数据
		delTx := tx.Delete("create_time < ?", ltTime.UnixMilli())
		if err := delTx.Error; err != nil {
			result[neTypeAndId] = err.Error()
			continue
		}
		result[neTypeAndId] = tx.RowsAffected

		// 删除本地文件
		s.deleteFile(neInfo.NeType, neInfo.NeId, ltTime)
	}

	return result, nil
}

// deleteFile 删除本地文件
func (s DeleteNeConfigBackupProcessor) deleteFile(neType, neId string, oldFileDate time.Time) {
	neTypeLower := strings.ToLower(neType)
	localPath := fmt.Sprintf("/usr/local/omc/backup/ne_config/%s/%s ", neTypeLower, neId)
	files, err := os.ReadDir(localPath)
	if err != nil {
		logger.Errorf("logger Remove ne_config File ReadDir err: %v", err.Error())
		return
	}
	for _, file := range files {
		// 跳过非指定文件名
		// zipFileName := fmt.Sprintf("%s-%s-etc-%s.zip", neTypeLower, neInfo.NeId, date.ParseDateToStr(time.Now(), date.YYYYMMDDHHMMSS))
		fileName := fmt.Sprintf("%s-%s-etc-", neTypeLower, neId)
		if !strings.HasPrefix(file.Name(), fileName) {
			continue
		}
		idx := strings.LastIndex(file.Name(), "-")
		if idx == -1 {
			continue
		}
		dateStr := file.Name()[idx+1 : idx+15]

		// 解析日期字符串
		fileDate, err := time.Parse(date.YYYYMMDDHHMMSS, dateStr)
		if err != nil {
			logger.Errorf("logger Remove ne_config name Parse err: %v", err.Error())
			continue
		}

		// 判断文件日期是否在给定日期之前
		if fileDate.Before(oldFileDate) {
			err := os.Remove(filepath.Join(localPath, file.Name()))
			if err != nil {
				logger.Errorf("logger Remove ne_config file err: %v", err.Error())
				continue
			}
		}
	}
}
