sys_group.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. Sort int `description:"排序"`
  16. CreateTime time.Time `description:"创建时间"`
  17. }
  18. func GetSysGroupCount(departmentId int, groupName string) (count int, err error) {
  19. sql := `SELECT COUNT(1) AS count FROM sys_group WHERE department_id=? AND group_name=? `
  20. o := orm.NewOrm()
  21. err = o.Raw(sql, departmentId, groupName).QueryRow(&count)
  22. return
  23. }
  24. func AddSysGroup(item *SysGroup) (lastId int64, err error) {
  25. o := orm.NewOrm()
  26. lastId, err = o.Insert(item)
  27. return
  28. }
  29. type SysGroupEditReq struct {
  30. GroupId int `description:"分组ID"`
  31. GroupName string `description:"分组名称"`
  32. }
  33. func GetSysGroupById(groupId int) (item *SysGroup, err error) {
  34. sql := `SELECT * FROM sys_group WHERE group_id=? `
  35. o := orm.NewOrm()
  36. err = o.Raw(sql, groupId).QueryRow(&item)
  37. return
  38. }
  39. func GetSysGroupByName(groupName string) (item *SysGroup, err error) {
  40. sql := `SELECT * FROM sys_group WHERE group_name=? `
  41. o := orm.NewOrm()
  42. err = o.Raw(sql, groupName).QueryRow(&item)
  43. return
  44. }
  45. func ModifySysGroup(groupName string, groupId int) (err error) {
  46. o := orm.NewOrm()
  47. to, err := o.Begin()
  48. if err != nil {
  49. return
  50. }
  51. defer func() {
  52. if err != nil {
  53. _ = to.Rollback()
  54. } else {
  55. _ = to.Commit()
  56. }
  57. }()
  58. sql := `UPDATE sys_group SET group_name=? WHERE group_id=? `
  59. _, err = to.Raw(sql, groupName, groupId).Exec()
  60. sql = `UPDATE admin SET group_name=? WHERE group_id=? `
  61. _, err = to.Raw(sql, groupName, groupId).Exec()
  62. return
  63. }
  64. type SysGroupDeleteReq struct {
  65. GroupId int `description:"分组ID"`
  66. }
  67. func DeleteSysGroup(groupId int) (err error) {
  68. sql := `DELETE FROM sys_group WHERE group_id=? `
  69. o := orm.NewOrm()
  70. _, err = o.Raw(sql, groupId).Exec()
  71. return
  72. }
  73. // 因前端显示需要,TopId字段用来当做一级部门id,DepartmentId为当前分组id
  74. type SysGroupList struct {
  75. GroupId int `orm:"column(group_id);pk" json:"DepartmentId" description:"分组ID"`
  76. ParentId int `json:"ParentId" description:"父级ID"`
  77. DepartmentId int `json:"TopId" description:"部门Id"`
  78. GroupName string `json:"DepartmentName" description:"分组名称"`
  79. Child []*SysTeamList `description:"小组"`
  80. CreateTime time.Time `description:"创建时间"`
  81. IsGroup bool `description:"是否为二级部门"`
  82. }
  83. func GetSysGroupByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  84. sql := `SELECT * FROM sys_group WHERE department_id=? AND parent_id=0 ORDER BY sort ASC, create_time ASC`
  85. o := orm.NewOrm()
  86. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  87. return
  88. }
  89. // GetSysGroupListByDepartmentId 获取该部门下的所有分组(包含大小分组,不包含 “无” 这个分组)
  90. func GetSysGroupListByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  91. sql := `SELECT * FROM sys_group WHERE department_id=? AND group_name<>'无' ORDER BY sort ASC, create_time ASC`
  92. o := orm.NewOrm()
  93. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  94. return
  95. }
  96. func ClearSysUserGroup(groupId int) (err error) {
  97. sql := `UPDATE admin SET group_id=0,group_name='' WHERE group_id=? `
  98. o := orm.NewOrm()
  99. _, err = o.Raw(sql, groupId).Exec()
  100. return
  101. }
  102. func GetSysGroupByDirectorId(directorId int) (items []*SysGroupList, err error) {
  103. sql := `SELECT * FROM sys_group WHERE group_id=? ORDER BY sort ASC, create_time ASC`
  104. o := orm.NewOrm()
  105. _, err = o.Raw(sql, directorId).QueryRows(&items)
  106. return
  107. }
  108. // GetSysGroupByGroupId 销售主管用,查找销售主管所在大组的名称
  109. func GetSysGroupByGroupId(groupId int) (items []*SysGroupList, err error) {
  110. sql := `SELECT * FROM sys_group WHERE department_id=2 AND parent_id=0 AND group_id=? ORDER BY sort ASC, create_time ASC`
  111. o := orm.NewOrm()
  112. _, err = o.Raw(sql, groupId).QueryRows(&items)
  113. return
  114. }
  115. // GetChildSysGroupByGroupId 通过上级分组id获取下级的分组id
  116. func GetChildSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  117. sql := `SELECT * FROM sys_group WHERE parent_id=? ORDER BY sort ASC, create_time ASC`
  118. o := orm.NewOrm()
  119. _, err = o.Raw(sql, groupId).QueryRows(&items)
  120. return
  121. }
  122. type SysFullGroup struct {
  123. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  124. DepartmentId int `description:"部门Id"`
  125. ParentId int `description:"父级Id"`
  126. GroupName string `description:"分组名称"`
  127. ParentGroupName string `description:"父级分组名称"`
  128. DepartmentName string `description:"部门名称"`
  129. CreateTime time.Time `description:"创建时间"`
  130. }
  131. // GetFullGroup 获取完整的分组信息
  132. func GetFullGroup() (list []*SysFullGroup, err error) {
  133. sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
  134. from sys_group s
  135. LEFT JOIN sys_group g on s.parent_id=g.group_id
  136. LEFT JOIN sys_department d on s.department_id=d.department_id ORDER BY s.sort ASC, s.create_time ASC`
  137. o := orm.NewOrm()
  138. _, err = o.Raw(sql).QueryRows(&list)
  139. return
  140. }
  141. type SysGroupSortReq struct {
  142. //ParentId int `description:"上级部门/分组ID"`
  143. DepartmentIds []int `description:"移动后的一级ID排序"`
  144. GroupIds []int `description:"移动后的二级ID排序"`
  145. TeamIds []int `description:"移动后的三级ID排序"`
  146. }
  147. type GroupSort struct {
  148. GroupId int
  149. Sort int
  150. }
  151. func MultiUpdateGroupSort(items []*GroupSort) (err error) {
  152. if len(items) == 0 {
  153. return
  154. }
  155. o := orm.NewOrm()
  156. p, err := o.Raw("UPDATE sys_group SET sort = ? WHERE group_id = ?").Prepare()
  157. if err != nil {
  158. return
  159. }
  160. defer func() {
  161. _ = p.Close()
  162. }()
  163. for _, v := range items {
  164. _, err = p.Exec(v.Sort, v.GroupId)
  165. if err != nil {
  166. return
  167. }
  168. }
  169. return
  170. }
  171. func GetGroupByDepartmentId(departmentId int) (list []*SysFullGroup, err error) {
  172. sql := `SELECT
  173. s.*, g.group_name AS parent_group_name,
  174. d.department_name
  175. FROM
  176. sys_group s
  177. LEFT JOIN sys_group g ON s.parent_id = g.group_id
  178. LEFT JOIN sys_department d ON s.department_id = d.department_id
  179. WHERE
  180. s.department_id = ?
  181. ORDER BY
  182. s.sort ASC,
  183. s.create_time ASC`
  184. o := orm.NewOrm()
  185. _, err = o.Raw(sql, departmentId).QueryRows(&list)
  186. return
  187. }
  188. type RoadshowGroupResp struct {
  189. List []RoadshowGroups
  190. }
  191. type RoadshowGroups struct {
  192. GroupId int `description:"分组ID"`
  193. GroupName string `description:"分组名称"`
  194. Child []RoadshowGroupSellers `description:"销售"`
  195. }
  196. type RoadshowGroupSellers struct {
  197. GroupId int `description:"大组ID"`
  198. TeamId int `description:"小组ID"`
  199. AdminId int `description:"销售ID"`
  200. AdminName string `description:"销售名称"`
  201. }