package com.gyee.frame.service.photovoltaic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.feign.RemoteServiceBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class PhotovoltaicService {
    @Resource
    private RemoteServiceBuilder remoteService;

    public List<Map> qy(Integer pageNum,Integer pageSize,String stId,String wtId, String category1,String category2,String rank,
                        String modelId,Long snapId,Integer messageType,String keyWord,String starttime,String endtime) {

        AjaxResult alarm = remoteService.earlyService().getPage(pageNum,pageSize,stId,wtId,category1,category2,rank,
                modelId,snapId,messageType,keyWord,starttime,endtime);
        String jsonStr = JSON.toJSONString(alarm);
        List<Map>cate = page(jsonStr);
        return cate;
    }

    public List<Map> page(String jsonData1) {
        JSONObject cate = JSON.parseObject(jsonData1);
        JSONObject data = cate.getJSONObject("data");

        JSONArray recordList = data.getJSONArray("records");

        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;

        for (int i = 0; i < recordList.size(); i++) {
            JSONObject record = recordList.getJSONObject(i);

            String ranks = record.getString("rank");

            if (StringUtils.isBlank(ranks)) {
                continue;
            }
            switch (ranks) {
                case "1":
                    a = a + 1;
                    break;
                case "2":
                    b = b + 1;
                    break;
                case "3":
                    c = c + 1;
                    break;
                case "4":
                    d = d + 1;
                    break;
                case "5":
                    e = e + 1;
                    break;
                default:
            }
        }
        double size = a + b + c + d + e;

        List<Map> conver = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00%");
        Map<String, String> maps = new LinkedHashMap<>();
        maps.put("rank1", df.format(a/size));
        maps.put("rank2", df.format(b/size));
        maps.put("rank3", df.format(c/size));
        maps.put("rank4", df.format(d/size));
        maps.put("rank5", df.format(e/size));

        Map<String, Double> map = new LinkedHashMap<>();
        map.put("rank1", (double) a);
        map.put("rank2", (double) b);
        map.put("rank3", (double) c);
        map.put("rank4", (double) d);
        map.put("rank5", (double) e);
        map.put("size",size);
        conver.add(maps);
        conver.add(map);
        return conver;
    }
}

