package com.syjy.calculate.conotroller;
import com.syjy.calculate.entity.CalculateRequest;
import com.syjy.calculate.entity.CalculationInfo;
import com.syjy.calculate.repository.CalculationFormulaRepository;
import com.syjy.calculate.service.AccuracyPassRateCalculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Controller
@RequestMapping("/testCalculate")
public class testCalculate {
    @Autowired
    private CalculationFormulaRepository calculationFormulaRepository;

    @Autowired
    private AccuracyPassRateCalculateService accuracyPassRateCalculateService;

    @ResponseBody
    @RequestMapping(value = "/test")
    public String testCalculate() {
        List<CalculationInfo> calculationInfoList = calculationFormulaRepository.getCalculationInfo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<List<CalculationInfo>> allList = new ArrayList<>();
        List<CalculationInfo> newList = new ArrayList<>();
        for (int i = 0; calculationInfoList.size() > 0; i++) {
            String dateStr = sdf.format(calculationInfoList.get(0).getGenTime());
            newList = calculationInfoList.stream().filter(calculation -> sdf.format(calculation.getGenTime()).equals(dateStr)).collect(Collectors.toList());
            allList.add(newList);
            calculationInfoList.removeAll(newList);
        }
        String show = "";
        CalculateRequest calculateRequest = new CalculateRequest();
        calculateRequest.setFormulaType("DAY_SHORT_ACCURACY");
        calculateRequest.setProvince("E45");
        calculateRequest.setElectricType("E2");
        calculateRequest.setStationCode("J00377");
        calculateRequest.setElectricCapacity(new BigDecimal("96.4"));

        show = "公式:"+calculateRequest.getFormulaType()+ "</br>省份:"+calculateRequest.getProvince()+"</br>风光:"+ calculateRequest.getElectricType()+"</br>容量:"+calculateRequest.getElectricCapacity()+"</br>";

        for(List<CalculationInfo> list : allList){
            calculateRequest.setCalculationInfoList(list);
            Map<String,Object> resultMap = accuracyPassRateCalculateService.calculate(calculateRequest);
            if(resultMap.get("data")!=null){
                System.out.println(sdf.format(list.get(0).getGenTime())+"短期准确率："+resultMap.get("data"));
                show = show+sdf.format(list.get(0).getGenTime())+"短期准确率："+resultMap.get("data")+"</br>";
            }else{
                System.out.println(sdf.format(list.get(0).getGenTime())+"短期准确率："+resultMap.get("msg"));
                show = show+sdf.format(list.get(0).getGenTime())+"短期准确率："+resultMap.get("msg")+"</br>";
            }

        }
        return show;
    }

}
