sys_group.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package system
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_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" 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. func GetSysGroupByGroupIds(groupIds []int) (items []*SysGroupList, err error) {
  110. lenArr := len(groupIds)
  111. if lenArr == 0 {
  112. return
  113. }
  114. sql := `SELECT * FROM sys_group WHERE group_id IN (` + utils.GetOrmInReplace(lenArr) + `) ORDER BY sort ASC, create_time ASC`
  115. o := orm.NewOrm()
  116. _, err = o.Raw(sql, groupIds).QueryRows(&items)
  117. return
  118. }
  119. // GetSysGroupByGroupId 销售主管用,查找销售主管所在大组的名称
  120. func GetSysGroupByGroupId(groupId int) (items []*SysGroupList, err error) {
  121. sql := `SELECT * FROM sys_group WHERE department_id=2 AND parent_id=0 AND group_id=? ORDER BY sort ASC, create_time ASC`
  122. o := orm.NewOrm()
  123. _, err = o.Raw(sql, groupId).QueryRows(&items)
  124. return
  125. }
  126. // GetChildSysGroupByGroupId 通过上级分组id获取下级的分组id
  127. func GetChildSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  128. sql := `SELECT * FROM sys_group WHERE parent_id=? ORDER BY sort ASC, create_time ASC`
  129. o := orm.NewOrm()
  130. _, err = o.Raw(sql, groupId).QueryRows(&items)
  131. return
  132. }
  133. type SysFullGroup struct {
  134. GroupId int `orm:"column(group_id);pk" description:"分组ID"`
  135. DepartmentId int `description:"部门Id"`
  136. ParentId int `description:"父级Id"`
  137. GroupName string `description:"分组名称"`
  138. ParentGroupName string `description:"父级分组名称"`
  139. DepartmentName string `description:"部门名称"`
  140. CreateTime time.Time `description:"创建时间"`
  141. }
  142. // GetFullGroup 获取完整的分组信息
  143. func GetFullGroup() (list []*SysFullGroup, err error) {
  144. sql := `SELECT s.*,g.group_name as parent_group_name , d.department_name
  145. from sys_group s
  146. LEFT JOIN sys_group g on s.parent_id=g.group_id
  147. LEFT JOIN sys_department d on s.department_id=d.department_id ORDER BY s.sort ASC, s.create_time ASC`
  148. o := orm.NewOrm()
  149. _, err = o.Raw(sql).QueryRows(&list)
  150. return
  151. }
  152. type SysGroupSortReq struct {
  153. //ParentId int `description:"上级部门/分组ID"`
  154. DepartmentIds []int `description:"移动后的一级ID排序"`
  155. GroupIds []int `description:"移动后的二级ID排序"`
  156. TeamIds []int `description:"移动后的三级ID排序"`
  157. }
  158. type GroupSort struct {
  159. GroupId int
  160. Sort int
  161. }
  162. func MultiUpdateGroupSort(items []*GroupSort) (err error) {
  163. if len(items) == 0 {
  164. return
  165. }
  166. o := orm.NewOrm()
  167. p, err := o.Raw("UPDATE sys_group SET sort = ? WHERE group_id = ?").Prepare()
  168. if err != nil {
  169. return
  170. }
  171. defer func() {
  172. _ = p.Close()
  173. }()
  174. for _, v := range items {
  175. _, err = p.Exec(v.Sort, v.GroupId)
  176. if err != nil {
  177. return
  178. }
  179. }
  180. return
  181. }
  182. func GetGroupByDepartmentId(departmentId int) (list []*SysFullGroup, err error) {
  183. sql := `SELECT
  184. s.*, g.group_name AS parent_group_name,
  185. d.department_name
  186. FROM
  187. sys_group s
  188. LEFT JOIN sys_group g ON s.parent_id = g.group_id
  189. LEFT JOIN sys_department d ON s.department_id = d.department_id
  190. WHERE
  191. s.department_id = ?
  192. ORDER BY
  193. s.sort ASC,
  194. s.create_time ASC`
  195. o := orm.NewOrm()
  196. _, err = o.Raw(sql, departmentId).QueryRows(&list)
  197. return
  198. }
  199. type RoadshowGroupResp struct {
  200. List []RoadshowGroups
  201. }
  202. type RoadshowGroups struct {
  203. GroupId int `description:"分组ID"`
  204. GroupName string `description:"分组名称"`
  205. Child []RoadshowGroupSellers `description:"销售"`
  206. }
  207. type RoadshowGroupSellers struct {
  208. GroupId int `description:"大组ID"`
  209. TeamId int `description:"小组ID"`
  210. AdminId int `description:"销售ID"`
  211. AdminName string `description:"销售名称"`
  212. }
  213. func GetAllSysGroupByGroupId(groupId int) (items []*SysGroup, err error) {
  214. sql := `SELECT * FROM sys_group WHERE parent_id=? OR parent_id = ? ORDER BY sort ASC, create_time ASC`
  215. o := orm.NewOrm()
  216. _, err = o.Raw(sql, groupId, groupId).QueryRows(&items)
  217. return
  218. }