// 自动生成模板SysDictionaryDetail
package dao

import (
	"server/global"
)

// 如果含有time.Time 请自行import time包
type SysDictionaryDetail struct {
	global.GVA_MODEL
	Label           string `json:"label" form:"label" gorm:"column:label;comment:展示值"`                                  // 展示值
	Value           string `json:"value" form:"value" gorm:"column:value;comment:字典值"`                                  // 字典值
	Extend          string `json:"extend" form:"extend" gorm:"column:extend;comment:扩展值"`                               // 扩展值
	Status          *bool  `json:"status" form:"status" gorm:"column:status;comment:启用状态"`                              // 启用状态
	Sort            int    `json:"sort" form:"sort" gorm:"column:sort;comment:排序标记"`                                    // 排序标记
	SysDictionaryID int    `json:"sysDictionaryID" form:"sysDictionaryID" gorm:"column:sys_dictionary_id;comment:关联标记"` // 关联标记
}

func (SysDictionaryDetail) TableName() string {
	return "sys_dictionary_details"
}

// TODO:字典详情查询

// GetSysDictionaryDetailById 查询字典详情 按id
func GetSysDictionaryDetailById(id uint) (sysDictionaryDetail SysDictionaryDetail, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&sysDictionaryDetail).Error
	return sysDictionaryDetail, err
}

// GetSysDictionaryDetailInfoList 分页获取字典详情列表
func (sdd SysDictionaryDetail) GetSysDictionaryDetailInfoList(limit, offset int) (sysDictionaryDetails []SysDictionaryDetail, total int64, err error) {
	// 创建db
	db := global.GVA_DB.Model(&SysDictionaryDetail{})
	// 如果有条件搜索 下方会自动创建搜索语句
	if sdd.Label != "" {
		db = db.Where("label LIKE ?", "%"+sdd.Label+"%")
	}
	if sdd.Value != "" {
		db = db.Where("value = ?", sdd.Value)
	}
	if sdd.Status != nil {
		db = db.Where("status = ?", sdd.Status)
	}
	if sdd.SysDictionaryID != 0 {
		db = db.Where("sys_dictionary_id = ?", sdd.SysDictionaryID)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Order("sort").Find(&sysDictionaryDetails).Error
	return sysDictionaryDetails, total, err
}

// GetDictionaryListByDictionaryId 查询字典列表 按字典id
func GetDictionaryListByDictionaryId(dictionaryID uint) (sysDictionaryDetails []SysDictionaryDetail, err error) {
	err = global.GVA_DB.Find(&sysDictionaryDetails, "sys_dictionary_id = ?", dictionaryID).Error
	return sysDictionaryDetails, err
}

// GetDictionaryListByType 查询字典列表 按字典类型
func GetDictionaryListByType(t string) (sysDictionaryDetails []SysDictionaryDetail, err error) {
	db := global.GVA_DB.Model(&SysDictionaryDetail{}).Joins("JOIN sys_dictionaries ON sys_dictionaries.id = sys_dictionary_details.sys_dictionary_id")
	err = db.Debug().Find(&sysDictionaryDetails, "type = ?", t).Error
	return sysDictionaryDetails, err
}

// GetDictionaryInfoByValue 查询字典列表 按字典内容
func GetDictionaryInfoByValue(dictionaryID uint, value uint) (sysDictionaryDetail SysDictionaryDetail, err error) {
	err = global.GVA_DB.First(&sysDictionaryDetail, "sys_dictionary_id = ? and value = ?", dictionaryID, value).Error
	return sysDictionaryDetail, err
}

// GetDictionaryInfoByTypeValue 查询字典列表 按字典类型和字典内容
func GetDictionaryInfoByTypeValue(t string, value uint) (sysDictionaryDetail SysDictionaryDetail, err error) {
	db := global.GVA_DB.Model(&SysDictionaryDetail{}).Joins("JOIN sys_dictionaries ON sys_dictionaries.id = sys_dictionary_details.sys_dictionary_id")
	err = db.First(&sysDictionaryDetail, "sys_dictionaries.type = ? and sys_dictionary_details.value = ?", t, value).Error
	return sysDictionaryDetail, err
}

// TODO:字典详情新增

// CreateSysDictionaryDetail 创建字典详情数据
func (sdd SysDictionaryDetail) CreateSysDictionaryDetail() error {
	return global.GVA_DB.Create(&sdd).Error
}

// TODO:字典详情修改

// UpdateSysDictionaryDetail 更新字典数据
func (sdd SysDictionaryDetail) UpdateSysDictionaryDetail() error {
	return global.GVA_DB.Save(&sdd).Error
}

// TODO:字典详情删除

// DeleteSysDictionaryDetail 删除字典详情
func (sdd SysDictionaryDetail) DeleteSysDictionaryDetail() error {
	return global.GVA_DB.Delete(&sdd).Error
}
