sa_compare_search_keyword.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package semantic_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. // SaCompareSearchKeyword 文档比对搜索关键词表
  9. type SaCompareSearchKeyword struct {
  10. SaCompareSearchKeywordId int `orm:"column(sa_compare_search_keyword_id);pk"`
  11. CompareId int `description:"文档比对ID"`
  12. Keyword string `description:"关键词"`
  13. CreateTime time.Time `description:"创建时间"`
  14. }
  15. var SaCompareSearchKeywordColumns = struct {
  16. SaCompareSearchKeywordId string
  17. CompareId string
  18. Keyword string
  19. CreateTime string
  20. }{
  21. SaCompareSearchKeywordId: "sa_compare_search_keyword_id",
  22. CompareId: "compare_id",
  23. Keyword: "keyword",
  24. CreateTime: "create_time",
  25. }
  26. func (m *SaCompareSearchKeyword) TableName() string {
  27. return "sa_compare_search_keyword"
  28. }
  29. func (m *SaCompareSearchKeyword) Create() (err error) {
  30. o := orm.NewOrm()
  31. id, err := o.Insert(m)
  32. if err != nil {
  33. return
  34. }
  35. m.SaCompareSearchKeywordId = int(id)
  36. return
  37. }
  38. func (m *SaCompareSearchKeyword) Update(cols []string) (err error) {
  39. o := orm.NewOrm()
  40. _, err = o.Update(m, cols...)
  41. return
  42. }
  43. func (m *SaCompareSearchKeyword) Del() (err error) {
  44. o := orm.NewOrm()
  45. sql := `DELETE FROM sa_compare_search_keyword WHERE sa_compare_search_keyword_id = ? LIMIT 1`
  46. _, err = o.Raw(sql, m.SaCompareSearchKeywordId).Exec()
  47. return
  48. }
  49. func (m *SaCompareSearchKeyword) GetItemById(id int) (err error) {
  50. o := orm.NewOrm()
  51. sql := `SELECT * FROM sa_compare_search_keyword WHERE sa_compare_search_keyword_id = ? LIMIT 1`
  52. err = o.Raw(sql, id).QueryRow(&m)
  53. return
  54. }
  55. func (m *SaCompareSearchKeyword) GetItemByCondition(condition string, pars []interface{}) (err error) {
  56. o := orm.NewOrm()
  57. sql := `SELECT * FROM sa_compare_search_keyword WHERE 1=1 `
  58. sql += condition
  59. sql += ` LIMIT 1`
  60. err = o.Raw(sql, pars).QueryRow(&m)
  61. return
  62. }
  63. func (m *SaCompareSearchKeyword) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  64. o := orm.NewOrm()
  65. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  66. err = o.Raw(sql, pars).QueryRow(&count)
  67. return
  68. }
  69. func (m *SaCompareSearchKeyword) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaCompareSearchKeyword, err error) {
  70. o := orm.NewOrm()
  71. fields := strings.Join(fieldArr, ",")
  72. if len(fieldArr) == 0 {
  73. fields = `*`
  74. }
  75. order := `ORDER BY create_time DESC`
  76. if orderRule != "" {
  77. order = ` ORDER BY ` + orderRule
  78. }
  79. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  80. _, err = o.Raw(sql, pars).QueryRows(&items)
  81. return
  82. }
  83. func (m *SaCompareSearchKeyword) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaCompareSearchKeyword, err error) {
  84. o := orm.NewOrm()
  85. fields := strings.Join(fieldArr, ",")
  86. if len(fieldArr) == 0 {
  87. fields = `*`
  88. }
  89. order := `ORDER BY create_time DESC`
  90. if orderRule != "" {
  91. order = ` ORDER BY ` + orderRule
  92. }
  93. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  94. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  95. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  96. return
  97. }
  98. sql += ` LIMIT ?,?`
  99. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  100. return
  101. }
  102. type SaCompareSearchKeywordSaveReq struct {
  103. SaCompareId int `description:"文档比对ID"`
  104. Keywords []string `description:"关键词"`
  105. }
  106. func (m *SaCompareSearchKeyword) DelAndCreateSearchKeywords(compareId int, keywords []string) (err error) {
  107. o := orm.NewOrm()
  108. tx, err := o.Begin()
  109. if err != nil {
  110. return
  111. }
  112. defer func() {
  113. if err != nil {
  114. _ = tx.Rollback()
  115. return
  116. }
  117. _ = tx.Commit()
  118. }()
  119. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, m.TableName(), SaCompareSearchKeywordColumns.CompareId)
  120. if _, e := o.Raw(sql, compareId).Exec(); e != nil {
  121. return e
  122. }
  123. if len(keywords) > 0 {
  124. items := make([]*SaCompareSearchKeyword, 0)
  125. for _, v := range keywords {
  126. items = append(items, &SaCompareSearchKeyword{
  127. CompareId: compareId,
  128. Keyword: v,
  129. CreateTime: time.Now().Local(),
  130. })
  131. }
  132. if _, e := o.InsertMulti(len(items), items); e != nil {
  133. return e
  134. }
  135. }
  136. return
  137. }