sys_group.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package system
  2. import (
  3. "eta/eta_forum_task/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. func ClearSysUserGroup(groupId int) (err error) {
  75. sql := `UPDATE admin SET group_id=0,group_name='' WHERE group_id=? `
  76. o := orm.NewOrm()
  77. _, err = o.Raw(sql, groupId).Exec()
  78. return
  79. }
  80. // GetSysGroupByGroupIds 销售主管用,查找销售主管所在大组的名称
  81. func GetSysGroupByGroupIds(groupIds []int) (items []*SysGroup, err error) {
  82. sql := `SELECT * FROM sys_group WHERE group_id in (` + utils.GetOrmInReplace(len(groupIds)) + `) ORDER BY sort ASC, create_time ASC`
  83. o := orm.NewOrm()
  84. _, err = o.Raw(sql, groupIds).QueryRows(&items)
  85. return
  86. }
  87. // GetChildSysGroupByGroupId 通过上级分组id获取下级的分组id
  88. func GetChildSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  89. sql := `SELECT * FROM sys_group WHERE parent_id=? ORDER BY sort ASC, create_time ASC`
  90. o := orm.NewOrm()
  91. _, err = o.Raw(sql, groupId).QueryRows(&items)
  92. return
  93. }
  94. type SysFullGroup struct {
  95. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  96. DepartmentId int `description:"部门Id"`
  97. ParentId int `description:"父级Id"`
  98. GroupName string `description:"分组名称"`
  99. ParentGroupName string `description:"父级分组名称"`
  100. DepartmentName string `description:"部门名称"`
  101. CreateTime time.Time `description:"创建时间"`
  102. }
  103. // GetFullGroup 获取完整的分组信息
  104. func GetFullGroup() (list []*SysFullGroup, err error) {
  105. sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
  106. from sys_group s
  107. LEFT JOIN sys_group g on s.parent_id=g.group_id
  108. LEFT JOIN sys_department d on s.department_id=d.department_id ORDER BY s.sort ASC, s.create_time ASC`
  109. o := orm.NewOrm()
  110. _, err = o.Raw(sql).QueryRows(&list)
  111. return
  112. }
  113. type SysGroupSortReq struct {
  114. //ParentId int `description:"上级部门/分组ID"`
  115. DepartmentIds []int `description:"移动后的一级ID排序"`
  116. GroupIds []int `description:"移动后的二级ID排序"`
  117. TeamIds []int `description:"移动后的三级ID排序"`
  118. }
  119. type GroupSort struct {
  120. GroupId int
  121. Sort int
  122. }
  123. func MultiUpdateGroupSort(items []*GroupSort) (err error) {
  124. if len(items) == 0 {
  125. return
  126. }
  127. o := orm.NewOrm()
  128. p, err := o.Raw("UPDATE sys_group SET sort = ? WHERE group_id = ?").Prepare()
  129. if err != nil {
  130. return
  131. }
  132. defer func() {
  133. _ = p.Close()
  134. }()
  135. for _, v := range items {
  136. _, err = p.Exec(v.Sort, v.GroupId)
  137. if err != nil {
  138. return
  139. }
  140. }
  141. return
  142. }
  143. func GetGroupByDepartmentId(departmentId int) (list []*SysFullGroup, err error) {
  144. sql := `SELECT
  145. s.*, g.group_name AS parent_group_name,
  146. d.department_name
  147. FROM
  148. sys_group s
  149. LEFT JOIN sys_group g ON s.parent_id = g.group_id
  150. LEFT JOIN sys_department d ON s.department_id = d.department_id
  151. WHERE
  152. s.department_id = ?
  153. ORDER BY
  154. s.sort ASC,
  155. s.create_time ASC`
  156. o := orm.NewOrm()
  157. _, err = o.Raw(sql, departmentId).QueryRows(&list)
  158. return
  159. }
  160. type RoadshowGroupResp struct {
  161. List []RoadshowGroups
  162. }
  163. type RoadshowGroups struct {
  164. GroupId int `description:"分组ID"`
  165. GroupName string `description:"分组名称"`
  166. Child []RoadshowGroupSellers `description:"销售"`
  167. }
  168. type RoadshowGroupSellers struct {
  169. GroupId int `description:"大组ID"`
  170. TeamId int `description:"小组ID"`
  171. AdminId int `description:"销售ID"`
  172. AdminName string `description:"销售名称"`
  173. }
  174. func GetAllSysGroup() (items []*SysGroup, err error) {
  175. sql := `SELECT * FROM sys_group `
  176. o := orm.NewOrm()
  177. _, err = o.Raw(sql).QueryRows(&items)
  178. return
  179. }