package service

import (
	"fmt"
	"nms_nbi/src/framework/constants/uploadsubpath"
	"nms_nbi/src/framework/i18n"
	"nms_nbi/src/framework/utils/file"
	"nms_nbi/src/framework/utils/parse"
	"nms_nbi/src/modules/api_rest_cxy/utils/common"
	systemModel "nms_nbi/src/modules/system/model"
	systemService "nms_nbi/src/modules/system/service"
)

// 实例化数据层 LogImpl 结构体
var NewLogImpl = &LogImpl{
	sysLogOperateService: systemService.NewSysLogOperateImpl,
	sysLogLoginService:   systemService.NewSysLogLoginImpl,
}

// 日志数据处理服务 服务层处理
type LogImpl struct {
	// 操作日志服务
	sysLogOperateService systemService.ISysLogOperate
	// 系统登录日志服务
	sysLogLoginService systemService.ISysLogLogin
}

// OperateLogToFile 操作日志写入文件
func (s *LogImpl) OperateLogToFile(language, startTime, endTime string) (int64, string) {
	var pageNum int64 = 1
	var pageSize int64 = 20
	dataArr := []any{}

	// 查询数据
	pageData := s.sysLogOperateService.SelectSysLogOperatePage(map[string]any{
		"beginTime": startTime,
		"endTime":   endTime,
		"pageNum":   pageNum,
		"pageSize":  pageSize,
	})

	total := parse.Number(pageData["total"])
	rows, ok := pageData["rows"].([]systemModel.SysLogOperate)
	if !ok || len(rows) <= 0 {
		return 0, ""
	}
	for _, v := range rows {
		v.Title = i18n.TKey(language, v.Title)
		v.OperLocation = i18n.TKey(language, v.OperLocation)
		v.Method = "-"
		dataArr = append(dataArr, v)
	}
	pageNum += 1

	// 按总数分批遍历
	batchNum := total / pageSize
	for i := 1; i <= int(batchNum); i++ {
		// 查询数据
		pageData := s.sysLogOperateService.SelectSysLogOperatePage(map[string]any{
			"beginTime": startTime,
			"endTime":   endTime,
			"pageNum":   pageNum,
			"pageSize":  pageSize,
		})
		rows, ok := pageData["rows"].([]systemModel.SysLogOperate)
		if !ok || len(rows) <= 0 {
			return int64(len(dataArr)), ""
		}
		for _, v := range rows {
			v.Title = i18n.TKey(language, v.Title)
			v.OperLocation = i18n.TKey(language, v.OperLocation)
			v.Method = "-"
			dataArr = append(dataArr, v)
		}
		pageNum += 1
	}

	// 文件名
	dataSaveFileName := common.DataSaveFileName{
		LogType: "operate",
	}
	fileName := dataSaveFileName.JSON()
	filePath := fmt.Sprintf("%s/%s", file.ParseUploadFileDir(uploadsubpath.EXPORT), fileName)
	err := file.WriterFileJSONLine(dataArr, filePath)
	if err != nil {
		return int64(len(dataArr)), ""
	}
	return int64(len(dataArr)), filePath
}

// SecurityLogToFile 安全日志写入文件
func (s *LogImpl) SecurityLogToFile(language, startTime, endTime string) (int64, string) {
	var pageNum int64 = 1
	var pageSize int64 = 20
	dataArr := []any{}

	// 查询数据
	pageData := s.sysLogLoginService.SelectSysLogLoginPage(map[string]any{
		"beginTime": startTime,
		"endTime":   endTime,
		"pageNum":   pageNum,
		"pageSize":  pageSize,
	})

	total := parse.Number(pageData["total"])
	rows, ok := pageData["rows"].([]systemModel.SysLogLogin)
	if !ok || len(rows) <= 0 {
		return 0, ""
	}
	for _, v := range rows {
		v.LoginLocation = i18n.TKey(language, v.LoginLocation)
		v.OS = i18n.TKey(language, v.OS)
		v.Browser = i18n.TKey(language, v.Browser)
		v.Msg = i18n.TKey(language, v.Msg)
		dataArr = append(dataArr, v)
	}
	pageNum += 1

	// 按总数分批遍历
	batchNum := total / pageSize
	for i := 1; i <= int(batchNum); i++ {
		// 查询数据
		pageData := s.sysLogLoginService.SelectSysLogLoginPage(map[string]any{
			"beginTime": startTime,
			"endTime":   endTime,
			"pageNum":   pageNum,
			"pageSize":  pageSize,
		})
		rows, ok := pageData["rows"].([]systemModel.SysLogLogin)
		if !ok || len(rows) <= 0 {
			return int64(len(dataArr)), ""
		}
		for _, v := range rows {
			v.LoginLocation = i18n.TKey(language, v.LoginLocation)
			v.OS = i18n.TKey(language, v.OS)
			v.Browser = i18n.TKey(language, v.Browser)
			v.Msg = i18n.TKey(language, v.Msg)
			dataArr = append(dataArr, v)
		}
		pageNum += 1
	}

	// 文件名
	dataSaveFileName := common.DataSaveFileName{
		LogType: "security",
	}
	fileName := dataSaveFileName.JSON()
	filePath := fmt.Sprintf("%s/%s", file.ParseUploadFileDir(uploadsubpath.EXPORT), fileName)
	err := file.WriterFileJSONLine(dataArr, filePath)
	if err != nil {
		return int64(len(dataArr)), ""
	}
	return int64(len(dataArr)), filePath
}
