kimi
2020-03-31 74472c9d22dddcb41383794caf0011043b20f817
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
package com.highdatas.mdm.controller;
 
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.MasterAuthorType;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kimi
 * @since 2020-03-23
 */
@RestController
@RequestMapping("/masterAuthor")
public class MasterAuthorController {
 
    @Autowired
    IMasterAuthorService authorService;
    @Autowired
    IMasterAuthorDetailService authorDetailService;
    @Autowired
    ITUserRoleService userRoleService;
    @Autowired
    IMenuMappingService menuMappingService;
    @Autowired
    ISysMenuService menuService;
 
    public static final String masterAuthorType = "masterAuthorType";
    public static final String masterId = "masterId";
    public static final String tableName = "table_name";
    public static final String characterId = "characterId";
    public static final String character_id = "character_id";
    public static final String fields = "fields";
    private String maintainFieldId = "maintainFieldId";
    private String maintain_field_id = "maintain_field_id";
 
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    public Result deleteModel(@RequestBody MasterAuthor masterAuthor)  {
        if (!StringUtils.isEmpty(masterAuthor.getId())) {
            masterAuthor.setUpdateTime(new Date());
        }else {
            masterAuthor.setId(DbUtils.getUUID()).setCreateTime(new Date());
        }
        String menuId = masterAuthor.getMenuId();
        String tableName = menuMappingService.getTableNameByMenu(menuId);
        masterAuthor.setTableName(tableName);
        //delete pre
        boolean delete = authorDetailService.delete(new EntityWrapper<MasterAuthorDetail>().eq(Constant.PARENT_ID, masterAuthor.getId()));
        if (!delete){
            return Result.error(CodeMsg.DELETE_ERROR);
        }
 
        for (MasterAuthorDetail detail : masterAuthor.getFields()) {
            detail.setId(DbUtils.getUUID()).setParentId(masterAuthor.getId()).insert();
        }
 
        boolean b = masterAuthor.insertOrUpdate();
        if (b) {
            return Result.success(masterAuthor);
        }else {
            return Result.error(CodeMsg.UPDATE_ERROR);
        }
    }
 
    @RequestMapping(value = "/get/{characterId}", method = RequestMethod.GET)
    public Result get(@PathVariable String characterId, @RequestParam MasterAuthorType type, HttpServletRequest request){
        List<MasterAuthor> masterAuthorList = authorService.selectList(new EntityWrapper<MasterAuthor>().eq("type", type.name()).eq("character_id", characterId));
        if (type.equals(MasterAuthorType.role) && masterAuthorList.isEmpty()) {
            return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
        }else if (type.equals(MasterAuthorType.user) && masterAuthorList.isEmpty()){
            //user 获取角色 多脚色混合
            String userId = characterId;
            List<TUserRole> tUserRoles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq(Constant.USERID, userId));
            List<String> roleIdList = tUserRoles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
            HashMap<String, MasterAuthor> tableMasterAuthor = authorService.merageRoleAuthor(roleIdList);
            if (tableMasterAuthor == null) {
                Result.success(null);
            }
            JSONObject object = new JSONObject();
            ArrayList<MasterAuthor> list = new ArrayList(tableMasterAuthor.values());
 
            object.fluentPut("type", MasterAuthorType.role);
            object.fluentPut("author",list);
 
            Set<String> collect = list.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toSet());
            LinkedHashSet<String>  menuIds= new LinkedHashSet<>(collect);
            LinkedHashSet<String> byParentId = menuService.getByParentId(menuIds);
            List<SysMenu> sysMenus = menuService.selectBatchIds(byParentId);
            object.fluentPut("audit",sysMenus);
            return Result.success(object);
        }
 
        for (MasterAuthor masterAuthor : masterAuthorList) {
            List<MasterAuthorDetail> masterAuthorDetails = authorDetailService
                    .selectList(new EntityWrapper<MasterAuthorDetail>()
                            .eq(Constant.PARENT_ID, masterAuthor.getId()));
            masterAuthor.setFields(masterAuthorDetails);
        }
 
        JSONObject object = new JSONObject();
        Set<String> collect = masterAuthorList.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toSet());
        LinkedHashSet<String>  menuIds= new LinkedHashSet<>(collect);
        LinkedHashSet<String> byParentId = menuService.getByParentId(menuIds);
        List<SysMenu> sysMenus = menuService.selectBatchIds(byParentId);
 
        object.fluentPut("type", MasterAuthorType.user);
        object.fluentPut("author",masterAuthorList);
        object.fluentPut("audit",sysMenus);
        return Result.success(object);
    }
 
    @RequestMapping(value = "/delete/{characterId}", method = RequestMethod.GET)
    public Result delete(@PathVariable String characterId, @RequestParam MasterAuthorType type){
        List<MasterAuthor> masterAuthorList = authorService.selectList(new EntityWrapper<MasterAuthor>()
                .eq(Constant.TYPE, type).eq(this.character_id, characterId));
        if (masterAuthorList.isEmpty()) {
            return Result.success(null);
        }
        boolean delete = false;
        for (MasterAuthor masterAuthor : masterAuthorList) {
            delete = authorDetailService.delete(new EntityWrapper<MasterAuthorDetail>().eq(Constant.PARENT_ID, masterAuthor.getId()));
            if (delete) {
                delete = masterAuthor.deleteById();
            }
        }
 
        if (delete) {
            return Result.success(CodeMsg.DELETE_SUCCESS);
        }else  {
            return Result.error(CodeMsg.DELETE_ERROR);
        }
    }
 
 
    @RequestMapping(value = "/delete/menu/{menuId}", method = RequestMethod.GET)
    public Result deleteTable(@PathVariable String menuId, @RequestParam String characterId, @RequestParam MasterAuthorType type, HttpServletRequest request){
        String maintainFieldId = request.getParameter(this.maintainFieldId);
        Wrapper<MasterAuthor> masterAuthorWrapper = new EntityWrapper<MasterAuthor>()
                .eq(Constant.TYPE, type)
                .eq(character_id, characterId)
                .eq("menu_id", menuId);
        if (!StringUtils.isEmpty(maintainFieldId)) {
            masterAuthorWrapper.eq(this.maintain_field_id, maintainFieldId);
        }
 
        List<MasterAuthor> masterAuthorList = authorService.selectList(masterAuthorWrapper);
        if (masterAuthorList.isEmpty()) {
            return Result.success(null);
        }
        boolean partDel = false;
        if (masterAuthorList.size() == 1 && !StringUtils.isEmpty(maintainFieldId)) {
            // 只有一个且删除 字段版本 保留本条
            partDel = true;
        }
        boolean delete = false;
        for (MasterAuthor masterAuthor : masterAuthorList) {
            delete = authorDetailService.delete(new EntityWrapper<MasterAuthorDetail>().eq(Constant.PARENT_ID, masterAuthor.getId()));
            if (delete) {
                if (partDel) {
                    delete = masterAuthor.setMaintainFieldId(null).updateById();
                }else {
                    delete = masterAuthor.deleteById();
                }
 
            }
        }
 
        if (delete) {
            return Result.success(CodeMsg.DELETE_SUCCESS);
        }else  {
            return Result.error(CodeMsg.DELETE_ERROR);
        }
    }
 
    @RequestMapping(value = "/addRole/{roleId}", method = RequestMethod.GET)
    public Result addRole(@PathVariable String roleId, HttpServletRequest request){
        TUser user = DbUtils.getUser(request);
        String userId = user.getUserId();
        List<TUserRole> tUserRoles = userRoleService.selectList(new EntityWrapper<TUserRole>().eq(Constant.USERID, userId));
        List<String> roleIdList = tUserRoles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
        roleIdList.add(roleId);
 
        HashMap<String, MasterAuthor> tableMasterAuthor = authorService.merageRoleAuthor(roleIdList);
        if (tableMasterAuthor == null) {
            Result.success(null);
        }
        return Result.success(tableMasterAuthor.values());
    }
}