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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants.Status;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.dao.BusiOfflineTransformerBrandDao;
import com.huatek.busi.dto.BusiOfflineTransformerBrandDto;
import com.huatek.busi.dto.LabReportMarkStatisticDto;
import com.huatek.busi.entity.BusiOfflineTransformerBrand;
import com.huatek.busi.mapper.BusiOfflineTransformerBrandMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.ILabReportMarkStatisticService;
import com.huatek.busi.service.transformer.BusiOfflineTransformerBrandService;
import com.huatek.busi.vo.BusiOfflineTransformerBrandVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.WherePrams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 变压器标牌信息 ServiceImpl
 * 
 * @author walker_wang
 *
 */
@Component
@Service(interfaceClass = BusiOfflineTransformerBrandService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineTransformerBrandServiceImpl implements BusiOfflineTransformerBrandService {

	@Autowired
	private BusiOfflineTransformerBrandMapper busiOfflineTransformerBrandMapper;

	@Autowired
	private BusiOfflineTransformerBrandDao busiOfflineTransformerBrandDao;

	@Autowired
	private BusiSiteTreeService busiSiteTreeService;

	@Autowired
	private BusiOfflineTransformerBrandService busiOfflineTransformerBrandService;

	@Autowired
	private ILabReportMarkStatisticService iLabReportMarkStatisticService;

	@Override
	public int saveBusiOfflineTransformerBrand(String token,
											   BusiOfflineTransformerBrandDto busiOfflineTransformerBrandDto) {
		BusiOfflineTransformerBrand busiOfflineTransformerBrand = new BusiOfflineTransformerBrand();
		BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineTransformerBrandDto, busiOfflineTransformerBrand);
		// TODO 变压器标牌数据保存校验
		// ...

		// 更新树节点上的主设备名称
		busiSiteTreeService.updateNodeFromEquipment(busiOfflineTransformerBrandDto.getMainEquipmentId(),
				busiOfflineTransformerBrandDto.getTransformerName());
		
		busiOfflineTransformerBrand.setStatus("1");


		return busiOfflineTransformerBrandDao.updateLocal(busiOfflineTransformerBrand);

//		BusiExternalEquipment find = getByPK(entityDto.getId());
//		if (null != find) {
//			String name = find.getEquipmentName();
//			find.setEquipmentName(entityDto.getEquipmentName());
//			find.setEquipmentBrand(entityDto.getEquipmentBrand());
//			find.setManufactor(entityDto.getManufactor());
//			find.setInstallLocation(entityDto.getInstallLocation());
//			// find.setMark(entity.getMark());
//			find.setProductionDate(entityDto.getProductionDate());
//			find.setUseDate(entityDto.getUseDate());
//			busiExternalEquipmentDao.update(find);
//			if (!entityDto.getEquipmentName().equals(name)) {
//				this.updateSubTreeName(find);
//			}

	}

	@Override
	public BusiOfflineTransformerBrandVo getBusiOfflineTransformerBrandVo(String siteId, String mainEquipmentId) {
		return busiOfflineTransformerBrandMapper.getBusiOfflineTransformerBrandVo(siteId, mainEquipmentId);
	}

	@Override
	public List<BusiOfflineTransformerBrand> findAll() {
		return busiOfflineTransformerBrandMapper.findAll();
	}

	
	
	@Override
	public void deleteByMainEquipmentId(String mainEquipmentId) {
		
		busiOfflineTransformerBrandMapper.updateStatusByMainEquipmentId(mainEquipmentId);
	}

	@Override
	public int saveOrUpdateBrand(String siteId, String mainEquipmentId, String name) {
		BusiOfflineTransformerBrand busiOfflineTransformerBrand = busiOfflineTransformerBrandMapper
				.getBusiOfflineTransformerBrand(siteId, mainEquipmentId);
		if (busiOfflineTransformerBrand == null) {
			busiOfflineTransformerBrand = new BusiOfflineTransformerBrand();
			busiOfflineTransformerBrand.setSiteId(siteId);
			busiOfflineTransformerBrand.setEquipmentCode(GenerateUtil.generateCode(17));
			busiOfflineTransformerBrand.setMainEquipmentId(mainEquipmentId);
			busiOfflineTransformerBrand.setCreateTime(LocalDateTime.now());
			busiOfflineTransformerBrand.setStatus(Status.NORMAL.getValue());
		}
		busiOfflineTransformerBrand.setTransformerName(name);
		return busiOfflineTransformerBrandDao.saveOrUpdate(busiOfflineTransformerBrand);
	}

	@Override
	public BusiOfflineTransformerBrand getByPK(String pk) {
		return this.busiOfflineTransformerBrandDao.get(pk);
	}

	@Override
	public void updateBrand(String equipmentId) {
		List<LabReportMarkStatisticDto> list = iLabReportMarkStatisticService.getLastImportInfo(equipmentId,"T");
		if (!CollectionUtils.isEmpty(list)){
			Map<String, String> map = list.stream().collect(Collectors.toMap(LabReportMarkStatisticDto::getERule, LabReportMarkStatisticDto::getCurrentValue));
			BusiOfflineTransformerBrand transformerBrand = busiOfflineTransformerBrandDao.get(new WherePrams().where("main_equipment_id", equipmentId).and("status", Status.NORMAL.getValue()));
			Set<String> strings = map.keySet();
			if (strings.contains("0032")){
				LocalDate localDate = parseLocalDate(map.get("0032"));
				if (!ObjectUtils.isEmpty(localDate)){
					transformerBrand.setEquipmentDeliveryTime(localDate);
				}
			}
			if (strings.contains("0033")) {
				transformerBrand.setSpecificGravity(map.get("0033"));
			}
			if (strings.contains("0034")) {
				LocalDate localDate = parseLocalDate(map.get("0034"));
				if (!ObjectUtils.isEmpty(localDate)){
					transformerBrand.setFactoryTime(localDate);
				}
			}
			if (strings.contains("0035")) {
				transformerBrand.setRatedCapacity(map.get("0035"));
			}
			if (strings.contains("0036")) {
				transformerBrand.setCoolingNode(map.get("0036"));
			}
			if (strings.contains("0037")) {
				transformerBrand.setManufacturer(map.get("0037"));
			}
			if (strings.contains("0038")) {
				transformerBrand.setRatedHighVoltage(map.get("0038"));
			}
			if (strings.contains("0039")) {
				transformerBrand.setRatedLowVoltage(map.get("0039"));
			}
			if (strings.contains("0040")) {
				transformerBrand.setRatedHighCurrent(map.get("0040"));
			}
			if (strings.contains("0041")) {
				transformerBrand.setRatedLowCurrent(map.get("0041"));
			}
			if (strings.contains("0042")) {
				transformerBrand.setOilWeight(map.get("0042"));
			}
			if (strings.contains("0043")) {
				transformerBrand.setDesignLife(map.get("0043"));
			}
			if (strings.contains("0044")) {
				transformerBrand.setEnvironmentLevel(map.get("0044"));
			}
			if (strings.contains("0045")) {
				LocalDate localDate = parseLocalDate(map.get("0045"));
				if (!ObjectUtils.isEmpty(localDate)){
					transformerBrand.setHealthAassessmentStartTime(localDate);
				}
			}
			busiOfflineTransformerBrandDao.updateLocal(transformerBrand);
		}
	}

	private LocalDate parseLocalDate(String date){
		LocalDate localDate = null;
		try {
			localDate = LocalDate.parse(date);
		}catch (Exception e){

		}
		return localDate;
	}

	//	@Override
//	public int saveBusiOfflineTransformerBrandSort(String token, BusiOfflineTransformerBrandDto busiOfflineTransformerBrandDto) {
//		String id = busiOfflineTransformerBrandDto.getMainEquipmentId();
//		String siteId = busiOfflineTransformerBrandDto.getSiteId();
//		busiOfflineTransformerBrandMapper.updateSort(id,siteId);
//		return 0;
//	}
}