123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- 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"`
- QuestionTitle string `gorm:"column:question_title;type:varchar(255);comment:问题标题;" description:"问题标题"`
- QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"`
- Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"排序"`
- SysUserId int `gorm:"column:sys_user_id;type:int(11);comment:添加人id;default:0;" description:"添加人id"`
- SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(255);comment:添加人真实名称;" 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
- QuestionTitle string
- QuestionContent string
- Sort string
- ModifyTime string
- CreateTime string
- }{
- QuestionID: "question_id",
- QuestionTitle: "question_title",
- 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"`
- QuestionTitle string `gorm:"column:question_title;type:varchar(255);comment:问题标题;" description:"问题标题"`
- QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"`
- Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"排序"`
- SysUserId int `gorm:"column:sys_user_id;type:int(11);comment:添加人id;default:0;" description:"添加人id"`
- SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(255);comment:添加人真实名称;" description:"添加人真实名称"`
- 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,
- QuestionTitle: m.QuestionTitle,
- QuestionContent: m.QuestionContent,
- Sort: m.Sort,
- SysUserId: m.SysUserId,
- SysUserRealName: m.SysUserRealName,
- 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(field, condition string, pars []interface{}, startSize, pageSize int) (items []*Question, err error) {
- if field == "" {
- field = "*"
- }
- sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s order by question_id desc LIMIT ?,?`, field, 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
- }
- func (m *Question) GetTitlePageListByCondition(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(`question_id,question_title,sort`, condition, pars, startSize, pageSize)
- }
- return
- }
|