package org.springblade.modules.rhzf.fwInfo.util;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.beetl.core.util.ArrayMap;
import org.springblade.core.tool.api.R;
import org.springblade.modules.rhzf.fwInfo.entity.LdTestEntity;
import org.springblade.modules.rhzf.fwInfo.entity.MpEntity;
import org.springblade.modules.rhzf.fwInfo.entity.MpEntitys;

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

public class GsonUtils {

	public static List toGsonStr(Object object) {
		Gson gson = new Gson();
		LdTestEntity entity = gson.fromJson(JSONObject.toJSONString(R.data(object)), LdTestEntity.class);
		List<LdTestEntity.DataBean> data = entity.getData();
		Map<String, List<LdTestEntity.DataBean>> collect = data.stream().collect(
			Collectors.groupingBy(ld -> ld.getSqjcwhmc() + ld.getJlxxqzdzrcmc())
		);
		Iterator<Map.Entry<String, List<LdTestEntity.DataBean>>> iterator = collect.entrySet().iterator();
		List<Object> myList = new ArrayList<>();
		while (iterator.hasNext()) {
			Map.Entry<String, List<LdTestEntity.DataBean>> next = iterator.next();
			String key = next.getKey();
			Map<String, Object> yjMap = new ArrayMap<>();
			yjMap.put("ldph", key);
			List<LdTestEntity.DataBean> value = next.getValue();
			Map<String, List<LdTestEntity.DataBean>> collect1 = value.stream().collect(Collectors.groupingBy(mph -> mph.getMlph()));
			Iterator<Map.Entry<String, List<LdTestEntity.DataBean>>> iterator1 = collect1.entrySet().iterator();
			List<MpEntitys> list = new ArrayList<>();
			while (iterator1.hasNext()) {
				Map.Entry<String, List<LdTestEntity.DataBean>> next1 = iterator1.next();
				String key1 = next1.getKey();
				List<LdTestEntity.DataBean> value1 = next1.getValue();
				MpEntitys mpEntity = new MpEntitys();
				mpEntity.setLdph(key1);
				System.out.println(key1 + "***");
				mpEntity.setData(value1);
				list.add(mpEntity);
				Map<String, List<LdTestEntity.DataBean>> collect2 = value1.stream().collect(Collectors.groupingBy(mph -> mph.getLdph()));
				Iterator<Map.Entry<String, List<LdTestEntity.DataBean>>> iterator2 = collect2.entrySet().iterator();
				while (iterator2.hasNext()) {
					Map.Entry<String, List<LdTestEntity.DataBean>> next2 = iterator2.next();
					List<LdTestEntity.DataBean> value2 = next2.getValue();
					Map<String, Object> ejMap = new HashMap<>();
					ejMap.put("ldph", key1);
					ejMap.put("data", value2);

				}
				yjMap.put("data", list);
			}
			myList.add(yjMap);
		}
//		String json = JSONObject.toJSONString(myList);
		return myList;
	}
}
