package fileManager

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"log-server/global"
	"log-server/model/fileManager"
	"log-server/model/fileManager/request"
	"log-server/model/fileManager/response"
	"log-server/model/task"
	"time"
)

type ServiceFileFolder struct {
}

// FileFolderList 获取当前父文件夹下所有的子文件夹
func (s *ServiceFileFolder) FileFolderList(ctx context.Context, api fileManager.FileFolder) (interface{}, error) {
	//开始查询当前父文件夹下的所有文件夹（目录）
	db := global.GVA_DB.Model(&fileManager.FileFolder{})
	db = db.Where("parent_id = ?", api.ParentId)
	//var statisticsLogs []*log.LogComputer
	var folderListRes []*response.FileFolderResponse
	err := db.Order("name").Find(&folderListRes).Error
	if err != nil {
		return nil, err
	}
	var folderList []*response.FileFolderResponse
	for _, one := range folderListRes {
		folder := new(response.FileFolderResponse)
		folder.Id = one.Id
		folder.UpdateTime = one.UpdateTime
		folder.CreateTime = one.CreateTime
		folder.Name = one.Name
		folder.ParentId = one.ParentId
		folder.TaskId = one.TaskId
		folderList = append(folderList, folder)
	}
	return folderList, err
}

// QueryFolder 条件查询文件夹
func (s *ServiceFileFolder) QueryFolder(ctx context.Context, api request.FileFolderRequest) (interface{}, error) {
	db := global.GVA_DB.Model(&fileManager.FileFolder{})
	db = db.Joins("LEFT JOIN game_task t ON t.task_id = file_folder.task_id")
	db = db.Where("parent_id = 0")
	if api.DirectorName != "" {
		db = db.Where("t.user = ?", api.DirectorName)
	}
	if api.Name != "" {
		db = db.Where("name = ?", api.Name)
	}
	//var statisticsLogs []*log.LogComputer
	var folderListRes []*response.FileFolderResponse
	err := db.Order("name").Find(&folderListRes).Error
	if err != nil {
		return nil, err
	}
	//var folderList []*response.FileFolderResponse
	//for _, one := range folderListRes {
	//	folder := new(response.FileFolderResponse)
	//	folder.Id = one.Id
	//	folder.UpdateTime = one.UpdateTime
	//	folder.CreateTime = one.CreateTime
	//	folder.Name = one.Name
	//	folder.ParentId = one.ParentId
	//	folder.TaskId = one.TaskId
	//	folderList = append(folderList, folder)
	//}
	return folderListRes, err
}

//
//func (s *ServiceRentComputer) GetRentComputerNum(ctx context.Context, api rentComputer.RentComputer) int64 {
//	var total int64
//
//	db := global.GVA_DB.Model(&rentComputer.RentComputer{})
//	db = db.Distinct("id")
//	//db = db.Where("is_off_shelf = 0 and is_expire != 1")
//	if api.PcNum != "" {
//		db = db.Where("rent_computer.pc_num = ?", api.PcNum)
//	}
//	if api.ShopId != 0 {
//		db = db.Where("rent_computer.shop_id = ?", api.ShopId)
//	}
//	if api.SetMealId != 0 {
//		db = db.Where("rent_computer.set_meal_id = ?", api.SetMealId)
//	}
//	if api.DirectorName != "ALL" {
//		if api.DirectorName == "" {
//			db = db.Where("rent_computer.director_name IS NULL")
//		} else {
//			db = db.Where("rent_computer.director_name = ?", api.DirectorName)
//		}
//	}
//	if api.IsExpire != -1 {
//		db = db.Where("rent_computer.is_expire = ?", api.IsExpire)
//	}
//	_ = db.Count(&total).Error
//	return total
//}

func (s *ServiceFileFolder) AddFileFolder(requestFolder request.FileFolderRequest) (parentId int, parentName string, err error) {
	if !errors.Is(global.GVA_DB.Where("name = ? and parent_id = ? and task_id = ?", requestFolder.Name, requestFolder.ParentId, requestFolder.TaskId).First(&fileManager.FileFolder{}).Error, gorm.ErrRecordNotFound) {
		return requestFolder.ParentId, requestFolder.Name, errors.New("文件夹已存在")
	}
	folder := new(fileManager.FileFolder)
	folder.Name = requestFolder.Name
	folder.ParentId = requestFolder.ParentId
	folder.TaskId = requestFolder.TaskId
	folder.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	folder.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
	err = global.GVA_DB.Create(&folder).Error
	if err != nil {
		return requestFolder.ParentId, requestFolder.Name, err
	}
	//查询刚刚创建文件夹的id
	insertFolder := fileManager.FileFolder{}
	err = global.GVA_DB.Where("name = ? and parent_id = ? and task_id = ?", requestFolder.Name, requestFolder.ParentId, requestFolder.TaskId).First(&insertFolder).Error
	if err != nil {
		return requestFolder.ParentId, requestFolder.Name, err
	}
	//判断如果是与任务相关联，则默认创建几个文件夹
	serviceFileType := new(ServiceFileType)
	typeList, _ := serviceFileType.GetAllFileType()

	//folderDefaultList := []string{"易语言脚本", "雷电镜像", "游戏包", "夜神镜像", "其他文件"}
	if requestFolder.TaskId != 0 {
		for _, v := range typeList {
			one := new(fileManager.FileFolder)
			one.Name = v.Name
			one.ParentId = insertFolder.Id
			one.TaskId = insertFolder.TaskId
			one.CreateTime = time.Now().Format("2006-01-02 15:04:05")
			one.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
			err = global.GVA_DB.Create(&one).Error
			if err != nil {
				return requestFolder.ParentId, requestFolder.Name, err
			}
		}
	}
	return insertFolder.Id, insertFolder.Name, err
}

// GetAllTaskSimple 查询所有任务
func (s *ServiceFileFolder) GetAllTaskSimple() (list interface{}, err error) {
	var taskList []task.GameTask
	db := global.GVA_DB.Model(&task.GameTask{})
	err = db.Order("id").Find(&taskList).Error
	return taskList, err
}

// SelectFolderMsg 查询目录信息
func (s *ServiceFileFolder) SelectFolderMsg(requestFolder request.FileFolderRequest) (interface{}, error) {
	var folder fileManager.FileFolder
	err := global.GVA_DB.Where("id = ?", requestFolder.Id).First(&folder).Error
	if err != nil {
		return nil, errors.New("目录不存在")
	} else {
		//if folder.TaskId == 0 {
		//	return nil, errors.New("不是上层目录")
		//}
		var taskInfo task.GameTask
		db := global.GVA_DB.Model(&task.GameTask{})
		db.Where("task_id", folder.TaskId)
		err := db.Find(&taskInfo).Error
		var res response.FileFolderResponse2
		res.Id = folder.Id
		res.Name = folder.Name
		res.ParentId = folder.ParentId
		res.TaskId = folder.TaskId
		res.TaskName = taskInfo.TaskName
		return res, err
	}
}
