dept_dao.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package dao
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "ulink-admin/modules/system/models/model"
  5. "ulink-admin/modules/system/models/req"
  6. "ulink-admin/pkg/base"
  7. )
  8. type DeptDao struct {
  9. }
  10. // TreeSelect 根据条件查询部门集合
  11. func (d DeptDao) TreeSelect(query req.DeptQuery) *[]model.SysDept {
  12. depots := make([]model.SysDept, 0)
  13. session := base.GetSession().Session.Where("del_flag = '0'")
  14. if query.ParentId > 0 {
  15. session.And("parent_id = ?", query.ParentId)
  16. }
  17. if !gotool.StrUtils.HasEmpty(query.DeptName) {
  18. session.And("dept_name like concat('%', ?, '%')", query.DeptName)
  19. }
  20. if !gotool.StrUtils.HasEmpty(query.Status) {
  21. session.And("status = ?", query.Status)
  22. }
  23. if !base.GetCurUser().IsAdmin {
  24. session.And("company_id = ?", base.GetCurUser().ComponyId)
  25. }
  26. err := session.OrderBy("parent_id").Desc("order_num").Find(&depots)
  27. if err != nil {
  28. gotool.Logs.ErrorLog().Println(err)
  29. return nil
  30. }
  31. return &depots
  32. }
  33. // SelectDeptListByRoleId 根据角色ID查询部门树信息
  34. func (d DeptDao) SelectDeptListByRoleId(id int64, strictly bool) *[]int64 {
  35. list := make([]int64, 0)
  36. session := base.GetSession().Session.Table([]string{"sys_dept", "d"}).Cols("d.id")
  37. session.Join("LEFT", []string{"sys_role_dept", "rd"}, "d.id = rd.id").
  38. Where("rd.id = ?", id)
  39. if strictly {
  40. session.And("d.id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.id = rd.id and rd.id = ?)", id)
  41. }
  42. if !base.GetCurUser().IsAdmin {
  43. session.And("d.company_id = ?", base.GetCurUser().ComponyId)
  44. }
  45. err := session.OrderBy("d.parent_id").OrderBy("d.order_num").Find(&list)
  46. if err != nil {
  47. gotool.Logs.ErrorLog().Println(err)
  48. return nil
  49. }
  50. return &list
  51. }
  52. // GetList 查询部门列表
  53. func (d DeptDao) GetList(query req.DeptQuery) *[]model.SysDept {
  54. list := make([]model.SysDept, 0)
  55. session := base.GetSession().Session.OrderBy("parent_id").Desc("order_num")
  56. session.Where("del_flag = '0'")
  57. if query.ParentId > 0 {
  58. session.And("parent_id = ?", query.ParentId)
  59. }
  60. if gotool.StrUtils.HasNotEmpty(query.DeptName) {
  61. session.And("dept_name like concat('%', ?, '%')", query.DeptName)
  62. }
  63. if gotool.StrUtils.HasNotEmpty(query.Status) {
  64. session.And("status = ?", query.Status)
  65. }
  66. if !base.GetCurUser().IsAdmin {
  67. session.And("company_id = ?", base.GetCurUser().ComponyId)
  68. }
  69. err := session.Find(&list)
  70. if err != nil {
  71. gotool.Logs.ErrorLog().Println(err)
  72. return nil
  73. }
  74. return &list
  75. }
  76. // GetDeptById 根据部门编号获取详细信息
  77. func (d DeptDao) GetDeptById(id int64) *model.SysDept {
  78. dept := model.SysDept{}
  79. _, err := base.GetSession().Session.Where("id = ?", id).Get(&dept)
  80. if err != nil {
  81. gotool.Logs.ErrorLog().Println(err)
  82. return nil
  83. }
  84. return &dept
  85. }
  86. // Insert 添加部门数据
  87. func (d DeptDao) Insert(dept model.SysDept) int64 {
  88. dept.DelFlag = "0"
  89. session := base.GetSession().Session
  90. session.Begin()
  91. insert, err := session.Insert(&dept)
  92. if err != nil {
  93. gotool.Logs.ErrorLog().Println(err)
  94. session.Rollback()
  95. return 0
  96. }
  97. session.Commit()
  98. return insert
  99. }
  100. // CheckDeptNameUnique 校验部门名称是否唯一
  101. func (d DeptDao) CheckDeptNameUnique(dept model.SysDept) int64 {
  102. session := base.GetSession().Session
  103. count, err := session.Table("sys_dept").Cols("id").Where("dept_name=?", dept.DeptName).And("parent_id = ?", dept.ParentId).Limit(1).Count()
  104. if err != nil {
  105. gotool.Logs.ErrorLog().Println(err)
  106. return 1
  107. }
  108. return count
  109. }
  110. // HasChildByDeptId 是否存在部门子节点
  111. func (d DeptDao) HasChildByDeptId(id int64) int64 {
  112. count, _ := base.GetSession().Session.Table("sys_dept").Cols("id").Where("parent_id = ?", id).
  113. And("del_flag = '0'").Limit(1).Count()
  114. return count
  115. }
  116. // CheckDeptExistUser 查询部门是否存在用户
  117. func (d DeptDao) CheckDeptExistUser(id int64) int64 {
  118. count, _ := base.GetSession().Session.Table("sys_user").Cols("id").Where("dept_id = ?", id).
  119. And("del_flag = '0'").Count()
  120. return count
  121. }
  122. // Delete 删除部门
  123. func (d DeptDao) Delete(id int64) int64 {
  124. dept := model.SysDept{
  125. Id: id,
  126. }
  127. session := base.GetSession().Session
  128. session.Begin()
  129. i, err := session.Where("id = ?", id).Delete(&dept)
  130. if err != nil {
  131. session.Rollback()
  132. gotool.Logs.ErrorLog().Println(err)
  133. return 0
  134. }
  135. session.Commit()
  136. return i
  137. }
  138. func (d DeptDao) Update(dept model.SysDept) int64 {
  139. session := base.GetSession().Session.Table("sys_dept")
  140. session.Begin()
  141. _, err := session.Where("id = ?", dept.Id).Update(&dept)
  142. if err != nil {
  143. gotool.Logs.ErrorLog().Println(err)
  144. session.Rollback()
  145. return 0
  146. }
  147. session.Commit()
  148. return 1
  149. }