en_permission.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package models
  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 EnPermission struct {
  10. EnPermissionId int `orm:"column(en_permission_id);pk" description:"英文品种权限ID"`
  11. EnPermissionName string `description:"品种权限名称"`
  12. CnPermissionName string `description:"对应的中文权限名称"`
  13. ParentId int `description:"父级ID"`
  14. Sort int `description:"排序"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ModifyTime time.Time `description:"修改时间"`
  17. }
  18. var EnPermissionColumns = struct {
  19. EnPermissionId string
  20. EnPermissionName string
  21. CnPermissionName string
  22. ParentId string
  23. Sort string
  24. CreateTime string
  25. ModifyTime string
  26. }{
  27. EnPermissionId: "en_permission_id",
  28. EnPermissionName: "en_permission_name",
  29. CnPermissionName: "cn_permission_name",
  30. ParentId: "parent_id",
  31. Sort: "sort",
  32. CreateTime: "create_time",
  33. ModifyTime: "modify_time",
  34. }
  35. func (m *EnPermission) TableName() string {
  36. return "en_permission"
  37. }
  38. func (m *EnPermission) Create() (err error) {
  39. o := orm.NewOrmUsingDB("rddp")
  40. id, err := o.Insert(m)
  41. if err != nil {
  42. return
  43. }
  44. m.EnPermissionId = int(id)
  45. return
  46. }
  47. func (m *EnPermission) Update(cols []string) (err error) {
  48. o := orm.NewOrmUsingDB("rddp")
  49. _, err = o.Update(m, cols...)
  50. return
  51. }
  52. func (m *EnPermission) Del() (err error) {
  53. o := orm.NewOrmUsingDB("rddp")
  54. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnPermissionColumns.EnPermissionId)
  55. _, err = o.Raw(sql, m.EnPermissionId).Exec()
  56. return
  57. }
  58. func (m *EnPermission) GetItemById(id int) (item *EnPermission, err error) {
  59. o := orm.NewOrmUsingDB("rddp")
  60. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnPermissionColumns.EnPermissionId)
  61. err = o.Raw(sql, id).QueryRow(&item)
  62. return
  63. }
  64. func (m *EnPermission) GetItemByCondition(condition string, pars []interface{}) (item *EnPermission, err error) {
  65. o := orm.NewOrmUsingDB("rddp")
  66. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 `, m.TableName())
  67. sql += condition
  68. sql += ` LIMIT 1`
  69. err = o.Raw(sql, pars).QueryRow(&item)
  70. return
  71. }
  72. func (m *EnPermission) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  73. o := orm.NewOrmUsingDB("rddp")
  74. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  75. err = o.Raw(sql, pars).QueryRow(&count)
  76. return
  77. }
  78. func (m *EnPermission) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EnPermission, err error) {
  79. o := orm.NewOrmUsingDB("rddp")
  80. fields := strings.Join(fieldArr, ",")
  81. if len(fieldArr) == 0 {
  82. fields = `*`
  83. }
  84. order := `ORDER BY create_time DESC`
  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. _, err = o.Raw(sql, pars).QueryRows(&items)
  90. return
  91. }
  92. func (m *EnPermission) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*EnPermission, err error) {
  93. o := orm.NewOrmUsingDB("rddp")
  94. fields := strings.Join(fieldArr, ",")
  95. if len(fieldArr) == 0 {
  96. fields = `*`
  97. }
  98. order := `ORDER BY create_time DESC`
  99. if orderRule != "" {
  100. order = ` ORDER BY ` + orderRule
  101. }
  102. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  103. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  104. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  105. return
  106. }
  107. sql += ` LIMIT ?,?`
  108. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  109. return
  110. }
  111. type EnPermissionAddReq struct {
  112. EnPermissionName string `description:"品种权限名称"`
  113. CnPermissionName string `description:"对应的中文权限名称"`
  114. ParentId int `description:"父级ID"`
  115. Sort int `description:"排序"`
  116. }
  117. type EnPermissionEditReq struct {
  118. EnPermissionId int `description:"英文品种权限ID"`
  119. EnPermissionAddReq
  120. }
  121. type EnPermissionRemoveReq struct {
  122. EnPermissionId int `description:"英文品种权限ID"`
  123. }
  124. type EnPermissionPageListResp struct {
  125. List []*EnPermissionItem
  126. Paging *paging.PagingItem `description:"分页数据"`
  127. }
  128. type EnPermissionItem struct {
  129. EnPermissionId int `description:"英文品种权限ID"`
  130. EnPermissionName string `description:"品种权限名称"`
  131. CnPermissionName string `description:"对应的中文权限名称"`
  132. ParentId int `description:"父级ID"`
  133. Sort int `description:"排序"`
  134. CreateTime string `description:"创建时间"`
  135. Child []*EnPermissionItem
  136. }
  137. func GetEnPermissionUnionList(condition string, pars []interface{}) (items []*EnPermission, err error) {
  138. o := orm.NewOrmUsingDB("rddp")
  139. sql := fmt.Sprintf(`
  140. SELECT
  141. *
  142. FROM
  143. (
  144. SELECT * FROM en_permission WHERE 1=1 %s
  145. UNION
  146. SELECT * FROM en_permission WHERE en_permission_id IN (
  147. SELECT parent_id FROM en_permission WHERE parent_id > 0 %s
  148. )
  149. ) AS t
  150. ORDER BY
  151. parent_id ASC,
  152. sort ASC,
  153. create_time ASC`, condition, condition)
  154. _, err = o.Raw(sql, pars, pars).QueryRows(&items)
  155. return
  156. }
  157. func ClearEnPermissionsByPermissionId(permissionId int) (err error) {
  158. o := orm.NewOrmUsingDB("rddp")
  159. tx, e := o.Begin()
  160. if e != nil {
  161. err = fmt.Errorf("orm begin err: %s", e.Error())
  162. return
  163. }
  164. defer func() {
  165. if err != nil {
  166. _ = tx.Rollback()
  167. return
  168. }
  169. _ = tx.Commit()
  170. }()
  171. sql := `DELETE FROM en_classify_permission WHERE en_permission_id = ?`
  172. _, e = tx.Raw(sql, permissionId).Exec()
  173. if e != nil {
  174. err = fmt.Errorf("delete en_classify_permission err: %s", e.Error())
  175. return
  176. }
  177. sql = `DELETE FROM en_company_permission WHERE en_permission_id = ?`
  178. _, e = tx.Raw(sql, permissionId).Exec()
  179. if e != nil {
  180. err = fmt.Errorf("delete en_company_permission err: %s", e.Error())
  181. return
  182. }
  183. return
  184. }