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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.prepare.harmonic.mapper.mysql.line.ROperatingMonitorDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.ROperatingMonitorMMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorMPO;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorMService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2023/4/21  15:41【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class ROperatingMonitorMServiceImpl extends MppServiceImpl<ROperatingMonitorMMapper, ROperatingMonitorMPO> implements ROperatingMonitorMService {
    private final ROperatingMonitorDMapper rOperatingMonitorDMapper;

    @Override
    public void handleMonth(CalculatedParam calculatedParam) {
        DateTime beginMonth = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime endMonth = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(beginMonth, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);
        List<String> lineIds = calculatedParam.getIdList();
        //以尺寸1000分片
        List<List<String>> pendingIds = ListUtils.partition(lineIds, 1000);
        List<ROperatingMonitorMPO> rOperatingMonitorMPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapper = new QueryWrapper<>();
        pendingIds.forEach(temp -> {
            queryWrapper.clear();
            queryWrapper.in("measurement_point_id", temp)
                    .between("data_date",beginMonth,endMonth);

            List<ROperatingMonitorDPO> list = rOperatingMonitorDMapper.selectList(queryWrapper);

            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            Map<String, List<ROperatingMonitorDPO>> collect1 = list.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getMeasurementPointId));
            collect1.forEach((k,v)->{
                ROperatingMonitorMPO rOperatingMonitorMPO = new ROperatingMonitorMPO();
                BigDecimal dataIntegrityRate = BigDecimal.valueOf(v.stream().mapToDouble(po -> po.getDataIntegrityRate().doubleValue()).average().getAsDouble());
                BigDecimal dataRightRate = BigDecimal.valueOf(v.stream().mapToDouble(po -> po.getDataRightRate().doubleValue()).average().getAsDouble());
                BigDecimal indexIntegrityRate = BigDecimal.valueOf(v.stream().mapToDouble(po -> po.getIndexIntegrityRate().doubleValue()).average().getAsDouble());
                rOperatingMonitorMPO.setMeasurementPointId(k);
                rOperatingMonitorMPO.setDataDate(localDate);
                rOperatingMonitorMPO.setDataIntegrityRate(dataIntegrityRate);
                rOperatingMonitorMPO.setDataRightRate(dataRightRate);
                rOperatingMonitorMPO.setIndexIntegrityRate(indexIntegrityRate);
                rOperatingMonitorMPO.setDevId(v.get(0).getDevId());
                List<Integer> collect = v.stream().map(ROperatingMonitorDPO::getIsUnusual).collect(Collectors.toList());
                rOperatingMonitorMPO.setIsUnusual(collect.contains(Integer.valueOf(0)) ? 0 : 1);
                rOperatingMonitorMPOList.add(rOperatingMonitorMPO);
            });

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