package com.highdatas.mdm.service.impl; import com.alibaba.fastjson.JSONObject; import com.highdatas.mdm.entity.Maintain; import com.highdatas.mdm.entity.SysField; import com.highdatas.mdm.entity.TableSchemaResult; import com.highdatas.mdm.mapper.MaintainFieldMapper; import com.highdatas.mdm.mapper.MasterModifiedMapper; import com.highdatas.mdm.mapper.TableInfoMapper; import com.highdatas.mdm.pojo.ActivitiStatus; import com.highdatas.mdm.pojo.Segment; 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.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author kimi * @since 2020-03-11 */ @Service public class MasterModifiedServiceImpl implements IMasterModifiedService { @Autowired IMaintainService maintainService; @Autowired ISysFieldService fieldService; @Autowired MasterModifiedMapper masterModifiedMapper; @Autowired TableInfoMapper tableInfoMapper; @Autowired IMaintainFieldService maintainFieldService; @Autowired MaintainFieldMapper maintainFieldMapper; @Autowired MasterDataService masterDataService; public static final String master_id = "master_id"; public static final String pre_record = "pre_record"; public static final String table_name = "table_name"; public static final String master_modified = "master_modified"; @Override public Map getModifiedUserById(String maintainId, String masterId) { return null; } @Override public List> getModifiedUserByIds(String maintainId, List masterId) { return null; } @Override public boolean updateModifiedUserById(String maintainId, String masterId, String userId, List modifiedFieldList) { if (StringUtils.isEmpty(maintainId)) { return false; } try { Maintain nowMaintain = maintainService.selectById(maintainId); List fieldByMaintain = fieldService.getFieldByMaintain(maintainId); if (nowMaintain == null) { return false; } Map masterModifiedMap = masterModifiedMapper.selectByTableMasterId(DbUtils.quotedStr(nowMaintain.getTableName()), DbUtils.quotedStr(masterId)); if (masterModifiedMap == null) { masterModifiedMap = new HashMap<>(); masterModifiedMap.put(table_name, nowMaintain.getTableName()); masterModifiedMap.put(master_id, masterId); } String preRecordStr = masterModifiedMap.get(pre_record); JSONObject preRecordJSON = null; if (!StringUtils.isEmpty(preRecordStr)) { preRecordJSON = JSONObject.parseObject(preRecordStr); }else { preRecordJSON = new JSONObject(); } for (String field : modifiedFieldList) { preRecordJSON.fluentPut(field, userId); } masterModifiedMap.put(pre_record, preRecordJSON.toJSONString()); String id = masterModifiedMap.get(Constant.ID); if (StringUtils.isEmpty(id)) { masterModifiedMap.put(Constant.ID, DbUtils.getUUID()); insert(masterModifiedMap); }else { updateById(masterModifiedMap); } return true; } catch (Exception e){ e.printStackTrace(); return false; } } private void insert(Map masterModifiedMap) { String fields = masterModifiedMap.keySet().stream().collect(Collectors.joining(Constant.COMMA)); String values = masterModifiedMap.values().stream().map(s -> DbUtils.quotedStr(s)).collect(Collectors.joining(Constant.COMMA)); tableInfoMapper.insert(master_modified, fields, DbUtils.bracketStr(values)); } private void updateById(Map masterModifiedMap) { String id = masterModifiedMap.get(Constant.ID); Segment whereSegment = new Segment(Constant.ID, id); List updateSgement = DbUtils.map2Segment(masterModifiedMap); String updateStr = updateSgement.stream().map(segment -> segment.toString()).collect(Collectors.joining(Constant.COMMA)); tableInfoMapper.update(master_modified, updateStr, whereSegment.toString()); } @Override @Async public void dealFlow(String maintainId, ActivitiStatus status) { try { Maintain maintain = maintainService.selectById(maintainId); if (maintain == null) { return; } String tableName = maintain.getTableName(); String recordTableName = tableName + Constant.RECORD; List> modifiedList = masterModifiedMapper.selectByMaintainId(recordTableName, DbUtils.quotedStr(maintainId)); switch (status) { case open: for (Map masterModifiedMap : modifiedList) { String preJson = masterModifiedMap.get(pre_record); JSONObject jsonObject = JSONObject.parseObject(preJson); if (jsonObject == null){ continue; } Set keySet = jsonObject.keySet(); HashMap userMap = new HashMap<>(); for (String field : keySet) { String userId = jsonObject.getString(field); if (userMap.containsKey(userId)) { String preFieldStr = userMap.get(userId); field = preFieldStr + Constant.SEMICOLON + field; } userMap.put(userId, field); } List tableField = tableInfoMapper.getTableField(master_modified); List existsFieldList = tableField.stream().map(TableSchemaResult::getFieldName).collect(Collectors.toList()); Set userIdList = userMap.keySet(); if (!existsFieldList.containsAll(userIdList)) { //新增之前不存在的用户 List unExistsUserList = userIdList.stream().filter(s -> !existsFieldList.contains(s)).collect(Collectors.toList()); for (String s : unExistsUserList) { //新增字段 maintainFieldMapper.addCloumn(master_modified, s, Constant.Text); } } masterModifiedMap.putAll(userMap); updateById(masterModifiedMap); } break; case close: List unUserIdList = new ArrayList<>(); unUserIdList.add(Constant.ID); unUserIdList.add(master_id); unUserIdList.add(table_name); unUserIdList.add(pre_record); for (Map masterModifiedMap : modifiedList) { //todo 反更新 perjson 获取当前表所有可更改的用户 之后待修改 先从本地user表中拿 Set keySet = masterModifiedMap.keySet(); List userIdList = keySet.stream().filter(s -> !unUserIdList.contains(s)).collect(Collectors.toList()); JSONObject jsonObject = new JSONObject(); for (String s : userIdList) { String fields = masterModifiedMap.get(s); if (StringUtils.isEmpty(fields)) { continue; } String[] split = fields.split(Constant.SEMICOLON); for (String field : split) { jsonObject.fluentPut(field, s); } } masterModifiedMap.put(pre_record, jsonObject.toJSONString()); updateById(masterModifiedMap); } break; } } catch (Exception e) { e.printStackTrace(); } } @Override public void dealAssemble(String maintainId, String userId, boolean audit) { List fieldByMaintain = fieldService.getFieldByMaintain(maintainId); if(fieldByMaintain == null) { return; } String fieldStr = fieldByMaintain.stream().map(SysField::getField).collect(Collectors.joining(Constant.SEMICOLON)); List tableField = tableInfoMapper.getTableField(master_modified); long count = tableField.stream().map(tableSchemaResult -> tableSchemaResult.getFieldName()).filter(s -> s.equalsIgnoreCase(userId)).count(); if (count == 0) { maintainFieldMapper.addCloumn(master_modified, userId, Constant.Text); } Maintain maintain = maintainService.selectById(maintainId); String tableName = maintain.getTableName(); String recordTableName = tableName + Constant.RECORD; if (!audit) { masterModifiedMapper.insertUserByTableNameUserId(DbUtils.quotedStr(maintainId), DbUtils.quotedStr(tableName), recordTableName, userId, DbUtils.quotedStr(fieldStr)); } else { JSONObject jsonObject = new JSONObject(); for (SysField field : fieldByMaintain) { String oneField = field.getField(); jsonObject.fluentPut(oneField, userId); } masterModifiedMapper.insertJsonByTableNameUserId(DbUtils.quotedStr(maintainId), DbUtils.quotedStr(tableName), recordTableName, DbUtils.quotedStr(jsonObject.toJSONString())); } } }