package crontab

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"main/config"
	"main/database"
	"main/model"
	"main/service/admin"
	"main/utils"
	"os"
	"path"
	"strings"
	"time"
)

// CornSaveNginxLog 定时任务处理nginx日志
func CornSaveNginxLog() {
	cPath, _ := os.Getwd()
	logPath := path.Join(cPath, config.Config.Logs.Nginx)
	fileList, _ := os.ReadDir(logPath)
	db := database.GetInstance().GetMysqlDb()
	var logDealList []model.LogFileDealLog
	var resLogList []model.Logs
	if err := db.Find(&logDealList).Error; err != nil {
		logrus.Errorln("sql执行失败：", err)
		return
	}
	for _, file := range fileList {
		if strings.Contains(file.Name(), "access-") {
			date := strings.Split(strings.Split(file.Name(), "access-")[1], ".")[0]
			if getDateDealBool(date, logDealList) || date == time.Now().Format("2006-01-02") {
				continue
			}
			logrus.Infoln(date, "：nginx日志开始处理")
			if err := db.Where("time like ?", fmt.Sprintf("%s%%", date)).Delete(&resLogList).Error; err != nil {
				logrus.Errorln("sql执行失败：", err)
				return
			}
			logList, _ := utils.FileRead(path.Join(logPath, file.Name()), true)
			for _, v := range logList {
				c := make(chan model.Logs)
				go admin.FormatLog(v, date, c)
				resLogItem := <-c
				if err := db.Create(&resLogItem).Error; err != nil {
					logrus.Errorln("sql执行失败：", err)
					return
				}
			}
			if err := db.Create(&model.LogFileDealLog{
				Date:    date,
				Success: true,
			}).Error; err != nil {
				logrus.Errorln("sql执行失败：", err)
				return
			}
			logrus.Infoln(date, "：nginx日志处理完成")
		}
	}
}

// 获取当前日期是否已处理
func getDateDealBool(date string, dealLog []model.LogFileDealLog) bool {
	for _, v := range dealLog {
		if v.Date == date && v.Success {
			return true
		}
	}
	return false
}
