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

import java.math.BigDecimal;
import java.time.LocalDateTime;
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.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.dao.GisMechanicalPropertiesDao;
import com.huatek.busi.dao.RptGisMonitorDao;
import com.huatek.busi.dto.GisMechanicalPropertiesDto;
import com.huatek.busi.entity.GisMechanicalProperties;
import com.huatek.busi.entity.RptGisMonitor;
import com.huatek.busi.mapper.GisMechanicalPropertiesMapper;
import com.huatek.busi.service.arrester.RptGisMonitorService;
import com.huatek.busi.service.gis.GisMechanicalPropertiesService;
import com.huatek.busi.vo.GisMechanicalPropertiesVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.util.uuid.Uuid;

/**
 * gis机械特性 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:39 PM
 */
@Component
@Service(interfaceClass = GisMechanicalPropertiesService.class)
@Transactional(rollbackFor = Exception.class)
public class GisMechanicalPropertiesServiceImpl implements GisMechanicalPropertiesService {

    @Autowired
    private GisMechanicalPropertiesMapper gisMechanicalPropertiesMapper;
    @Autowired
    private GisMechanicalPropertiesDao gisMechanicalPropertiesDao;
    @Autowired
	private RptGisMonitorService rptGisMonitorService;
    @Autowired
   	private RptGisMonitorDao rptGisMonitorDao;
    @Override
    public Map<String, Object> findGisMechanicalPropertiesPage(GisMechanicalPropertiesDto gisMechanicalPropertiesDto){
        if(gisMechanicalPropertiesDto.getPage() != null && gisMechanicalPropertiesDto.getLimit() != null){
            PageHelper.startPage(gisMechanicalPropertiesDto.getPage(), gisMechanicalPropertiesDto.getLimit());
        }
        Page<GisMechanicalPropertiesVo> gisMechanicalPropertiess = gisMechanicalPropertiesMapper.selectGisMechanicalPropertiesPage(gisMechanicalPropertiesDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, gisMechanicalPropertiess.getTotal(), "",
                gisMechanicalPropertiess.getResult());
    }

    @Override
    public int addGisMechanicalProperties(String token,GisMechanicalPropertiesDto gisMechanicalPropertiesDto){
        GisMechanicalProperties gisMechanicalProperties = new GisMechanicalProperties();
        BeanUtils.copyPropertiesWithIgnoreNull(gisMechanicalPropertiesDto, gisMechanicalProperties);
        gisMechanicalProperties.setId(Uuid.getUUID());
        gisMechanicalProperties.setCreateTime(LocalDateTime.now());
        int count = gisMechanicalPropertiesDao.add(gisMechanicalProperties);
        
        RptGisMonitor gisMonitor = rptGisMonitorService.findRptGisMonitor(gisMechanicalPropertiesDto.getEquipmentId(),gisMechanicalPropertiesDto.getSiteId());
		if(gisMonitor != null){
			rptGisMonitorDao.del(gisMonitor.getId());
		}
		RptGisMonitor monitor = new RptGisMonitor();
		monitor.setId(Uuid.getUUID());
		monitor.setMainTreeId(gisMechanicalPropertiesDto.getEquipmentId());
		monitor.setSiteId(gisMechanicalPropertiesDto.getSiteId());
		List<GisMechanicalPropertiesVo> gisList = findByEquipmentId(gisMechanicalPropertiesDto.getSiteId(),gisMechanicalPropertiesDto.getEquipmentId());
		monitor.setTurnoffCount((long) gisList.size());
		monitor.setLastTurnoffTime(gisList.get(0).getCreateTime());
		
		if(gisMechanicalPropertiesDto.getGfTime()!=""&&gisMechanicalPropertiesDto.getGfTime()!=null){
			monitor.setDiffTime(gisMechanicalPropertiesDto.getGfTime().toString());
		}
		if(gisMechanicalPropertiesDto.getAGfSpeend()!=""&&gisMechanicalPropertiesDto.getAGfSpeend()!=null){
			monitor.setASpeed(new BigDecimal(gisMechanicalPropertiesDto.getAGfSpeend()));
		}
		if(gisMechanicalPropertiesDto.getAOpenCurrent()!=""&&gisMechanicalPropertiesDto.getAOpenCurrent()!=null){
			monitor.setAElectric(new BigDecimal(gisMechanicalPropertiesDto.getAOpenCurrent()));
		}
		if(gisMechanicalPropertiesDto.getBGfSpeend()!=""&&gisMechanicalPropertiesDto.getBGfSpeend()!=null){
			monitor.setBSpeed(new BigDecimal(gisMechanicalPropertiesDto.getBGfSpeend()));
		}
		if(gisMechanicalPropertiesDto.getBGfSpeend()!=""&&gisMechanicalPropertiesDto.getBGfSpeend()!=null){
			monitor.setBSpeed(new BigDecimal(gisMechanicalPropertiesDto.getBGfSpeend()));
		}
		if(gisMechanicalPropertiesDto.getBOpenCurrent()!=""&&gisMechanicalPropertiesDto.getBOpenCurrent()!=null){
			monitor.setBElectric(new BigDecimal(gisMechanicalPropertiesDto.getBOpenCurrent()));
		}
		if(gisMechanicalPropertiesDto.getCGfSpeend()!=""&&gisMechanicalPropertiesDto.getCGfSpeend()!=null){
			monitor.setCSpeed(new BigDecimal(gisMechanicalPropertiesDto.getCGfSpeend()));
		}
		if(gisMechanicalPropertiesDto.getCOpenCurrent()!=""&&gisMechanicalPropertiesDto.getCOpenCurrent()!=null){
			monitor.setCElectric(new BigDecimal(gisMechanicalPropertiesDto.getCOpenCurrent()));
		}
		monitor.setModifyTime(LocalDateTime.now());
		rptGisMonitorDao.add(monitor);
        
        return count;
    }

    @Override
    public GisMechanicalPropertiesVo findGisMechanicalPropertiesVo(String id){
        return gisMechanicalPropertiesMapper.getGisMechanicalPropertiesVo(id);
    }

    @Override
    public GisMechanicalPropertiesVo lastGisMechanicalProperties(String siteId, String mainEquipmentId) {
        return gisMechanicalPropertiesMapper.lastGisMechanicalProperties(siteId, mainEquipmentId);
    }

	@Override
	public List<GisMechanicalPropertiesVo> findByEquipmentId(String siteId,String equipmentId) {
		 return gisMechanicalPropertiesMapper.findByEquipmentId(siteId,equipmentId);
	}
}