package rag import ( "database/sql" "eta/eta_api/global" "eta/eta_api/utils" "fmt" "time" ) type Question struct { QuestionId int `gorm:"column:question_id;type:int(9) UNSIGNED;primaryKey;not null;" description:"question_id"` QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"` Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"排序"` ModifyTime time.Time `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"` CreateTime time.Time `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"` } // TableName get sql table name.获取数据库表名 func (m *Question) TableName() string { return "question" } // QuestionColumns get sql column name.获取数据库列名 var QuestionColumns = struct { QuestionID string QuestionContent string Sort string ModifyTime string CreateTime string }{ QuestionID: "question_id", QuestionContent: "question_content", Sort: "sort", ModifyTime: "modify_time", CreateTime: "create_time", } func (m *Question) Create() (err error) { err = global.DbMap[utils.DbNameAI].Create(&m).Error return } func (m *Question) Update(updateCols []string) (err error) { err = global.DbMap[utils.DbNameAI].Select(updateCols).Updates(&m).Error return } func (m *Question) Del() (err error) { err = global.DbMap[utils.DbNameAI].Delete(&m).Error return } type QuestionView struct { QuestionId int `gorm:"column:question_id;type:int(9) UNSIGNED;primaryKey;not null;" description:"question_id"` QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"` // Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"sort"` // 排序 ModifyTime string `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"` CreateTime string `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"` } func (m *Question) ToView() QuestionView { var modifyTime, createTime string if !m.CreateTime.IsZero() { createTime = m.CreateTime.Format(utils.FormatDateTime) } if !m.ModifyTime.IsZero() { modifyTime = m.ModifyTime.Format(utils.FormatDateTime) } return QuestionView{ QuestionId: m.QuestionId, QuestionContent: m.QuestionContent, Sort: m.Sort, ModifyTime: modifyTime, CreateTime: createTime, } } func (m *Question) ListToViewList(list []*Question) (wechatArticleViewList []QuestionView) { wechatArticleViewList = make([]QuestionView, 0) for _, v := range list { wechatArticleViewList = append(wechatArticleViewList, v.ToView()) } return } func (m *Question) GetByID(id int) (item *Question, err error) { err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", QuestionColumns.QuestionID), id).First(&item).Error return } func (m *Question) GetByCondition(condition string, pars []interface{}) (item *Question, err error) { sqlStr := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).First(&item).Error return } func (m *Question) GetListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*Question, err error) { sqlStr := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s order by sort asc,question_id asc LIMIT ?,?`, m.TableName(), condition) pars = append(pars, startSize, pageSize) err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error return } func (m *Question) GetCountByCondition(condition string, pars []interface{}) (total int, err error) { var intNull sql.NullInt64 sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error if err == nil && intNull.Valid { total = int(intNull.Int64) } return } func (m *Question) GetPageListByCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*Question, err error) { total, err = m.GetCountByCondition(condition, pars) if err != nil { return } if total > 0 { items, err = m.GetListByCondition(condition, pars, startSize, pageSize) } return }