package com.njcn.prepare.harmonic.service.mysql.Impl.area;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.utils.PubUtils;
import com.njcn.harmonic.pojo.po.RMpDevEvaluateDetailPO;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpDevEvaluateDetailPOMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.ROperatingMonitorDMapper;
import com.njcn.prepare.harmonic.pojo.mysql.RMpMonitorEvaluateDPO;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.param.DeviceParam;
import com.njcn.prepare.harmonic.pojo.po.RMpDevSolveDetailPO;
import com.njcn.prepare.harmonic.pojo.po.RStatDevSignalDPO;
import com.njcn.prepare.harmonic.service.mysql.area.RMpDevEvaluateDetailPOService;
import com.njcn.prepare.harmonic.service.mysql.area.ROperatingIndexDPOService;
import com.njcn.prepare.harmonic.service.mysql.device.RMpDevSolveDetailPOService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.OptionalDouble;

/**
 * 
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/12/1  9:18【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RMpDevEvaluateDetailPOServiceImpl extends MppServiceImpl<RMpDevEvaluateDetailPOMapper, RMpDevEvaluateDetailPO> implements RMpDevEvaluateDetailPOService{

    private final ROperatingMonitorDMapper rOperatingMonitorDMapper;
    private final RMpDevSolveDetailPOService rMpDevSolveDetailPOService;
    private final DecimalFormat df = new DecimalFormat("#.00");

    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    public void handler(DeviceParam deviceParam) {

        List<RMpDevEvaluateDetailPO> rMpDevEvaluateDetailPOList = new ArrayList<>();
        Date date = DateUtil.parse(deviceParam.getDataDate());
        QueryWrapper<ROperatingMonitorDPO> wrapper = new QueryWrapper<>();
        QueryWrapper<RMpDevSolveDetailPO> wrapper1 = new QueryWrapper<>();

        deviceParam.getDetailList().forEach(temp -> {

            RMpDevEvaluateDetailPO rMpDevEvaluateDetailPO = new RMpDevEvaluateDetailPO();
            rMpDevEvaluateDetailPO.setDeviceId(temp.getDevId());
            rMpDevEvaluateDetailPO.setDataDate(date);

            wrapper.clear();
            wrapper1.clear();
            wrapper1.select(RMpDevSolveDetailPO.COL_DEFECT_SEVERITY).
                    eq(RMpDevSolveDetailPO.COL_DEVICE_ID,temp.getDevId()).
                    eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",deviceParam.getDataDate());
            RMpDevSolveDetailPO one = rMpDevSolveDetailPOService.getOne(wrapper1);
            Double defectSeverity = PubUtils.getDefectSeverity(one.getDefectSeverity());
            rMpDevEvaluateDetailPO.setDevScore(Double.valueOf(df.format(defectSeverity/3)));

            List<ROperatingMonitorDPO> rOperatingMonitorDPOS = new ArrayList<>();

            List<String> lineIds = temp.getLineIds();
            if(lineIds.size() > 0){
                wrapper.in("measurement_point_id", lineIds);
                rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(wrapper);
            }
            double asDouble1=0.00 ,asDouble2=0.00,aDouble=0.00;
            if(rOperatingMonitorDPOS.size()>0){
                asDouble1= rOperatingMonitorDPOS.stream().mapToDouble(po -> po.getDataIntegrityRate().doubleValue()).average().getAsDouble();
                asDouble2 = rOperatingMonitorDPOS.stream().mapToDouble(po -> po.getIndexIntegrityRate().doubleValue()).average().getAsDouble();
                long count = rOperatingMonitorDPOS.stream().filter(po -> po.getIsUnusual() == 1 && po.getDataIntegrityRate().compareTo(new BigDecimal(0.95)) == 1).count();
                aDouble = Double.valueOf(count) / rOperatingMonitorDPOS.size();
            }
            rMpDevEvaluateDetailPO.setDevDataRate(Double.valueOf(df.format(asDouble1)));
            rMpDevEvaluateDetailPO.setDevTargetRate(Double.valueOf(df.format(asDouble2)));
            rMpDevEvaluateDetailPO.setDevEffectiveRate(Double.valueOf(df.format(aDouble)));
            rMpDevEvaluateDetailPOList.add(rMpDevEvaluateDetailPO);

        });
        this.saveOrUpdateBatchByMultiId(rMpDevEvaluateDetailPOList,500);
    }


}
