sys_group.go 7.6 KB

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