en_permission.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. Enabled int `description:"是否可用,1可用,0禁用"`
  18. }
  19. var EnPermissionColumns = struct {
  20. EnPermissionId string
  21. EnPermissionName string
  22. CnPermissionName string
  23. ParentId string
  24. Sort string
  25. CreateTime string
  26. ModifyTime string
  27. Enabled string
  28. }{
  29. EnPermissionId: "en_permission_id",
  30. EnPermissionName: "en_permission_name",
  31. CnPermissionName: "cn_permission_name",
  32. ParentId: "parent_id",
  33. Sort: "sort",
  34. CreateTime: "create_time",
  35. ModifyTime: "modify_time",
  36. Enabled: "enabled",
  37. }
  38. func (m *EnPermission) TableName() string {
  39. return "en_permission"
  40. }
  41. func (m *EnPermission) Create() (err error) {
  42. o := orm.NewOrmUsingDB("rddp")
  43. id, err := o.Insert(m)
  44. if err != nil {
  45. return
  46. }
  47. m.EnPermissionId = int(id)
  48. return
  49. }
  50. func (m *EnPermission) Update(cols []string) (err error) {
  51. o := orm.NewOrmUsingDB("rddp")
  52. _, err = o.Update(m, cols...)
  53. return
  54. }
  55. func (m *EnPermission) Del() (err error) {
  56. o := orm.NewOrmUsingDB("rddp")
  57. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnPermissionColumns.EnPermissionId)
  58. _, err = o.Raw(sql, m.EnPermissionId).Exec()
  59. return
  60. }
  61. func (m *EnPermission) GetItemById(id int) (item *EnPermission, err error) {
  62. o := orm.NewOrmUsingDB("rddp")
  63. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), EnPermissionColumns.EnPermissionId)
  64. err = o.Raw(sql, id).QueryRow(&item)
  65. return
  66. }
  67. func (m *EnPermission) GetItemByCondition(condition string, pars []interface{}) (item *EnPermission, err error) {
  68. o := orm.NewOrmUsingDB("rddp")
  69. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 `, m.TableName())
  70. sql += condition
  71. sql += ` LIMIT 1`
  72. err = o.Raw(sql, pars).QueryRow(&item)
  73. return
  74. }
  75. func (m *EnPermission) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  76. o := orm.NewOrmUsingDB("rddp")
  77. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  78. err = o.Raw(sql, pars).QueryRow(&count)
  79. return
  80. }
  81. func (m *EnPermission) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EnPermission, err error) {
  82. o := orm.NewOrmUsingDB("rddp")
  83. fields := strings.Join(fieldArr, ",")
  84. if len(fieldArr) == 0 {
  85. fields = `*`
  86. }
  87. order := `ORDER BY create_time DESC`
  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. _, err = o.Raw(sql, pars).QueryRows(&items)
  93. return
  94. }
  95. func (m *EnPermission) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*EnPermission, err error) {
  96. o := orm.NewOrmUsingDB("rddp")
  97. fields := strings.Join(fieldArr, ",")
  98. if len(fieldArr) == 0 {
  99. fields = `*`
  100. }
  101. order := `ORDER BY create_time DESC`
  102. if orderRule != "" {
  103. order = ` ORDER BY ` + orderRule
  104. }
  105. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  106. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  107. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  108. return
  109. }
  110. sql += ` LIMIT ?,?`
  111. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  112. return
  113. }
  114. type EnPermissionAddReq struct {
  115. PermissionName string `description:"品种权限名称"`
  116. CnPermissionName string `description:"对应的中文权限名称"`
  117. ParentId int `description:"父级ID"`
  118. Enabled int `description:"是否可用,1可用,0禁用"`
  119. }
  120. type EnPermissionEditReq struct {
  121. PermissionId int `description:"英文品种权限ID"`
  122. PermissionName string `description:"品种权限名称"`
  123. CnPermissionName string `description:"对应的中文权限名称"`
  124. ParentId int `description:"父级ID"`
  125. }
  126. type EnPermissionEnabledReq struct {
  127. PermissionId int `description:"英文品种权限ID"`
  128. Enabled int `description:"是否可用,1可用,0禁用"`
  129. }
  130. type EnPermissionRemoveReq struct {
  131. PermissionId int `description:"英文品种权限ID"`
  132. }
  133. type EnPermissionPageListResp struct {
  134. List []*EnPermissionItem
  135. Paging *paging.PagingItem `description:"分页数据"`
  136. }
  137. type EnPermissionItem struct {
  138. PermissionId int `description:"英文品种权限ID"`
  139. PermissionName string `description:"品种权限名称"`
  140. CnPermissionName string `description:"对应的中文权限名称"`
  141. ParentId int `description:"父级ID"`
  142. Sort int `description:"排序"`
  143. CreateTime string `description:"创建时间"`
  144. Child []*EnPermissionItem
  145. Enabled int `description:"是否可用,1可用,0禁用"`
  146. }
  147. type EnPermissionMoveReq struct {
  148. PermissionId int `description:"品种id"`
  149. PrevPermissionId int `description:"上一个兄弟节点品种id"`
  150. NextPermissionId int `description:"下一个兄弟节点品种id"`
  151. }
  152. func GetEnPermissionUnionList(condition string, pars []interface{}) (items []*EnPermission, err error) {
  153. o := orm.NewOrmUsingDB("rddp")
  154. sql := fmt.Sprintf(`
  155. SELECT
  156. *
  157. FROM
  158. (
  159. SELECT * FROM en_permission WHERE 1=1 %s
  160. UNION
  161. SELECT * FROM en_permission WHERE en_permission_id IN (
  162. SELECT parent_id FROM en_permission WHERE parent_id > 0 %s
  163. )
  164. ) AS t
  165. ORDER BY
  166. parent_id ASC,
  167. sort ASC,
  168. create_time ASC`, condition, condition)
  169. _, err = o.Raw(sql, pars, pars).QueryRows(&items)
  170. return
  171. }
  172. func ClearEnPermissionsByPermissionId(permissionId int) (err error) {
  173. o := orm.NewOrmUsingDB("rddp")
  174. tx, e := o.Begin()
  175. if e != nil {
  176. err = fmt.Errorf("orm begin err: %s", e.Error())
  177. return
  178. }
  179. defer func() {
  180. if err != nil {
  181. _ = tx.Rollback()
  182. return
  183. }
  184. _ = tx.Commit()
  185. }()
  186. sql := `DELETE FROM en_classify_permission WHERE en_permission_id = ?`
  187. _, e = tx.Raw(sql, permissionId).Exec()
  188. if e != nil {
  189. err = fmt.Errorf("delete en_classify_permission err: %s", e.Error())
  190. return
  191. }
  192. sql = `DELETE FROM en_company_permission WHERE en_permission_id = ?`
  193. _, e = tx.Raw(sql, permissionId).Exec()
  194. if e != nil {
  195. err = fmt.Errorf("delete en_company_permission err: %s", e.Error())
  196. return
  197. }
  198. return
  199. }
  200. // UpdateEnPermissionSortByParentId 根据父类id更新排序
  201. func UpdateEnPermissionSortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
  202. o := orm.NewOrmUsingDB("rddp")
  203. sql := ` update en_permission set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  204. if permissionId > 0 {
  205. sql += ` or ( en_permission_id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  206. }
  207. _, err = o.Raw(sql, parentId, nowSort).Exec()
  208. return
  209. }
  210. // GetMaxSortByParentId 获取最大的排序值
  211. func (m *EnPermission) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  212. o := orm.NewOrmUsingDB("rddp")
  213. sql := fmt.Sprintf(`SELECT max(%s) AS sort FROM %s WHERE %s = ? `, EnPermissionColumns.Sort, m.TableName(), EnPermissionColumns.ParentId)
  214. err = o.Raw(sql, parentId).QueryRow(&maxSort)
  215. return
  216. }
  217. // GetMaxSort 获取最大的排序值
  218. func (m *EnPermission) GetMaxSort() (maxSort int, err error) {
  219. o := orm.NewOrmUsingDB("rddp")
  220. sql := fmt.Sprintf(`SELECT max(%s) AS sort FROM %s`, EnPermissionColumns.Sort, m.TableName())
  221. err = o.Raw(sql).QueryRow(&maxSort)
  222. return
  223. }
  224. // GetFirstEnPermissionByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  225. func (m *EnPermission) GetFirstEnPermissionByParentId(parentId int) (item *EnPermission, err error) {
  226. o := orm.NewOrmUsingDB("rddp")
  227. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? order by sort asc, en_permission_id asc limit 1`, m.TableName(), EnPermissionColumns.ParentId)
  228. err = o.Raw(sql, parentId).QueryRow(&item)
  229. return
  230. }
  231. // SetEnabled 更新启动禁用
  232. func (m *EnPermission) SetEnabled(parentId, enabled int) (err error) {
  233. o := orm.NewOrmUsingDB("rddp")
  234. sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ?`, m.TableName(), EnPermissionColumns.Enabled, EnPermissionColumns.ParentId)
  235. _, err = o.Raw(sql, enabled, parentId).Exec()
  236. return
  237. }
  238. // SetEnabledByPermissionId 更新启动禁用
  239. func (m *EnPermission) SetEnabledByPermissionId(permissionId, enabled int) (err error) {
  240. o := orm.NewOrmUsingDB("rddp")
  241. sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ?`, m.TableName(), EnPermissionColumns.Enabled, EnPermissionColumns.EnPermissionId)
  242. _, err = o.Raw(sql, enabled, permissionId).Exec()
  243. return
  244. }