ZhuDongming
2019-11-08 b284d078f196af80a105dc3bcb610d8ed37d9251
src/main/java/com/moral/service/impl/RoleServiceImpl.java
@@ -1,5 +1,7 @@
package com.moral.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -8,11 +10,14 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.moral.common.bean.Constants;
import com.moral.common.bean.PageBean;
import com.moral.common.util.ExampleUtil;
import com.moral.common.util.MyBatisBaseMapUtil;
import com.moral.entity.AccountRole;
import com.moral.entity.Role;
import com.moral.mapper.AccountRoleMapper;
import com.moral.mapper.RoleMapper;
import com.moral.service.RoleService;
import com.moral.util.TkMybatisUtils;
@@ -27,10 +32,8 @@
    @Resource
    RoleMapper roleMapper;
    @Override
    public PageBean<Role> queryByPageBean(PageBean pageBean) {
        return MyBatisBaseMapUtil.queryPage(roleMapper, pageBean, ENTITY_CLASS);
    }
    @Resource
    AccountRoleMapper accountRoleMapper;
    @Override
    @Transactional
@@ -45,10 +48,7 @@
                roleQuery.setRoleCode(role.getRoleCode());
                roleQuery.setIsDelete(Constants.IS_DELETE_FALSE);
                Role roleResult = roleMapper.selectOne(roleQuery);
                if (roleResult != null) {
                    role.setId(roleResult.getId());
                    roleMapper.updateByPrimaryKeySelective(role);
                } else {
                if (roleResult == null) {
                    role.setIsDelete(Constants.IS_DELETE_FALSE);
                    role.setCreateTime(new Date());
                    role.setCreateUser(role.getCreateUser());
@@ -61,16 +61,6 @@
    }
    @Override
    @Transactional
    public int deleteByIds(List<Integer> ids) {
        Role role = new Role();
        role.setIsDelete(Constants.IS_DELETE_TRUE);
        Example example = new Example(ENTITY_CLASS);
        example.or().andIn("id", ids);
        return roleMapper.updateByExampleSelective(role, example);
    }
    @Override
    public int countByExample(PageBean pageBean) {
        Example example = ExampleUtil.generateExample(ENTITY_CLASS, pageBean);
        TkMybatisUtils.addDeletesToExample(example);
@@ -78,9 +68,64 @@
    }
    @Override
    public List<Role> getRoleList(String roleName) {
        List<Role> roleList = roleMapper.getRoleList(roleName);
    public List<Role> getRoleListByName(String roleName) {
        List<Role> roleList = roleMapper.getRoleListByName(roleName);
        return roleList;
    }
    @Override
    public PageBean getRoleList(PageBean pageBean) {
        Example example = ExampleUtil.generateExample(ENTITY_CLASS, pageBean);
        TkMybatisUtils.addDeletesToExample(example);
        if (pageBean.getPageSize() > 0) {
            PageHelper.startPage(pageBean.getPageIndex(), pageBean.getPageSize());
        }
        List<Role> roleList = roleMapper.getRoleList(example);
        return new PageBean(roleList);
    }
    @Override
    @Transactional
    public void deleteByIds(Integer... ids) {
        Role role = new Role();
        role.setIsDelete(Constants.IS_DELETE_TRUE);
        Example example = new Example(ENTITY_CLASS);
        example.or().andIn("id", Arrays.asList(ids));
        roleMapper.updateByExampleSelective(role, example);
    }
    @Override
    public List<Integer> getRoleIds(int accountId) {
        AccountRole accountRole = new AccountRole();
        accountRole.setAccountId(accountId);
        List<AccountRole> accountRoleList = accountRoleMapper.select(accountRole);
        List<Integer> roleIds = new ArrayList<>();
        for(AccountRole accRole:accountRoleList ){
            roleIds.add(accRole.getRoleId());
        }
        return roleIds;
    }
    @Override
    public void allotRole(Integer accountId, Integer[] roleIds) {
        Example example = new Example(AccountRole.class);
        example.or().andEqualTo("accountId",accountId);
        accountRoleMapper.deleteByExample(example);
        if(roleIds!=null && roleIds.length >0){
            List<AccountRole> accountRoleList = new ArrayList<>();
            for(int roleId:roleIds){
                AccountRole accountRole = new AccountRole();
                accountRole.setAccountId(accountId);
                accountRole.setRoleId(roleId);
                accountRoleList.add(accountRole);
            }
            accountRoleMapper.insertList(accountRoleList);
        }
    }
    @Override
    public PageBean<Role> queryByPageBean(PageBean pageBean){
        return MyBatisBaseMapUtil.queryPage(roleMapper,pageBean,ENTITY_CLASS);
    }
}