kimi42345
2020-03-17 6c6fdb4db59a2a2343e43ffd73a07f17b057c4fa
src/main/java/com/highdatas/mdm/service/impl/MasterModifiedServiceImpl.java
@@ -1,18 +1,26 @@
package com.highdatas.mdm.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.SysField;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.entity.TableSchemaResult;
import com.highdatas.mdm.mapper.MaintainFieldMapper;
import com.highdatas.mdm.mapper.MasterModifiedMapper;
import com.highdatas.mdm.service.IMaintainService;
import com.highdatas.mdm.service.IMasterModifiedService;
import com.highdatas.mdm.service.ISysFieldService;
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.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -30,9 +38,22 @@
    ISysFieldService fieldService;
    @Autowired
    MasterModifiedMapper masterModifiedMapper;
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    IMaintainFieldService maintainFieldService;
    @Autowired
    ITUserService userService;
    @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<String, String> getModifiedUserById(String maintainId, String masterId) {
        return null;
@@ -44,17 +65,156 @@
    }
    @Override
    public boolean updateModifiedUserById(String maintainId, String masterId, List<String> modifiedFieldList) {
    public boolean updateModifiedUserById(String maintainId, String masterId, String userId, List<String> modifiedFieldList) {
        if (StringUtils.isEmpty(maintainId)) {
            return false;
        }
        Maintain nowMaintain = maintainService.selectById(maintainId);
        List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintainId);
        Map<String, String> masterModifiedMap = masterModifiedMapper.selectByTableMasterId(nowMaintain.getTableName(), masterId);
        String preRecordStr = masterModifiedMap.get(pre_record);
        JSONObject preRecordJSON = JSONObject.parseObject(preRecordStr);
        try {
            Maintain nowMaintain = maintainService.selectById(maintainId);
            List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintainId);
            if (nowMaintain == null) {
                return false;
            }
            Map<String, String> 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;
        }
    }
        return false;
    private void insert(Map<String, String> 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<String, String> masterModifiedMap) {
        String id = masterModifiedMap.get(Constant.ID);
        Segment whereSegment = new Segment(Constant.ID, id);
        List<Segment> 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) {
        Maintain maintain = maintainService.selectById(maintainId);
        String tableName = maintain.getTableName();
        String recordTableName = tableName + Constant.RECORD;
        List<Map<String,String>> modifiedList = masterModifiedMapper.selectByMaintainId(recordTableName, DbUtils.quotedStr(maintainId));
        switch (status) {
            case open:
                for (Map<String, String> masterModifiedMap : modifiedList) {
                    String preJson = masterModifiedMap.get(pre_record);
                    JSONObject jsonObject = JSONObject.parseObject(preJson);
                    if (jsonObject == null){
                        continue;
                    }
                    Set<String> keySet = jsonObject.keySet();
                    HashMap<String, String> 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<TableSchemaResult> tableField = tableInfoMapper.getTableField(master_modified);
                    List<String> existsFieldList = tableField.stream().map(TableSchemaResult::getFieldName).collect(Collectors.toList());
                    Set<String> userIdList = userMap.keySet();
                    if (!existsFieldList.containsAll(userIdList)) {
                        //新增之前不存在的用户
                        List<String> 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<String> unUserIdList = new ArrayList<>();
                unUserIdList.add(Constant.ID);
                unUserIdList.add(master_id);
                unUserIdList.add(table_name);
                unUserIdList.add(pre_record);
                for (Map<String, String> masterModifiedMap : modifiedList) {
                    //todo   反更新 perjson  获取当前表所有可更改的用户 之后待修改 先从本地user表中拿
                    Set<String> keySet = masterModifiedMap.keySet();
                    List<String> 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;
        }
    }
    @Override
    public void dealAssemble(String maintainId, String userId, boolean audit) {
        List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintainId);
        String fieldStr = fieldByMaintain.stream().map(SysField::getField).collect(Collectors.joining(Constant.SEMICOLON));
        List<TableSchemaResult> 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()));
        }
    }
}