/**
 * 
 */
package com.huatek.busi.vo.routinetest.transformer;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * @ClassName: InsulationOilTest
 * @FullClassPath: com.huatek.busi.vo.routinetest.transformer.InsulationOilTest
 * @Description: 绝缘油试验
 * @author: Arno
 * @date: Aug 23, 2019 4:47:16 PM
 * @version: 1.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class InsulationOilTest extends TestBase implements Serializable {

	/** @Fields serialVersionUID : */
	private static final long serialVersionUID = 4035625282531119648L;

	public List<TestItem> testItems;

	public List<GasItem> gasItems;

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	class TestItem implements Serializable {

		/** @Fields serialVersionUID : */
		private static final long serialVersionUID = -6841492158114482457L;

		/** @Fields item : */
		public String item;

		/** @Fields value : 试验结果 */
		public String value;
	}

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	class GasItem implements Serializable {

		/** @Fields serialVersionUID : */
		private static final long serialVersionUID = -1195257751070000427L;

		/** @Fields item : */
		@JSONField(ordinal = 1)
		public String item;

		/** @Fields beforeInsulation : 绝缘试验前 */
		@JSONField(ordinal = 2)
		public String beforeInsulation;

		/** @Fields afterImpact : 冲击试验后 */
		@JSONField(ordinal = 3)
		public String afterImpact;

		/** @Fields beforeNoLoad : 感应试验后长时空载试验前 */
		@JSONField(ordinal = 4)
		public String beforeNoLoad;

		/** @Fields beforeTemperature : 长时空载试验后温升试验前 */
		@JSONField(ordinal = 5)
		public String beforeTemperature;

		/** @Fields beforeInsulation : 温升试验后 */
		@JSONField(ordinal = 6)
		public String afterTemperature;

	}

	public static void main(String[] args) {
		InsulationOilTest entity = new InsulationOilTest();
		entity.setTestContent("绝缘油试验");
		entity.setTestEquipment("");
		entity.setChecker("王科");
		entity.setTester("刘天天");
		entity.setSignature("林冲");
		entity.setTestTime(LocalDateTime.of(2018, 11, 13, 12, 24, 56));
		entity.setRemarks("合格");

		List<TestItem> testItems = new ArrayList<>();
		TestItem e11 = entity.new TestItem(TestContant.InsulationOilEnum.DQGD.getValue(), "1");
		TestItem e12 = entity.new TestItem(TestContant.InsulationOilEnum.SFHL.getValue(), "4");
		TestItem e13 = entity.new TestItem(TestContant.InsulationOilEnum.TANO.getValue(), "8");
		TestItem e14 = entity.new TestItem(TestContant.InsulationOilEnum.HQL.getValue(), "0.23");
		testItems.add(e11);
		testItems.add(e12);
		testItems.add(e13);
		testItems.add(e14);
		entity.setTestItems(testItems);

		List<GasItem> gasItems = new ArrayList<>();

		GasItem e = entity.new GasItem(TestContant.InsulationOilEnum.N2.getValue(), "0", "0", "0", "0", "0");
		GasItem e2 = entity.new GasItem(TestContant.InsulationOilEnum.O2.getValue(), "0", "0", "0", "0", "0");
		GasItem e3 = entity.new GasItem(TestContant.InsulationOilEnum.CH4.getValue(), "0.23", "0.23", "0.23", "0.23",
				"0.23");
		GasItem e4 = entity.new GasItem(TestContant.InsulationOilEnum.C2H4.getValue(), "0", "0", "0", "0", "0");
		GasItem e5 = entity.new GasItem(TestContant.InsulationOilEnum.C2H6.getValue(), "0", "0", "0", "0", "0");
		GasItem e6 = entity.new GasItem(TestContant.InsulationOilEnum.C2H2.getValue(), "0", "0", "0", "0", "0");
		GasItem e7 = entity.new GasItem(TestContant.InsulationOilEnum.H2.getValue(), "0", "0", "0", "0", "0");
		GasItem e8 = entity.new GasItem(TestContant.InsulationOilEnum.CO.getValue(), "20.03", "20.03", "20.03", "20.03",
				"20.03");
		GasItem e9 = entity.new GasItem(TestContant.InsulationOilEnum.CO2.getValue(), "423", "423", "423", "423",
				"423");
		GasItem e10 = entity.new GasItem(TestContant.InsulationOilEnum.TJ.getValue(), "0.32", "0.32", "0.32", "0.32",
				"0.32");

		gasItems.add(e);
		gasItems.add(e2);
		gasItems.add(e3);
		gasItems.add(e4);
		gasItems.add(e5);
		gasItems.add(e6);
		gasItems.add(e7);
		gasItems.add(e8);
		gasItems.add(e9);
		gasItems.add(e10);

		entity.setGasItems(gasItems);

		String josn = JSONObject.toJSONString(entity, SerializerFeature.WriteMapNullValue);
		System.out.println(josn);
		InsulationOilTest parseObject = JSONObject.parseObject(josn, InsulationOilTest.class);
		System.out.println(parseObject);
		System.out.println("********************************************");
		//Map<String, String> convert = convert2(entity);
		
		Map<String, Object> convert2 = entity.convert();
		convert2.forEach(( k, v )-> {
            System.out.println(k +"--->"+ v);
		});
	}

	public static Map<String, String> convert2(InsulationOilTest test) {
		Map<String, String> map = new HashMap<String, String>();
		List<GasItem> gasItems2 =test.getGasItems();
		List<TestItem> testItems2 = test.getTestItems();
		Map<String, String> gasMap = gasItems2.stream()
				.collect(Collectors.toMap(GasItem::getItem, GasItem::getBeforeInsulation));
		Map<String, String> testMap= testItems2.stream().filter(item -> item.getItem().equals(TestContant.InsulationOilEnum.SFHL.getValue()))
				.collect(Collectors.toMap(TestItem::getItem, TestItem::getValue));
		map.putAll(testMap);
		map.putAll(gasMap);
		return map;
	}
	
	
	public  Map<String, Object> convert() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<GasItem> gasItems2 = getGasItems();
		List<TestItem> testItems2 = getTestItems();
		Map<String, Object> gasMap = gasConvert(gasItems2);
		Map<String, Object> testMap= testItems2.stream().filter(item -> item.getItem().equals(TestContant.InsulationOilEnum.SFHL.getValue()))
				.collect(Collectors.toMap(TestItem::getItem, TestItem::getValue));
		map.putAll(testMap);
		map.putAll(gasMap);
		return map;
	}

	private Map<String, Object> gasConvert(List<GasItem> gasItems2) {
		Map<String, Object> gasMap = gasItems2.stream()
				.collect(Collectors.toMap(GasItem::getItem, GasItem::getAfterTemperature));
		List<TestItem> testItems2 = getTestItems();
		Map<String, Object> testMap= testItems2.stream().filter(item -> item.getItem().equals(TestContant.InsulationOilEnum.SFHL.getValue()))
				.collect(Collectors.toMap(TestItem::getItem, TestItem::getValue));
		if(testMap != null && testMap.containsKey("SFHL")){
			gasMap.put("H2O", testMap.get("SFHL"));
		}else{
			gasMap.put("H2O", "");
		}
		
		return gasMap;
	}

}
