sa_compare_doc.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package semantic_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. )
  7. type SaCompareDoc struct {
  8. SaCompareDocId int `orm:"column(sa_compare_doc_id);pk"`
  9. CompareId int `description:"比对ID"`
  10. DocId int `description:"文档ID"`
  11. }
  12. var SaCompareDocColumns = struct {
  13. SaCompareDocId string
  14. CompareId string
  15. DocId string
  16. }{
  17. SaCompareDocId: "sa_compare_doc_id",
  18. CompareId: "compare_id",
  19. DocId: "doc_id",
  20. }
  21. func (m *SaCompareDoc) TableName() string {
  22. return "sa_compare_doc"
  23. }
  24. func (m *SaCompareDoc) Create() (err error) {
  25. o := orm.NewOrm()
  26. id, err := o.Insert(m)
  27. if err != nil {
  28. return
  29. }
  30. m.SaCompareDocId = int(id)
  31. return
  32. }
  33. func (m *SaCompareDoc) Update(cols []string) (err error) {
  34. o := orm.NewOrm()
  35. _, err = o.Update(m, cols...)
  36. return
  37. }
  38. func (m *SaCompareDoc) Del() (err error) {
  39. o := orm.NewOrm()
  40. sql := `DELETE FROM sa_compare_doc WHERE sa_compare_doc_id = ? LIMIT 1`
  41. _, err = o.Raw(sql, m.SaCompareDocId).Exec()
  42. return
  43. }
  44. func (m *SaCompareDoc) GetItemById(id int) (err error) {
  45. o := orm.NewOrm()
  46. sql := `SELECT * FROM sa_compare_doc WHERE sa_compare_doc_id = ? LIMIT 1`
  47. err = o.Raw(sql, id).QueryRow(&m)
  48. return
  49. }
  50. func (m *SaCompareDoc) GetItemByCondition(condition string, pars []interface{}) (err error) {
  51. o := orm.NewOrm()
  52. sql := `SELECT * FROM sa_compare_doc WHERE 1=1 `
  53. sql += condition
  54. sql += ` LIMIT 1`
  55. err = o.Raw(sql, pars).QueryRow(&m)
  56. return
  57. }
  58. func (m *SaCompareDoc) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  59. o := orm.NewOrm()
  60. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  61. err = o.Raw(sql, pars).QueryRow(&count)
  62. return
  63. }
  64. func (m *SaCompareDoc) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaCompareDoc, err error) {
  65. o := orm.NewOrm()
  66. fields := strings.Join(fieldArr, ",")
  67. if len(fieldArr) == 0 {
  68. fields = `*`
  69. }
  70. order := ``
  71. if orderRule != "" {
  72. order = ` ORDER BY ` + orderRule
  73. }
  74. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  75. _, err = o.Raw(sql, pars).QueryRows(&items)
  76. return
  77. }
  78. func (m *SaCompareDoc) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaCompareDoc, err error) {
  79. o := orm.NewOrm()
  80. fields := strings.Join(fieldArr, ",")
  81. if len(fieldArr) == 0 {
  82. fields = `*`
  83. }
  84. order := ``
  85. if orderRule != "" {
  86. order = ` ORDER BY ` + orderRule
  87. }
  88. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  89. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  90. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  91. return
  92. }
  93. sql += ` LIMIT ?,?`
  94. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  95. return
  96. }
  97. // SaCompareDocSection 比对文档段落信息
  98. type SaCompareDocSection struct {
  99. DocId int `description:"文档ID"`
  100. Title string `description:"文档标题"`
  101. Theme string `description:"文档主题"`
  102. ClassifyName string `description:"分类名称"`
  103. SectionId int `description:"段落ID"`
  104. Content string `description:"段落内容"`
  105. Sort int `description:"段落排序"`
  106. }
  107. // GetSaCompareDocAndSections 获取比对关联的文档段落
  108. func GetSaCompareDocAndSections(compareId int) (items []*SaCompareDocSection, err error) {
  109. o := orm.NewOrm()
  110. sql := `SELECT
  111. a.doc_id,
  112. b.title,
  113. b.theme,
  114. b.classify_name,
  115. c.sa_doc_section_id AS section_id,
  116. c.content,
  117. c.sort
  118. FROM
  119. sa_compare_doc AS a
  120. JOIN sa_doc AS b ON a.doc_id = b.sa_doc_id
  121. JOIN sa_doc_section AS c ON a.doc_id = c.doc_id
  122. WHERE
  123. a.compare_id = ?
  124. ORDER BY
  125. a.doc_id,
  126. c.sort ASC`
  127. _, err = o.Raw(sql, compareId).QueryRows(&items)
  128. return
  129. }