sys_dept.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package system
  2. import (
  3. "context"
  4. "hongze/hrms_api/global"
  5. "hongze/hrms_api/models/base"
  6. )
  7. // 部门表
  8. type SysDept struct {
  9. DeptId int64 `gorm:"primaryKey;column:dept_id" json:"dept_id"`
  10. ParentId int64 `gorm:"column:parent_id" json:"parent_id"` //父级部门ID
  11. DeptName string `gorm:"column:dept_name" json:"dept_name"` //部门名称
  12. RootId int64 `gorm:"column:root_id" json:"root_id"` //顶层部门ID
  13. Sort int8 `gorm:"column:sort" json:"sort"` //排序序号
  14. Level int8 `gorm:"column:level" json:"level"` //当前层级
  15. base.TimeBase
  16. }
  17. // TableName get sql table name.获取数据库表名
  18. func (d *SysDept) TableName() string {
  19. return "sys_dept"
  20. }
  21. // SysDeptColumns get sql column name.获取数据库列名
  22. var SysDeptColumns = struct {
  23. DeptID string
  24. ParentID string
  25. DeptName string
  26. CreateTime string
  27. ModifyTime string
  28. }{
  29. DeptID: "dept_id",
  30. ParentID: "parent_id",
  31. DeptName: "dept_name",
  32. CreateTime: "create_time",
  33. ModifyTime: "modify_time",
  34. }
  35. // SelectPage 分页查询
  36. func (d *SysDept) SelectPage(page base.IPage, condition string, pars []interface{}) (count int64, results []SysDept, err error) {
  37. results = make([]SysDept, 0)
  38. query := global.DEFAULT_MYSQL.WithContext(context.TODO()).Model(SysDept{}).Where(condition, pars...)
  39. query.Count(&count)
  40. if len(page.GetOrderItemsString()) > 0 {
  41. query = query.Order(page.GetOrderItemsString())
  42. }
  43. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  44. return
  45. }
  46. func (d *SysDept) GetDeptByName(name string) (list []*SysDept, err error) {
  47. err = global.DEFAULT_MYSQL.Model(d).Where("dept_name like ?", name).Find(&list).Error
  48. return
  49. }
  50. func (d *SysDept) GetDeptByDeptId(deptId int64) (item *SysDept, err error) {
  51. err = global.DEFAULT_MYSQL.Model(d).Where("dept_id = ?", deptId).First(&item).Error
  52. return
  53. }
  54. func (d *SysDept) GetDeptListByCondition(condition string, pars []interface{}, orderStr string) (list []*SysDept, err error) {
  55. list = make([]*SysDept, 0)
  56. if orderStr == "" {
  57. orderStr = "sort asc, create_time desc"
  58. }
  59. err = global.DEFAULT_MYSQL.WithContext(context.TODO()).Model(d).
  60. Where(condition, pars...).Order(orderStr).Find(&list).Error
  61. return
  62. }
  63. type SysDeptAddReq struct {
  64. ParentId int64 `json:"parent_id"` // id
  65. DeptName string `json:"dept_name" binding:"required"` // 部门名称
  66. Sort int8 `json:"sort"`
  67. }
  68. type SysDeptEditReq struct {
  69. DeptId int64 `json:"dept_id" binding:"required,gte=1"`
  70. ParentId int64 `json:"parent_id"` // id
  71. DeptName string `json:"dept_name" binding:"required"` // 部门名称
  72. Sort int8 `json:"sort"`
  73. }
  74. // 新增
  75. func (d *SysDept) Add() (err error) {
  76. err = global.DEFAULT_MYSQL.Create(d).Error
  77. return
  78. }
  79. // 删除
  80. func (d *SysDept) DeleteByCondition(condition string, pars []interface{}) (err error) {
  81. err = global.DEFAULT_MYSQL.Where(condition, pars...).Delete(d).Error
  82. return
  83. }
  84. // 修改
  85. func (d *SysDept) Update(updateCols []string) (err error) {
  86. err = global.DEFAULT_MYSQL.Model(d).Select(updateCols).Updates(d).Error
  87. return
  88. }
  89. type DeptListReq struct {
  90. base.PageReq
  91. DeptName string `json:"dept_name" form:"dept_name"` //部门名称
  92. }
  93. type DeptListItemResp struct {
  94. DeptId int64 `json:"dept_id"`
  95. ParentId int64 `json:"parent_id"` // id
  96. DeptName string `json:"dept_name"` // 部门名称
  97. Sort int8 `json:"sort"` //排序
  98. CreateTime string `json:"create_time"` //创建时间
  99. ModifyTime string `json:"modify_time"` //最后更新时间
  100. Children []*DeptListItemResp
  101. }
  102. type DelSysDeptReq struct {
  103. DeptId int64 `json:"dept_id" binding:"required,gte=1"`
  104. }
  105. type DeptAdminListTmpItem struct {
  106. AdminId uint64 `json:"admin_id"` //账号id
  107. RealName string `json:"real_name"` //真实姓名
  108. DeptId int64 `json:"dept_id"`
  109. ParentId int64 `json:"parent_id"` // id
  110. DeptName string `json:"dept_name"` // 部门名称
  111. Sort int8 `json:"sort"` //排序
  112. }
  113. type DeptAdminListItem struct {
  114. AdminId uint64 `json:"admin_id"` //账号id
  115. Name string `json:"name"` //账号名
  116. DeptId int64 `json:"dept_id"`
  117. ParentId int64 `json:"parent_id"` // id
  118. Sort int8 `json:"sort"` //排序
  119. Children []*DeptAdminListItem
  120. }