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

import cn.hutool.core.date.DateUtil;
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.ROperatingMonitorMMapper;
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 com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * 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 ROperatingMonitorService rOperatingMonitorService;


    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    public void handlerMonitorOperatingM(List<String> indexLists, String dataDate) {
        Date date = DateUtil.parse (dataDate);
        List<ROperatingMonitorMPO> rOperatingMonitorMPOList = new ArrayList<> ( );
        QueryWrapper<ROperatingMonitorDPO> queryWrapper = new QueryWrapper<> ( );

        indexLists.forEach (temp -> {
            queryWrapper.clear ( );
            queryWrapper.eq ("measurement_point_id", temp).
                    eq ("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring (0, 7));
            List<ROperatingMonitorDPO> list = rOperatingMonitorService.list (queryWrapper);

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