sys_group.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package system
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type SysGroupAddReq struct {
  7. List []*SysGroup
  8. }
  9. type SysGroup struct {
  10. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  11. DepartmentId int `description:"部门Id"`
  12. ParentId int `description:"父级Id"`
  13. GroupName string `description:"分组名称"`
  14. Sort int `description:"排序"`
  15. CreateTime string `description:"创建时间"`
  16. }
  17. func GetSysGroupCount(departmentId int, groupName string) (count int, err error) {
  18. sql := `SELECT COUNT(1) AS count FROM sys_group WHERE department_id=? AND group_name=? `
  19. o := orm.NewOrm()
  20. err = o.Raw(sql, departmentId, groupName).QueryRow(&count)
  21. return
  22. }
  23. func AddSysGroup(item *SysGroup) (lastId int64, err error) {
  24. o := orm.NewOrm()
  25. lastId, err = o.Insert(item)
  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. o := orm.NewOrm()
  35. err = o.Raw(sql, groupId).QueryRow(&item)
  36. return
  37. }
  38. func GetSysGroupByName(groupName string) (item *SysGroup, err error) {
  39. sql := `SELECT * FROM sys_group WHERE group_name=? `
  40. o := orm.NewOrm()
  41. err = o.Raw(sql, groupName).QueryRow(&item)
  42. return
  43. }
  44. func ModifySysGroup(groupName string, groupId int) (err error) {
  45. o := orm.NewOrm()
  46. to, err := o.Begin()
  47. if err != nil {
  48. return
  49. }
  50. defer func() {
  51. if err != nil {
  52. _ = to.Rollback()
  53. } else {
  54. _ = to.Commit()
  55. }
  56. }()
  57. sql := `UPDATE sys_group SET group_name=? WHERE group_id=? `
  58. _, err = to.Raw(sql, groupName, groupId).Exec()
  59. sql = `UPDATE admin SET group_name=? WHERE group_id=? `
  60. _, err = to.Raw(sql, groupName, groupId).Exec()
  61. return
  62. }
  63. type SysGroupDeleteReq struct {
  64. GroupId int `description:"分组ID"`
  65. }
  66. func DeleteSysGroup(groupId int) (err error) {
  67. sql := `DELETE FROM sys_group WHERE group_id=? `
  68. o := orm.NewOrm()
  69. _, err = o.Raw(sql, groupId).Exec()
  70. return
  71. }
  72. type SysFullGroup struct {
  73. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  74. DepartmentId int `description:"部门Id"`
  75. ParentId int `description:"父级Id"`
  76. GroupName string `description:"分组名称"`
  77. ParentGroupName string `description:"父级分组名称"`
  78. DepartmentName string `description:"部门名称"`
  79. CreateTime time.Time `description:"创建时间"`
  80. }
  81. type SysGroupSortReq struct {
  82. //ParentId int `description:"上级部门/分组ID"`
  83. DepartmentIds []int `description:"移动后的一级ID排序"`
  84. GroupIds []int `description:"移动后的二级ID排序"`
  85. TeamIds []int `description:"移动后的三级ID排序"`
  86. }
  87. type GroupSort struct {
  88. GroupId int
  89. Sort int
  90. }
  91. func MultiUpdateGroupSort(items []*GroupSort) (err error) {
  92. if len(items) == 0 {
  93. return
  94. }
  95. o := orm.NewOrm()
  96. p, err := o.Raw("UPDATE sys_group SET sort = ? WHERE group_id = ?").Prepare()
  97. if err != nil {
  98. return
  99. }
  100. defer func() {
  101. _ = p.Close()
  102. }()
  103. for _, v := range items {
  104. _, err = p.Exec(v.Sort, v.GroupId)
  105. if err != nil {
  106. return
  107. }
  108. }
  109. return
  110. }
  111. func GetGroupByDepartmentId(departmentId int) (list []*SysFullGroup, err error) {
  112. sql := `SELECT
  113. s.*, g.group_name AS parent_group_name,
  114. d.department_name
  115. FROM
  116. sys_group s
  117. LEFT JOIN sys_group g ON s.parent_id = g.group_id
  118. LEFT JOIN sys_department d ON s.department_id = d.department_id
  119. WHERE
  120. s.department_id = ?
  121. ORDER BY
  122. s.sort ASC,
  123. s.create_time ASC`
  124. o := orm.NewOrm()
  125. _, err = o.Raw(sql, departmentId).QueryRows(&list)
  126. return
  127. }
  128. type RoadshowGroupResp struct {
  129. List []RoadshowGroups
  130. }
  131. type RoadshowGroups struct {
  132. GroupId int `description:"分组ID"`
  133. GroupName string `description:"分组名称"`
  134. Child []RoadshowGroupSellers `description:"销售"`
  135. }
  136. type RoadshowGroupSellers struct {
  137. GroupId int `description:"大组ID"`
  138. TeamId int `description:"小组ID"`
  139. AdminId int `description:"销售ID"`
  140. AdminName string `description:"销售名称"`
  141. }
  142. func GetAllSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  143. sql := `SELECT * FROM sys_group WHERE parent_id=? OR parent_id = ? ORDER BY sort ASC, create_time ASC`
  144. o := orm.NewOrm()
  145. _, err = o.Raw(sql, groupId, groupId).QueryRows(&items)
  146. return
  147. }
  148. func GetAllSysGroup() (items []*SysGroup, err error) {
  149. sql := `SELECT * FROM sys_group `
  150. o := orm.NewOrm()
  151. _, err = o.Raw(sql).QueryRows(&items)
  152. return
  153. }
  154. func GetParentIdFromGroup(gid int) (items *int, err error) {
  155. o := orm.NewOrm()
  156. sql := `SELECT parent_id FROM sys_group WHERE group_id=? `
  157. err = o.Raw(sql, gid).QueryRow(&items)
  158. return
  159. }
  160. // Update 更新用户基础信息
  161. func (item *SysGroup) AddOrUpdate() (err error) {
  162. o := orm.NewOrm()
  163. _, err = o.InsertOrUpdate(item)
  164. return
  165. }