package system

import (
	"errors"
	"wails-app/internal/dao"

	"gorm.io/gorm"
	"wails-app/internal/global"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysDictionary
//@description: 创建字典数据
//@param: sysDictionary model.SysDictionary
//@return: err error

type DictionaryService struct{}

func (dictionaryService *DictionaryService) CreateSysDictionary(sysDictionary dao.SysDictionary) (err error) {
	_, err = dao.QueryDictionaryByType(sysDictionary.Type)
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("存在相同的type，不允许创建")
	}
	return sysDictionary.CreateDictionary()
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysDictionary
//@description: 删除字典数据
//@param: sysDictionary model.SysDictionary
//@return: err error

func (dictionaryService *DictionaryService) DeleteSysDictionary(sysDictionary dao.SysDictionary) (err error) {
	_, err = dao.QueryDictionaryAndDetailsById(sysDictionary.ID)
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("请不要搞事")
	}
	if err != nil {
		return err
	}
	err = sysDictionary.DeleteDictionary()
	if err != nil {
		return err
	}

	if sysDictionary.SysDictionaryDetails != nil {
		for _, SysDictionaryDetail := range sysDictionary.SysDictionaryDetails {
			err = SysDictionaryDetail.DeleteSysDictionaryDetail()
		}
		return err
	}
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSysDictionary
//@description: 更新字典数据
//@param: sysDictionary *model.SysDictionary
//@return: err error

func (dictionaryService *DictionaryService) UpdateSysDictionary(sysDictionary *dao.SysDictionary) (err error) {
	var dict dao.SysDictionary

	_, err = dao.QueryDictionaryAndDetailsById(sysDictionary.ID)
	if err != nil {
		global.GVA_LOG.Debug(err.Error())
		return errors.New("查询字典数据失败")
	}
	if dict.Type != sysDictionary.Type {
		_, err = dao.QueryDictionaryByType(sysDictionary.Type)
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			return errors.New("存在相同的type，不允许创建")
		}
	}

	return dao.SysDictionary{
		Name:   sysDictionary.Name,
		Type:   sysDictionary.Type,
		Status: sysDictionary.Status,
		Desc:   sysDictionary.Desc,
	}.UpdateDictionary()
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysDictionary
//@description: 根据id或者type获取字典单条数据
//@param: Type string, Id uint
//@return: err error, sysDictionary model.SysDictionary

func (dictionaryService *DictionaryService) GetSysDictionary(Type string, Id uint, status *bool) (sysDictionary dao.SysDictionary, err error) {
	var flag = false
	if status == nil {
		flag = true
	} else {
		flag = *status
	}
	return dao.GetSysDictionary(Type, Id, flag)
}

//@author: [piexlmax](https://github.com/piexlmax)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: GetSysDictionaryInfoList
//@description: 分页获取字典列表
//@param: info request.SysDictionarySearch
//@return: err error, list interface{}, total int64

func (dictionaryService *DictionaryService) GetSysDictionaryInfoList() (sysDictionaries []dao.SysDictionary, err error) {
	return dao.GetSysDictionaryInfoList()
}
