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

com.addplus.server.security.service.service.RoleMenuFunctionBaseService Maven / Gradle / Ivy

The newest version!
package com.addplus.server.security.service.service;

import com.addplus.server.core.constant.CacheConstsBase;
import com.addplus.server.core.constant.ConfigConstsBase;
import com.addplus.server.core.exception.ErrorCodeBase;
import com.addplus.server.core.exception.ErrorException;
import com.addplus.server.core.model.authority.data.SysRoleMenuFunction;
import com.addplus.server.core.model.authority.datatransfer.SysRoleMenuFunctionDTO;
import com.addplus.server.core.utils.DataUtils;
import com.addplus.server.security.service.mapper.SysRoleMenuFunctionMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 类名: RoleMenufunctionService
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2017/12/11 下午1:48
 * @description 类描述: 角色-功能关联实现类
 */
@Service
public class RoleMenuFunctionBaseService {

    @Autowired
    private SysRoleMenuFunctionMapper sysRoleMenufunctionMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    public List getRoleMenuByRoleId(Long id) throws Exception {
        if (DataUtils.EmptyOrNegativeOrZero(id)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysRoleMenuFunction roleMenu = new SysRoleMenuFunction();
        roleMenu.setRId(id);
        roleMenu.setIsDeleted(0);
        List sysRoleMenufunctionList = sysRoleMenufunctionMapper.selectList(new QueryWrapper(roleMenu));
        if (sysRoleMenufunctionList == null) {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_NULL_DATA);
        }

        return sysRoleMenufunctionList;
    }

    public List getRoleMenuByRoleAndMenuId(Long menuId, Long roleId) throws Exception {
        if (DataUtils.EmptyOrNegativeOrZero(roleId, menuId)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysRoleMenuFunction roleMenu = new SysRoleMenuFunction();
        roleMenu.setRId(roleId);
        roleMenu.setMId(menuId);
        roleMenu.setIsDeleted(0);
        List sysRoleMenufunctionList = sysRoleMenufunctionMapper.selectList(new QueryWrapper(roleMenu));
        if (sysRoleMenufunctionList == null) {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_NULL_DATA);
        }

        return sysRoleMenufunctionList;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean addRoleMenufunction(SysRoleMenuFunction sysRoleMenufunction,String userAccount) throws Exception {
        if (sysRoleMenufunction == null || DataUtils.EmptyOrNegativeOrZero(sysRoleMenufunction.getMId(), sysRoleMenufunction.getRId())||DataUtils.isEmpty(userAccount)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        sysRoleMenufunction.setIsDeleted(0);
        LocalDateTime localDateTime = LocalDateTime.now();
        sysRoleMenufunction.setGmtCreate(localDateTime);
        sysRoleMenufunction.setGmtModified(localDateTime);
        sysRoleMenufunction.setModifyUser(userAccount);
        int count = sysRoleMenufunctionMapper.insert(sysRoleMenufunction);
        if (count > 0) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    // 事务提交成功后,异步方法调用
                    redisTemplate.convertAndSend(CacheConstsBase.REDIS_SHRIO_PERMISSION_TPOIC, CacheConstsBase.REDIS_SHRIO_PERMISSION_TPOIC);
                }
            });
            return true;
        } else {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_DATABASE_FAIL, false);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean logicalDeletedRoleMenufunctionById(Long id,String userAccount) throws Exception {
        if (DataUtils.EmptyOrNegativeOrZero(id)) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        SysRoleMenuFunction sysRoleMenufunction = new SysRoleMenuFunction();
        sysRoleMenufunction.setId(id);
        sysRoleMenufunction.setIsDeleted(0);
        int count = sysRoleMenufunctionMapper.selectCount(new QueryWrapper(sysRoleMenufunction));
        if (count > 0) {
            sysRoleMenufunction.setIsDeleted(1);
            sysRoleMenufunction.setGmtModified(LocalDateTime.now());
            sysRoleMenufunction.setModifyUser(userAccount);
            count = sysRoleMenufunctionMapper.updateById(sysRoleMenufunction);
            if (count > 0) {
                //更新内容shrio里面内容
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                    @Override
                    public void afterCommit() {
                        // 事务提交成功后,异步方法调用
                        redisTemplate.convertAndSend(CacheConstsBase.REDIS_SHRIO_PERMISSION_TPOIC, CacheConstsBase.REDIS_SHRIO_PERMISSION_TPOIC);
                    }
                });
                return true;
            } else {
                throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_DATABASE_FAIL, false);
            }
        } else {
            throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_NOT_AVAILABLE);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean saveRoleMenuFunction(SysRoleMenuFunctionDTO sysRoleMenuFunctionExt) throws Exception {
        if (sysRoleMenuFunctionExt == null) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        if (DataUtils.EmptyOrNegativeOrZero(sysRoleMenuFunctionExt.getRoleId())) {
            throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_PARAM);
        }
        if (sysRoleMenuFunctionExt.getMId() != null && sysRoleMenuFunctionExt.getMId().length > 0) {
            List sysRoleMenuFunctionList = getRoleMenuByRoleId(sysRoleMenuFunctionExt.getRoleId());
            List addList = new ArrayList<>();
            List delList = new ArrayList<>();
            for (Long mId : sysRoleMenuFunctionExt.getMId()) {
                boolean res = true;
                for (SysRoleMenuFunction r : sysRoleMenuFunctionList) {
                    if (mId.equals(r.getMId())) {
                        res = false;
                    }
                }
                if (res) {
                    addList.add(mId);
                }
            }
            for (SysRoleMenuFunction rm : sysRoleMenuFunctionList) {
                boolean res = true;
                for (Long m : sysRoleMenuFunctionExt.getMId()) {
                    if (rm.getMId().equals(m)) {
                        res = false;
                    }
                }
                if (res) {
                    delList.add(rm.getMId());
                }
            }
            int addRes = 0;
            int delRes = 0;
            if (delList.size() > 0) {
                addRes = sysRoleMenufunctionMapper.batchDelete(sysRoleMenuFunctionExt.getRoleId(), delList);
                if (addRes <= 0) {
                    throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_DATABASE_FAIL);
                }
            }
            if (addList.size() > 0) {
                delRes = sysRoleMenufunctionMapper.batchInsert(sysRoleMenuFunctionExt.getRoleId(), addList);
                if (delRes <= 0) {
                    throw new ErrorException(ErrorCodeBase.SYSTEM_ERROR_DATABASE_FAIL);
                }
            }
            if (addRes > 0 || delRes > 0) {
                //更新内容shrio里面内容
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                    @Override
                    public void afterCommit() {
                        // 事务提交成功后,异步方法调用
                        redisTemplate.convertAndSend(CacheConstsBase.REDIS_SHRIO_PERMISSION_TPOIC, CacheConstsBase.REDIS_SHRIO_PERMISSION_TPOIC);
                    }
                });
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy