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

import java.time.LocalDateTime;
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.huatek.busi.dao.BusiTransformerExperimentDao;
import com.huatek.busi.entity.BusiOfflineTransformerBrand;
import com.huatek.busi.entity.BusiOutlineTransformerExperimentData;
import com.huatek.busi.entity.BusiTransformerExperiment;
import com.huatek.busi.mapper.BusiTransformerExperimentMapper;
import com.huatek.busi.service.transformer.BusiOfflineTransformerBrandService;
import com.huatek.busi.service.transformer.BusiTransformerExperimentService;
import com.huatek.torch.util.uuid.Uuid;

/**
 * @ClassName: BusiTransformerExperimentServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.transformer.
 *                 BusiTransformerExperimentServiceImpl
 * @Description: 变压器例行试验七气一水值 服务
 * @author: Arno
 * @date: Jul 15, 2019 10:46:35 AM
 * @version: 1.0
 */

@Component
@Service(interfaceClass = BusiTransformerExperimentService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiTransformerExperimentServiceImpl implements BusiTransformerExperimentService {

	@Autowired
	private BusiTransformerExperimentDao busiTransformerExperimentDao;

	@Autowired
	private BusiTransformerExperimentMapper busiTransformerExperimentMapper;
	
	@Autowired
	private BusiOfflineTransformerBrandService busiOfflineTransformerBrandService ;

	@Override
	public void createBusiTransformerExperiment(BusiOutlineTransformerExperimentData experimentData) {
		boolean flag = false ;
		String siteId = experimentData.getSiteId();
		String equipmentId = experimentData.getEquipmentId();
		BusiTransformerExperiment find = this.findByEquipmentId(siteId, equipmentId);
		if (find == null) {
			find = new BusiTransformerExperiment();
			flag = true ;
		}
		find.setC2h2(experimentData.getC2h2());
		find.setC2h4(experimentData.getC2h4());
		find.setC2h6(experimentData.getC2h6());
		find.setCh4(experimentData.getCh4());
		find.setCo(experimentData.getCo());
		find.setCo2(experimentData.getCo2());
		find.setH2(experimentData.getH2());
		find.setH2o(experimentData.getYzWater());
		find.setZj(experimentData.getZt());
		find.setEquipmentId(equipmentId);
		find.setSiteId(siteId);
		//find.setCreateTime(new Date());
		find.setIsUse("1");
		find.setDiffTime(null);
		if(flag){
			busiTransformerExperimentDao.add(find);
		}else{
			busiTransformerExperimentDao.updateLocal(find);
		}
	}

	@Override
	public BusiTransformerExperiment findByEquipmentId(String siteId, String equipmentId) {
		return busiTransformerExperimentMapper.findByEquipmentId(siteId, equipmentId);
	}

	@Override
	public void updateBusiTransformerExperiment(BusiTransformerExperiment entity) {
		busiTransformerExperimentDao.updateLocal(entity);
	}

	@Override
	public void createBusiTransformerExperiment(Map<String, Object> map) {
		 LocalDateTime now = (LocalDateTime) map.get("date");
		 String equipmentId = map.get("equipmentId").toString();
		 BusiOfflineTransformerBrand brand = busiOfflineTransformerBrandService.getByPK(equipmentId);
		if(null != brand){
			String siteId = brand.getSiteId();
			BusiTransformerExperiment find = this.findByEquipmentId(siteId, equipmentId);
			if(null == find ){
				find = new BusiTransformerExperiment();
				find.setId(Uuid.getUUID());
				find.setEquipmentId(equipmentId);
				find.setSiteId(siteId);
				setting(map, find, now);
				busiTransformerExperimentDao.add(find);
			}else{
				LocalDateTime createTime = find.getCreateTime();
				if(createTime.isBefore(now) || createTime.equals(now)){
					setting(map, find, now);
					busiTransformerExperimentDao.updateLocal(find);
				}
			}
		}
	}

	private void setting(Map<String, Object> map, BusiTransformerExperiment find, LocalDateTime time) {
		find.setC2h2(this.getValue("乙炔（uL/L)", map));
		find.setC2h4(this.getValue("乙烯C2H4（uL/L)", map));
		find.setC2h6(this.getValue("乙烷C2H6（uL/L)", map));
		find.setCh4(this.getValue("甲烷CH4（uL/L)", map));
		find.setCo(this.getValue("一氧化碳CO（uL/L)", map));
		find.setCo2(this.getValue("二氧化碳CO（uL/L)", map));
		find.setH2(this.getValue("氢H2（uL/L)", map));
		find.setH2o(this.getValue("H2O", map));
		find.setZj(this.getValue("总烃含量（uL/L)", map));
		find.setN2(this.getValue("O2（uL/L)", map));
		find.setO2(this.getValue("N2（uL/L)", map));
		find.setCreateTime(time);
		find.setIsUse("1");
	}

	public String getValue(String key, Map<String, Object> map){
		if(map.containsKey(key)){
			return map.get(key) == null ? "" : map.get(key) .toString();
		}
		return "";
	}
	
}
