authority.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package dao
  2. import (
  3. "strings"
  4. "time"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/ecode"
  7. xtime "go-common/library/time"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // const for db table name
  11. const (
  12. // table user
  13. TableUser = "authority_user"
  14. // table taskgroup
  15. TableGroup = "authority_task_group"
  16. // table taskrole
  17. TableRole = "authority_task_role"
  18. // table privilege
  19. TablePrivilege = "authority_privilege"
  20. )
  21. func queryAddIsDeleted(query string) string {
  22. if len(query) != 0 {
  23. query += " AND is_deleted = 0"
  24. } else {
  25. query = "is_deleted = 0"
  26. }
  27. return query
  28. }
  29. // GetAuthorityUsersInfo get users info by query
  30. func (d *Dao) GetAuthorityUsersInfo(query, members string) (users []*model.User, err error) {
  31. query = queryAddIsDeleted(query)
  32. err = d.db.Table(TableUser).Select(members).Where(query).Find(&users).Error
  33. return
  34. }
  35. // ListAuthorityUsers find all authority users from db
  36. func (d *Dao) ListAuthorityUsers(query string, from, limit int, sort string) (users []*model.User, total int, err error) {
  37. query = queryAddIsDeleted(query)
  38. err = d.db.Table(TableUser).Where(query).Count(&total).Error
  39. if err != nil {
  40. return
  41. }
  42. if strings.HasPrefix(sort, "-") {
  43. sort = strings.TrimPrefix(sort, "-")
  44. sort = sort + " " + "desc"
  45. }
  46. err = d.db.Table(TableUser).Order(sort).Offset(from).Where(query).Limit(limit).Find(&users).Error
  47. return
  48. }
  49. // AddAuthorityUser add one user to authority-manage
  50. func (d *Dao) AddAuthorityUser(user *model.User) (err error) {
  51. var u model.User
  52. err = d.db.Table(TableUser).Where("username = ?", user.Username).Find(&u).Error
  53. if err == gorm.ErrRecordNotFound {
  54. err = d.db.Table(TableUser).Create(user).Error
  55. return
  56. }
  57. if err != nil {
  58. return
  59. }
  60. if u.IsDeleted == 1 {
  61. update := map[string]interface{}{
  62. "nickname": user.Nickname,
  63. "atime": xtime.Time(time.Now().Unix()),
  64. "is_deleted": 0,
  65. }
  66. err = d.db.Table(TableUser).Where("username = ?", user.Username).Updates(update).Error
  67. } else {
  68. err = ecode.GrowupAuthorityExist
  69. }
  70. return
  71. }
  72. // UpdateAuthorityUser update user to db
  73. func (d *Dao) UpdateAuthorityUser(id int64, update map[string]interface{}) (err error) {
  74. return d.db.Table(TableUser).Where("id = ? AND is_deleted = 0", id).Updates(update).Error
  75. }
  76. // DeleteAuthorityUser modify user's is_delete = 1
  77. func (d *Dao) DeleteAuthorityUser(id int64) (err error) {
  78. update := map[string]interface{}{
  79. "task_group": "",
  80. "task_role": "",
  81. "is_deleted": 1,
  82. }
  83. return d.db.Table(TableUser).Where("id = ? AND is_deleted = 0", id).Updates(update).Error
  84. }
  85. // ListAuthorityTaskGroups find all authority task groups from db
  86. func (d *Dao) ListAuthorityTaskGroups(query string, from, limit int, sort string) (groups []*model.TaskGroup, total int, err error) {
  87. query = queryAddIsDeleted(query)
  88. err = d.db.Table(TableGroup).Where(query).Count(&total).Error
  89. if err != nil {
  90. return
  91. }
  92. if strings.HasPrefix(sort, "-") {
  93. sort = strings.TrimPrefix(sort, "-")
  94. sort = sort + " " + "desc"
  95. }
  96. err = d.db.Table(TableGroup).Order(sort).Offset(from).Where(query).Limit(limit).Find(&groups).Error
  97. return
  98. }
  99. // GetAuthorityTaskGroup get authority task group
  100. func (d *Dao) GetAuthorityTaskGroup(query string) (group model.TaskGroup, err error) {
  101. query = queryAddIsDeleted(query)
  102. err = d.db.Table(TableGroup).Where(query).Find(&group).Error
  103. return
  104. }
  105. // AddAuthorityTaskGroup add new task group
  106. func (d *Dao) AddAuthorityTaskGroup(taskGroup *model.TaskGroup) (err error) {
  107. var group model.TaskGroup
  108. err = d.db.Table(TableGroup).Where("name = ?", taskGroup.Name).Find(&group).Error
  109. if err == gorm.ErrRecordNotFound {
  110. err = d.db.Table(TableGroup).Create(taskGroup).Error
  111. return
  112. }
  113. if err != nil {
  114. return
  115. }
  116. if group.IsDeleted == 1 {
  117. update := map[string]interface{}{
  118. "desc": taskGroup.Desc,
  119. "atime": xtime.Time(time.Now().Unix()),
  120. "is_deleted": 0,
  121. }
  122. err = d.db.Table(TableGroup).Where("name = ?", taskGroup.Name).Updates(update).Error
  123. } else {
  124. return ecode.GrowupAuthorityExist
  125. }
  126. return
  127. }
  128. // UpdateAuthorityTaskGroup update task group info to db
  129. func (d *Dao) UpdateAuthorityTaskGroup(id int64, update map[string]interface{}) (err error) {
  130. return d.db.Table(TableGroup).Where("id = ? AND is_deleted = 0", id).Updates(update).Error
  131. }
  132. // DeleteAuthorityTaskGroup modify task group's is_deleted = 1
  133. func (d *Dao) DeleteAuthorityTaskGroup(id int64) (err error) {
  134. return d.db.Table(TableGroup).Where("id = ? AND is_deleted = 0", id).Updates(map[string]interface{}{"is_deleted": 1}).Error
  135. }
  136. // GetAuthorityTaskGroups get task groups id and name
  137. func (d *Dao) GetAuthorityTaskGroups(query string) (groups []*model.Group, err error) {
  138. query = queryAddIsDeleted(query)
  139. err = d.db.Table(TableGroup).Where(query).Scan(&groups).Error
  140. return
  141. }
  142. // GetAuthorityTaskGroupName get task group name
  143. func (d *Dao) GetAuthorityTaskGroupName(groupID int64) (name string, err error) {
  144. var group model.Group
  145. err = d.db.Table(TableGroup).Where("id = ? AND is_deleted = 0", groupID).Scan(&group).Error
  146. if err != nil {
  147. return
  148. }
  149. name = group.Name
  150. return
  151. }
  152. // GetAuthorityTaskGroupPrivileges get task group privileges
  153. func (d *Dao) GetAuthorityTaskGroupPrivileges(groupID int64) (privileges string, err error) {
  154. var group model.TaskGroup
  155. err = d.db.Table(TableGroup).Where("id = ? AND is_deleted = 0", groupID).Scan(&group).Error
  156. if err != nil {
  157. return
  158. }
  159. privileges = group.Privileges
  160. return
  161. }
  162. // GetAuthorityTaskGroupNames get task group names from ids(not use)
  163. func (d *Dao) GetAuthorityTaskGroupNames(groupIDs []string) (names []string, err error) {
  164. err = d.db.Table(TableGroup).Where("id in (?) AND is_deleted = 0", groupIDs).Pluck("name", &names).Error
  165. return
  166. }
  167. // ListAuthorityTaskRoles find all authority task roles from db
  168. func (d *Dao) ListAuthorityTaskRoles(query string, from, limit int, sort string) (roles []*model.TaskRole, total int, err error) {
  169. query = queryAddIsDeleted(query)
  170. err = d.db.Table(TableRole).Where(query).Count(&total).Error
  171. if err != nil {
  172. return
  173. }
  174. if strings.HasPrefix(sort, "-") {
  175. sort = strings.TrimPrefix(sort, "-")
  176. sort = sort + " " + "desc"
  177. }
  178. err = d.db.Table(TableRole).Order(sort).Offset(from).Where(query).Limit(limit).Find(&roles).Error
  179. return
  180. }
  181. // AddAuthorityTaskRole add task role to db
  182. func (d *Dao) AddAuthorityTaskRole(taskRole *model.TaskRole) (err error) {
  183. var role model.TaskRole
  184. err = d.db.Table(TableRole).Where("name = ?", taskRole.Name).Find(&role).Error
  185. if err == gorm.ErrRecordNotFound {
  186. err = d.db.Table(TableRole).Create(taskRole).Error
  187. return
  188. }
  189. if err != nil {
  190. return
  191. }
  192. if role.IsDeleted == 1 {
  193. update := map[string]interface{}{
  194. "desc": taskRole.Desc,
  195. "group_id": taskRole.GroupID,
  196. "atime": xtime.Time(time.Now().Unix()),
  197. "is_deleted": 0,
  198. }
  199. err = d.db.Table(TableRole).Where("name = ?", taskRole.Name).Updates(update).Error
  200. } else {
  201. err = ecode.GrowupAuthorityExist
  202. }
  203. return
  204. }
  205. // UpdateAuthorityTaskRole update task role to db
  206. func (d *Dao) UpdateAuthorityTaskRole(id int64, update map[string]interface{}) (err error) {
  207. return d.db.Table(TableRole).Where("id = ? AND is_deleted = 0", id).Updates(update).Error
  208. }
  209. // DeleteAuthorityTaskRole modify task role's is_deleted = 1
  210. func (d *Dao) DeleteAuthorityTaskRole(id int64) (err error) {
  211. return d.db.Table(TableRole).Where("id = ? AND is_deleted = 0", id).Updates(map[string]interface{}{"is_deleted": 1}).Error
  212. }
  213. // GetAuthorityTaskRoles get task roles id and name
  214. func (d *Dao) GetAuthorityTaskRoles(query string) (roles []*model.Role, err error) {
  215. query = queryAddIsDeleted(query)
  216. err = d.db.Table(TableRole).Where(query).Scan(&roles).Error
  217. return
  218. }
  219. // GetAuthorityTaskRolePrivileges get task role's privileges
  220. func (d *Dao) GetAuthorityTaskRolePrivileges(roleID int64) (privileges string, err error) {
  221. var role model.TaskRole
  222. err = d.db.Table(TableRole).Where("id = ? AND is_deleted = 0", roleID).Scan(&role).Error
  223. if err != nil {
  224. return
  225. }
  226. privileges = role.Privileges
  227. return
  228. }
  229. // AddPrivilege add privilege to db
  230. func (d *Dao) AddPrivilege(privilege *model.Privilege) (err error) {
  231. var p model.Privilege
  232. err = d.db.Table(TablePrivilege).Where("name = ?", privilege.Name).Find(&p).Error
  233. if err == gorm.ErrRecordNotFound {
  234. err = d.db.Table(TablePrivilege).Create(privilege).Error
  235. return
  236. }
  237. if err != nil {
  238. return
  239. }
  240. if p.IsDeleted == 1 {
  241. update := map[string]interface{}{
  242. "name": privilege.Name,
  243. "level": privilege.Level,
  244. "father_id": privilege.FatherID,
  245. "is_deleted": 0,
  246. }
  247. err = d.db.Table(TablePrivilege).Where("name = ?", privilege.Name).Updates(update).Error
  248. } else {
  249. err = ecode.GrowupAuthorityExist
  250. }
  251. return
  252. }
  253. // UpdatePrivilege update privilege to db
  254. func (d *Dao) UpdatePrivilege(id int64, update map[string]interface{}) (err error) {
  255. return d.db.Table(TablePrivilege).Where("id = ? AND is_deleted = 0", id).Updates(update).Error
  256. }
  257. // GetLevelPrivileges get sprivileges by query
  258. func (d *Dao) GetLevelPrivileges(query string) (p []*model.SPrivilege, err error) {
  259. query = queryAddIsDeleted(query)
  260. err = d.db.Table(TablePrivilege).Select("id, name, level, is_router").Where(query).Scan(&p).Error
  261. return
  262. }