package com.engine.htsc.workflow.cmd;

import com.alibaba.fastjson.JSON;
import com.engine.integration.util.StringUtils;
import org.junit.Test;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.wechat.util.Utils;

import java.util.*;
import java.util.stream.Collectors;

public class DesignatedProcessorSettingsCmdTest {
    @Test
    public void test() {
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", "24947");
        param.put("workflownum", "zbzysx");
        DesignatedProcessorSettingsCmd designatedProcessorSettingsCmd = new DesignatedProcessorSettingsCmd(param);
        designatedProcessorSettingsCmd.execute(null);
        String jsonString = "  {\n" +
                "    \"field_name\": \"所在部门\",\n" +
                "    \"field_value\": \"15\",\n" +
                "    \"html_type\": \"3\",\n" +
                "    \"fieldd_id\": \"6966\",\n" +
                "    \"zd\": \"6966\",\n" +
                "    \"fieldname\": \"szbm\",\n" +
                "    \"fieldhtmltype\": \"3\",\n" +
                "    \"type\": 4,\n" +
                "    \"designated_processor\": \"6185\"\n" +
                "  }\n";
        List<Map<String, Object>> dataMapList = new ArrayList<>();

        Map<String, String> mockData = JSON.parseObject(jsonString, Map.class);
        String designatedProcessor = Utils.null2String(mockData.get("designated_processor"));
        String fieldName = mockData.get("field_name");
        String fieldValue = mockData.get("field_value");
        String fieldType = mockData.get("html_type");
        String fieldId = mockData.get("fieldd_id");
        String newFieldValue = fieldValue;
        dataMapList.addAll(Arrays.stream(designatedProcessor.split(",")).map(processor -> {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("key", fieldName);
            dataMap.put("value", newFieldValue);
            dataMap.put("processorId", processor);
            if (StringUtils.equals("3", fieldType)) {
                String fielddbtype = Util.null2String(mockData.get("fielddbtype"));
                String fieldid = Util.null2String(mockData.get("zd"));
                String fieldname = Util.null2String(mockData.get("fieldname"));
                String fieldhtmltype = Util.null2String(mockData.get("fieldhtmltype"));
                String type = String.valueOf(mockData.get("type"));
                dataMap.put("fielddbtype", fielddbtype);
                dataMap.put("fieldid", fieldid);
                dataMap.put("fieldname", fieldname);
                dataMap.put("fieldhtmltype", fieldhtmltype);
                dataMap.put("fieldValue", newFieldValue);
                dataMap.put("type", type);
            }
            return dataMap;
        }).collect(Collectors.toList()));
        Map<String, List<Map<String, Object>>> data = dataMapList.stream().collect(Collectors.groupingBy(map -> Utils.null2String(map.get("processorId"))));
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, String> processorsMap = queryProcessors(data.keySet());
        data.forEach((key, value) -> {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("processorName", processorsMap.get(key));
            dataMap.put("processorId", key);
            dataMap.put("details", data.get(key));
            dataList.add(dataMap);
        });
        System.out.println(JSON.toJSONString(dataList));

    }

    private Map<String, String> queryProcessors(Set<String> userIds) {
        Map<String,String> nameMap = new HashMap<>();
        nameMap.put("6185","曹啸");
        nameMap.put("22131","段华毅");
        return nameMap;
    }
}
