package task

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"log-server/global"
	"log-server/model/common/request"
	"log-server/model/task"
)

type CentralControlService struct {

}

//创建配置记录
func (s *CentralControlService) CreateCentralControlConf(conf task.CentralControlConf) (err error) {
	//查找是否有这个taskId的记录
	var entity task.CentralControlConf
	err = global.GVA_DB.Model(&task.CentralControlConf{}).Where("task_id", conf.TaskId).First(&entity).Error
	//如果有，报错返回
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("已有配置记录，请勿重复添加")
	}
	//如果没有，直接创建
	return global.GVA_DB.Model(&task.CentralControlConf{}).Omit("create_time", "update_time").Create(&conf).Error
}

//删除中控配置记录
func (s *CentralControlService) DeleteCentralControlConf(conf task.CentralControlConf) (err error)  {
	var entity task.CentralControlConf
	err = global.GVA_DB.Model(&task.CentralControlConf{}).Where("id", conf.Id).First(&entity).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("此配置不存在，无法删除")
	}
	return global.GVA_DB.Model(&task.CentralControlConf{}).Delete(&entity).Error
}

//批量删除中控配置记录
func (s *CentralControlService) DeleteCentralControlConfByIds(ids []int) (err error) {
	err = global.GVA_DB.Model(&task.CentralControlConf{}).Delete("id in ?", ids).Error
	return err
}

//更新中控配置列表
func (s *CentralControlService) UpdateCentralControlConf(conf task.CentralControlConf) (err error) {
	var entity task.CentralControlConf
	err = global.GVA_DB.Model(&task.CentralControlConf{}).Where("task_id = ? and id != ?", conf.TaskId, conf.Id).First(&entity).Error
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("已存在相同的task_id，无法更改")
	}
	return global.GVA_DB.Model(&task.CentralControlConf{}).Where("id", conf.Id).Save(&conf).Error
}

//通过id获取单条配置
func (s *CentralControlService) GetCentralControlConfById(id int) (conf task.CentralControlConf, err error) {
	err = global.GVA_DB.Model(&task.CentralControlConf{}).Where("id", id).First(&conf).Error
	return conf, err
}

//获取配置列表
func (s *CentralControlService) GetCentralControlConfList(conf task.CentralControlConf, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error){
	//获取limit和offset
	limit := info.PageSize
	offset := (info.Page - 1) * info.PageSize
	db := global.GVA_DB.Model(&task.CentralControlConf{})
	var confList []task.CentralControlConf

	//先条件过滤
	if conf.TaskId != 0 {
		//条件过滤记录数
		db = db.Where("task_id", conf.TaskId)
	}

	err = db.Count(&total).Error
	if err != nil{
		//如果出错直接返回
		return confList, total, err
	} else {
		//先分页再排序
		db = db.Limit(limit).Offset(offset)
		if order != ""{
			//传入排序字段，进行排序
			//定义orderStr存储完整的排序字段
			var  orderStr string
			//为了避免sql注入，自己创建数组匹配
			orderMap := make(map[string]bool, 3)
			orderMap["task_id"] = true
			orderMap["create_time"] = true
			orderMap["update_time"] = true
			if orderMap[order] {
				if desc {
					orderStr = order + " desc"
				} else {
					orderStr = order
				}
			} else {
				//传入非法字段，不能排序
				err = fmt.Errorf("传入非法字段 %v", order)
				return confList, total, err
			}
			//排序
			err = db.Order(orderStr).Find(&confList).Error
		} else {
			//没有传入排序字段，默认按照类型名称降序排序
			err = db.Order("id desc").Find(&confList).Error
		}
	}
	//是因为只有切片能排序，model没办法排序;还是说为了将数组返回至前端才find扫描至切片呢？
	return confList, total, err
}