sys_group.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package system
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type SysGroupAddReq struct {
  7. DepartmentId int `description:"部门Id"`
  8. GroupName string `description:"分组名称,多个用英文逗号隔开"`
  9. }
  10. type SysGroup struct {
  11. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  12. DepartmentId int `description:"部门Id"`
  13. ParentId int `description:"父级Id"`
  14. GroupName string `description:"分组名称"`
  15. CreateTime time.Time `description:"创建时间"`
  16. }
  17. func GetSysGroupCount(departmentId int, groupName string) (count int, err error) {
  18. sql := `SELECT COUNT(1) AS count FROM sys_group WHERE department_id=? AND group_name=? `
  19. o := orm.NewOrm()
  20. err = o.Raw(sql, departmentId, groupName).QueryRow(&count)
  21. return
  22. }
  23. func AddSysGroup(item *SysGroup) (lastId int64, err error) {
  24. o := orm.NewOrm()
  25. lastId, err = o.Insert(item)
  26. return
  27. }
  28. type SysGroupEditReq struct {
  29. GroupId int `description:"分组ID"`
  30. GroupName string `description:"分组名称"`
  31. }
  32. func GetSysGroupById(groupId int) (item *SysGroup, err error) {
  33. sql := `SELECT * FROM sys_group WHERE group_id=? `
  34. o := orm.NewOrm()
  35. err = o.Raw(sql, groupId).QueryRow(&item)
  36. return
  37. }
  38. func GetSysGroupByName(groupName string) (item *SysGroup, err error) {
  39. sql := `SELECT * FROM sys_group WHERE group_name=? `
  40. o := orm.NewOrm()
  41. err = o.Raw(sql, groupName).QueryRow(&item)
  42. return
  43. }
  44. func ModifySysGroup(groupName string, groupId int) (err error) {
  45. o := orm.NewOrm()
  46. to, err := o.Begin()
  47. if err != nil {
  48. return
  49. }
  50. defer func() {
  51. if err != nil {
  52. _ = to.Rollback()
  53. } else {
  54. _ = to.Commit()
  55. }
  56. }()
  57. sql := `UPDATE sys_group SET group_name=? WHERE group_id=? `
  58. _, err = to.Raw(sql, groupName, groupId).Exec()
  59. sql = `UPDATE admin SET group_name=? WHERE group_id=? `
  60. _, err = to.Raw(sql, groupName, groupId).Exec()
  61. return
  62. }
  63. type SysGroupDeleteReq struct {
  64. GroupId int `description:"分组ID"`
  65. }
  66. func DeleteSysGroup(groupId int) (err error) {
  67. sql := `DELETE FROM sys_group WHERE group_id=? `
  68. o := orm.NewOrm()
  69. _, err = o.Raw(sql, groupId).Exec()
  70. return
  71. }
  72. //因前端显示需要,TopId字段用来当做一级部门id,DepartmentId为当前分组id
  73. type SysGroupList struct {
  74. GroupId int `orm:"column(group_id);pk" json:"DepartmentId" description:"分组ID"`
  75. ParentId int `json:"ParentId" description:"父级ID"`
  76. DepartmentId int `json:"TopId" description:"部门Id"`
  77. GroupName string `json:"DepartmentName" description:"分组名称"`
  78. Child []*SysTeamList `description:"小组"`
  79. CreateTime time.Time `description:"创建时间"`
  80. IsGroup bool `description:"是否为二级部门"`
  81. }
  82. func GetSysGroupByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  83. sql := `SELECT * FROM sys_group WHERE department_id=? AND parent_id=0`
  84. o := orm.NewOrm()
  85. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  86. return
  87. }
  88. // GetSysGroupListByDepartmentId 获取该部门下的所有分组(包含大小分组,不包含 “无” 这个分组)
  89. func GetSysGroupListByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  90. sql := `SELECT * FROM sys_group WHERE department_id=? AND group_name<>'无' `
  91. o := orm.NewOrm()
  92. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  93. return
  94. }
  95. func ClearSysUserGroup(groupId int) (err error) {
  96. sql := `UPDATE admin SET group_id=0,group_name='' WHERE group_id=? `
  97. o := orm.NewOrm()
  98. _, err = o.Raw(sql, groupId).Exec()
  99. return
  100. }
  101. func GetSysGroupByDirectorId(directorId int) (items []*SysGroupList, err error) {
  102. sql := `SELECT * FROM sys_group WHERE group_id=? `
  103. o := orm.NewOrm()
  104. _, err = o.Raw(sql, directorId).QueryRows(&items)
  105. return
  106. }
  107. // GetSysGroupByGroupId 销售主管用,查找销售主管所在大组的名称
  108. func GetSysGroupByGroupId(groupId int) (items []*SysGroupList, err error) {
  109. sql := `SELECT * FROM sys_group WHERE department_id=2 AND parent_id=0 AND group_id=?`
  110. o := orm.NewOrm()
  111. _, err = o.Raw(sql, groupId).QueryRows(&items)
  112. return
  113. }
  114. // GetChildSysGroupByGroupId 通过上级分组id获取下级的分组id
  115. func GetChildSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  116. sql := `SELECT * FROM sys_group WHERE parent_id=?`
  117. o := orm.NewOrm()
  118. _, err = o.Raw(sql, groupId).QueryRows(&items)
  119. return
  120. }
  121. type SysFullGroup struct {
  122. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  123. DepartmentId int `description:"部门Id"`
  124. ParentId int `description:"父级Id"`
  125. GroupName string `description:"分组名称"`
  126. ParentGroupName string `description:"父级分组名称"`
  127. DepartmentName string `description:"部门名称"`
  128. CreateTime time.Time `description:"创建时间"`
  129. }
  130. //GetFullGroup 获取完整的分组信息
  131. func GetFullGroup() (list []*SysFullGroup, err error) {
  132. sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
  133. from sys_group s
  134. LEFT JOIN sys_group g on s.parent_id=g.group_id
  135. LEFT JOIN sys_department d on s.department_id=d.department_id`
  136. o := orm.NewOrm()
  137. _, err = o.Raw(sql).QueryRows(&list)
  138. return
  139. }