|  | @@ -6,7 +6,10 @@ import (
 | 
	
		
			
				|  |  |  	"fmt"
 | 
	
		
			
				|  |  |  	"kpt-pasture/model"
 | 
	
		
			
				|  |  |  	"net/http"
 | 
	
		
			
				|  |  | -	"strings"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	"go.uber.org/zap"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 | 
	
		
			
				|  |  |  	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 | 
	
	
		
			
				|  | @@ -161,27 +164,33 @@ func (s *StoreEntry) SystemDepDelete(ctx context.Context, id int64) error {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func (s *StoreEntry) SystemDeptCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDeptRequest) error {
 | 
	
		
			
				|  |  |  	if req.Id > 0 {
 | 
	
		
			
				|  |  | -		barn := &model.SystemDept{Id: int64(req.Id)}
 | 
	
		
			
				|  |  | -		if err := s.DB.Model(&model.SystemDept{}).First(barn).Error; err != nil {
 | 
	
		
			
				|  |  | -			if !errors.Is(err, gorm.ErrRecordNotFound) {
 | 
	
		
			
				|  |  | -				return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -			}
 | 
	
		
			
				|  |  | +		systemDept := &model.SystemDept{}
 | 
	
		
			
				|  |  | +		if err := s.DB.Model(new(model.SystemDept)).
 | 
	
		
			
				|  |  | +			Where("id = ?", req.Id).
 | 
	
		
			
				|  |  | +			Where("pasture_id = ?", req.PastureId).
 | 
	
		
			
				|  |  | +			First(systemDept).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("部门信息错误", zap.Any("req", req))
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if err := s.DB.Model(systemDept).
 | 
	
		
			
				|  |  | +			Where("id = ?", req.Id).
 | 
	
		
			
				|  |  | +			Where("pasture_id = ?", req.PastureId).
 | 
	
		
			
				|  |  | +			Updates(map[string]interface{}{
 | 
	
		
			
				|  |  | +				"name":      req.Name,
 | 
	
		
			
				|  |  | +				"parent_id": req.ParentId,
 | 
	
		
			
				|  |  | +				"sort":      req.Sort,
 | 
	
		
			
				|  |  | +				"remarks":   req.Remarks,
 | 
	
		
			
				|  |  | +				"is_show":   req.IsShow,
 | 
	
		
			
				|  |  | +				"is_delete": req.IsDelete,
 | 
	
		
			
				|  |  | +			}).Error; err != nil {
 | 
	
		
			
				|  |  | +			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	} else {
 | 
	
		
			
				|  |  | +		newSystemDepth := model.NewSystemDept(req)
 | 
	
		
			
				|  |  | +		if err := s.DB.Model(&model.SystemDept{}).Create(newSystemDepth).Error; err != nil {
 | 
	
		
			
				|  |  | +			return xerr.WithStack(err)
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	if err := s.DB.Model(&model.SystemDept{}).Where(map[string]interface{}{
 | 
	
		
			
				|  |  | -		"id": req.Id,
 | 
	
		
			
				|  |  | -	}).Assign(map[string]interface{}{
 | 
	
		
			
				|  |  | -		"name":      req.Name,
 | 
	
		
			
				|  |  | -		"sort":      req.Sort,
 | 
	
		
			
				|  |  | -		"parent_id": req.ParentId,
 | 
	
		
			
				|  |  | -		"remarks":   req.Remarks,
 | 
	
		
			
				|  |  | -		"is_delete": operationPb.IsShow_OK,
 | 
	
		
			
				|  |  | -		"is_show":   req.IsShow,
 | 
	
		
			
				|  |  | -		"dept_type": pasturePb.Depth_Department,
 | 
	
		
			
				|  |  | -		"leader_id": req.LeaderId,
 | 
	
		
			
				|  |  | -	}).FirstOrCreate(&model.SystemDept{}).Error; err != nil {
 | 
	
		
			
				|  |  | -		return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	return nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -203,7 +212,7 @@ func (s *StoreEntry) SearchUserPastureList(ctx context.Context) (*pasturePb.Syst
 | 
	
		
			
				|  |  |  	}, err
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func (s *StoreEntry) SystemDeptTree(ctx context.Context) (*pasturePb.DeptTreeResponse, error) {
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SystemDeptTree(ctx context.Context, req *pasturePb.SearchDeptRequest) (*pasturePb.DeptTreeResponse, error) {
 | 
	
		
			
				|  |  |  	currentUser, err := s.GetCurrentSystemUser(ctx)
 | 
	
		
			
				|  |  |  	if err != nil {
 | 
	
		
			
				|  |  |  		return nil, xerr.WithStack(err)
 | 
	
	
		
			
				|  | @@ -214,18 +223,104 @@ func (s *StoreEntry) SystemDeptTree(ctx context.Context) (*pasturePb.DeptTreeRes
 | 
	
		
			
				|  |  |  		return nil, xerr.Customf("请先将该用户绑定牧场!谢谢!")
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	pastureIds := strings.Split(pastureIdsStr, ",")
 | 
	
		
			
				|  |  | +	pastureIds := currentUser.GetPastureIds()
 | 
	
		
			
				|  |  |  	systemDeptList := make([]*model.SystemDept, 0)
 | 
	
		
			
				|  |  | -	if err = s.DB.Model(new(model.SystemDept)).
 | 
	
		
			
				|  |  | -		Where("is_delete = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | -		Where("is_show = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | -		Where("pasture_id IN ?", pastureIds).
 | 
	
		
			
				|  |  | -		Find(&systemDeptList).Error; err != nil {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	pref := s.DB.Model(new(model.SystemDept)).
 | 
	
		
			
				|  |  | +		Where("is_delete = ?", pasturePb.IsShow_Ok)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if req.PastureId > 0 {
 | 
	
		
			
				|  |  | +		pref.Where("pasture_id = ?", req.PastureId)
 | 
	
		
			
				|  |  | +	} else {
 | 
	
		
			
				|  |  | +		pref.Where("pasture_id IN ?", pastureIds)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if req.Name != "" {
 | 
	
		
			
				|  |  | +		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if req.IsShow > 0 {
 | 
	
		
			
				|  |  | +		pref.Where("is_show = ?", req.IsShow)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if err = pref.Find(&systemDeptList).Error; err != nil {
 | 
	
		
			
				|  |  |  		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if req.Name != "" {
 | 
	
		
			
				|  |  | +		parentDepths, err := s.findAllParentDepths(ctx, systemDeptList, pastureIds)
 | 
	
		
			
				|  |  | +		if err != nil {
 | 
	
		
			
				|  |  | +			return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		systemDeptList = append(systemDeptList, parentDepths...)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 没有搜到部门数据,默认返回所有牧场一级数据
 | 
	
		
			
				|  |  | +	if len(systemDeptList) == 0 {
 | 
	
		
			
				|  |  | +		systemDeptList = s.SearchDepthPastureList(ctx)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	return &pasturePb.DeptTreeResponse{
 | 
	
		
			
				|  |  |  		Code: http.StatusOK,
 | 
	
		
			
				|  |  |  		Msg:  "ok",
 | 
	
		
			
				|  |  |  		Data: model.SystemDeptSlice(systemDeptList).ToTree(),
 | 
	
		
			
				|  |  |  	}, nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SearchDepthPastureList(ctx context.Context) []*model.SystemDept {
 | 
	
		
			
				|  |  | +	systemDeptList := make([]*model.SystemDept, 0)
 | 
	
		
			
				|  |  | +	currentUser, err := s.GetCurrentSystemUser(ctx)
 | 
	
		
			
				|  |  | +	if err != nil {
 | 
	
		
			
				|  |  | +		return systemDeptList
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	pastureIds := currentUser.GetPastureIds()
 | 
	
		
			
				|  |  | +	if err = s.DB.Model(new(model.SystemDept)).
 | 
	
		
			
				|  |  | +		Where("is_delete = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | +		Where("pasture_id IN ?", pastureIds).
 | 
	
		
			
				|  |  | +		Where("parent_id = ?", 0).
 | 
	
		
			
				|  |  | +		Find(&systemDeptList).Error; err != nil {
 | 
	
		
			
				|  |  | +		return systemDeptList
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return systemDeptList
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// findAllParentDepths 递归查找所有上级部门
 | 
	
		
			
				|  |  | +func (s *StoreEntry) findAllParentDepths(ctx context.Context, depths []*model.SystemDept, pastureIds []int64) ([]*model.SystemDept, error) {
 | 
	
		
			
				|  |  | +	var allDepths []*model.SystemDept
 | 
	
		
			
				|  |  | +	var parentIDs []int64
 | 
	
		
			
				|  |  | +	foundDepths := make(map[int64]bool)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 收集所有需要查找的父部门ID
 | 
	
		
			
				|  |  | +	for _, dept := range depths {
 | 
	
		
			
				|  |  | +		if dept.ParentId > 0 && !foundDepths[dept.ParentId] {
 | 
	
		
			
				|  |  | +			parentIDs = append(parentIDs, dept.ParentId)
 | 
	
		
			
				|  |  | +			foundDepths[dept.ParentId] = true
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if len(parentIDs) == 0 {
 | 
	
		
			
				|  |  | +		return allDepths, nil
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 查询这些父部门
 | 
	
		
			
				|  |  | +	var parentDepths []*model.SystemDept
 | 
	
		
			
				|  |  | +	if err := s.DB.Model(new(model.SystemDept)).
 | 
	
		
			
				|  |  | +		Where("id IN ?", parentIDs).
 | 
	
		
			
				|  |  | +		Where("pasture_id IN ?", pastureIds).
 | 
	
		
			
				|  |  | +		Where("is_delete = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | +		Find(&parentDepths).Error; err != nil {
 | 
	
		
			
				|  |  | +		return nil, err
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 递归查找更上级的部门
 | 
	
		
			
				|  |  | +	higherParents, err := s.findAllParentDepths(ctx, parentDepths, pastureIds)
 | 
	
		
			
				|  |  | +	if err != nil {
 | 
	
		
			
				|  |  | +		return nil, err
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	allDepths = append(allDepths, parentDepths...)
 | 
	
		
			
				|  |  | +	allDepths = append(allDepths, higherParents...)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return allDepths, nil
 | 
	
		
			
				|  |  | +}
 |