package com.huatek.busi.task.transformer;

import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.entity.XTransformerEvaluateData;
import com.huatek.busi.mapper.TransformerOnlineMapper;
import com.huatek.busi.mapper.XTransformerEvaluateDataMapper;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import com.huatek.busi.vo.epa.*;
import com.huatek.frame.base.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @version V1.0
 * @ClassName: TransformerDateOnlineService
 * @Description: 在线数据.
 * @Author: Arno
 * @Date: 2023/10/12 15:31
 */
@Service
@Slf4j
public class TransformerDateOnlineService {


    @Resource
    private XTransformerEvaluateDataMapper xTransformerEvaluateDataMapper;


    @Autowired
    private TransformerOnlineMapper transformerOnlineMapper;

    @Autowired
    private BusiEqmWarningService busiEqmWarningService;

    /**
     * 油色谱评测数据
     *
     * @param mainEquipmentId
     * @return
     */
    public EpaEvaluateDataVo getEpaEvaluateData(String mainEquipmentId, String sensorCode) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaEvaluateDataVo vo = transformerOnlineMapper.getYspEvaluateData(mainEquipmentId, startDate, endDate);
        log.info("-->>油色谱评测数据:[{}]", vo);
        return vo;
    }

    /**
     * 局放评测数据
     *
     * @param mainTreeId
     * @return
     */
    public EpaPdDataVo getPdEvaluateData(String mainTreeId) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaPdDataVo vo = transformerOnlineMapper.getPdEvaluateData(mainTreeId, startDate, endDate);
        log.info("-->>局放评测数据:[{}]", vo);
        return vo;
    }

    /**
     * 铁芯评测数据
     *
     * @param mainEquipmentId
     * @return
     */
    public EpaIcgeDataVo getIcgeEvaluateData(String mainEquipmentId) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaIcgeDataVo vo = transformerOnlineMapper.getIcgeEvaluateData(mainEquipmentId, startDate, endDate);
        log.info("-->>铁芯评测数据:[{}]", vo);
        return vo;
    }

    /**
     * 声级振动评测数据
     *
     * @param mainTreeId
     * @return
     */
    public EpaSoundVibrationVo getSoundEvaluateData(String mainTreeId) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaSoundVibrationVo vo = transformerOnlineMapper.getSoundEvaluateData(mainTreeId, startDate, endDate);
        log.info("-->>声级振动评测数据:[{}]", vo);
        return vo;
    }

    /**
     * @param mainTreeId
     * @Description: 104 运行工况数据
     * @param:
     * @Return: java.lang.Object
     */
    public EpaRunStatusDataVo getRunStatusEvaluateData(String mainTreeId) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaRunStatusDataVo vo = transformerOnlineMapper.getRunStatusEvaluateData(mainTreeId, startDate, endDate);
        log.info("-->>运行工况数据:[{}]", vo);
        return vo;
    }


    /**
     * 红外温度
     */
    public EpaInfraredMonitorDataVo getInfraredMonitorEvaluateData(String mainTreeId) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaInfraredMonitorDataVo vo = transformerOnlineMapper.geInfraredMonitorEvaluateData(mainTreeId, startDate, endDate);
        log.info("-->>红外温度评测数据:[{}]", vo);
        return vo;
    }


    /**
     * 环境温度
     */
    public EpaEnvironmentDataVo getEnvironmentEvaluateData(String mainTreeId) {
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaEnvironmentDataVo vo = transformerOnlineMapper.getEnvironmentEvaluateData(mainTreeId, startDate, endDate);
        log.info("-->>环境温度评测数据:[{}]", vo);
        return vo;
    }



    /**
     * 绝缘介质在线数据
     * */
    public EpaCapacitanceDataVo getCapacitanceEvaluateData(String mainTreeId){
        Date endDate = new Date();
        Date startDate = DateUtil.getIntervalDate(endDate, 8);
        EpaCapacitanceDataVo vo = transformerOnlineMapper.getCapacitanceEvaluateData(mainTreeId, startDate, endDate);
        if(null != vo ){
            BigDecimal aFullCurrent = cover(vo.getAFullCurrent());
            BigDecimal aCapacitance = cover(vo.getACapacitance());

            BigDecimal bFullCurrent = cover(vo.getBFullCurrent());
            BigDecimal bCapacitance = cover(vo.getBCapacitance());

            BigDecimal cFullCurrent = cover(vo.getCFullCurrent());
            BigDecimal cCapacitance =cover( vo.getCCapacitance());

            double max1 = max(aFullCurrent, bFullCurrent, cFullCurrent);
            double max2 = max(aCapacitance, bCapacitance, cCapacitance);

            ////全电流 (mA) == 泄漏电流
            double v = max1 / 1000; //转为A
            vo.setTotalCurrent(v +"");
            vo.setCapacitance(max2+"");
        }
        log.info("-->>绝缘介质在线数据评测数据:[{}]", vo);
        return vo;
    }

    /**
     * 获取导入的评测数据
     * <p>
     * x_transformer_evaluate_data 表
     *
     * @param mainTreeId
     * @return
     */
    public TransformerEvaluateDataVo getTransformerEvaluateDataVo(String mainTreeId) {
        List<XTransformerEvaluateData> list = xTransformerEvaluateDataMapper.selectByMainTreeId(mainTreeId);
        TransformerEvaluateDataVo vo = null;
        if (list != null && !list.isEmpty()) {
            vo = new TransformerEvaluateDataVo();
            BeanUtils.copyPropertiesWithIgnoreNull(list.get(0), vo);
        }
        return vo;
    }

    public void warringEvaluating(String eqType, String gzType, String warningMsg, String mainTreeId, String name) {
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                busiEqmWarningService.warringEvaluating(eqType, gzType, warningMsg, mainTreeId, name);
            }
        };
        runnable.run();
    }

    public BigDecimal cover(String value){
        if(StringUtils.isBlank(value)){
            return BigDecimal.ZERO;
        }
        return new BigDecimal(value);
    }

    public double max(BigDecimal a, BigDecimal b , BigDecimal c){
        double max = Math.max(a.doubleValue(), b.doubleValue());
        return Math.max(max, c.doubleValue());
    }
}
