sys_group.go 7.9 KB

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