Vestmore_GO/model/killara_catalog_language_logic.go
2024-04-15 16:08:17 +08:00

151 lines
4.5 KiB
Go

package model
import (
"strings"
"gorm.io/gorm"
)
type KillaraCatalogLanguageModel struct {
// fields ...
db *gorm.DB
TableName string // 表名
}
func (m *KillaraCatalogLanguageModel) InsertLanguage(language *KillaraCatalogLanguage) (uint64, error) {
if err := m.db.Model(&KillaraCatalogLanguage{}).Create(language).Error; err != nil {
return 0, err
}
return *language.LanguageId, nil
}
func (m *KillaraCatalogLanguageModel) UpdateLanguage(language *KillaraCatalogLanguage) error {
return m.db.Model(&KillaraCatalogLanguage{}).Where("language_id = ?", *language.LanguageId).Updates(language).Error
}
func (m *KillaraCatalogLanguageModel) DeleteLanguage(language *KillaraCatalogLanguage) error {
return m.db.Where("language_id = ?", *language.LanguageId).Delete(&KillaraCatalogLanguage{}).Error
}
func (m *KillaraCatalogLanguageModel) GetLanguage(languageID uint64) (*KillaraCatalogLanguage, error) {
var language KillaraCatalogLanguage
err := m.db.Where("language_id = ?", languageID).First(&language).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &language, err
}
func (m *KillaraCatalogLanguageModel) GetLanguagesForBackEnd(data map[string]interface{}) ([]*KillaraCatalogLanguage, error) {
var languages []*KillaraCatalogLanguage
query := m.db.Model(&KillaraCatalogLanguage{})
var conditions []interface{}
if filterName, ok := data["filter_name"].(string); ok && filterName != "" {
conditions = append(conditions, m.db.Where("name LIKE ?", filterName+"%"))
}
if filterCode, ok := data["filter_code"].(string); ok && filterCode != "" {
conditions = append(conditions, m.db.Where("code LIKE ?", filterCode+"%"))
}
if filterStatus, ok := data["filter_status"]; ok {
conditions = append(conditions, m.db.Where("status = ?", filterStatus))
}
if len(conditions) > 0 {
query = query.Where(strings.Join(make([]string, len(conditions)), " AND "), conditions...)
}
if sort, ok := data["sort"].(string); ok && sort != "" {
order := "DESC"
if sortOrder, ok := data["order"].(string); ok {
order = sortOrder
}
query = query.Order(sort + " " + order)
}
if start, ok := data["start"].(int); ok && start > 0 {
query = query.Offset(start)
}
if limit, ok := data["limit"].(int); ok && limit > 0 {
query = query.Limit(limit)
} else {
query = query.Limit(10)
}
err := query.Find(&languages).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return languages, err
}
func (m *KillaraCatalogLanguageModel) GetTotalLanguagesForBackEnd(data map[string]interface{}) (int64, error) {
var count int64
query := m.db.Model(&KillaraCatalogLanguage{})
var conditions []interface{}
if filterName, ok := data["filter_name"].(string); ok && filterName != "" {
conditions = append(conditions, m.db.Where("name LIKE ?", filterName+"%"))
}
if filterCode, ok := data["filter_code"].(string); ok && filterCode != "" {
conditions = append(conditions, m.db.Where("code LIKE ?", filterCode+"%"))
}
if filterStatus, ok := data["filter_status"]; ok {
conditions = append(conditions, m.db.Where("status = ?", filterStatus))
}
if len(conditions) > 0 {
query = query.Where(strings.Join(make([]string, len(conditions)), " AND "), conditions...)
}
err := query.Count(&count).Error
if err == gorm.ErrRecordNotFound {
return 0, nil
}
return count, err
}
func (m *KillaraCatalogLanguageModel) GetLanguageForBackEnd(languageID uint64) (*KillaraCatalogLanguage, error) {
var language KillaraCatalogLanguage
err := m.db.Where("language_id = ?", languageID).First(&language).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &language, err
}
func (m *KillaraCatalogLanguageModel) GetLanguageByCodeForBackEnd(code string) (*KillaraCatalogLanguage, error) {
var language KillaraCatalogLanguage
err := m.db.Where("code = ?", code).First(&language).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &language, err
}
// 前台使用的方法
func (m *KillaraCatalogLanguageModel) GetLanguagesForFrontEnd() ([]*KillaraCatalogLanguage, error) {
var languages []*KillaraCatalogLanguage
err := m.db.Where("status = 1").Order("sort_order").Find(&languages).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return languages, err
}
func (m *KillaraCatalogLanguageModel) GetLanguageForFrontEnd(languageID uint64) (*KillaraCatalogLanguage, error) {
var language KillaraCatalogLanguage
err := m.db.Where("status = 1 AND language_id = ?", languageID).First(&language).Error
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return &language, err
}