ZhuDongming
2019-11-08 b284d078f196af80a105dc3bcb610d8ed37d9251
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.moral.service.impl;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
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;
 
import tk.mybatis.mapper.entity.Example;
 
@Service
public class RoleServiceImpl implements RoleService {
 
    private static Class ENTITY_CLASS = Role.class;
 
    @Resource
    RoleMapper roleMapper;
 
    @Resource
    AccountRoleMapper accountRoleMapper;
 
    @Override
    @Transactional
    public void addOrModify(Role role) {
        try {
            role.setUpdateTime(new Date());
            role.setUpdateUser(role.getUpdateUser());
            if (role.getId() != null) {
                roleMapper.updateByPrimaryKeySelective(role);
            } else {
                Role roleQuery = new Role();
                roleQuery.setRoleCode(role.getRoleCode());
                roleQuery.setIsDelete(Constants.IS_DELETE_FALSE);
                Role roleResult = roleMapper.selectOne(roleQuery);
                if (roleResult == null) {
                    role.setIsDelete(Constants.IS_DELETE_FALSE);
                    role.setCreateTime(new Date());
                    role.setCreateUser(role.getCreateUser());
                    roleMapper.insertSelective(role);
                }
            }
        } catch (Exception ex) {
            throw ex;
        }
    }
 
    @Override
    public int countByExample(PageBean pageBean) {
        Example example = ExampleUtil.generateExample(ENTITY_CLASS, pageBean);
        TkMybatisUtils.addDeletesToExample(example);
        return roleMapper.selectCountByExample(example);
    }
 
    @Override
    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);
    }
 
}