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

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

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

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

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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.SysRole;
import com.github.javaclub.base.domain.query.SysRoleQuery;
import com.github.javaclub.base.mapper.RoleMenuMapper;
import com.github.javaclub.base.mapper.SysRoleMapper;
import com.github.javaclub.base.mapper.UserRoleMapper;
import com.github.javaclub.base.service.SysRoleService;
import com.github.javaclub.sword.BizException;
import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Collections;
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 lombok.AllArgsConstructor;

/**
 * SysRoleService 业务逻辑实现
 *
 * @version $Id: SysRoleServiceImpl.java 2023-09-06 19:11:11 Exp $
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl implements SysRoleService {
	
	private final RoleMenuMapper roleMenuMapper;
	private final UserRoleMapper userRoleMapper;
	private final SysRoleMapper sysRoleMapper;

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

	@Override
	public ResultDO saveEntity(SysRole sysRole) {
		try {
			if (null == sysRole.getId()) {
				Long id = create(sysRole);
				if (Numbers.isPositiveNumber(id)) {
					return ResultDO.success(true);
				}
			}
			SysRole db = sysRoleMapper.selectById(sysRole.getId());
			if (null == db) {
				return ResultDO.failure(BasicMessage.NOT_FOUND);
			}
			boolean suc = update(sysRole);
			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(SysRole sysRole) {
		this.doCreateCheck(sysRole);
		int num = sysRoleMapper.insert(sysRole);
		if (0 >= num) {
			throw new BizException(BasicMessage.DB_INSERT_FAILED.getCode(), "创建记录失败!");
		}
		return sysRole.getId();
	}
	
	@Override
	public boolean update(SysRole sysRole) {
		this.doUpdateCheck(sysRole);
		int num = sysRoleMapper.updateById(sysRole);
		return num > 0;
	}
	
	@Override
	public SysRole selectById(Long id) {
		return sysRoleMapper.selectById(id);
	}
	
	@Override
	public SysRole selectOne(SysRoleQuery query) {
		QueryWrapper qw = query.queryWrapper();
		List list = sysRoleMapper.selectList(qw);
		if(null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

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

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

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


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveRoleAndRoleMenu(SysRole role) {
		this.save(role);
		if (Collections.isEmpty(role.getMenuIdList())) {
			return;
		}
		// 保存角色与菜单关系
		roleMenuMapper.insertRoleAndRoleMenu(role.getId(), role.getMenuIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateRoleAndRoleMenu(SysRole role) {
		// 更新角色
		sysRoleMapper.updateById(role);
		//先删除角色与菜单关系
		roleMenuMapper.deleteBatch(new Long[] { role.getId() });
		if (Collections.isEmpty(role.getMenuIdList())) {
			return;
		}
		//保存角色与菜单关系
		roleMenuMapper.insertRoleAndRoleMenu(role.getId(), role.getMenuIdList());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(Long[] roleIds) {
		//删除角色
		sysRoleMapper.deleteBatch(roleIds);

		//删除角色与菜单关联
		roleMenuMapper.deleteBatch(roleIds);

		//删除角色与用户关联
		userRoleMapper.deleteBatch(roleIds);
	}
	
	@Override
	public List listRoleIdByUserId(Long userId) {
		return sysRoleMapper.listRoleIdByUserId(userId);
	}
	
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy