123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package dao
- import (
- "github.com/druidcaesa/gotool"
- "ulink-admin/modules/system/models/model"
- "ulink-admin/modules/system/models/req"
- "ulink-admin/pkg/base"
- )
- type DeptDao struct {
- }
- // TreeSelect 根据条件查询部门集合
- func (d DeptDao) TreeSelect(query req.DeptQuery) *[]model.SysDept {
- depots := make([]model.SysDept, 0)
- session := base.GetSession().Session.Where("del_flag = '0'")
- if query.ParentId > 0 {
- session.And("parent_id = ?", query.ParentId)
- }
- if !gotool.StrUtils.HasEmpty(query.DeptName) {
- session.And("dept_name like concat('%', ?, '%')", query.DeptName)
- }
- if !gotool.StrUtils.HasEmpty(query.Status) {
- session.And("status = ?", query.Status)
- }
- if !base.GetCurUser().IsAdmin {
- session.And("company_id = ?", base.GetCurUser().ComponyId)
- }
- err := session.OrderBy("parent_id").Desc("order_num").Find(&depots)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return &depots
- }
- // SelectDeptListByRoleId 根据角色ID查询部门树信息
- func (d DeptDao) SelectDeptListByRoleId(id int64, strictly bool) *[]int64 {
- list := make([]int64, 0)
- session := base.GetSession().Session.Table([]string{"sys_dept", "d"}).Cols("d.id")
- session.Join("LEFT", []string{"sys_role_dept", "rd"}, "d.id = rd.id").
- Where("rd.id = ?", id)
- if strictly {
- 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)
- }
- if !base.GetCurUser().IsAdmin {
- session.And("d.company_id = ?", base.GetCurUser().ComponyId)
- }
- err := session.OrderBy("d.parent_id").OrderBy("d.order_num").Find(&list)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return &list
- }
- // GetList 查询部门列表
- func (d DeptDao) GetList(query req.DeptQuery) *[]model.SysDept {
- list := make([]model.SysDept, 0)
- session := base.GetSession().Session.OrderBy("parent_id").Desc("order_num")
- session.Where("del_flag = '0'")
- if query.ParentId > 0 {
- session.And("parent_id = ?", query.ParentId)
- }
- if gotool.StrUtils.HasNotEmpty(query.DeptName) {
- session.And("dept_name like concat('%', ?, '%')", query.DeptName)
- }
- if gotool.StrUtils.HasNotEmpty(query.Status) {
- session.And("status = ?", query.Status)
- }
- if !base.GetCurUser().IsAdmin {
- session.And("company_id = ?", base.GetCurUser().ComponyId)
- }
- err := session.Find(&list)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return &list
- }
- // GetDeptById 根据部门编号获取详细信息
- func (d DeptDao) GetDeptById(id int64) *model.SysDept {
- dept := model.SysDept{}
- _, err := base.GetSession().Session.Where("id = ?", id).Get(&dept)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return &dept
- }
- // Insert 添加部门数据
- func (d DeptDao) Insert(dept model.SysDept) int64 {
- dept.DelFlag = "0"
- session := base.GetSession().Session
- session.Begin()
- insert, err := session.Insert(&dept)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- session.Rollback()
- return 0
- }
- session.Commit()
- return insert
- }
- // CheckDeptNameUnique 校验部门名称是否唯一
- func (d DeptDao) CheckDeptNameUnique(dept model.SysDept) int64 {
- session := base.GetSession().Session
- count, err := session.Table("sys_dept").Cols("id").Where("dept_name=?", dept.DeptName).And("parent_id = ?", dept.ParentId).Limit(1).Count()
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return 1
- }
- return count
- }
- // HasChildByDeptId 是否存在部门子节点
- func (d DeptDao) HasChildByDeptId(id int64) int64 {
- count, _ := base.GetSession().Session.Table("sys_dept").Cols("id").Where("parent_id = ?", id).
- And("del_flag = '0'").Limit(1).Count()
- return count
- }
- // CheckDeptExistUser 查询部门是否存在用户
- func (d DeptDao) CheckDeptExistUser(id int64) int64 {
- count, _ := base.GetSession().Session.Table("sys_user").Cols("id").Where("dept_id = ?", id).
- And("del_flag = '0'").Count()
- return count
- }
- // Delete 删除部门
- func (d DeptDao) Delete(id int64) int64 {
- dept := model.SysDept{
- Id: id,
- }
- session := base.GetSession().Session
- session.Begin()
- i, err := session.Where("id = ?", id).Delete(&dept)
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return 0
- }
- session.Commit()
- return i
- }
- func (d DeptDao) Update(dept model.SysDept) int64 {
- session := base.GetSession().Session.Table("sys_dept")
- session.Begin()
- _, err := session.Where("id = ?", dept.Id).Update(&dept)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- session.Rollback()
- return 0
- }
- session.Commit()
- return 1
- }
|