sys_dept.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // ============================================================================
  2. // This is auto-generated by gf cli tool only once. Fill this file as you wish.
  3. // ============================================================================
  4. package sys_dept
  5. import (
  6. "database/sql"
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gtime"
  10. )
  11. //数据列表数据结构
  12. type Dept struct {
  13. SearchValue interface{} `json:"searchValue"`
  14. CreateBy string `json:"createBy" orm:"create_by"`
  15. CreateTime *gtime.Time `json:"createTime" orm:"create_time"`
  16. UpdateBy string `json:"updateBy" orm:"update_by"`
  17. UpdateTime *gtime.Time `json:"updateTime" orm:"update_time"`
  18. Remark string `json:"remark"`
  19. DataScope interface{} `json:"dataScope"`
  20. Params map[string]interface{} `json:"params"`
  21. DeptID int64 `json:"deptId" orm:"dept_id"`
  22. ParentID int64 `json:"parentId" orm:"parent_id"`
  23. Ancestors string `json:"ancestors" orm:"ancestors"`
  24. DeptName string `json:"deptName" orm:"dept_name"`
  25. OrderNum int `json:"orderNum" orm:"order_num" `
  26. Leader string `json:"leader" orm:"leader"`
  27. Phone string `json:"phone" orm:"phone"`
  28. Email string `json:"email" orm:"email"`
  29. Status string `json:"status" orm:"status"`
  30. DelFlag string `json:"delFlag" orm:"del_flag" `
  31. ParentName string `json:"parentName"`
  32. Children []interface{} `json:"children"`
  33. }
  34. //查询参数
  35. //文章搜索参数
  36. type SearchParams struct {
  37. DeptName string `p:"deptName"`
  38. Status string `p:"status"`
  39. }
  40. type AddParams struct {
  41. ParentID int `json:"parentId" orm:"parent_id" p:"parentId" v:"required#父级不能为空"`
  42. DeptName string `json:"deptName" orm:"dept_name" p:"deptName" v:"required#部门名称不能为空"`
  43. OrderNum int `json:"orderNum" orm:"order_num" p:"orderNum" v:"required#排序不能为空"`
  44. Leader string `json:"leader" orm:"leader" p:"leader" v:"required#负责人不能为空"`
  45. Phone string `json:"phone" orm:"Phone" p:"phone" v:"required#电话不能为空"`
  46. Email string `json:"email" orm:"email" p:"email" v:"required#邮箱不能为空"`
  47. Status string `json:"status" orm:"status" p:"status" v:"required#状态必须"`
  48. Ancestors string `json:"ancestors" orm:"ancestors"`
  49. DelFlag string `json:"delFlag" orm:"del_flag"`
  50. CreateBy string `json:"createBy" orm:"create_by"`
  51. CreateTime *gtime.Time `json:"createTime" orm:"create_time"`
  52. UpdateBy string `json:"updateBy" orm:"update_by"`
  53. UpdateTime *gtime.Time `json:"updateTime" orm:"update_time"`
  54. }
  55. type EditParams struct {
  56. DeptID int64 `json:"deptId" orm:"dept_id" p:"id" v:"integer|min:1#ID只能为整数|ID只能为正数"`
  57. AddParams
  58. }
  59. //获取列表数据
  60. func GetList(searchParams *SearchParams) ([]*Dept, error) {
  61. model := g.DB().Table(Table)
  62. if searchParams.DeptName != "" {
  63. model.Where("dept_name like ?", "%"+searchParams.DeptName+"%")
  64. }
  65. if searchParams.Status != "" {
  66. model.Where("status", searchParams.Status)
  67. }
  68. depts := ([]*Dept)(nil)
  69. if err := model.Structs(&depts); err != nil {
  70. return nil, err
  71. }
  72. for _, v := range depts {
  73. if v.Children == nil {
  74. v.Children = []interface{}{}
  75. }
  76. }
  77. return depts, nil
  78. }
  79. //添加
  80. func AddDept(data *AddParams) (sql.Result, error) {
  81. data.DelFlag = "0"
  82. data.CreateBy = ""
  83. data.CreateTime = gtime.Now()
  84. return Model.Data(data).Insert()
  85. }
  86. //编辑
  87. func EditDept(data *EditParams) error {
  88. data.UpdateBy = ""
  89. data.UpdateTime = gtime.Now()
  90. if _, err := Model.Where("dept_id", data.DeptID).Data(data).Update(); err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. //删除失败
  96. func DelDept(id int64) error {
  97. ids, _ := GetChilderenIds(id)
  98. _, err := Model.Where("dept_id IN(?)", ids).Delete()
  99. if err != nil {
  100. return gerror.New("删除失败")
  101. }
  102. return nil
  103. }
  104. //根据部门id获取部门信息
  105. func GetDeptById(id int64) (*Dept, error) {
  106. dept := (*Dept)(nil)
  107. if err := Model.Where("dept_id", id).Struct(&dept); err != nil {
  108. return nil, err
  109. } else {
  110. return dept, nil
  111. }
  112. }
  113. /**
  114. 获取排除节点
  115. */
  116. func Exclude(id int64) ([]*Dept, error) {
  117. ids, err := GetChilderenIds(id)
  118. if err != nil {
  119. return nil, err
  120. }
  121. model := g.DB().Table(Table)
  122. if len(ids) > 0 {
  123. model.Where("dept_id NOT IN(?)", ids)
  124. }
  125. depts := ([]*Dept)(nil)
  126. if err := model.Structs(&depts); err != nil {
  127. return nil, err
  128. }
  129. for _, v := range depts {
  130. if v.Children == nil {
  131. v.Children = []interface{}{}
  132. }
  133. }
  134. return depts, nil
  135. }
  136. /**
  137. 根据id获取子孙节点id集合包含本身
  138. */
  139. func GetChilderenIds(id int64) ([]int64, error) {
  140. list, err := GetChildrenById(id)
  141. if err != nil {
  142. return nil, err
  143. }
  144. if len(list) == 0 {
  145. return []int64{}, nil
  146. }
  147. var newResult []int64
  148. for _, v := range list {
  149. newResult = append(newResult, v.DeptId)
  150. }
  151. return newResult, nil
  152. }
  153. /**
  154. 根据id获取所有子孙节点包含本身
  155. */
  156. func GetChildrenById(id int64) ([]*Entity, error) {
  157. depts, err := Model.All()
  158. if err != nil {
  159. return nil, err
  160. }
  161. result := recursion(id, depts, true)
  162. return result, nil
  163. }
  164. /**
  165. 根据id获取所有子孙元素
  166. hasroot true - 包含自身 false - 不含自身
  167. */
  168. func recursion(id int64, depts []*Entity, hasRoot bool) (result []*Entity) {
  169. for _, v := range depts {
  170. if hasRoot == true && v.DeptId == id {
  171. result = append(result, v)
  172. }
  173. if v.ParentId == id {
  174. data := recursion(v.DeptId, depts, false)
  175. result = append(result, v)
  176. result = append(result, data...)
  177. }
  178. }
  179. return
  180. }