lizijie
2021-04-14 c8748cf690e2806b59a5cf39732ee56995dd4e7b
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.ManageAccount;
import com.moral.api.entity.ManageAccountRole;
import com.moral.api.entity.ManageLog;
import com.moral.api.entity.ManageRole;
import com.moral.api.mapper.ManageAccountRoleMapper;
import com.moral.api.mapper.ManageLogMapper;
import com.moral.api.mapper.ManageRoleMapper;
import com.moral.api.mapper.ManageRoleMenuMapper;
import com.moral.api.pojo.dto.login.AccountInfoDTO;
import com.moral.api.service.ManageRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>
 * 后台角色表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class ManageRoleServiceImpl extends ServiceImpl<ManageRoleMapper, ManageRole> implements ManageRoleService {
 
    @Resource
    ManageRoleMapper roleMapper;
 
    public ManageRole getManageRoleByAccountId(String accountId){
        if(StringUtils.isEmpty(accountId))
            return null;
        return null;
    }
 
    @Autowired(required = false)
    private ManageRoleMapper manageRoleMapper;
 
    @Autowired(required = false)
    private ManageRoleMenuMapper manageRoleMenuMapper;
 
    @Autowired(required = false)
    private ManageAccountRoleMapper manageAccountRoleMapper;
 
    @Autowired
    LogUtils logUtils;
 
    @Override
    public Map<String,Object> getAllWithPagingQuery(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ManageRole> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        QueryWrapper<ManageRole> wrapper = new QueryWrapper();
        wrapper.eq("is_delete",0);
        wrapper.orderByDesc("create_time");
        com.baomidou.mybatisplus.extension.plugins.pagination.Page resultpage = manageRoleMapper.selectPage(page,wrapper);
        List<ManageRole> manageRoles = resultpage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> manageRolesList = new ArrayList<>();
        for (ManageRole manageRole:manageRoles) {
            Map manageRoleMap = JSON.parseObject(JSON.toJSONString(manageRole),Map.class);
            String createTime = SDF.format(manageRole.getCreateTime());
            String updateTime = SDF.format(manageRole.getUpdateTime());
            manageRoleMap.put("createTime",createTime);
            manageRoleMap.put("updateTime",updateTime);
            manageRoleMap.put("key",manageRole.getId());
            manageRolesList.add(manageRoleMap);
        }
        resultMap.put("manageRoles",manageRolesList);
        int totalNumber = manageRoleMapper.selectCount(wrapper);
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
 
    @Override
    @Transactional
    public Map<String,Object> insertOne(ManageRole manageRole, List list) {
        Map<String,Object> resultMap = new HashMap<>();
        if (manageRole.getName()==null){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (manageRoleMapper.getManageRoleByName(manageRole.getName()) != null){
            resultMap.put("code",ResponseCodeEnum.ROLE_IS_EXPIRE.getCode());
            resultMap.put("msg",ResponseCodeEnum.ROLE_IS_EXPIRE.getMsg());
            return resultMap;
        }
        manageRoleMapper.insertOne(manageRole);
        ManageRole manageRoleIns = manageRoleMapper.getManageRoleByName(manageRole.getName());
        int role_id = manageRoleIns.getId();
        List insertList = new ArrayList();
        for (Object temp: list) {
            Map<String,Integer> insertMap = new HashMap<>();
            insertMap.put("role_id",role_id);
            insertMap.put("menu_id",Integer.parseInt(temp.toString()));
            insertList.add(insertMap);
        }
        //判断在新建角色时,是否添加菜单
        if (insertList.size()>0){
            manageRoleMenuMapper.insertBatch(insertList);
        }
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "添加角色:"+manageRole.getName()+";";
        logUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE);
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    @Override
    @Transactional
    public Map<String,Object> updateManageRole(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (!map.containsKey("id")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (manageRoleMapper.getManageRoleById(Integer.parseInt(map.get("id").toString())) == null){
            resultMap.put("code",ResponseCodeEnum.ROLE_IS_NULL.getCode());
            resultMap.put("msg",ResponseCodeEnum.ROLE_IS_NULL.getMsg());
        }else {
            //在更新之前获取原来信息
            ManageRole manageRoleOld = manageRoleMapper.selectById(Integer.parseInt(map.get("id").toString()));
            manageRoleMapper.updateManageRoleById(map);
            //操作插入日志
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String content = "修改了角色:"+manageRoleOld.getName()+";";
            for (Object key:map.keySet()) {
                if (key.toString().equals("name")){
                    content = content+"角色名:"+manageRoleOld.getName()+"->"+map.get(key)+";";
                }
                if (key.toString().equals("desc")){
                    content = content+"备注:"+manageRoleOld.getDesc()+"->"+map.get(key)+";";
                }
            }
            logUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE);
            resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        }
        return resultMap;
    }
 
    @Override
    public Map<String,Object> getManageRoleByNameFuzzy(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ManageRole> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        QueryWrapper<ManageRole> wrapper = new QueryWrapper();
        wrapper.eq("is_delete",0);
        wrapper.like("name",map.get("name"));
        com.baomidou.mybatisplus.extension.plugins.pagination.Page resultpage = manageRoleMapper.selectPage(page,wrapper);
        List<ManageRole> manageRoles = resultpage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> manageRolesList = new ArrayList<>();
        for (ManageRole manageRole:manageRoles) {
            Map manageRoleMap = JSON.parseObject(JSON.toJSONString(manageRole),Map.class);
            String createTime = SDF.format(manageRole.getCreateTime());
            String updateTime = SDF.format(manageRole.getUpdateTime());
            manageRoleMap.put("createTime",createTime);
            manageRoleMap.put("updateTime",updateTime);
            manageRolesList.add(manageRoleMap);
        }
        resultMap.put("manageRoles",manageRolesList);
        int totalNumber = manageRoleMapper.selectCount(wrapper);
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
 
    @Override
    public Map<String, Object> deleteManageRole(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (!map.containsKey("id")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (manageRoleMapper.getManageRoleById(Integer.parseInt(map.get("id").toString())) == null){
            resultMap.put("code",ResponseCodeEnum.ROLE_IS_NULL.getCode());
            resultMap.put("msg",ResponseCodeEnum.ROLE_IS_NULL.getMsg());
        }else {
            ManageRole manageRole = new ManageRole();
            manageRole.setId(Integer.parseInt(map.get("id").toString()));
            manageRole.setIsDelete(Constants.DELETE);
            Map deleteMap = new HashMap();
            deleteMap.put("id",Integer.parseInt(map.get("id").toString()));
            deleteMap.put("is_delete",1);
            manageRoleMapper.updateManageRoleById(deleteMap);
            ManageAccountRole manageAccountRole = new ManageAccountRole();
            manageAccountRole.setIsDelete("1");
            QueryWrapper<ManageAccountRole> wrapper = new QueryWrapper();
            wrapper.eq("role_id",Integer.parseInt(map.get("id").toString()));
            manageAccountRoleMapper.update(manageAccountRole,wrapper);
            //操作插入日志
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            ManageRole manageRole1 = manageRoleMapper.selectById(Integer.parseInt(map.get("id").toString()));
            String content = "删除角色:"+manageRole1.getName()+";";
            logUtils.saveOperationForManage(request,content,Constants.DELETE_OPERATE_TYPE);
            resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        }
        return resultMap;
    }
}