package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.dto.BusiSensorDto;
import com.huatek.busi.dto.BusiSensorQueryParamDto;
import com.huatek.busi.mapper.BusiGeneratorSensorMapper;
import com.huatek.busi.mapper.BusiTransformerSensorMapper;
import com.huatek.busi.mapper.generator.BusiGeneratorAmplTypeMapper;
import com.huatek.busi.service.BusiOtherSensorService;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.IBusiCableSensorService;
import com.huatek.busi.service.arrester.BusiArresterSensorService;
import com.huatek.busi.service.generatro.BusiGeneratorSensorService;
import com.huatek.busi.service.gis.BusiGisSensorService;
import com.huatek.busi.service.temperature.BusiTemperPointService;
import com.huatek.busi.service.transformer.BusiTransformerSensorService;
import com.huatek.busi.vo.BusiSensorVo;
import com.huatek.busi.vo.generator.BusiGeneratorAmplTypeVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @ClassName: BusiSensorServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.BusiSensorServiceImpl
 * @Description:
 * @author: Arno
 * @date: Jul 18, 2019 3:25:57 PM
 * @version: 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = BusiSensorService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiSensorServiceImpl implements BusiSensorService {
    @Autowired
    private BusiTransformerSensorService busiTransformerSensorService;
    @Autowired
    private BusiGisSensorService busiGisSensorService;
    @Autowired
    private BusiArresterSensorService busiArresterSensorService;
    @Autowired
    private BusiTemperPointService busiTemperPointService;
    @Autowired
    private BusiOtherSensorService busiOtherSensorService;

    @Autowired
    private BusiGeneratorSensorService busiGeneratorSensorService;

    @Autowired
    private BusiGeneratorSensorMapper busiGeneratorSensorMapper;

    @Autowired
    private BusiGeneratorAmplTypeMapper busiGeneratorAmplTypeMapper;

    @Reference(check = false)
    IBusiCableSensorService busiCableSensorService;
	
	@Autowired
    private BusiTransformerSensorMapper busiTransformerSensorMapper;
	
    @Override
    public Map<String, Object> page(BusiSensorDto busiSensorDto) {
        Page<BusiSensorVo> page = null;
        PageHelper.startPage(busiSensorDto.getPage(), busiSensorDto.getLimit());
        String equipmentType = busiSensorDto.getEquipmentType();
        String equipmentId = busiSensorDto.getEquipmentId();
        if (ExternalTypeEnum.BYQ.getValue().equals(equipmentType)) {
            page = busiTransformerSensorService.page(equipmentId);
        } else if (ExternalTypeEnum.GIS.getValue().equals(equipmentType)) {
            page = busiGisSensorService.page(equipmentId);
        } else if (ExternalTypeEnum.CW.getValue().equals(equipmentType)) {
            page = busiTemperPointService.page(equipmentId);
        } else if (ExternalTypeEnum.BLQ.getValue().equals(equipmentType)) {
            page = busiArresterSensorService.page(equipmentId);
        } else if (ExternalTypeEnum.OTHER.getValue().equals(equipmentType)) {
            page = busiOtherSensorService.page(equipmentId);
        }else if (ExternalTypeEnum.GEN.getValue().equals(equipmentType)) {
            page = busiGeneratorSensorService.page(equipmentId);
        }else if (ExternalTypeEnum.DL.getValue().equals(equipmentType)) {
            page = busiCableSensorService.page(equipmentId);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, page.getTotal(), "BUSINESS.QUERY.SUCCESS", page.getResult());
    }

    @Override
    public boolean add(BusiSensorVo busiSensorVo) {
        String equipmentType = busiSensorVo.getEquipmentType();
        if (SiteConstants.ExternalTypeEnum.BYQ.getValue().equals(equipmentType)) {
            busiTransformerSensorService.add(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.GIS.getValue().equals(equipmentType)) {
            busiGisSensorService.add(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.CW.getValue().equals(equipmentType)) {
            busiTemperPointService.add(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.BLQ.getValue().equals(equipmentType)) {
            busiArresterSensorService.add(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.OTHER.getValue().equals(equipmentType)) {
            busiOtherSensorService.add(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.GEN.getValue().equals(equipmentType)) {
            busiGeneratorSensorService.add(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.DL.getValue().equals(equipmentType)) {
            busiCableSensorService.add(busiSensorVo);
        }
        return false;
    }

    @Override
    public boolean update(String token, BusiSensorVo busiSensorVo) {
        String equipmentType = busiSensorVo.getEquipmentType();
        if (SiteConstants.ExternalTypeEnum.BYQ.getValue().equals(equipmentType)) {
            busiTransformerSensorService.update(token, busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.GIS.getValue().equals(equipmentType)) {
            busiGisSensorService.update(token, busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.CW.getValue().equals(equipmentType)) {
            busiTemperPointService.update(busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.BLQ.getValue().equals(equipmentType)) {
            busiArresterSensorService.update(token, busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.OTHER.getValue().equals(equipmentType)) {
            busiOtherSensorService.update(token, busiSensorVo);
        } else if (SiteConstants.ExternalTypeEnum.DL.getValue().equals(equipmentType)) {
            busiCableSensorService.update(token, busiSensorVo);
        }
        else if (SiteConstants.ExternalTypeEnum.GEN.getValue().equals(equipmentType)) {
            busiGeneratorSensorService.update(token, busiSensorVo);
        }

        return false;
    }

    @Override
    public boolean delete(String ids, String equipmentType) {
        List<String> list = new ArrayList<>();
        if (ids.contains(",")) {
            list.addAll(Arrays.asList(ids.split(",")));
        } else {
            list.add(ids);
        }
        if (SiteConstants.ExternalTypeEnum.BYQ.getValue().equals(equipmentType)) {
            busiTransformerSensorService.delete(list);
        } else if (SiteConstants.ExternalTypeEnum.GIS.getValue().equals(equipmentType)) {
            busiGisSensorService.delete(list);
        } else if (SiteConstants.ExternalTypeEnum.CW.getValue().equals(equipmentType)) {
            busiTemperPointService.delete(list);
        } else if (SiteConstants.ExternalTypeEnum.BLQ.getValue().equals(equipmentType)) {
            busiArresterSensorService.delete(list);
        } else if (SiteConstants.ExternalTypeEnum.OTHER.getValue().equals(equipmentType)) {
            busiOtherSensorService.delete(list);
        } else if (ExternalTypeEnum.GEN.getValue().equals(equipmentType)) {
            busiGeneratorSensorService.delete(list);
        }
        else if(SiteConstants.ExternalTypeEnum.DL.getValue().equals(equipmentType)){
            busiCableSensorService.delete(list);
        }
        return false;
    }

    @Override
    public BusiSensorVo getBusiSensorVo(ExternalTypeEnum type, String sensorCode) {
        if (ExternalTypeEnum.BYQ == type) {
            return busiTransformerSensorService.getBusiSensorVo(sensorCode);
        }
        if (ExternalTypeEnum.GIS == type) {
            return busiGisSensorService.getBusiSensorVo(sensorCode);
        }
        if (ExternalTypeEnum.CW == type) {
            return busiTemperPointService.getBusiSensorVo(sensorCode);
        }
        if (ExternalTypeEnum.BLQ == type) {
            return busiArresterSensorService.getBusiSensorVo(sensorCode);
        }
        if (ExternalTypeEnum.OTHER == type) {
            return busiOtherSensorService.getBusiSensorVo(sensorCode);
        }
        if (ExternalTypeEnum.DL == type) {
            return busiCableSensorService.getBusiSensorVo(sensorCode);
        }
        log.warn("未知ExternalTypeEnum类型：{}", type);
        return null;
    }

    @Override
    public BusiSensorVo getBusiSensorVo(SiteTreeEnum siteTreeEnum, String sensorCode) {
        BusiSensorVo vo = null;
        switch (siteTreeEnum) {
            case BLQ:
            case BLQ_HL:
            case BLQ_HL_EQM:
                vo = busiArresterSensorService.getBusiSensorVo(sensorCode);
                break;
            case BYQ:
            case BYQ_EQM:
                vo = busiTransformerSensorService.getBusiSensorVo(sensorCode);
                break;
            case CW:
            case CW_HL:
            case CW_HL_EQM:
                vo = busiTemperPointService.getBusiSensorVo(sensorCode);
                break;
            case GIS:
            case GIS_HL:
            case GIS_HL_EQM:
                vo = busiGisSensorService.getBusiSensorVo(sensorCode);
                break;
            case OTHER:
            case OTHER_EQM:
                vo = busiOtherSensorService.getBusiSensorVo(sensorCode);
                break;
            case DL:
            case DL_EQM:
                vo = busiCableSensorService.getBusiSensorVo(sensorCode);
                break;
            case ROOT:
            case SITE:
                break;
        }
        log.debug("传感器>>>type: {}, sensorCode: {}, vo: {}", siteTreeEnum, sensorCode, vo);
        return vo;
    }

    @Override
    public BusiSensorVo getBusiSensorVoById(SiteTreeEnum siteTreeEnum, String id) {
        BusiSensorVo vo = null;
        switch (siteTreeEnum) {
            case BLQ:
            case BLQ_HL:
            case BLQ_HL_EQM:
                vo = busiArresterSensorService.getBusiSensorVoById(id);
                break;
            case BYQ:
            case BYQ_EQM:
                vo = busiTransformerSensorService.getBusiSensorVoById(id);
                break;
            case CW:
            case CW_HL:
            case CW_HL_EQM:
                vo = busiTemperPointService.getBusiSensorVoById(id);
                break;
            case GIS:
            case GIS_HL:
            case GIS_HL_EQM:
                vo = busiGisSensorService.getBusiSensorVoById(id);
                break;
            case OTHER:
            case OTHER_EQM:
                vo = busiOtherSensorService.getBusiSensorVoById(id);
                break;
            case DL:
            case DL_EQM:
                vo = busiCableSensorService.getBusiSensorVoById(id);
                break;
            case ROOT:
            case SITE:
                break;
        }
        log.debug("传感器>>>type: {}, id: {}, vo: {}", siteTreeEnum, id, vo);
        return vo;
    }

    @Override
    public List<BusiSensorVo> querySensorByEquipMentIdAndType(String equipmentId, String type) {
        SiteTreeEnum siteTreeEnum = SiteTreeEnum.fromValue(type);
        return findSensorList(siteTreeEnum, equipmentId);
    }

    @Override
    public List<BusiSensorVo> findSensorList(ExternalTypeEnum type, String equipmentId) {
        List<BusiSensorVo> list = null;
        BusiSensorQueryParamDto param = new BusiSensorQueryParamDto();
        param.setEquipmentId(equipmentId);
        switch (type) {
            case BLQ:
                list = busiArresterSensorService.findSensorList(param);
                break;
            case BYQ:
                list = busiTransformerSensorService.findSensorList(param);
                break;
            case CW:
                list = busiTemperPointService.findSensorList(equipmentId);
                break;
            case GIS:
                list = busiGisSensorService.findSensorList(param);
                break;
            case OTHER:
                list = busiOtherSensorService.findSensorList(param);
                break;
            case DL:
                list = busiCableSensorService.findSensorList(param);
                break;
        }
        return list;
    }

    @Override
    public List<BusiSensorVo> findSensorList(SiteTreeEnum siteTreeEnum, String equipmentId) {
        BusiSensorQueryParamDto param = new BusiSensorQueryParamDto();
        param.setEquipmentId(equipmentId);
        return this.findSensorList(siteTreeEnum, param);
    }

    @Override
    public List<BusiSensorVo> findSensorList(SiteTreeEnum siteTreeEnum, String[] equipmentTypes) {
        BusiSensorQueryParamDto param = new BusiSensorQueryParamDto();
        if (equipmentTypes != null) {
            param.setEquipmentTypeList(Arrays.asList(equipmentTypes));
        }
        return this.findSensorList(siteTreeEnum, param);
    }

    @Override
    public List<BusiSensorVo> findSensorList(SiteTreeEnum siteTreeEnum, BusiSensorQueryParamDto param) {
        List<BusiSensorVo> sensorList = null;

        switch (siteTreeEnum) {
            case BLQ:
            case BLQ_HL:
            case BLQ_HL_EQM:
                sensorList = busiArresterSensorService.findSensorList(param);
                break;
            case BYQ:
            case BYQ_EQM:
                sensorList = busiTransformerSensorService.findSensorList(param);
                break;
            case CW:
            case CW_HL:
            case CW_HL_EQM:
                if (param == null) {
                    sensorList = busiTemperPointService.findSensorList(null);
                } else {
                    sensorList = busiTemperPointService.findSensorList(param.getEquipmentId());
                }
                break;
            case GIS:
            case GIS_HL:
            case GIS_HL_EQM:
                sensorList = busiGisSensorService.findSensorList(param);
                break;
            case OTHER:
            case OTHER_EQM:
                sensorList = busiOtherSensorService.findSensorList(param);
                break;
            case DL:
            case DL_EQM:
                sensorList = busiCableSensorService.findSensorList(param);
                break;
            case GEN:
            case GEN_EQM:
                sensorList = busiGeneratorSensorService.findSensorList(param);
                break;
            case ROOT:
            case SITE:
                break;
        }
        return sensorList;
    }

    @Override
    public int findSensorCount(String externalType, String equipmentType) {
        int sensorCount = 0;
        if (externalType.equals(SiteTreeEnum.BYQ.getValue())) {
            sensorCount = busiTransformerSensorService.findSensorCount(equipmentType);
        } else if (externalType.equals(SiteTreeEnum.GIS.getValue())) {
            sensorCount = busiGisSensorService.findSensorCount(equipmentType);
        } else if (externalType.equals(SiteTreeEnum.BLQ.getValue())) {
            sensorCount = busiArresterSensorService.findSensorCount(equipmentType);
        } else if (externalType.equals(SiteTreeEnum.OTHER.getValue())) {
            sensorCount = busiOtherSensorService.findSensorCount(equipmentType);
        }else if (externalType.equals(SiteTreeEnum.DL.getValue())) {
            sensorCount = busiCableSensorService.findSensorCount(equipmentType);
        }
        return sensorCount;
    }

    @Override
    public List<BusiSensorVo> findOilSensor(String equipmentId){
        String status = SiteConstants.Status.NORMAL.getValue();
        String oilType = SiteConstants.TransfromerTreeEnum.YSP_EQM.getValue();
        return busiTransformerSensorMapper.findOilSensor(equipmentId,status,oilType);
    }

    @Override
    public List<BusiSensorVo> findZdSensor(String equipmentId) {
        String status = SiteConstants.Status.NORMAL.getValue();
        String zdType = SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue();
        return busiGeneratorSensorMapper.findZdSensor(equipmentId,status,zdType);
    }

    @Override
    public List<BusiGeneratorAmplTypeVo> findAmplTypeList(String equipmentType) {
        Map<String, Object> params = new HashMap<>();
        params.put("equipmentType", equipmentType);
        return busiGeneratorAmplTypeMapper.selectAmplTypeList(params);
    }
}
