sys_group.go 6.8 KB

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