en_permission.go 10 KB

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