|  | @@ -0,0 +1,215 @@
 | 
	
		
			
				|  |  | +package backend
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import (
 | 
	
		
			
				|  |  | +	"context"
 | 
	
		
			
				|  |  | +	"errors"
 | 
	
		
			
				|  |  | +	"fmt"
 | 
	
		
			
				|  |  | +	"kpt-pasture/model"
 | 
	
		
			
				|  |  | +	"net/http"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 | 
	
		
			
				|  |  | +	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 | 
	
		
			
				|  |  | +	"gitee.com/xuyiping_admin/pkg/xerr"
 | 
	
		
			
				|  |  | +	"gorm.io/gorm"
 | 
	
		
			
				|  |  | +)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// CreateOrUpdateSystemMenu 添加或者更新系统菜单权限
 | 
	
		
			
				|  |  | +func (s *StoreEntry) CreateOrUpdateSystemMenu(ctx context.Context, req *pasturePb.SearchMenuRequest) error {
 | 
	
		
			
				|  |  | +	if req.Id > 0 {
 | 
	
		
			
				|  |  | +		systemMenu := &model.SystemMenu{Id: int64(req.Id)}
 | 
	
		
			
				|  |  | +		if err := s.DB.Model(&model.SystemMenu{}).First(systemMenu).Error; err != nil {
 | 
	
		
			
				|  |  | +			if !errors.Is(err, gorm.ErrRecordNotFound) {
 | 
	
		
			
				|  |  | +				return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if err := s.DB.Model(&model.SystemMenu{}).Where(map[string]interface{}{
 | 
	
		
			
				|  |  | +		"id": req.Id,
 | 
	
		
			
				|  |  | +	}).Assign(map[string]interface{}{
 | 
	
		
			
				|  |  | +		"name":             req.Name,
 | 
	
		
			
				|  |  | +		"path":             req.Path,
 | 
	
		
			
				|  |  | +		"title":            req.Title,
 | 
	
		
			
				|  |  | +		"menu_type":        req.MenuType,
 | 
	
		
			
				|  |  | +		"parent_id":        req.ParentId,
 | 
	
		
			
				|  |  | +		"frame_src":        req.FrameSrc,
 | 
	
		
			
				|  |  | +		"frame_loading":    model.BooleanToIsShow(req.FrameLoading),
 | 
	
		
			
				|  |  | +		"keepalive":        model.BooleanToIsShow(req.Keepalive),
 | 
	
		
			
				|  |  | +		"hidden_tag":       model.BooleanToIsShow(req.HiddenTag),
 | 
	
		
			
				|  |  | +		"show_link":        model.BooleanToIsShow(req.ShowLink),
 | 
	
		
			
				|  |  | +		"show_parent":      model.BooleanToIsShow(req.ShowParent),
 | 
	
		
			
				|  |  | +		"icon":             req.Icon,
 | 
	
		
			
				|  |  | +		"component":        req.Component,
 | 
	
		
			
				|  |  | +		"redirect":         req.Redirect,
 | 
	
		
			
				|  |  | +		"auths":            req.Auths,
 | 
	
		
			
				|  |  | +		"rank":             req.Rank,
 | 
	
		
			
				|  |  | +		"extra_icon":       req.ExtraIcon,
 | 
	
		
			
				|  |  | +		"enter_transition": req.EnterTransition,
 | 
	
		
			
				|  |  | +		"leave_transition": req.LeaveTransition,
 | 
	
		
			
				|  |  | +		"active_path":      req.ActivePath,
 | 
	
		
			
				|  |  | +		"is_delete":        pasturePb.IsShow_Ok,
 | 
	
		
			
				|  |  | +		"is_show":          pasturePb.IsShow_Ok,
 | 
	
		
			
				|  |  | +	}).FirstOrCreate(&model.SystemMenu{}).Error; err != nil {
 | 
	
		
			
				|  |  | +		return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return nil
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SystemMenuTree(ctx context.Context) (*pasturePb.SystemMenuTreeResponse, error) {
 | 
	
		
			
				|  |  | +	systemMenuList := make([]*model.SystemMenu, 0)
 | 
	
		
			
				|  |  | +	if err := s.DB.Model(new(model.SystemMenu)).Where("is_delete = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | +		Where("is_show = ?", pasturePb.IsShow_Ok).Find(&systemMenuList).Error; err != nil {
 | 
	
		
			
				|  |  | +		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return &pasturePb.SystemMenuTreeResponse{
 | 
	
		
			
				|  |  | +		Code: http.StatusOK,
 | 
	
		
			
				|  |  | +		Msg:  "ok",
 | 
	
		
			
				|  |  | +		Data: model.SystemMenuSlice(systemMenuList).ToPB(),
 | 
	
		
			
				|  |  | +	}, nil
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// SearchSystemMenuList 菜单列表查询
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SearchSystemMenuList(ctx context.Context, req *pasturePb.SearchMenuRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMenuResponse, error) {
 | 
	
		
			
				|  |  | +	systemMenuList := make([]*model.SystemMenu, 0)
 | 
	
		
			
				|  |  | +	var count int64 = 0
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if err := s.DB.Model(new(model.SystemMenu)).
 | 
	
		
			
				|  |  | +		Where("is_delete = ? ", operationPb.IsShow_OK).
 | 
	
		
			
				|  |  | +		Order("parent_id").Count(&count).
 | 
	
		
			
				|  |  | +		Find(&systemMenuList).Error; err != nil {
 | 
	
		
			
				|  |  | +		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return &pasturePb.SearchMenuResponse{
 | 
	
		
			
				|  |  | +		Code: http.StatusOK,
 | 
	
		
			
				|  |  | +		Msg:  "ok",
 | 
	
		
			
				|  |  | +		Data: &pasturePb.SearchMenuData{
 | 
	
		
			
				|  |  | +			Page:     pagination.Page,
 | 
	
		
			
				|  |  | +			Total:    int32(count),
 | 
	
		
			
				|  |  | +			PageSize: pagination.PageSize,
 | 
	
		
			
				|  |  | +			List:     model.SystemMenuSlice(systemMenuList).ToPB(),
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}, nil
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// DeleteSystemMenu 删除系统菜单
 | 
	
		
			
				|  |  | +func (s *StoreEntry) DeleteSystemMenu(ctx context.Context, menuId int64) error {
 | 
	
		
			
				|  |  | +	systemMenu := &model.SystemMenu{Id: menuId}
 | 
	
		
			
				|  |  | +	if err := s.DB.First(systemMenu).Error; err != nil {
 | 
	
		
			
				|  |  | +		if errors.Is(err, gorm.ErrRecordNotFound) {
 | 
	
		
			
				|  |  | +			return xerr.Custom("该数据不存在")
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if err := s.DB.Model(systemMenu).Update("is_delete", pasturePb.IsShow_No).Error; err != nil {
 | 
	
		
			
				|  |  | +		return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return nil
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SearchSystemDeptList(ctx context.Context, req *pasturePb.SearchDeptRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDeptResponse, error) {
 | 
	
		
			
				|  |  | +	deptList := make([]*model.SystemDept, 0)
 | 
	
		
			
				|  |  | +	var count int64 = 0
 | 
	
		
			
				|  |  | +	pref := s.DB.Model(new(model.SystemDept)).Where("is_delete = ?", operationPb.IsShow_OK)
 | 
	
		
			
				|  |  | +	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.Order("sort desc").
 | 
	
		
			
				|  |  | +		Count(&count).
 | 
	
		
			
				|  |  | +		Limit(int(pagination.PageSize)).
 | 
	
		
			
				|  |  | +		Offset(int(pagination.PageOffset)).
 | 
	
		
			
				|  |  | +		Find(&deptList).Error; err != nil {
 | 
	
		
			
				|  |  | +		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	systemUserList, _ := s.SystemUserList(ctx)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return &pasturePb.SearchDeptResponse{
 | 
	
		
			
				|  |  | +		Code: http.StatusOK,
 | 
	
		
			
				|  |  | +		Msg:  "ok",
 | 
	
		
			
				|  |  | +		Data: &pasturePb.DeptData{
 | 
	
		
			
				|  |  | +			List:     model.SystemDeptSlice(deptList).ToPB(systemUserList),
 | 
	
		
			
				|  |  | +			Total:    int32(count),
 | 
	
		
			
				|  |  | +			PageSize: pagination.PageSize,
 | 
	
		
			
				|  |  | +			Page:     pagination.Page,
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}, nil
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SystemDepDelete(ctx context.Context, id int64) error {
 | 
	
		
			
				|  |  | +	dept := &model.SystemDept{Id: id}
 | 
	
		
			
				|  |  | +	if err := s.DB.First(dept).Error; err != nil {
 | 
	
		
			
				|  |  | +		return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if dept.IsShow == pasturePb.IsShow_No {
 | 
	
		
			
				|  |  | +		return nil
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	if err := s.DB.Model(dept).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
 | 
	
		
			
				|  |  | +		return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return nil
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +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)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	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
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SearchUserPastureList(ctx context.Context) (*pasturePb.SystemUserPastureListResponse, error) {
 | 
	
		
			
				|  |  | +	currentUser, err := s.GetCurrentSystemUser(ctx)
 | 
	
		
			
				|  |  | +	if err != nil {
 | 
	
		
			
				|  |  | +		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	farmList, err := s.FindPastureList(ctx, currentUser.PastureIds)
 | 
	
		
			
				|  |  | +	if err != nil {
 | 
	
		
			
				|  |  | +		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return &pasturePb.SystemUserPastureListResponse{
 | 
	
		
			
				|  |  | +		Code: http.StatusOK,
 | 
	
		
			
				|  |  | +		Msg:  "ok",
 | 
	
		
			
				|  |  | +		Data: model.AppPastureListSlice(farmList).ToPB(),
 | 
	
		
			
				|  |  | +	}, err
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func (s *StoreEntry) SystemDeptTree(ctx context.Context) (*pasturePb.DeptTreeResponse, error) {
 | 
	
		
			
				|  |  | +	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).Find(&systemDeptList).Error; err != nil {
 | 
	
		
			
				|  |  | +		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return &pasturePb.DeptTreeResponse{
 | 
	
		
			
				|  |  | +		Code: http.StatusOK,
 | 
	
		
			
				|  |  | +		Msg:  "ok",
 | 
	
		
			
				|  |  | +		Data: model.SystemDeptSlice(systemDeptList).ToTree(),
 | 
	
		
			
				|  |  | +	}, nil
 | 
	
		
			
				|  |  | +}
 |