sys_group.go 8.1 KB

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