All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.base.service.impl.SysMenuServiceImpl Maven / Gradle / Ivy

/* Automatic generated by CrudCodeGenerator wirtten by Gerald Chen
 *
 * @(#)SysMenuServiceImpl.java	 2023-09-06
 *
 * Copyright (c) 2023 - 2099. All Rights Reserved.
 *
 */

package com.github.javaclub.base.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.javaclub.base.domain.SysMenu;
import com.github.javaclub.base.domain.query.SysMenuQuery;
import com.github.javaclub.base.mapper.RoleMenuMapper;
import com.github.javaclub.base.mapper.SysMenuMapper;
import com.github.javaclub.base.service.SysMenuService;
import com.github.javaclub.sword.BizException;
import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Numbers;
import com.github.javaclub.sword.domain.QueryResult;
import com.github.javaclub.sword.domain.ResultDO;
import com.github.javaclub.sword.domain.enumtype.BasicMessage;
import com.github.javaclub.toolbox.utils.TreeNodeUtil;
import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;

/**
 * SysMenuService 业务逻辑实现
 *
 * @version $Id: SysMenuServiceImpl.java 2023-09-06 19:11:11 Exp $
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl implements SysMenuService {
	
	private final SysMenuMapper sysMenuMapper;
	private final RoleMenuMapper roleMenuMapper;

	/**
	 * 创建时的业务校验
	 * 
	 * @param sysMenu 业务实体
	 */
	void doCreateCheck(SysMenu sysMenu) {
	    BizObjects.requireNotNull(sysMenu, "DB实体不能为空");
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 更新时的业务校验
	 * 
	 * @param sysMenu 业务实体
	 */
	SysMenu doUpdateCheck(SysMenu sysMenu) {
		BizObjects.requireNotNull(sysMenu, "DB实体不能为空");
		BizObjects.requireNotNullGtZero(sysMenu.getId(), "主键ID不能为空");
		// TODO Auto-generated method stub
		
		return null;
	}

	@Override
	public ResultDO saveEntity(SysMenu sysMenu) {
		try {
			if (null == sysMenu.getId()) {
				Long id = create(sysMenu);
				if (Numbers.isPositiveNumber(id)) {
					return ResultDO.success(true);
				}
			}
			SysMenu db = sysMenuMapper.selectById(sysMenu.getId());
			if (null == db) {
				return ResultDO.failure(BasicMessage.NOT_FOUND);
			}
			boolean suc = update(sysMenu);
			if (suc) {
				return ResultDO.success(true);
			}
		} catch (BizException e) {
			return ResultDO.failure(e.getMessage(), e.getCode());
		} catch (Exception e) {
			return ResultDO.failure("数据库执行失败!", BasicMessage.EXCEPTIONED.getCode());
		}
		return ResultDO.failure();
	}

	@Override
	public Long create(SysMenu sysMenu) {
		this.doCreateCheck(sysMenu);
		int num = sysMenuMapper.insert(sysMenu);
		if (0 >= num) {
			throw new BizException(BasicMessage.DB_INSERT_FAILED.getCode(), "创建记录失败!");
		}
		return sysMenu.getId();
	}
	
	@Override
	public boolean update(SysMenu sysMenu) {
		this.doUpdateCheck(sysMenu);
		int num = sysMenuMapper.updateById(sysMenu);
		return num > 0;
	}
	
	@Override
	public SysMenu selectById(Long id) {
		return sysMenuMapper.selectById(id);
	}
	
	@Override
	public SysMenu selectOne(SysMenuQuery query) {
		QueryWrapper qw = query.queryWrapper();
		List list = sysMenuMapper.selectList(qw);
		if(null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int count(SysMenuQuery query) {
		Integer count = sysMenuMapper.selectCount(query.queryWrapper());
		return null != count ? count.intValue() : 0;
	}

	@Override
	public List findList(SysMenuQuery query) {
		QueryWrapper qw = query.queryWrapper();
		return sysMenuMapper.selectList(qw);
	}

	@Override
	public QueryResult findListWithCount(SysMenuQuery query) {
		QueryResult queryResult = new QueryResult();
		
		QueryWrapper qw = query.queryWrapper();
		Page page = new Page<>(query.getPageNo(), query.getPageSize());
		IPage pageResult = sysMenuMapper.selectPage(page, qw);
		
		if (null != pageResult) {
			queryResult.setTotalCount(pageResult.getTotal());
			queryResult.setEntry(pageResult.getRecords());
			queryResult.setSuccess(true);
		}
		return queryResult;
	}
	

	@Override
	public List listMenuByUserId(Long userId) {
		boolean isAdmin = Numbers.isIn(userId, "0,1");
		// 用户的所有菜单信息
		List sysMenus = isAdmin ? sysMenuMapper.listMenu() : 
										sysMenuMapper.listMenuByUserId(userId);
		if (com.github.javaclub.sword.core.Collections.isEmpty(sysMenus)) {
			return Collections.emptyList();
		}
		List menus = TreeNodeUtil.buildTree(Lists.newArrayList(sysMenus), SysMenu.class);
		
		return menus.stream()
				.sorted(Comparator.comparing(SysMenu::getOrderNum))
				.collect(Collectors.toList());
	}

	@Override
	public void deleteMenuAndRoleMenu(Long menuId) {
		//删除菜单
		this.removeById(menuId);
		//删除菜单与角色关联
		roleMenuMapper.deleteByMenuId(menuId);
	}

	
	@Override
	public List listMenuIdByRoleId(Long roleId) {
		return sysMenuMapper.listMenuIdByRoleId(roleId);
	}


	@Override
	public List listSimpleMenuNoButton() {
		return sysMenuMapper.listSimpleMenuNoButton();
	}

	@Override
	public List listRootMenu() {
		return sysMenuMapper.listRootMenu();
	}

	@Override
	public List listChildrenMenuByParentId(Long parentId) {
		return sysMenuMapper.listChildrenMenuByParentId(parentId);
	}

	@Override
	public List listMenuAndBtn() {
		return sysMenuMapper.listMenuAndBtn();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy