package com.shining.cloud.serviceImpl.information;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.njcn.utils.AppConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.njcn.pojo.advanced.EntityAdvancedData;
import com.njcn.pojo.commons.EventEigDetail;
import com.njcn.pojo.commons.EventInfoDetail;
import com.njcn.pojo.commons.WaveData;
import com.njcn.pojo.wave.WaveDataDetail;
import com.njcn.service.commons.IWaveService;
import com.njcn.utils.PubUtils;
import com.shining.cloud.mapper.information.EventInfoMapper;
import com.shining.cloud.mapper.information.EventMsgMapper;
import com.shining.cloud.pojo.information.EventInfo;
import com.shining.cloud.pojo.information.EventWaveDetail;
import com.shining.cloud.service.information.EventInfoService;
import com.shining.cloud.service.information.SteadyAssService;
import com.shining.cloud.service.report.ShiningReportService;

/**
 * @description: 暂态消息详情ServiceImpl
 * @author: denghuajun
 * @time: 2019-10-17 14:12:37
 **/
@Service
public class EventInfoServiceImpl implements EventInfoService {
    private static final Logger logger = LoggerFactory.getLogger(EventInfoServiceImpl.class);
    @Resource
    private EventInfoMapper eventInfoMapper;
    @Resource
    private IWaveService iWaveService;
    @Resource
    private EventMsgMapper eventMsgMapper;

    @Autowired
    private ShiningReportService shiningReportService;

    @Resource
    private SteadyAssService steadyAssService;

    @Autowired
    private IWaveService waveService;

    @Resource
    private AppConfig appConfig;

    private int backNumber = -1;

    @Override
    public EventInfoDetail eventDetailBaseInfo(String eventMsgIndex) {
        EventInfoDetail eventInfoDetails = new EventInfoDetail();
        try {
            eventInfoDetails = eventInfoMapper.eventDetailBaseInfo(eventMsgIndex);
            eventMsgMapper.updateEventState(eventMsgIndex);
        } catch (Exception e) {
            logger.error("查询暂降事件发生异常，异常是"+e.getMessage());
        }
        return eventInfoDetails;
    }

    @Override
    public List<EventEigDetail> eventDetailEigenvalue(String eventDetailIndex) throws Exception {
        List<EventEigDetail> eventInfoDetails = new ArrayList<>();
        EntityAdvancedData entityAdvancedData = iWaveService.analysis(eventDetailIndex);

        //根据事件获取监测点接线方式
        Integer pttype = iWaveService.getPtType(eventDetailIndex);

        if (entityAdvancedData.backNumber != -1) {
            backNumber = entityAdvancedData.backNumber;
            for (int i = 0; i < entityAdvancedData.backNumber; i++) {
                EventEigDetail eventEigDetail = new EventEigDetail();
                eventEigDetail.setHold_time_dq(entityAdvancedData.evt_buf[i].hold_time_dq * 1000);
                eventEigDetail.setPow_a(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].POW_a));
                eventEigDetail.setPow_b(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].POW_b));
                eventEigDetail.setPow_c(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].POW_c));

                eventEigDetail.setVoltagechange_Va(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].Voltagechange_Va / 1000));
                eventEigDetail.setVoltagechange_Vb(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].Voltagechange_Vb / 1000));
                eventEigDetail.setVoltagechange_Vc(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].Voltagechange_Vc / 1000));


                eventEigDetail.setUa_min(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].ua_min[0]));
                eventEigDetail.setUb_min(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].ub_min[0]));
                eventEigDetail.setUc_min(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].ua_min[0]));

                eventEigDetail.setAngle_diff_ap(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].angle_diff_ap[0]));
                eventEigDetail.setAngle_diff_bp(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].angle_diff_bp[0]));
                eventEigDetail.setAngle_diff_cp(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].angle_diff_cp[0]));

                eventEigDetail.setBph_max_value(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].bph_max_value[0]));
                eventEigDetail.setSagReason(entityAdvancedData.sagReason[0]);//暂降原因，暂降原因都一样
                eventEigDetail.setSagType(entityAdvancedData.sagType[i]);//暂降类型
                eventInfoDetails.add(eventEigDetail);
                eventEigDetail.setPttype(pttype);
            }
        } else {
            backNumber = 0;
            eventInfoDetails = null;
        }
        //eventInfoDetails = eventInfoMapper.eventDetailEigenvalue(eventDetailIndex);
        return eventInfoDetails;
    }

    @Override
    public List<EventEigDetail> reportEigen(String eventDetailIndex,WaveData waveData) throws Exception {
        List<String> tmpWaveTitle = waveData.getTmpWaveTitle();
        int picCounts =(tmpWaveTitle.size()-1)/waveData.getiPhasic();
        
        if(picCounts != 3){
        	return null;
        }
        List<EventEigDetail> eventInfoDetails = new ArrayList<>();
        EntityAdvancedData entityAdvancedData = iWaveService.analysis(eventDetailIndex);

        //根据事件获取监测点接线方式
        Integer pttype = iWaveService.getPtType(eventDetailIndex);

        if (entityAdvancedData.backNumber != -1) {
            backNumber = entityAdvancedData.backNumber;
            for (int i = 0; i < entityAdvancedData.backNumber; i++) {
                EventEigDetail eventEigDetail = new EventEigDetail();
                eventEigDetail.setHold_time_dq(entityAdvancedData.evt_buf[i].hold_time_dq * 1000);
                eventEigDetail.setPow_a(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].POW_a));
                eventEigDetail.setPow_b(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].POW_b));
                eventEigDetail.setPow_c(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].POW_c));

                eventEigDetail.setVoltagechange_Va(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].Voltagechange_Va / 1000));
                eventEigDetail.setVoltagechange_Vb(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].Voltagechange_Vb / 1000));
                eventEigDetail.setVoltagechange_Vc(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].Voltagechange_Vc / 1000));


                eventEigDetail.setUa_min(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].ua_min[0]));
                eventEigDetail.setUb_min(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].ub_min[0]));
                eventEigDetail.setUc_min(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].ua_min[0]));

                eventEigDetail.setAngle_diff_ap(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].angle_diff_ap[0]));
                eventEigDetail.setAngle_diff_bp(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].angle_diff_bp[0]));
                eventEigDetail.setAngle_diff_cp(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].angle_diff_cp[0]));

                eventEigDetail.setBph_max_value(PubUtils.floatRound(2, entityAdvancedData.evt_buf[i].bph_max_value[0]));
                eventEigDetail.setSagReason(entityAdvancedData.sagReason[0]);//暂降原因，暂降原因都一样
                eventEigDetail.setSagType(entityAdvancedData.sagType[i]);//暂降类型
                eventInfoDetails.add(eventEigDetail);
                eventEigDetail.setPttype(pttype);
            }
        } else {
            backNumber = 0;
            eventInfoDetails = null;
        }
        //eventInfoDetails = eventInfoMapper.eventDetailEigenvalue(eventDetailIndex);
        return eventInfoDetails;
    }
    
    @Override
    public EventWaveDetail eventDetailWave(String eventDetailIndex, HttpServletRequest request) throws Exception {
        EventWaveDetail eventWaveDetails = new EventWaveDetail();
        EventInfo eventInfo = new EventInfo();
        eventInfo.setEventDetailIndex(eventDetailIndex);
        eventInfo = eventInfoMapper.selectByPrimaryKey(eventInfo);
        eventWaveDetails = eventInfoMapper.eventDetailWave(eventDetailIndex);
        if (StringUtils.isEmpty(eventWaveDetails.getInstantWaveUrl())) {
            //没有波形再绘制一次
            WaveData waveData = waveService.getWavedata(eventDetailIndex, 2);
            if (!CollectionUtils.isEmpty(waveData.getSunData())) {
                //数据筛选,如果是双路电压的话，会存在2个波形数据
                List<WaveDataDetail> waveDataDetails = waveService.filteWaveData(waveData);
                String url = "";
                Properties pros = PubUtils.readProperties(getClass().getClassLoader(),"java.properties");
                url = pros.get("REPORT_PATH").toString();
                url = url + "eventInfo";

                String path = request.getSession().getServletContext().getRealPath("/appfiles");
                path = path + "\\eventInfo";
                Date now = new Date();
                File tmpfile = new File(path);
                if (!tmpfile.exists()) {
                    tmpfile.mkdir();
                }
                //瞬时波形图
                String sunpath = path + "\\" + PubUtils.date2String(now, "yyyyMMddHHmmss") + "_" + eventDetailIndex + "shun.png";
                steadyAssService.generateImageShun(waveData,waveDataDetails, path,sunpath);
                eventInfo.setInstantWavePath(url + "/" + PubUtils.date2String(now, "yyyyMMddHHmmss") + "_" + eventDetailIndex + "shun.png");
                eventWaveDetails.setInstantWaveUrl(url + "/" + PubUtils.date2String(now, "yyyyMMddHHmmss") + "_" + eventDetailIndex + "shun.png");
                //RMS波形图
                path = request.getSession().getServletContext().getRealPath("/appfiles");
                path = path + "\\eventInfo";
                String rmspath = path + "\\" + PubUtils.date2String(now, "yyyyMMddHHmmss") + "_" + eventDetailIndex + "rms.png";
                steadyAssService.generateImageRMS(waveData,waveDataDetails, path,rmspath);
                eventInfo.setRmsWavePath(url + "/" + PubUtils.date2String(now, "yyyyMMddHHmmss") + "_" + eventDetailIndex + "rms.png");
                eventWaveDetails.setRmsWaveUrl(url + "/" + PubUtils.date2String(now, "yyyyMMddHHmmss") + "_" + eventDetailIndex + "rms.png");
                //更新eventInfo
                eventInfoMapper.updateByPrimaryKey(eventInfo);
            }
        }
        return eventWaveDetails;
    }

    @Override
    public int eventDetailEvaluate(String eventDetailIndex, int evaluate, String userIndex) {
        return eventInfoMapper.eventDetailEvaluate(eventDetailIndex, evaluate, userIndex);
    }

    @Override
    public EventInfoDetail eventDetailBaseInfoByIndex(String eventdetailIndex) {
        EventInfoDetail eventInfoDetails = new EventInfoDetail();
        try {
            eventInfoDetails = eventInfoMapper.eventDetailBaseInfoByIndex(eventdetailIndex);
        } catch (Exception e) {
            logger.error("查询暂降事件详情发生异常，异常是"+e.getMessage());
        }
        return eventInfoDetails;
    }

    @Override
    public int backNumber() {

        return backNumber;
    }


}
