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

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

import com.huatek.busi.util.CalcUtil;
import org.apache.commons.lang3.ObjectUtils;
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.WarningType;
import com.huatek.busi.common.ReportType;
import com.huatek.busi.common.SiteConstants.GisTreeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.mapper.GisSf6PressureMapper;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.gis.BusiOfflineGisRepairService;
import com.huatek.busi.service.impl.report.helper.ReportImpl;
import com.huatek.busi.service.report.BusiCommonReportService;

@ReportImpl(value = "busiSF6ReportService", exportTypes = {ReportType.SF6_MONTHLY_REPORT})
public class BusiSF6ReportServiceImpl implements BusiCommonReportService {
    @Autowired
    private BusiSubTreeService busiSubTreeService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;
    @Autowired
    private BusiOfflineGisRepairService busiOfflineGisRepairService;
    @Autowired
    private BusiSensorService busiSensorService;
    @Autowired
    private GisSf6PressureMapper gisSf6PressureMapper;

    @Override
    public Map<String, Object> buildExportData(String exportType, List<String> sensors, String exportTime,String startTime, String endTime) {
        return this.buildExportData(sensors, exportTime,startTime,endTime);
    }

    private Map<String, Object> buildExportData(List<String> sernsors, String exportTime,String startTime,String endTime) {
        EquipmentInfoDto equipmentInfo = new EquipmentInfoDto();
        String sensorCode = sernsors.get(0);
        equipmentInfo = busiSubTreeService.findEquipmentInfo(sensorCode, SiteTreeEnum.GIS.getValue());

        Map<String, Object> param = Maps.newHashMap();
        Map<String, Object> resultData = Maps.newLinkedHashMapWithExpectedSize(35);
        Map<String, Object> result = Maps.newHashMap();
        Map<String, LocalDateTime> mapDate = CalcUtil.getcurrentMonthDateMap();
        if (equipmentInfo != null) {
            param.put("mainTreeId", equipmentInfo.getMainTreeId());
            param.put("sensorCode", sensorCode);
            param.put("startTime", mapDate.get("startTime"));
            param.put("endTime", mapDate.get("endTime"));
            BigDecimal totalCount = DecimalUtil.getVal(busiEqmWarningService.getBusiEqmWarningCount(param));

            // 获取检修次数
            int repairCount = busiOfflineGisRepairService.getRepairCount(param);
            // 获取sf6检测设备总数
            int sensorCount =
                busiSensorService.findSensorCount(SiteTreeEnum.GIS.getValue(), GisTreeEnum.SF6_EQM.getValue());
            // 获取检修率
            String repareRate = "0";
            if (sensorCount != 0 && repairCount != 0) {
                // 转换成百分比
                DecimalFormat df = new DecimalFormat("0.00");// 设置保留位数
                repareRate =
                    df.format(DecimalUtil.divide(DecimalUtil.getVal(repairCount).multiply(BigDecimal.valueOf(100)),
                        DecimalUtil.getVal(sensorCount), 2));
            }

            param.put("warningType", WarningType.FAULT);
            BigDecimal gzCount = 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));
            }

            resultData.put("reportCode", "");
            resultData.put("reportDate", LocalDate.now());
            resultData.put("sensorNumber", "");
            resultData.put("installPosition", equipmentInfo.getSensorPositionDes());
            resultData.put("operationDate",
                equipmentInfo.getSensorUseDate() == null ? "" : equipmentInfo.getSensorUseDate());
            resultData.put("sensorType", "");
            resultData.put("manufacturer",
                equipmentInfo.getSubEquipmentManufactor() == null ? "" : equipmentInfo.getSubEquipmentManufactor());
            resultData.put("overhaulNumber", repairCount);
            resultData.put("overhaulRate", repareRate);

            List<Map<String, Object>> pdList1 = Lists.newArrayListWithCapacity(10);
            Map<String, Object> item = Maps.newLinkedHashMapWithExpectedSize(7);

            item.put("totalWarnNumber", totalCount);
            item.put("falseAlarmRate", falseAlarmRate);
            item.put("gzCount", gzCount);

            pdList1.add(item);
            resultData.put("sfList", pdList1);

            resultData.put("reporter", "");
            resultData.put("signatory", "");
            resultData.put("reportDescription", "");

            List<Map<String, Object>> monthList1 = Lists.newArrayListWithCapacity(10);
//            busiEqmWarningDto.setSensorId(sensorCode);
//            LocalDateTime warnStartTime = DateUtil.getMinusMonths(exportTime);
//            busiEqmWarningDto.setWarningTime(warnStartTime);
//            busiEqmWarningDto.setWarningTimeEnd(endTime);
            Map<String, Object> mapParams = Maps.newLinkedHashMapWithExpectedSize(7);
            mapParams.put("sensorCode", sensorCode);
            mapParams.put("timeStamp", mapDate.get("startTime"));
            mapParams.put("timeStampEnd", mapDate.get("endTime"));
//            if (StringUtils.isNotEmpty(startTime)) {
//            	mapParams.put("timeStamp", startTime);
//            } else {
//            	mapParams.put("timeStamp", DateUtil.format(DateUtil.atStartOfDay(LocalDate.now())));
//            }
//            if (StringUtils.isNotEmpty(endTime)) {
//            	mapParams.put("timeStampEnd", endTime);
//            } else {
//            	mapParams.put("timeStampEnd", DateUtil.format(DateUtil.atEndOfDay(LocalDate.now())));
//            }
            List<Map<String,Object>> voList = gisSf6PressureMapper.exportReportData(mapParams);
//            List<BusiEqmWarningVo> voList = busiEqmWarningService.findBusiEqmWarningList(busiEqmWarningDto);
            if (!voList.isEmpty()) {
                for (int i = 0; i < voList.size(); i++) {
                    Map<String, Object> monthitem = Maps.newLinkedHashMapWithExpectedSize(8);
                    Map<String, Object> sf6PressureVoVo = voList.get(i);
                    if (ObjectUtils.isNotEmpty(sf6PressureVoVo)) {
                    	monthitem.put("serialNumber", i + 1);
                    	monthitem.put("type",sf6PressureVoVo.get("sensorId"));
                    	monthitem.put("warningMsg",sf6PressureVoVo.get("temperature")); //温度
                    	monthitem.put("dealStatus", sf6PressureVoVo.get("moisture"));   //微水
                    	monthitem.put("workStatus",sf6PressureVoVo.get("pressure"));    //压力
                    	monthitem.put("warnTime", sf6PressureVoVo.get("timeStamp"));    //采集时间
                    	monthList1.add(monthitem);
                    }
//                    if (busiEqmWarningVo != null) {
//                        monthitem.put("serialNumber", i + 1);
//                        monthitem.put("type", BusiConstant.getGzTypeDescribe(busiEqmWarningVo.getGzType() == null ? "" : busiEqmWarningVo.getGzType()));
//                        monthitem.put("warningMsg",busiEqmWarningVo.getWarningMsg() == null ? "" : busiEqmWarningVo.getWarningMsg());
//                        if ("0".equals(busiEqmWarningVo.getDealStatus())) {
//                            monthitem.put("dealStatus", "未处理");
//                        } else if ("1".equals(busiEqmWarningVo.getDealStatus())) {
//                            monthitem.put("dealStatus", "已处理");
//                        }
//                        monthitem.put("workStatus", SiteConstants.getIecTypeDescribe( busiEqmWarningVo.getRunState() == null ? "" : busiEqmWarningVo.getRunState()));
//                        monthitem.put("warnTime", busiEqmWarningVo.getWarningTime() == null ? "" : DateUtil.format(busiEqmWarningVo.getWarningTime()));
//                        monthitem.put("remark", "");
//                        monthList1.add(monthitem);
//                    }
                }
            } else {
                Map<String, Object> monthitem = Maps.newLinkedHashMapWithExpectedSize(8);

                monthitem.put("serialNumber", "");
                monthitem.put("type", "");
                monthitem.put("warningMsg", "");
                monthitem.put("dealStatus", "");
                monthitem.put("workStatus", "");
                monthitem.put("warnTime", "");
                monthitem.put("remark", "");
                monthList1.add(monthitem);
            }
            resultData.put("sfMonthList", monthList1);
        } else {
            resultData.put("reportCode", "");
            resultData.put("reportDate", exportTime);
            resultData.put("sensorNumber", "");
            resultData.put("installMode", "");
            resultData.put("operationDate", "");
            resultData.put("sensorType", "");
            resultData.put("manufacturer", "");
            resultData.put("overhaulNumber", "");
            resultData.put("overhaulRate", "");

            List<Map<String, Object>> pdList1 = Lists.newArrayListWithCapacity(10);
            Map<String, Object> item = Maps.newLinkedHashMapWithExpectedSize(7);

            item.put("totalWarnNumber", "");
            item.put("falseAlarmRate", "");
            item.put("gzCount", "");
            pdList1.add(item);
            resultData.put("sfList", pdList1);

            resultData.put("reporter", "");
            resultData.put("signatory", "");
            resultData.put("reportDescription", "");

            List<Map<String, Object>> monthList1 = Lists.newArrayListWithCapacity(10);
            Map<String, Object> monthitem = Maps.newLinkedHashMapWithExpectedSize(8);

            monthitem.put("serialNumber", "");
            monthitem.put("type", "");
            monthitem.put("warningMsg", "");
            monthitem.put("dealStatus", "");
            monthitem.put("workStatus", "");
            monthitem.put("warnTime", "");
            monthitem.put("remark", "");
            monthList1.add(monthitem);
            resultData.put("sfMonthList", monthList1);

        }
        result.put("fileName", "SF6检测装置管理考核");
        result.put("data", resultData);
        return result;
    }
}
