package com.huatek.busi.service.impl;

import java.util.Date;
import java.util.Map;

import com.huatek.busi.service.cable.BusiOfflineCableRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Maps;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.arrester.BusiOfflineArresterRepairService;
import com.huatek.busi.service.gis.BusiOfflineGisRepairService;
import com.huatek.busi.service.transformer.BusiOfflineTransformerRepairService;

import lombok.extern.slf4j.Slf4j;

/**
 * 通用业务接口Service实现
 * 
 * @author anker_cheng
 * @date 2019/09/16
 */
@Slf4j
@Component
@Service(interfaceClass = BusiCommonService.class)
public class BusiCommonServiceImpl implements BusiCommonService {
    @Autowired
    private BusiOfflineTransformerRepairService busiOfflineTransformerRepairService;
    @Autowired
    private BusiOfflineArresterRepairService busiOfflineArresterRepairService;
    @Autowired
    private BusiOfflineGisRepairService busiOfflineGisRepairService;
    @Autowired
    private BusiOfflineCableRepairService busiOfflineCableRepairService;

    @Override
    public Integer getEquipHalfYearRepairCount(ExternalTypeEnum externalTypeEnum, String subEquipmentId) {
        // 开始时间、结束时间
        Date end = new Date();
        Date begin = getBeforeHalfYear(end);
        // 参数map
        Map<String, Object> params = Maps.newLinkedHashMap();
        params.put("startTime", begin);
        params.put("endTime", end);
        params.put("subEquipmentId", subEquipmentId);
        // 查询
        Integer repairCount = 0;
        switch (externalTypeEnum) {
            case BLQ:
                repairCount = busiOfflineArresterRepairService.getRepairCountForArrester(params);
                break;
            case BYQ:
                repairCount = busiOfflineTransformerRepairService.getTransformerRepairCount(params);
                break;
            case CW:
                break;
            case GIS:
                repairCount = busiOfflineGisRepairService.getRepairCountForGis(params);
                break;
            case OTHER:
                break;
            case DL:
                repairCount = busiOfflineCableRepairService.getRepairCount(params);
                break;
        }
        log.debug("维修次数：{}，参数:{}", repairCount, params);
        return repairCount;
    }

    private Date getBeforeHalfYear(Date end) {
        Long halfYear = 365L * 12L * 3600L * 1000L;
        Long pastResult = end.getTime() - halfYear; // 半年前的时间（毫秒单位）
        Date begin = new Date(pastResult);
        return begin;
    }

    @Override
    public Integer getSensorHalfYearRepairCount(SiteTreeEnum siteTreeEnum, String sensorCode) {
        // 开始时间、结束时间
        Date end = new Date();
        Date begin = getBeforeHalfYear(end);
        // 参数Map
        Map<String, Object> params = Maps.newLinkedHashMap();
        params.put("startTime", begin);
        params.put("endTime", end);
        params.put("sensorCode", sensorCode);
        // 查询
        Integer repairCount = 0;
        switch (siteTreeEnum) {
            case BLQ:
            case BLQ_HL:
            case BLQ_HL_EQM:
                repairCount = busiOfflineArresterRepairService.getRepairCountForArrester(params);
                break;
            case BYQ:
            case BYQ_EQM:
                repairCount = busiOfflineTransformerRepairService.getTransformerRepairCount(params);
                break;
            case CW:
            case CW_HL:
            case CW_HL_EQM:
                break;
            case GIS:
            case GIS_HL:
            case GIS_HL_EQM:
                repairCount = busiOfflineGisRepairService.getRepairCountForGis(params);
                break;
            case OTHER:
            case OTHER_EQM:
            case ROOT:
            case SITE:
            default:
                break;
        }
        log.debug("维修次数：{}，参数:{}", repairCount, params);
        return repairCount;
    }
}
