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

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.huatek.busi.common.BusiConstant;
import com.huatek.busi.common.BusiConstant.WarningType;
import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.report.BusiCommonReportService;
import com.huatek.busi.service.transformer.TransformerPdService;

/**
 * 局放报告抽象类
 * 
 * @author anker_cheng
 * @date 2019/09/25
 */
public abstract class AbstractPdMonthlyReprotService implements BusiCommonReportService {
    @Autowired
    protected TransformerPdService transformerPdService;
    @Autowired
    protected BusiSubTreeService busiSubTreeService;
    @Autowired
    protected BusiEqmWarningService busiEqmWarningService;

    protected abstract EquipmentInfoDto findEquipmentInfo(String sensorCode);

    protected abstract String fileName();

    @Override
    public Map<String, Object> buildExportData(String exportType, List<String> sensors, String exportTime,String startTime,String endTime) {
        Map<String, Object> result = Maps.newLinkedHashMapWithExpectedSize(2);
        Map<String, Object> data = Maps.newLinkedHashMapWithExpectedSize(30);
        result.put("fileName", fileName());
        result.put("data", data);

//      LocalDate day = LocalDate.parse(exportTime, DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATE));
//      LocalDateTime startTime = DateUtil.monthStartTime(day);
//      LocalDateTime endTime = DateUtil.monthEndTime(day);
        String sensorCode = sensors.get(0);
        EquipmentInfoDto equipmentInfo = this.findEquipmentInfo(sensorCode);

        // 报告编号
        data.put("rpNum", "");
        // 报告日期
        data.put("rpDate", LocalDate.now());
        // 安装位置
        data.put("rpLoc", "");
        // 安装方式
        data.put("insType", "");
        // 投运日期
        data.put("useDate", "");
        // 传感器类型
        data.put("sensorType", "");
        // 检修周期（月）
        data.put("rpCycle", "");
        // 生产厂家
        data.put("factor", "");
        // 报告评价
        data.put("rpEva", "");
        // 处理意见
        data.put("dealOpt", "");

        // 报告评价说明
        data.put("rpEvaDes", "");
        // 报告人
        data.put("rpPerson", "");
        // 签字
        data.put("rpSign", "");
        List<Map<String, Object>> pdList1 = Lists.newArrayListWithCapacity(1);
        if (equipmentInfo != null) {
            // 安装位置
            data.put("rpLoc", equipmentInfo.getSensorPositionDes()==null ? "":equipmentInfo.getSensorPositionDes());
            // 投运日期
            data.put("useDate", equipmentInfo.getSensorUseDate()==null ? "":equipmentInfo.getSensorUseDate());
            // 生产厂家
            data.put("factor", equipmentInfo.getSensorManufactor()== null ? " ":equipmentInfo.getSensorManufactor());

            Map<String, Object> param = Maps.newHashMap();
            param.put("mainTreeId", equipmentInfo.getMainTreeId());
            param.put("startTime", startTime);
            param.put("endTime", endTime);
            param.put("sensorCode", sensorCode);
            BigDecimal totalCount = DecimalUtil.getVal(busiEqmWarningService.getBusiEqmWarningCount(param));
            param.put("warningType", WarningType.MISREPORT);
            BigDecimal wbCount = DecimalUtil.getVal(busiEqmWarningService.getBusiEqmWarningCount(param));

            String falseAlarmRate = "0";
            if (BigDecimal.ZERO.compareTo(totalCount) != 0 && BigDecimal.ZERO.compareTo(wbCount) != 0) {
                // 转换成百分比
                DecimalFormat df = new DecimalFormat("0.00");// 设置保留位数
                falseAlarmRate = df.format(DecimalUtil.divide(wbCount.multiply(BigDecimal.valueOf(100)), totalCount, 2));
            }

            // 统计数据psList1
            Map<String, Object> pdList1Item = Maps.newLinkedHashMapWithExpectedSize(5);
            // 传感器
            pdList1Item.put("val1", sensorCode);
            // 稳定性
            pdList1Item.put("val2", "--");
            // 误报率
            pdList1Item.put("val3", falseAlarmRate);
            // 误报次数
            pdList1Item.put("val4", wbCount);
            // 告警次数
            pdList1Item.put("val5", totalCount);
            pdList1.add(pdList1Item);
        }
        data.put("pdList1", pdList1);
        // 统计数据pdList2
        if (StringUtils.isEmpty(startTime)) {
        	startTime = DateUtil.format(DateUtil.atStartOfDay(LocalDate.now()));
    	}
        if (StringUtils.isEmpty(endTime)) {
        	endTime = DateUtil.format(DateUtil.atEndOfDay(LocalDate.now()));
    	}
        List<Map<String, Object>> pdList2 = transformerPdService.findReportDataList(sensorCode, startTime, endTime);
        if (pdList2 == null) {
            pdList2 = Collections.emptyList();
        }
        for (int index = 0; index < pdList2.size(); index++) {
            pdList2.get(index).put("val1", index + 1);
            //获取放电类型
            if(pdList2.get(index).get("val3") != null)
            {
	            pdList2.get(index).put("val3", BusiConstant.getDschTypeDescribe(pdList2.get(index).get("val3").toString()));
	        }
            else {
            	pdList2.get(index).put("val3","");
            }
        }
        data.put("pdList2", pdList2);
        return result;
    }

}
