sys_group.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "time"
  5. )
  6. type SysGroupAddReq struct {
  7. DepartmentId int `description:"部门Id"`
  8. GroupName string `description:"分组名称,多个用英文逗号隔开"`
  9. }
  10. type SysGroup struct {
  11. GroupId int `gorm:"primaryKey"`
  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. err = global.DEFAULT_DmSQL.Raw(sql, departmentId, groupName).Scan(&count).Error
  21. return
  22. }
  23. func AddSysGroup(item *SysGroup) (lastId int64, err error) {
  24. err = global.DEFAULT_DmSQL.Create(item).Error
  25. return
  26. }
  27. type SysGroupEditReq struct {
  28. GroupId int `description:"分组ID"`
  29. GroupName string `description:"分组名称"`
  30. }
  31. func GetSysGroupById(groupId int) (item *SysGroup, err error) {
  32. sql := `SELECT * FROM sys_group WHERE group_id=? `
  33. err = global.DEFAULT_DmSQL.Raw(sql, groupId).First(&item).Error
  34. return
  35. }
  36. func GetSysGroupByName(groupName string) (item *SysGroup, err error) {
  37. sql := `SELECT * FROM sys_group WHERE group_name=? `
  38. err = global.DEFAULT_DmSQL.Raw(sql, groupName).First(&item).Error
  39. return
  40. }
  41. func ModifySysGroup(groupName string, groupId int) (err error) {
  42. tx := global.DEFAULT_DmSQL.Begin()
  43. defer func() {
  44. if err != nil {
  45. _ = tx.Rollback()
  46. return
  47. }
  48. _ = tx.Commit()
  49. }()
  50. sql := `UPDATE sys_group SET group_name=? WHERE group_id=? `
  51. err = tx.Exec(sql, groupName, groupId).Error
  52. if err != nil {
  53. return
  54. }
  55. sql = `UPDATE "admin" SET group_name=? WHERE group_id=? `
  56. err = tx.Exec(sql, groupName, groupId).Error
  57. return
  58. }
  59. type SysGroupDeleteReq struct {
  60. GroupId int `description:"分组ID"`
  61. }
  62. func DeleteSysGroup(groupId int) (err error) {
  63. sql := `DELETE FROM sys_group WHERE group_id=? `
  64. err = global.DEFAULT_DmSQL.Exec(sql, groupId).Error
  65. return
  66. }
  67. // 因前端显示需要,TopId字段用来当做一级部门id,DepartmentId为当前分组id
  68. type SysGroupList struct {
  69. GroupId int `orm:"column(group_id);pk" json:"DepartmentId" description:"分组ID"`
  70. ParentId int `json:"ParentId" description:"父级ID"`
  71. DepartmentId int `json:"TopId" description:"部门Id"`
  72. GroupName string `json:"DepartmentName" description:"分组名称"`
  73. Child []*SysTeamList `gorm:"-" description:"小组"`
  74. CreateTime time.Time `description:"创建时间"`
  75. IsGroup bool `description:"是否为二级部门"`
  76. }
  77. func GetSysGroupList() (items []*SysGroupList, err error) {
  78. sql := `SELECT * FROM sys_group ORDER BY sort ASC, create_time ASC `
  79. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  80. return
  81. }
  82. func GetSysGroupByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  83. sql := `SELECT * FROM sys_group WHERE department_id=? AND parent_id=0 ORDER BY sort ASC, create_time ASC`
  84. err = global.DEFAULT_DmSQL.Raw(sql, departmentId).Find(&items).Error
  85. return
  86. }
  87. // GetSysGroupListByDepartmentId 获取该部门下的所有分组(包含大小分组,不包含 “无” 这个分组)
  88. func GetSysGroupListByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  89. sql := `SELECT * FROM sys_group WHERE department_id=? AND group_name<>'无' ORDER BY sort ASC, create_time ASC`
  90. err = global.DEFAULT_DmSQL.Raw(sql, departmentId).Find(&items).Error
  91. return
  92. }
  93. func ClearSysUserGroup(groupId int) (err error) {
  94. sql := `UPDATE "admin" SET group_id=0,group_name='' WHERE group_id=? `
  95. err = global.DEFAULT_DmSQL.Exec(sql, groupId).Error
  96. return
  97. }
  98. func GetSysGroupByDirectorId(directorId int) (items []*SysGroupList, err error) {
  99. sql := `SELECT * FROM sys_group WHERE group_id=? ORDER BY sort ASC, create_time ASC`
  100. err = global.DEFAULT_DmSQL.Raw(sql, directorId).Find(&items).Error
  101. return
  102. }
  103. // GetSysGroupByGroupId 销售主管用,查找销售主管所在大组的名称
  104. func GetSysGroupByGroupId(groupId int) (items []*SysGroupList, err error) {
  105. sql := `SELECT * FROM sys_group WHERE department_id=2 AND parent_id=0 AND group_id=? ORDER BY sort ASC, create_time ASC`
  106. err = global.DEFAULT_DmSQL.Raw(sql, groupId).Find(&items).Error
  107. return
  108. }
  109. type SysFullGroup struct {
  110. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  111. DepartmentId int `description:"部门Id"`
  112. ParentId int `description:"父级Id"`
  113. GroupName string `description:"分组名称"`
  114. ParentGroupName string `description:"父级分组名称"`
  115. DepartmentName string `description:"部门名称"`
  116. CreateTime time.Time `description:"创建时间"`
  117. }
  118. // GetFullGroup 获取完整的分组信息
  119. func GetFullGroup() (list []*SysFullGroup, err error) {
  120. sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
  121. from sys_group s
  122. LEFT JOIN sys_group g on s.parent_id=g.group_id
  123. LEFT JOIN sys_department d on s.department_id=d.department_id ORDER BY s.sort ASC, s.create_time ASC`
  124. err = global.DEFAULT_DmSQL.Raw(sql).Find(&list).Error
  125. return
  126. }
  127. type SysGroupSortReq struct {
  128. DepartmentIds []int `description:"移动后的一级ID排序"`
  129. GroupIds []int `description:"移动后的二级ID排序"`
  130. TeamIds []int `description:"移动后的三级ID排序"`
  131. }
  132. type GroupSort struct {
  133. GroupId int
  134. Sort int
  135. }
  136. func MultiUpdateGroupSort(items []*GroupSort) (err error) {
  137. if len(items) == 0 {
  138. return
  139. }
  140. sql := "UPDATE sys_group SET sort = ? WHERE group_id = ?"
  141. for _, v := range items {
  142. err = global.DEFAULT_DmSQL.Exec(sql, v.Sort, v.GroupId).Error
  143. if err != nil {
  144. return
  145. }
  146. }
  147. return
  148. }
  149. type RoadshowGroupResp struct {
  150. List []RoadshowGroups
  151. }
  152. type RoadshowGroups struct {
  153. GroupId int `description:"分组ID"`
  154. GroupName string `description:"分组名称"`
  155. Child []RoadshowGroupSellers `description:"销售"`
  156. }
  157. type RoadshowGroupSellers struct {
  158. GroupId int `description:"大组ID"`
  159. TeamId int `description:"小组ID"`
  160. AdminId int `description:"销售ID"`
  161. AdminName string `description:"销售名称"`
  162. }