question.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package rag
  2. import (
  3. "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. type Question struct {
  10. QuestionId int `gorm:"column:question_id;type:int(9) UNSIGNED;primaryKey;not null;" description:"question_id"`
  11. QuestionTitle string `gorm:"column:question_title;type:varchar(255);comment:问题标题;" description:"问题标题"`
  12. QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"`
  13. Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"排序"`
  14. SysUserId int `gorm:"column:sys_user_id;type:int(11);comment:添加人id;default:0;" description:"添加人id"`
  15. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(255);comment:添加人真实名称;" description:"添加人真实名称"`
  16. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"`
  17. CreateTime time.Time `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"`
  18. }
  19. // TableName get sql table name.获取数据库表名
  20. func (m *Question) TableName() string {
  21. return "question"
  22. }
  23. // QuestionColumns get sql column name.获取数据库列名
  24. var QuestionColumns = struct {
  25. QuestionID string
  26. QuestionTitle string
  27. QuestionContent string
  28. Sort string
  29. ModifyTime string
  30. CreateTime string
  31. }{
  32. QuestionID: "question_id",
  33. QuestionTitle: "question_title",
  34. QuestionContent: "question_content",
  35. Sort: "sort",
  36. ModifyTime: "modify_time",
  37. CreateTime: "create_time",
  38. }
  39. func (m *Question) Create() (err error) {
  40. err = global.DbMap[utils.DbNameAI].Create(&m).Error
  41. return
  42. }
  43. func (m *Question) Update(updateCols []string) (err error) {
  44. err = global.DbMap[utils.DbNameAI].Select(updateCols).Updates(&m).Error
  45. return
  46. }
  47. func (m *Question) Del() (err error) {
  48. err = global.DbMap[utils.DbNameAI].Delete(&m).Error
  49. return
  50. }
  51. type QuestionView struct {
  52. QuestionId int `gorm:"column:question_id;type:int(9) UNSIGNED;primaryKey;not null;" description:"question_id"`
  53. QuestionTitle string `gorm:"column:question_title;type:varchar(255);comment:问题标题;" description:"问题标题"`
  54. QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"`
  55. Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"排序"`
  56. SysUserId int `gorm:"column:sys_user_id;type:int(11);comment:添加人id;default:0;" description:"添加人id"`
  57. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(255);comment:添加人真实名称;" description:"添加人真实名称"`
  58. ModifyTime string `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"`
  59. CreateTime string `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"`
  60. }
  61. func (m *Question) ToView() QuestionView {
  62. var modifyTime, createTime string
  63. if !m.CreateTime.IsZero() {
  64. createTime = m.CreateTime.Format(utils.FormatDateTime)
  65. }
  66. if !m.ModifyTime.IsZero() {
  67. modifyTime = m.ModifyTime.Format(utils.FormatDateTime)
  68. }
  69. return QuestionView{
  70. QuestionId: m.QuestionId,
  71. QuestionTitle: m.QuestionTitle,
  72. QuestionContent: m.QuestionContent,
  73. Sort: m.Sort,
  74. SysUserId: m.SysUserId,
  75. SysUserRealName: m.SysUserRealName,
  76. ModifyTime: modifyTime,
  77. CreateTime: createTime,
  78. }
  79. }
  80. func (m *Question) ListToViewList(list []*Question) (wechatArticleViewList []QuestionView) {
  81. wechatArticleViewList = make([]QuestionView, 0)
  82. for _, v := range list {
  83. wechatArticleViewList = append(wechatArticleViewList, v.ToView())
  84. }
  85. return
  86. }
  87. func (m *Question) GetByID(id int) (item *Question, err error) {
  88. err = global.DbMap[utils.DbNameAI].Where(fmt.Sprintf("%s = ?", QuestionColumns.QuestionID), id).First(&item).Error
  89. return
  90. }
  91. func (m *Question) GetByCondition(condition string, pars []interface{}) (item *Question, err error) {
  92. sqlStr := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  93. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).First(&item).Error
  94. return
  95. }
  96. func (m *Question) GetListByCondition(field, condition string, pars []interface{}, startSize, pageSize int) (items []*Question, err error) {
  97. if field == "" {
  98. field = "*"
  99. }
  100. sqlStr := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s order by question_id desc LIMIT ?,?`, field, m.TableName(), condition)
  101. pars = append(pars, startSize, pageSize)
  102. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Find(&items).Error
  103. return
  104. }
  105. func (m *Question) GetCountByCondition(condition string, pars []interface{}) (total int, err error) {
  106. var intNull sql.NullInt64
  107. sqlStr := fmt.Sprintf(`SELECT COUNT(1) total FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  108. err = global.DbMap[utils.DbNameAI].Raw(sqlStr, pars...).Scan(&intNull).Error
  109. if err == nil && intNull.Valid {
  110. total = int(intNull.Int64)
  111. }
  112. return
  113. }
  114. func (m *Question) GetPageListByCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*Question, err error) {
  115. total, err = m.GetCountByCondition(condition, pars)
  116. if err != nil {
  117. return
  118. }
  119. if total > 0 {
  120. items, err = m.GetListByCondition(``, condition, pars, startSize, pageSize)
  121. }
  122. return
  123. }
  124. func (m *Question) GetTitlePageListByCondition(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*Question, err error) {
  125. total, err = m.GetCountByCondition(condition, pars)
  126. if err != nil {
  127. return
  128. }
  129. if total > 0 {
  130. items, err = m.GetListByCondition(`question_id,question_title,sort`, condition, pars, startSize, pageSize)
  131. }
  132. return
  133. }