kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.highdatas.mdm.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.MenuMapping;
import com.highdatas.mdm.entity.SysMenu;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.mapper.MenuMappingMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import com.highdatas.mdm.util.WorkflowUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kimi
 * @since 2019-12-16
 */
@Service
public class MenuMappingServiceImpl extends ServiceImpl<MenuMappingMapper, MenuMapping> implements IMenuMappingService {
    @Autowired
    TableInfoMapper tableInfoMapper;
 
    @Autowired
    MenuMappingMapper menuMappingMapper;
 
    @Autowired
    IMaintainService maintainService;
 
    @Autowired
    ActivitiService activitiService;
 
    @Autowired
    ISysMenuService menuService;
    @Autowired
    IMaintainFieldService maintainFieldService;
    @Autowired
    IMasterAuthorService masterAuthorService;
 
    public static final String key = "process";
    /**
     *
     * @description:  创建menumapping对象
     * @param json menu mapping josn数据
     * @param session 用户从session里获取
     * @return: menu mapping对象
     *
     */
    @Override
    public MenuMapping create(String json, HttpSession session) {
        JSONObject jsonObject = JSONObject.parseObject(json);
//        //1
//        Flows flows = activitiService.start(key, session);
//        if (flows == null) {
//            return false;
//        }
//        //2
//        String flowsId = flows.getId();
        TUser user = (TUser)session.getAttribute(Constant.USER);
        String userId = user.getUserId();
        //Maintain maintain = createMaintain(userId, jsonObject, null);
        /*if (maintain == null) {
            return false;
        }*/
        //String maintainId = maintain.getId();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MenuMapping menuMapping = null;
        try {
            menuMapping = objectMapper.readValue(json, MenuMapping.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
        String menuId = menuMapping.getMenuId();
        if (StringUtils.isEmpty(menuId)) {
            return null;
        }
        int cnt = selectCount(new EntityWrapper<MenuMapping>().eq("menu_id", menuId));
        if (cnt != 0)  {
            return null;
        }
        //menuMapping.setMaintainFieldId(maintainId);
        return  create(menuMapping);
    }
    @Deprecated
    private Maintain createMaintain(String userId, JSONObject jsonObject, String flowsId) {
        Maintain maintain = new Maintain();
        maintain.setCreateTime(new Date());
 
        maintain.setChargeId(userId);
//        maintain.setFlowId(flowsId);
        maintain.setVersion(Constant.VERSION_Default);
        maintain.setId(DbUtils.getUUID());
        String desp = jsonObject.getString("desp");
        String tableName = jsonObject.getString("tableName");
        if(StringUtils.isEmpty(tableName)) {
            return null;
        }
        maintain.setDesp(desp);
        maintain.setTableName(tableName);
        boolean insert = maintainService.insert(maintain);
        if (insert) {
            return maintain;
        }
        return null;
    }
    /**
     *
     * @description:  保存menumapping对象
     * @param menuMapping menumapping 对象
     * @return: menu mapping对象
     *
     */
    @Override
    public MenuMapping create(MenuMapping menuMapping) {
        menuMapping.setCreateTime(new Date());
        menuMapping.setId(DbUtils.getUUID());
        String name = menuMapping.getName();
        name = DbUtils.getChineseOrEnglishOrNumber(name);
        String tableName = WorkflowUtils.toFirstChar(name.toLowerCase());
        tableName = Constant.MD + tableName;
        String tableByName = tableInfoMapper.selectTableByName(tableName);
 
        if (!StringUtils.isEmpty(tableByName)){
            int sameCount = selectCount(new EntityWrapper<MenuMapping>().eq("table_name", tableName));
            if (sameCount > 0) {
                tableName =  tableName + "_" +  DbUtils.getUUID(5);
            }
        }
        int length = 0;
        try {
            length = tableName.getBytes("UTF-8").length;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (length > 18) {
            tableName = Constant.MD + DbUtils.getUUID(10);
        }
        menuMapping.setTableName(tableName);
        boolean insert = this.insert(menuMapping);
        if (insert) {
            return menuMapping;
        } else {
            return null;
        }
    }
 
    @Override
    public boolean update(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MenuMapping menuMapping = null;
        try {
            menuMapping = objectMapper.readValue(json, MenuMapping.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
 
        //update menu name
        String menuId = menuMapping.getMenuId();
        SysMenu sysMenu = menuService.selectById(menuId);
        if (sysMenu == null) {
            return false;
        }
        sysMenu.setName(menuMapping.getName());
        sysMenu.updateById();
 
        return this.updateById(menuMapping);
    }
    /**
     *
     * @description:  通过主题获取menumapping
     * @param id 主题id
     * @param session 用户从session里获取
     * @return: menu mapping对象
     *
     */
    @Override
    public Result getMapping(HttpSession session, String id) {
        SysMenu sysMenu = menuService.selectById(id);
        if (sysMenu == null) {
            return Result.error(CodeMsg.SELECT_ERROR);
        }
        String menuType = sysMenu.getMenuType();
        if (StringUtils.isEmpty(menuType)) {
            return Result.error(CodeMsg.SELECT_ERROR);
        }
 
        if (menuType.equalsIgnoreCase("StructureMenu")) {
            return Result.success(null);
        }
 
        Map mapping = menuMappingMapper.getMapping(id);
 
        Wrapper<MenuMapping> menuWrapper = new EntityWrapper<MenuMapping>().eq("menu_id", sysMenu.getId());
        MenuMapping menuMapping = selectOne(menuWrapper);
        if (menuMapping == null) {
            return Result.error(new CodeMsg(10008,"当前主题无版本数据"));
        }
        String tableName = menuMapping.getTableName();
        String chargeId = menuMapping.getChargeId();
        TUser userByChargeId = DbUtils.getUserById(chargeId);
 
        TUser user = (TUser) session.getAttribute(Constant.USER);
 
        Maintain resultMaintain = masterAuthorService.getMaxVersionMaintain(user, tableName);
        Maintain nowVersion = maintainService.getNowVersion(tableName);
 
        if (resultMaintain != null) {
            if (nowVersion.getOrderNo().equals(resultMaintain.getOrderNo())) {
                mapping.put("maintainType", 0);
            }else {
                mapping.put("maintainType", 1);
            }
            mapping.put("version", resultMaintain.getVersion());
            mapping.put("maintainId", resultMaintain.getId());
        }
        if (userByChargeId != null) {
            mapping.put("userName", userByChargeId.getUserName());
        }
        if (nowVersion != null) {
            mapping.put("hasData", true);
        }else {
            mapping.put("hasData", false);
        }
        return Result.success(mapping);
 
    }
    /**
     *
     * @description:  通过表名获取主题
     * @param tableName 表名
     * @return: 主题对象
     *
     */
    @Override
    public SysMenu getMenuByTableName(String tableName) {
        MenuMapping menuMapping = selectOne(new EntityWrapper<MenuMapping>().eq("table_name", tableName).orderBy("create_time desc"));
        if (menuMapping == null) {
            return null;
        }
        String menuId = menuMapping.getMenuId();
        SysMenu sysMenu = menuService.selectById(menuId);
        return sysMenu;
    }
    /**
     *
     * @description:  通过主题获取menumapping
     * @param id 主题id
     * @param session 用户从session里获取
     * @return: menu mapping对象
     *
     */
    @Override
    public MenuMapping getMenuMappingByTableName(String tableName) {
        MenuMapping menuMapping = selectOne(new EntityWrapper<MenuMapping>().eq("table_name", tableName).orderBy("create_time desc"));
        if (menuMapping == null) {
            return null;
        }
        return menuMapping;
    }
 
 
    /**
     *
     * @description:  通过主题获取menumapping
     * @param menuId 主题id
     * @return: menumapping对象
     *
     */
    @Override
    public String getTableNameByMenu(String menuId) {
        MenuMapping menuMapping = selectOne(new EntityWrapper<MenuMapping>().eq("menu_id", menuId).orderBy("create_time desc"));
        if (menuMapping == null) {
            return null;
        }
        return menuMapping.getTableName();
 
    }
}