en_classify_permission.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package models
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. type EnClassifyPermission struct {
  10. EnClassifyPermissionId int `orm:"column(en_classify_permission_id);pk" description:"英文品种权限ID"`
  11. EnClassifyId int `description:"英文分类ID"`
  12. EnPermissionId int `description:"英文品种ID"`
  13. CreateTime time.Time `description:"创建时间"`
  14. }
  15. var EnClassifyPermissionColumns = struct {
  16. EnClassifyPermissionId string
  17. EnClassifyId string
  18. EnPermissionId string
  19. CreateTime string
  20. }{
  21. EnClassifyPermissionId: "en_classify_permission_id",
  22. EnClassifyId: "en_classify_id",
  23. EnPermissionId: "en_permission_id",
  24. CreateTime: "create_time",
  25. }
  26. func (m *EnClassifyPermission) TableName() string {
  27. return "en_classify_permission"
  28. }
  29. func (m *EnClassifyPermission) Create() (err error) {
  30. o := orm.NewOrmUsingDB("rddp")
  31. id, err := o.Insert(m)
  32. if err != nil {
  33. return
  34. }
  35. m.EnClassifyPermissionId = int(id)
  36. return
  37. }
  38. func (m *EnClassifyPermission) Update(cols []string) (err error) {
  39. o := orm.NewOrmUsingDB("rddp")
  40. _, err = o.Update(m, cols...)
  41. return
  42. }
  43. func (m *EnClassifyPermission) Del() (err error) {
  44. o := orm.NewOrmUsingDB("rddp")
  45. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnClassifyPermissionColumns.EnClassifyPermissionId)
  46. _, err = o.Raw(sql, m.EnClassifyPermissionId).Exec()
  47. return
  48. }
  49. func (m *EnClassifyPermission) GetItemById(id int) (item *EnClassifyPermission, err error) {
  50. o := orm.NewOrmUsingDB("rddp")
  51. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnClassifyPermissionColumns.EnClassifyPermissionId)
  52. err = o.Raw(sql, id).QueryRow(&item)
  53. return
  54. }
  55. func (m *EnClassifyPermission) GetItemByCondition(condition string, pars []interface{}) (item *EnClassifyPermission, err error) {
  56. o := orm.NewOrmUsingDB("rddp")
  57. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 `, m.TableName())
  58. sql += condition
  59. sql += ` LIMIT 1`
  60. err = o.Raw(sql, pars).QueryRow(&item)
  61. return
  62. }
  63. func (m *EnClassifyPermission) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  64. o := orm.NewOrmUsingDB("rddp")
  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 *EnClassifyPermission) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EnClassifyPermission, err error) {
  70. o := orm.NewOrmUsingDB("rddp")
  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 *EnClassifyPermission) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*EnClassifyPermission, err error) {
  84. o := orm.NewOrmUsingDB("rddp")
  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. func ClearAndCreateEnClassifyPermissions(classifyId int, permissions []*EnClassifyPermission) (err error) {
  103. o := orm.NewOrmUsingDB("rddp")
  104. sql := `DELETE FROM en_classify_permission WHERE en_classify_id = ?`
  105. if _, e := o.Raw(sql, classifyId).Exec(); e != nil {
  106. err = fmt.Errorf("delete err: %s", e.Error())
  107. return
  108. }
  109. if len(permissions) > 0 {
  110. if _, e := o.InsertMulti(len(permissions), permissions); e != nil {
  111. err = fmt.Errorf("insert multi err: %s", e.Error())
  112. return
  113. }
  114. }
  115. return
  116. }
  117. type EnClassifyPermissionEditReq struct {
  118. ClassifyId int `description:"分类ID"`
  119. EnPermissions []int `description:"权限IDs"`
  120. }
  121. // GetEnglishReportIdsByEnPermissionIds 根据权限IDs获取报告IDs
  122. func GetEnglishReportIdsByEnPermissionIds(permissionIds []int) (reportIds []int, err error) {
  123. if len(permissionIds) == 0 {
  124. return
  125. }
  126. o := orm.NewOrmUsingDB("rddp")
  127. sql := `SELECT id FROM english_report WHERE classify_id_second IN (
  128. SELECT DISTINCT en_classify_id FROM en_classify_permission WHERE en_permission_id IN (` + utils.GetOrmInReplace(len(permissionIds)) + `)
  129. )`
  130. _, err = o.Raw(sql, permissionIds).QueryRows(&reportIds)
  131. return
  132. }
  133. // GetEnPermissionIdsByEnglishReportId 获取英文报告权限IDs
  134. func GetEnPermissionIdsByEnglishReportId(reportId int) (permissionIds []int, err error) {
  135. o := orm.NewOrmUsingDB("rddp")
  136. sql := `SELECT en_permission_id FROM en_classify_permission WHERE en_classify_id = (
  137. SELECT classify_id_second FROM english_report WHERE id = ?
  138. )`
  139. _, err = o.Raw(sql, reportId).QueryRows(&permissionIds)
  140. return
  141. }
  142. // GetEnPermissionIdsByEnglishVideoId 获取英文视频权限IDs
  143. func GetEnPermissionIdsByEnglishVideoId(videoId int) (permissionIds []int, err error) {
  144. o := orm.NewOrmUsingDB("rddp")
  145. sql := `SELECT en_permission_id FROM en_classify_permission WHERE en_classify_id = (
  146. SELECT classify_id_second FROM english_video WHERE id = ?
  147. )`
  148. _, err = o.Raw(sql, videoId).QueryRows(&permissionIds)
  149. return
  150. }