sys_group.go 8.7 KB

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