package com.jiayue.passback.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.passback.entity.AccuracyPassRate;
import com.jiayue.passback.mapper.AccuracyPassRateMapper;
import com.jiayue.passback.service.AccuracyPassRateService;
import com.jiayue.passback.util.DateMomentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AccuracyPassRateServiceImpl extends ServiceImpl<AccuracyPassRateMapper, AccuracyPassRate> implements AccuracyPassRateService {

    @Override
    public Map<String, Object> GenerateData(Date date) {
        Long startTime = DateMomentUtil.getDayStartTime(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
        Long endTime = DateMomentUtil.getDayLastTime(startTime);
        Map<String, Object> map = new HashMap<>();

        List<AccuracyPassRate> accuracyPassRateList = list(lambdaQuery().between(AccuracyPassRate::getTime, startTime, endTime).getWrapper());

        accuracyPassRateList.stream().filter(f -> !f.getShortTermAccuracy().equals("无可用数据计算") && !f.getShortTermAccuracy().equals("无计算公式")).collect(Collectors.toList());

        if (accuracyPassRateList.size() > 0) {
            accuracyPassRateList.sort(Comparator.comparing(AccuracyPassRate::getTime).reversed());
            map.put("accuracy", accuracyPassRateList.get(0).getShortTermAccuracy().replace("%", ""));
        } else {
            map.put("accuracy", "NULL");
        }

        log.info("获取两个细则短期准确率：{}", map.get("accuracy"));
        return map;
    }

}
