sa_compare_search_keyword.go 4.5 KB

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