/**
 * 
 */
package com.huatek.busi.service.impl.transformer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dao.TransformerOfflineExperimentReportDao;
import com.huatek.busi.entity.TransformerOfflineExperimentReport;
import com.huatek.busi.mapper.TransformerOfflineExperimentReportMapper;
import com.huatek.busi.service.transformer.BusiTransformerExperimentService;
import com.huatek.busi.service.transformer.TransformerOfflineExperimentReportService;
import com.huatek.busi.vo.routinetest.TransformerTestPageVo;
import com.huatek.busi.vo.routinetest.TransformerTestTmpDetailVo;
import com.huatek.busi.vo.routinetest.TransformerTestTmpVo;
import com.huatek.busi.vo.routinetest.TransformerTestVo;
import com.huatek.busi.vo.routinetest.transformer.BushLossTangent;
import com.huatek.busi.vo.routinetest.transformer.BushingCapacitanceMediumLoss;
import com.huatek.busi.vo.routinetest.transformer.CardboardTest;
import com.huatek.busi.vo.routinetest.transformer.ExcitationCharacteristict;
import com.huatek.busi.vo.routinetest.transformer.FurfuralTest;
import com.huatek.busi.vo.routinetest.transformer.InsulationOilTest;
import com.huatek.busi.vo.routinetest.transformer.LowVoltageNoLoad;
import com.huatek.busi.vo.routinetest.transformer.NoloadCurrentAndNoloadLoss;
import com.huatek.busi.vo.routinetest.transformer.SealingTest;
import com.huatek.busi.vo.routinetest.transformer.ShortCircuitImpedanceLoadLoss;
import com.huatek.busi.vo.routinetest.transformer.SoundLevelMeasurement;
import com.huatek.busi.vo.routinetest.transformer.TemperatureRiseTest;
import com.huatek.busi.vo.routinetest.transformer.WindingBushLossTangent;
import com.huatek.busi.vo.routinetest.transformer.WindingBushingDCLeakageCurrent;
import com.huatek.busi.vo.routinetest.transformer.WindingBushingDCResistance;
import com.huatek.busi.vo.routinetest.transformer.WindingInsulationResistanceAndCapacitanceDielectricLoss;
import com.huatek.busi.vo.routinetest.transformer.WindingIronCoreIsulationResistance;
import com.huatek.busi.vo.routinetest.transformer.WindingResistance;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;

/**
 * @ClassName: transformerOfflinExperimentReportServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.transformer.
 *                 transformerOfflinExperimentReportServiceImpl
 * @Description: TODO
 * @author: Arno
 * @date: Aug 27, 2019 5:48:13 PM
 * @version: 1.0
 */

@Component
@Service(interfaceClass = TransformerOfflineExperimentReportService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerOfflineExperimentReportServiceImpl implements TransformerOfflineExperimentReportService {

	@Autowired
	private TransformerOfflineExperimentReportDao transformerOfflineExperimentReportDao;

	@Autowired
	private TransformerOfflineExperimentReportMapper transformerOfflineExperimentReportMapper;
	
	@Autowired
    private BusiTransformerExperimentService busiTransformerExperimentService ;
	
	@Override
	public TransformerOfflineExperimentReport findByPk(String pk) {
		return  transformerOfflineExperimentReportDao.get(pk);
	}

	@Override
	public TransformerTestVo info(String id) {
		TransformerOfflineExperimentReport find = findByPk(id);
		TransformerTestVo vo =new TransformerTestVo();
		vo.setId(find.getId());
		vo.setTransformerId(find.getTransformerId());
		vo.setMainTreeId(find.getMainTreeId());
		this.objectTransformation2(vo, find);
		return vo;
	}

	@Override
	public Boolean add(String token, TransformerTestVo vo) {
		Integer count = 0 ;
		TransformerOfflineExperimentReport report = new TransformerOfflineExperimentReport();
		LocalUser user = UserUtil.getLocalUser(token);
		if (null != user) {
			report.setCreateName(user.getUserName());
			report.setCreateUser(user.getUid());
		}
		LocalDateTime now = LocalDateTime.now();
		report.setCreateTime( now);
		this.objectTransformation(vo, report);
		report.setId(Uuid.getUUID());
		report.setTransformerId(vo.getTransformerId());
		report.setMainTreeId(vo.getMainTreeId());
		report.setStatus("1");
		count = transformerOfflineExperimentReportDao.add(report);
		
		boolean  flag =  count > 0 ? true : false ;
		if(flag){
			buildBusiTransformerExperiment(vo, now);
		}
		return flag ;
	}



	@Override
	public Boolean update(String token, TransformerTestVo vo) {
		boolean  flag = false ;
		LocalUser user = UserUtil.getLocalUser(token);
		TransformerOfflineExperimentReport find = this.findByPk(vo.getId());
		if(null != find){
			if (null != user) {
				find.setModifiedName(user.getUserName());
				find.setModifiedUser(user.getUid());
			}
			find.setModifiedTime( LocalDateTime.now());
			find.setTransformerId(vo.getTransformerId());
			find.setMainTreeId(vo.getMainTreeId());
			this.objectTransformation(vo, find);
			int count = transformerOfflineExperimentReportDao.updateLocal(find);
		    flag =  count > 0 ? true : false ;
			if(flag){
				buildBusiTransformerExperiment(vo, find.getCreateTime());
			}
		}
		return flag;
	}

	@Override
	public int delete(String token, List<String> ids) {
		return transformerOfflineExperimentReportMapper.delete(ids);
	}

	@Override
	public List<TransformerTestTmpVo> getTransformerList() {
		return transformerOfflineExperimentReportMapper.getTransformerList();
	}

	@Override
	public TransformerTestTmpDetailVo getTransformerInfo(String transformerId) {
		return transformerOfflineExperimentReportMapper.getTransformerInfo(transformerId);
	}
	
	
	
	@Override
	public PageInfo<TransformerTestPageVo> page(String name, Integer page, Integer limit,LocalDate timeStampStart,LocalDate timeStampEnd) {		
		PageInfo<TransformerTestPageVo>  pageInfo = PageHelper.startPage(page == null ? 1 :page , limit == null? 10 : limit)
        .doSelectPageInfo(() -> transformerOfflineExperimentReportMapper.page(name,timeStampStart,timeStampEnd));
		pageInfo.getList().stream().forEach(transformerTestPageVo -> {
			TransformerOfflineExperimentReport find = findByPk(transformerTestPageVo.getId());
//			transformerTestPageVo.setWindingBushingDCResistance(this.jsonToBean(find.getWindingBushingDCResistance(), WindingBushingDCResistance.class ));
			
//			LocalDateTime dataTime = this.jsonToBean(find.getWindingBushingDCResistance(), WindingBushingDCResistance.class ).getTestTime();
			transformerTestPageVo.setTestTime(this.jsonToBean(find.getWindingBushingDCResistance(), WindingBushingDCResistance.class ).getTestTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		});
		return pageInfo;
	}
	
	
	

	@Override
	public TransformerTestVo getByTreeId(String mainTreeId) {
		TransformerOfflineExperimentReport find = transformerOfflineExperimentReportMapper.selectByTreeId(mainTreeId);
		if(find == null) {
		    return null;
		}
		TransformerTestVo vo =new TransformerTestVo();
		vo.setId(find.getId());
		vo.setTransformerId(find.getTransformerId());
		vo.setMainTreeId(find.getMainTreeId());
		this.objectTransformation2(vo, find);
		return vo ;
	}

	public void objectTransformation(TransformerTestVo vo, TransformerOfflineExperimentReport entity ){
		
		entity.setBushingCapacitanceMediumLoss(this.beanToJson(vo.getBushingCapacitanceMediumLoss()));
		entity.setBushLossTangent(this.beanToJson(vo.getBushLossTangent()));
		
		entity.setCardboardTest(this.beanToJson(vo.getCardboardTest()));
		entity.setExcitationCharacteristict(this.beanToJson(vo.getExcitationCharacteristict()));
		
		entity.setFurfuralTest(this.beanToJson(vo.getFurfuralTest()));
		entity.setInsulationOilTest(this.beanToJson(vo.getInsulationOilTest()));
		
		entity.setLowVoltageNoLoad(this.beanToJson(vo.getLowVoltageNoLoad()));
		entity.setNoloadCurrentAndNoloadLoss(this.beanToJson(vo.getNoloadCurrentAndNoloadLoss()));
		
		entity.setSealingTest(this.beanToJson(vo.getSealingTest()));
		entity.setShortCircuitImpedanceLoadLoss(this.beanToJson(vo.getShortCircuitImpedanceLoadLoss()));
		
		entity.setSoundLevelMeasurement(this.beanToJson(vo.getSoundLevelMeasurement()));
		entity.setTemperatureRiseTest(this.beanToJson(vo.getTemperatureRiseTest()));
		
		entity.setWindingBushingDCLeakageCurrent(this.beanToJson(vo.getWindingBushingDCLeakageCurrent()));
		entity.setWindingBushingDCResistance(this.beanToJson(vo.getWindingBushingDCResistance()));
		
		entity.setWindingBushLossTangent(this.beanToJson(vo.getWindingBushLossTangent()));
		entity.setWindingInsulationResistanceAndCapacitanceDielectricLoss(this.beanToJson(vo.getWindingInsulationResistanceAndCapacitanceDielectricLoss()));
		
		entity.setWindingIronCoreIsulationResistance(this.beanToJson(vo.getWindingIronCoreIsulationResistance()));
		entity.setWindingResistance(this.beanToJson(vo.getWindingResistance()));

		setTransformerTime(vo, entity);

	}

	private void setTransformerTime(TransformerTestVo vo, TransformerOfflineExperimentReport entity){

		if (vo.getWindingBushingDCResistance() != null) {
			entity.setTestTime(vo.getWindingBushingDCResistance().getTestTime());
			return;
		}
		if (vo.getExcitationCharacteristict() != null) {
			entity.setTestTime(vo.getExcitationCharacteristict().getTestTime());
			return;
		}
		if (vo.getBushingCapacitanceMediumLoss() != null) {
			entity.setTestTime(vo.getBushingCapacitanceMediumLoss().getTestTime());
			return;
		}
		if (vo.getBushLossTangent() != null) {
			entity.setTestTime(vo.getBushLossTangent().getTestTime());
			return;
		}
		if (vo.getCardboardTest() != null) {
			entity.setTestTime(vo.getCardboardTest().getTestTime());
			return;
		}
		if (vo.getFurfuralTest() != null) {
			entity.setTestTime(vo.getFurfuralTest().getTestTime());
			return;
		}
		if (vo.getInsulationOilTest() != null) {
			entity.setTestTime(vo.getInsulationOilTest().getTestTime());
			return;
		}
		if (vo.getLowVoltageNoLoad() != null) {
			entity.setTestTime(vo.getLowVoltageNoLoad().getTestTime());
			return;
		}
		if (vo.getNoloadCurrentAndNoloadLoss() != null) {
			entity.setTestTime(vo.getNoloadCurrentAndNoloadLoss().getTestTime());
			return;
		}
		if (vo.getSealingTest() != null) {
			entity.setTestTime(vo.getSealingTest().getTestTime());
			return;
		}
		if (vo.getShortCircuitImpedanceLoadLoss() != null) {
			entity.setTestTime(vo.getShortCircuitImpedanceLoadLoss().getTestTime());
			return;
		}
		if (vo.getSoundLevelMeasurement() != null) {
			entity.setTestTime(vo.getSoundLevelMeasurement().getTestTime());
			return;
		}
		if (vo.getTemperatureRiseTest() != null) {
			entity.setTestTime(vo.getTemperatureRiseTest().getTestTime());
			return;
		}
		if (vo.getWindingBushingDCLeakageCurrent() != null) {
			entity.setTestTime(vo.getWindingBushingDCLeakageCurrent().getTestTime());
			return;
		}
		if (vo.getWindingBushLossTangent() != null) {
			entity.setTestTime(vo.getWindingBushLossTangent().getTestTime());
			return;
		}
		if (vo.getWindingInsulationResistanceAndCapacitanceDielectricLoss() != null) {
			entity.setTestTime(vo.getWindingInsulationResistanceAndCapacitanceDielectricLoss().getTestTime());
			return;
		}
		if (vo.getWindingIronCoreIsulationResistance() != null) {
			entity.setTestTime(vo.getWindingIronCoreIsulationResistance().getTestTime());
			return;
		}
		if (vo.getWindingResistance() != null) {
			entity.setTestTime(vo.getWindingResistance().getTestTime());
		}
	}
	
	
	public void objectTransformation2(TransformerTestVo vo, TransformerOfflineExperimentReport entity ){
		
		vo.setBushingCapacitanceMediumLoss(this.jsonToBean(entity.getBushingCapacitanceMediumLoss(), BushingCapacitanceMediumLoss.class ));
		vo.setBushLossTangent(this.jsonToBean(entity.getBushLossTangent(), BushLossTangent.class));
		
		vo.setCardboardTest(this.jsonToBean(entity.getCardboardTest(), CardboardTest.class));
		vo.setExcitationCharacteristict(this.jsonToBean(entity.getExcitationCharacteristict(),ExcitationCharacteristict.class));
		
		vo.setFurfuralTest(this.jsonToBean(entity.getFurfuralTest(), FurfuralTest.class));
		vo.setInsulationOilTest(this.jsonToBean(entity.getInsulationOilTest(),InsulationOilTest.class));
		
		vo.setLowVoltageNoLoad(this.jsonToBean(entity.getLowVoltageNoLoad(), LowVoltageNoLoad.class));
		vo.setNoloadCurrentAndNoloadLoss(this.jsonToBean(entity.getNoloadCurrentAndNoloadLoss(), NoloadCurrentAndNoloadLoss.class));
		
		vo.setSealingTest(this.jsonToBean(entity.getSealingTest(), SealingTest.class));
		vo.setShortCircuitImpedanceLoadLoss(this.jsonToBean(entity.getShortCircuitImpedanceLoadLoss(), ShortCircuitImpedanceLoadLoss.class));
		
		vo.setSoundLevelMeasurement(this.jsonToBean(entity.getSoundLevelMeasurement(), SoundLevelMeasurement.class));
		vo.setTemperatureRiseTest(this.jsonToBean(entity.getTemperatureRiseTest(), TemperatureRiseTest.class));
		
		vo.setWindingBushingDCLeakageCurrent(this.jsonToBean(entity.getWindingBushingDCLeakageCurrent(), WindingBushingDCLeakageCurrent.class));
		vo.setWindingBushingDCResistance(this.jsonToBean(entity.getWindingBushingDCResistance(),WindingBushingDCResistance.class));
		
		vo.setWindingBushLossTangent(this.jsonToBean(entity.getWindingBushLossTangent(), WindingBushLossTangent.class));
		vo.setWindingInsulationResistanceAndCapacitanceDielectricLoss(this.jsonToBean(entity.getWindingInsulationResistanceAndCapacitanceDielectricLoss(),WindingInsulationResistanceAndCapacitanceDielectricLoss.class));
		
		vo.setWindingIronCoreIsulationResistance(this.jsonToBean(entity.getWindingIronCoreIsulationResistance(), WindingIronCoreIsulationResistance.class));
		vo.setWindingResistance(this.jsonToBean(entity.getWindingResistance(),WindingResistance.class));

		setTransformerTime(vo, entity);
	}
	
	public String  beanToJson (Object object){
		return null == object ? null :JSONObject.toJSONString(object);
	}
	
	public <T> T  jsonToBean (String json, Class<T> classes){
		return JSONObject.parseObject(json, classes);
	}

	private void buildBusiTransformerExperiment(TransformerTestVo vo, LocalDateTime now) {
		InsulationOilTest test = vo.getInsulationOilTest();
		if(null != test){
			Map<String, Object> map = test.convert();
			map.put("date", now);
			map.put("equipmentId", vo.getTransformerId());
			busiTransformerExperimentService.createBusiTransformerExperiment(map);
		}
	}

	@Override
	public int findByTransformerId(String transformerId) {
		// TODO Auto-generated method stub
		return transformerOfflineExperimentReportMapper.findByTransformerId(transformerId);
	}
	
	@Override
	public String getTransformerId(String sensorCode) {
		// TODO Auto-generated method stub
		return transformerOfflineExperimentReportMapper.getTransformerId(sensorCode);
	}
	
}
