sys_group.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package system
  2. import (
  3. "eta/eta_api/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. // 因前端显示需要,TopId字段用来当做一级部门id,DepartmentId为当前分组id
  75. type SysGroupList struct {
  76. GroupId int `orm:"column(group_id);pk" json:"DepartmentId" description:"分组ID"`
  77. ParentId int `json:"ParentId" description:"父级ID"`
  78. DepartmentId int `json:"TopId" description:"部门Id"`
  79. GroupName string `json:"DepartmentName" description:"分组名称"`
  80. Child []*SysTeamList `description:"小组"`
  81. CreateTime time.Time `description:"创建时间"`
  82. IsGroup bool `description:"是否为二级部门"`
  83. }
  84. func GetSysGroupByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  85. sql := `SELECT * FROM sys_group WHERE department_id=? AND parent_id=0 ORDER BY sort ASC, create_time ASC`
  86. o := orm.NewOrm()
  87. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  88. return
  89. }
  90. // GetSysGroupListByDepartmentId 获取该部门下的所有分组(包含大小分组,不包含 “无” 这个分组)
  91. func GetSysGroupListByDepartmentId(departmentId int) (items []*SysGroupList, err error) {
  92. sql := `SELECT * FROM sys_group WHERE department_id=? AND group_name<>'无' ORDER BY sort ASC, create_time ASC`
  93. o := orm.NewOrm()
  94. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  95. return
  96. }
  97. func ClearSysUserGroup(groupId int) (err error) {
  98. sql := `UPDATE admin SET group_id=0,group_name='' WHERE group_id=? `
  99. o := orm.NewOrm()
  100. _, err = o.Raw(sql, groupId).Exec()
  101. return
  102. }
  103. func GetSysGroupByDirectorId(directorId int) (items []*SysGroupList, err error) {
  104. sql := `SELECT * FROM sys_group WHERE group_id=? ORDER BY sort ASC, create_time ASC`
  105. o := orm.NewOrm()
  106. _, err = o.Raw(sql, directorId).QueryRows(&items)
  107. return
  108. }
  109. // GetSysGroupByGroupId 销售主管用,查找销售主管所在大组的名称
  110. func GetSysGroupByGroupId(groupId int) (items []*SysGroupList, err error) {
  111. sql := `SELECT * FROM sys_group WHERE department_id=2 AND parent_id=0 AND group_id=? ORDER BY sort ASC, create_time ASC`
  112. o := orm.NewOrm()
  113. _, err = o.Raw(sql, groupId).QueryRows(&items)
  114. return
  115. }
  116. // GetSysGroupByGroupIds 销售主管用,查找销售主管所在大组的名称
  117. func GetSysGroupByGroupIds(groupIds []int) (items []*SysGroup, err error) {
  118. sql := `SELECT * FROM sys_group WHERE group_id in (` + utils.GetOrmInReplace(len(groupIds)) + `) ORDER BY sort ASC, create_time ASC`
  119. o := orm.NewOrm()
  120. _, err = o.Raw(sql, groupIds).QueryRows(&items)
  121. return
  122. }
  123. // GetChildSysGroupByGroupId 通过上级分组id获取下级的分组id
  124. func GetChildSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  125. sql := `SELECT * FROM sys_group WHERE parent_id=? ORDER BY sort ASC, create_time ASC`
  126. o := orm.NewOrm()
  127. _, err = o.Raw(sql, groupId).QueryRows(&items)
  128. return
  129. }
  130. type SysFullGroup struct {
  131. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  132. DepartmentId int `description:"部门Id"`
  133. ParentId int `description:"父级Id"`
  134. GroupName string `description:"分组名称"`
  135. ParentGroupName string `description:"父级分组名称"`
  136. DepartmentName string `description:"部门名称"`
  137. CreateTime time.Time `description:"创建时间"`
  138. }
  139. // GetFullGroup 获取完整的分组信息
  140. func GetFullGroup() (list []*SysFullGroup, err error) {
  141. sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
  142. from sys_group s
  143. LEFT JOIN sys_group g on s.parent_id=g.group_id
  144. LEFT JOIN sys_department d on s.department_id=d.department_id ORDER BY s.sort ASC, s.create_time ASC`
  145. o := orm.NewOrm()
  146. _, err = o.Raw(sql).QueryRows(&list)
  147. return
  148. }
  149. type SysGroupSortReq struct {
  150. //ParentId int `description:"上级部门/分组ID"`
  151. DepartmentIds []int `description:"移动后的一级ID排序"`
  152. GroupIds []int `description:"移动后的二级ID排序"`
  153. TeamIds []int `description:"移动后的三级ID排序"`
  154. }
  155. type GroupSort struct {
  156. GroupId int
  157. Sort int
  158. }
  159. func MultiUpdateGroupSort(items []*GroupSort) (err error) {
  160. if len(items) == 0 {
  161. return
  162. }
  163. o := orm.NewOrm()
  164. p, err := o.Raw("UPDATE sys_group SET sort = ? WHERE group_id = ?").Prepare()
  165. if err != nil {
  166. return
  167. }
  168. defer func() {
  169. _ = p.Close()
  170. }()
  171. for _, v := range items {
  172. _, err = p.Exec(v.Sort, v.GroupId)
  173. if err != nil {
  174. return
  175. }
  176. }
  177. return
  178. }
  179. func GetGroupByDepartmentId(departmentId int) (list []*SysFullGroup, err error) {
  180. sql := `SELECT
  181. s.*, g.group_name AS parent_group_name,
  182. d.department_name
  183. FROM
  184. sys_group s
  185. LEFT JOIN sys_group g ON s.parent_id = g.group_id
  186. LEFT JOIN sys_department d ON s.department_id = d.department_id
  187. WHERE
  188. s.department_id = ?
  189. ORDER BY
  190. s.sort ASC,
  191. s.create_time ASC`
  192. o := orm.NewOrm()
  193. _, err = o.Raw(sql, departmentId).QueryRows(&list)
  194. return
  195. }
  196. type RoadshowGroupResp struct {
  197. List []RoadshowGroups
  198. }
  199. type RoadshowGroups struct {
  200. GroupId int `description:"分组ID"`
  201. GroupName string `description:"分组名称"`
  202. Child []RoadshowGroupSellers `description:"销售"`
  203. }
  204. type RoadshowGroupSellers struct {
  205. GroupId int `description:"大组ID"`
  206. TeamId int `description:"小组ID"`
  207. AdminId int `description:"销售ID"`
  208. AdminName string `description:"销售名称"`
  209. }