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

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.entity.BusiCableThreshold;
import com.huatek.busi.entity.BusiOtherThreshold;
import com.huatek.busi.entity.IecBydworkParam;
import com.huatek.busi.mapper.*;
import com.huatek.busi.mapper.cable.BusiOtherThresholdMapper;
import com.huatek.busi.service.BusiEquipmentRelationshipService;
import com.huatek.busi.service.cable.BusiCableThresholdService;
import com.huatek.busi.util.CalcUtil;
import com.huatek.busi.vo.TransformerTmpVo;
import com.huatek.busi.vo.cable.CableCirculationDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 其他阈值接口实现
 *
 */
@Slf4j
@Component
@Service(interfaceClass = BusiCableThresholdService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiCableThresholdServiceImpl implements BusiCableThresholdService {

    @Autowired
    private BusiCableThresholdMapper busiCableThresholdMapper;
    @Autowired
    private BusiEquipmentRelationshipService busiEquipmentRelationshipService;
    @Autowired
    private IecBydworkParamMapper iecBydworkParamMapper;
    @Autowired
    private BusiEqmWarningMapper busiEqmWarningMapper;
    @Autowired
    private BusiOfflineCableRepairMapper busiOfflineCableRepairMapper;


    @Override
    public BusiCableThreshold getCableThreshold(String mainTreeId, String runState, String sensorCode) {
        return busiCableThresholdMapper.getCableThreshold(mainTreeId, runState, sensorCode);
    }

    @Override
    public CableCirculationDataVo getCableData(String sensorCode, String transformerId) {
        String status = "";
        int runStatus = 10 ;
        List<TransformerTmpVo> transformerList = busiEquipmentRelationshipService.getCurrentTransformerInfo(transformerId);
        if (CollectionUtils.isEmpty(transformerList)) {
            status="";
        }
        else {
            for(TransformerTmpVo transformer : transformerList) {
                IecBydworkParam iecBydworkParam = iecBydworkParamMapper.getIecType2(transformer.getTransformerId());
                if(iecBydworkParam == null || iecBydworkParam.getStype() == null) {
                    continue;
                }
                if(runStatus == 0) {
                    runStatus = iecBydworkParam.getStype();
                }else if(runStatus > iecBydworkParam.getStype().intValue()){
                    runStatus = iecBydworkParam.getStype();
                }
            }
            status = runStatus+"";
        }
        // 查询信息
        CableCirculationDataVo dataVos = busiCableThresholdMapper.getCableData(sensorCode,status);
        if(dataVos!=null)
        {
            // 所有告警类型
            Integer count = busiEqmWarningMapper.getWarningCount(sensorCode, null, DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay());
            // 误报次数
            Integer warningCount = busiEqmWarningMapper.getWarningCount(sensorCode, "2",DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay());
            // 维修次数
            Map<String, Object> param = new HashMap<>();
            param.put("sensorCode", sensorCode);
            param.put("startTime", DateUtil.getHalfYearDate());
            param.put("endTime", DateUtil.getCurrentHourDate());
            // 获取前半年的维修次数
            Integer repairCount = busiOfflineCableRepairMapper.getRepairCountForCable(param);
            if (warningCount != null && (count.compareTo(0) > 0 && warningCount.compareTo(0) >0) || count.equals(warningCount))
            {
//                dataVos.setAlarmRate(new BigDecimal(warningCount/count).setScale(2, BigDecimal.ROUND_HALF_UP));
                BigDecimal A = CalcUtil.calcRate(BigDecimal.valueOf(warningCount),BigDecimal.valueOf(count));
                dataVos.setAlarmRate(A);
            } else {
                dataVos.setAlarmRate(BigDecimal.valueOf(0));
            }
            dataVos.setMaintenanceCount(repairCount);
        }
        else
        {
            dataVos = new CableCirculationDataVo();
        }
        return dataVos;
    }
}
