sa_doc_section.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package semantic_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. )
  7. type SaDocSection struct {
  8. SaDocSectionId int `orm:"column(sa_doc_section_id);pk" description:"段落ID"`
  9. DocId int `description:"文档ID"`
  10. Content string `description:"段落内容"`
  11. Sort int `description:"排序"`
  12. }
  13. var SaDocSectionColumns = struct {
  14. SaDocSectionId string
  15. DocId string
  16. Content string
  17. Sort string
  18. }{
  19. SaDocSectionId: "sa_doc_section_id",
  20. DocId: "doc_id",
  21. Content: "content",
  22. Sort: "sort",
  23. }
  24. func (m *SaDocSection) TableName() string {
  25. return "sa_doc_section"
  26. }
  27. func (m *SaDocSection) Create() (err error) {
  28. o := orm.NewOrm()
  29. id, err := o.Insert(m)
  30. if err != nil {
  31. return
  32. }
  33. m.SaDocSectionId = int(id)
  34. return
  35. }
  36. func (m *SaDocSection) Update(cols []string) (err error) {
  37. o := orm.NewOrm()
  38. _, err = o.Update(m, cols...)
  39. return
  40. }
  41. func (m *SaDocSection) Del() (err error) {
  42. o := orm.NewOrm()
  43. sql := `DELETE FROM sa_doc_section WHERE sa_doc_section_id = ? LIMIT 1`
  44. _, err = o.Raw(sql, m.SaDocSectionId).Exec()
  45. return
  46. }
  47. func (m *SaDocSection) GetItemById(id int) (err error) {
  48. o := orm.NewOrm()
  49. sql := `SELECT * FROM sa_doc_section WHERE sa_doc_section_id = ? LIMIT 1`
  50. err = o.Raw(sql, id).QueryRow(&m)
  51. return
  52. }
  53. func (m *SaDocSection) GetItemByCondition(condition string, pars []interface{}) (err error) {
  54. o := orm.NewOrm()
  55. sql := `SELECT * FROM sa_doc_section WHERE 1=1 `
  56. sql += condition
  57. sql += ` LIMIT 1`
  58. err = o.Raw(sql, pars).QueryRow(&m)
  59. return
  60. }
  61. func (m *SaDocSection) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  62. o := orm.NewOrm()
  63. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  64. err = o.Raw(sql, pars).QueryRow(&count)
  65. return
  66. }
  67. func (m *SaDocSection) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaDocSection, err error) {
  68. o := orm.NewOrm()
  69. fields := strings.Join(fieldArr, ",")
  70. if len(fieldArr) == 0 {
  71. fields = `*`
  72. }
  73. order := `ORDER BY sort ASC`
  74. if orderRule != "" {
  75. order = ` ORDER BY ` + orderRule
  76. }
  77. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  78. _, err = o.Raw(sql, pars).QueryRows(&items)
  79. return
  80. }
  81. func (m *SaDocSection) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaDocSection, err error) {
  82. o := orm.NewOrm()
  83. fields := strings.Join(fieldArr, ",")
  84. if len(fieldArr) == 0 {
  85. fields = `*`
  86. }
  87. order := `ORDER BY sort ASC`
  88. if orderRule != "" {
  89. order = ` ORDER BY ` + orderRule
  90. }
  91. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  92. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  93. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  94. return
  95. }
  96. sql += ` LIMIT ?,?`
  97. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  98. return
  99. }
  100. type SaDocSectionItem struct {
  101. SaDocSection
  102. UseNum int `description:"整段引用数量"`
  103. UsePartNum int `description:"片段引用数量"`
  104. }