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

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.BusiOfflineGisBrandDao;
import com.huatek.busi.dao.BusiOfflineTransformerBrandDao;
import com.huatek.busi.dto.BusiOffllineGisBrandDto;
import com.huatek.busi.dto.LabReportMarkStatisticDto;
import com.huatek.busi.entity.BusiOfflineGisBrand;
import com.huatek.busi.entity.BusiOfflineTransformerBrand;
import com.huatek.busi.mapper.BusiOfflineGisBrandMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.ILabReportMarkStatisticService;
import com.huatek.busi.service.gis.BusiOfflineGisBrandService;
import com.huatek.busi.vo.BusiOfflineGisBrandVo;
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;

/**
 * GIS标牌信息表 ServiceImpl
 *
 * @author walker_wang
 * @date Jul 4, 2019 1:50:15 PM
 */
@Component
@Service(interfaceClass = BusiOfflineGisBrandService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineGisBrandServiceImpl implements BusiOfflineGisBrandService {

    @Autowired
    private BusiOfflineGisBrandMapper busiOffllineGisBrandMapper;

    @Autowired
    private BusiOfflineGisBrandDao busiOfflineGisBrandDao;

    @Autowired
    private BusiSiteTreeService busiSiteTreeService;

    @Autowired
    private ILabReportMarkStatisticService iLabReportMarkStatisticService;


    @Override
    public int updateBusiOffllineGisBrand(String token, BusiOffllineGisBrandDto busiOffllineGisBrandDto) {
        BusiOfflineGisBrand busiOffllineGisBrand = new BusiOfflineGisBrand();
        BeanUtils.copyPropertiesWithIgnoreNull(busiOffllineGisBrandDto, busiOffllineGisBrand);
        // TODO GIS标牌保存数据校验
        // ...

        // 更新树节点上的主设备名称
        busiSiteTreeService.updateNodeFromEquipment(busiOffllineGisBrandDto.getMainEquipmentId(),
            busiOffllineGisBrandDto.getGisName());
        return busiOfflineGisBrandDao.updateLocal(busiOffllineGisBrand);
    }

    @Override
    public BusiOfflineGisBrandVo findBusiOfflineGISBrand(String siteId, String mainTreeId) {
        return busiOffllineGisBrandMapper.findBusiOfflineGISBrandVo(siteId, mainTreeId);
    }

    @Override
    public BusiOfflineGisBrandVo findBusiOfflineGISBrand(String siteId, String mainEquipmentId, String equTypeId){
        return busiOffllineGisBrandMapper.findBusiOfflineGISBrandVo1(siteId, mainEquipmentId, equTypeId);
    }

    @Override
    public List<BusiOfflineGisBrand> findAll() {
        return busiOffllineGisBrandMapper.findAll();
    }

    @Override
    public int saveOrUpdateBrand(String siteId, String mainEquipmentId, String name) {
        BusiOfflineGisBrand busiOfflineGisBrand =
            busiOffllineGisBrandMapper.findBusiOfflineGISBrand(siteId, mainEquipmentId);
        if (busiOfflineGisBrand == null) {
            busiOfflineGisBrand = new BusiOfflineGisBrand();
            busiOfflineGisBrand.setSiteId(siteId);
            busiOfflineGisBrand.setMainEquipmentId(mainEquipmentId);
            busiOfflineGisBrand.setEquipmentCode(GenerateUtil.generateCode(17));
            busiOfflineGisBrand.setCreateTime(LocalDateTime.now());
            busiOfflineGisBrand.setStatus(Status.NORMAL.getValue());
        }
        busiOfflineGisBrand.setGisName(name);
        return busiOfflineGisBrandDao.saveOrUpdate(busiOfflineGisBrand);
    }

    @Override
    public int insert(BusiOfflineGisBrand busiOfflineGisBrand){
        return busiOfflineGisBrandDao.insert(busiOfflineGisBrand);
    }

    @Override
    public int update(BusiOfflineGisBrand busiOfflineGisBrand){
        return busiOfflineGisBrandDao.updateLocal(busiOfflineGisBrand);
    }

    @Override
    public void updateBrand(String equipmentId) {
        List<LabReportMarkStatisticDto> list = iLabReportMarkStatisticService.getLastImportInfo(equipmentId,"G");
        if (!CollectionUtils.isEmpty(list)){
            Map<String, String> map = list.stream().collect(Collectors.toMap(LabReportMarkStatisticDto::getERule, LabReportMarkStatisticDto::getCurrentValue));
            BusiOfflineGisBrand offlineGisBrand = busiOfflineGisBrandDao.get(new WherePrams().where("main_equipment_id", equipmentId).and("status", Status.NORMAL.getValue()));
            Set<String> strings = map.keySet();

            //额定电压(KV)
            if (strings.contains("0046")) {
                offlineGisBrand.setNominalVoltage(map.get("0046"));
            }
            //额定电流(KA)
            if (strings.contains("0047")) {
                offlineGisBrand.setRatedFrequency(map.get("0047"));
            }
            if (strings.contains("0048")) {
                offlineGisBrand.setElectricCurrent(map.get("0048"));
            }
            if (strings.contains("0049")) {
                offlineGisBrand.setBreakTime(map.get("0049"));
            }

            if (strings.contains("0050")) {
                LocalDateTime localDate = parseLocalDate(map.get("0050"));
                if (!ObjectUtils.isEmpty(localDate)){
                    offlineGisBrand.setEvaluateTime(localDate);
                }
            }
            if (strings.contains("0051")) {
                LocalDateTime localDate = parseLocalDate(map.get("0051"));
                if (!ObjectUtils.isEmpty(localDate)){
                    offlineGisBrand.setTransportTime(localDate);
                }
            }
            if (strings.contains("0052")) {
                offlineGisBrand.setAssetValue(map.get("0052"));
            }
            if (strings.contains("0053")) {
                offlineGisBrand.setProductMaturity(map.get("0053"));
            }
            if (strings.contains("0054")) {
                offlineGisBrand.setPopularity(map.get("0054"));
            }
            if (strings.contains("0055")) {
                LocalDateTime localDate = parseLocalDate(map.get("0055"));
                if (!ObjectUtils.isEmpty(localDate)){
                    offlineGisBrand.setFactoryTime(localDate);
                }
            }
            if (strings.contains("0056")) {
                offlineGisBrand.setYxEnvironment(map.get("0056"));
            }
            if (strings.contains("0057")) {
                offlineGisBrand.setServiceTime(map.get("0057"));
            }

            busiOfflineGisBrandDao.updateLocal(offlineGisBrand);
        }
    }

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

        }
        return localDate;
    }
}