pdf.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package yb
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "strings"
  7. "time"
  8. )
  9. type Pdf struct {
  10. PdfId int `orm:"column(pdf_id);pk"`
  11. PdfName string `description:"PDF名称"`
  12. PdfUrl string `description:"PDF地址"`
  13. OriginName string `description:"文件原始名称"`
  14. ShareUrl string `description:"分享地址"`
  15. IsDeleted int `description:"删除状态: 0-正常; 1-已删除"`
  16. SysAdminId int `description:"创建人ID"`
  17. SysAdminName string `description:"创建人名称"`
  18. CreateTime time.Time `description:"创建时间"`
  19. ModifyTime time.Time `description:"修改时间"`
  20. }
  21. var PdfColumns = struct {
  22. PdfId string
  23. PdfName string
  24. OriginName string
  25. PdfUrl string
  26. ShareUrl string
  27. IsDeleted string
  28. SysAdminId string
  29. SysAdminName string
  30. CreateTime string
  31. ModifyTime string
  32. }{
  33. PdfId: "pdf_id",
  34. PdfName: "pdf_name",
  35. OriginName: "origin_name",
  36. PdfUrl: "pdf_url",
  37. ShareUrl: "share_url",
  38. IsDeleted: "is_deleted",
  39. SysAdminId: "sys_admin_id",
  40. SysAdminName: "sys_admin_name",
  41. CreateTime: "create_time",
  42. ModifyTime: "modify_time",
  43. }
  44. func (m *Pdf) TableName() string {
  45. return "yb_pdf"
  46. }
  47. func (m *Pdf) Create() (err error) {
  48. o := orm.NewOrm()
  49. id, err := o.Insert(m)
  50. if err != nil {
  51. return
  52. }
  53. m.PdfId = int(id)
  54. return
  55. }
  56. func (m *Pdf) Update(cols []string) (err error) {
  57. o := orm.NewOrm()
  58. _, err = o.Update(m, cols...)
  59. return
  60. }
  61. func (m *Pdf) Del() (err error) {
  62. o := orm.NewOrm()
  63. sql := fmt.Sprintf(`UPDATE %s SET is_deleted = 1 WHERE pdf_id = ? LIMIT 1`, m.TableName())
  64. _, err = o.Raw(sql, m.PdfId).Exec()
  65. return
  66. }
  67. func (m *Pdf) GetItemById(id int) (err error) {
  68. o := orm.NewOrm()
  69. sql := fmt.Sprintf(`SELECT * FROM %s WHERE pdf_id = ? AND is_deleted = 0 LIMIT 1`, m.TableName())
  70. err = o.Raw(sql, id).QueryRow(&m)
  71. return
  72. }
  73. func (m *Pdf) GetItemByCondition(condition string, pars []interface{}) (err error) {
  74. o := orm.NewOrm()
  75. sql := fmt.Sprintf(`SELECT * FROM %s WHERE is_deleted = 0 `, m.TableName())
  76. sql += condition
  77. sql += ` LIMIT 1`
  78. err = o.Raw(sql, pars).QueryRow(&m)
  79. return
  80. }
  81. func (m *Pdf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  82. o := orm.NewOrm()
  83. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE is_deleted = 0 %s`, m.TableName(), condition)
  84. err = o.Raw(sql, pars).QueryRow(&count)
  85. return
  86. }
  87. func (m *Pdf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Pdf, err error) {
  88. o := orm.NewOrm()
  89. fields := strings.Join(fieldArr, ",")
  90. if len(fieldArr) == 0 {
  91. fields = `*`
  92. }
  93. order := `ORDER BY create_time DESC`
  94. if orderRule != "" {
  95. order = ` ORDER BY ` + orderRule
  96. }
  97. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE is_deleted = 0 %s %s`, fields, m.TableName(), condition, order)
  98. _, err = o.Raw(sql, pars).QueryRows(&items)
  99. return
  100. }
  101. func (m *Pdf) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*Pdf, err error) {
  102. o := orm.NewOrm()
  103. fields := strings.Join(fieldArr, ",")
  104. if len(fieldArr) == 0 {
  105. fields = `*`
  106. }
  107. order := `ORDER BY create_time DESC`
  108. if orderRule != "" {
  109. order = ` ORDER BY ` + orderRule
  110. }
  111. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE is_deleted = 0 %s %s`, fields, m.TableName(), condition, order)
  112. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  113. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  114. return
  115. }
  116. sql += ` LIMIT ?,?`
  117. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  118. return
  119. }
  120. type PdfAddReq struct {
  121. PdfName string `description:"PDF名称"`
  122. PdfUrl string `description:"PDF地址"`
  123. OriginName string `description:"文件原始名称"`
  124. }
  125. type PdfEditReq struct {
  126. PdfId int
  127. PdfAddReq
  128. }
  129. type PdfDelReq struct {
  130. PdfId int
  131. }
  132. type PdfPageListResp struct {
  133. List []*PdfItem
  134. Paging *paging.PagingItem `description:"分页数据"`
  135. }
  136. type PdfItem struct {
  137. PdfId int
  138. PdfName string `description:"PDF名称"`
  139. OriginName string `description:"文件原始名称"`
  140. PdfUrl string `description:"PDF地址"`
  141. ShareUrl string `description:"分享地址"`
  142. SysAdminId int `description:"创建人ID"`
  143. SysAdminName string `description:"创建人名称"`
  144. CreateTime string `description:"创建时间"`
  145. ModifyTime string `description:"修改时间"`
  146. }