package com.zhgd.xmgl.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhgd.jeecg.common.mybatis.EntityMap;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: wisdomSite
 * @description: 数据结果处理
 * @author: Mr.Peng
 * @create: 2021-08-25 18:06
 **/

public class DataObjUtils {

    /**
     * 获取层级数据
     * @return
     */
    public static List<Map<String, Object>> getClassData(List<EntityMap> list,String name){
        List<Map<String, Object>> dataList=new ArrayList<>();
        if(list!=null&&list.size()>0){
            List<String> controlTypeNameList=new ArrayList<>();
            //有管控要点分类
            List<EntityMap> yesNameList=new ArrayList<>();
            //没用管控要点分类
            List<EntityMap> notNameList=new ArrayList<>();
            for(EntityMap data:list){
                String controlTypeName= MapUtils.getString(data,name);
                if(StringUtils.isNotEmpty(controlTypeName)){
                    yesNameList.add(data);
                    if(!controlTypeNameList.contains(controlTypeName)){
                        controlTypeNameList.add(controlTypeName);
                    }
                }else{
                    notNameList.add(data);
                }
            }
            if(controlTypeNameList.size()>0){
                //讲有管控要点分类的数据按照层级显示
                List<Map<String, Object>> yesList=new ArrayList<>();
                for(String controlTypeName:controlTypeNameList){
                    List<EntityMap> children=new ArrayList<>();
                    for(EntityMap data:list){
                        if(controlTypeName.equals(MapUtils.getString(data,name))){
                            children.add(data);
                        }
                    }
                    Map<String, Object> data=new HashMap<>();
                    data.put("id",0);
                    data.put(name,controlTypeName);
                    data.put("children",children);
                    yesList.add(data);
                }
                dataList.addAll(yesList);
            }
            if(notNameList.size()>0){
                dataList.addAll(notNameList);
            }
        }
        return dataList;
    }

    public static List<Map<String, Object>> getThreeClassData(List<EntityMap> list,String name,String twoName){
        List<Map<String, Object>> dataList=new ArrayList<>();
        if(list!=null&&list.size()>0){
            List<String> controlTypeNameList=new ArrayList<>();
            List<String> twoNameList=new ArrayList<>();
            for(EntityMap data:list){
                String controlTypeName= MapUtils.getString(data,name);
                String twoTypeName= MapUtils.getString(data,twoName);
                if(!controlTypeNameList.contains(controlTypeName)){
                    controlTypeNameList.add(controlTypeName);
                }
                if(!twoNameList.contains(controlTypeName+"_"+twoTypeName)){
                    twoNameList.add(controlTypeName+"_"+twoTypeName);
                }
            }
            //讲有管控要点分类的数据按照层级显示
            List<Map<String, Object>> yesList=new ArrayList<>();
            if(twoNameList.size()>0){
                for(String typeName:twoNameList){
                    List<EntityMap> children=new ArrayList<>();
                    StringBuilder ids=new StringBuilder();
                    for(EntityMap data:list){
                        String controlTypeName= MapUtils.getString(data,name);
                        String twoTypeName= MapUtils.getString(data,twoName);
                        if(typeName.equals(controlTypeName+"_"+twoTypeName)){
                            children.add(data);
                            if(ids.length()>0){
                                ids.append(",");
                            }
                            ids.append(MapUtils.getString(data,"id"));
                        }
                    }
                    String[] arr=typeName.split("_");
                    Map<String, Object> data=new HashMap<>();
                    data.put("id",0);
                    data.put("idList",ids.toString());
                    data.put(name,arr[0]);
                    data.put(twoName,arr[1]);
                    data.put("name",arr[1]);
                    data.put("children",children);
                    yesList.add(data);
                }
            }
            if(controlTypeNameList.size()>0){
                for(String typeName:controlTypeNameList){
                    List<Map<String, Object>> children=new ArrayList<>();
                    StringBuilder ids=new StringBuilder();
                    for(Map<String, Object> data:yesList){
                        String controlTypeName= MapUtils.getString(data,name);
                        if(typeName.equals(controlTypeName)){
                            children.add(data);
                            if(ids.length()>0){
                                ids.append(",");
                            }
                            ids.append(MapUtils.getString(data,"idList"));
                        }
                    }
                    Map<String, Object> data=new HashMap<>();
                    data.put("id",0);
                    data.put("idList",ids.toString());
                    data.put(name,typeName);
                    data.put("name",typeName);
                    data.put("children",children);
                    dataList.add(data);
                }
            }

        }
        return dataList;
    }

    public static Map<String,Object> getCountCooseResult(List<Map<String,Object>> contList,String chooseValue){
        Map<String,Object> dataMap=new HashMap<>();
        int num=0;
        String result=null;
        Map<String,Integer> countMap=new HashMap<>();
        if(contList!=null&&contList.size()>0){
            for(Map<String,Object> data:contList){
                Integer tempNum=MapUtils.getInteger(data,"num");
                countMap.put(MapUtils.getString(data,"chooseType"),tempNum);
                num=num+tempNum;
            }
        }
        if(StringUtils.isNotEmpty(chooseValue)){
            JSONArray array= JSONUtil.parseArray(chooseValue);
            for (int a = 0; a < array.size(); a++) {
                JSONObject obj2 = array.getJSONObject(a);
                Integer tempNum=MapUtils.getInteger(countMap,obj2.get("value"),0);
                obj2.put("num",tempNum);
            }
            result=array.toString();

        }
        dataMap.put("num",num);
        dataMap.put("result",result);
        return dataMap;
    }

    /**
     * 计算百分比
     * @param num1
     * @param num2
     * @return
     */
    public static double getPercentage(String num1,String num2){
        double grade=0d;
        if(StringUtils.isEmpty(num1)){
            num1="0";
        }
        if(StringUtils.isEmpty(num2)){
            num2="0";
        }
        BigDecimal bigDecimal=new BigDecimal(num1);
        BigDecimal bigDecima2=new BigDecimal(num2);
        if(bigDecimal.doubleValue()!=0d&&bigDecima2.doubleValue()!=0d){
            grade=bigDecimal.divide(bigDecima2,2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).doubleValue();
        }
        return grade;
    }

    public static String divide(String num1,String num2){
        String grade="0";
        if(StringUtils.isEmpty(num1)){
            num1="0";
        }
        if(StringUtils.isEmpty(num2)){
            num2="0";
        }
        BigDecimal bigDecimal=new BigDecimal(num1);
        BigDecimal bigDecima2=new BigDecimal(num2);
        if(bigDecimal.doubleValue()!=0d&&bigDecima2.doubleValue()!=0d){
            grade=bigDecimal.divide(bigDecima2,2,BigDecimal.ROUND_HALF_UP).toString();
        }
        return grade;
    }

    /**
     * 进制转换
     * @param value   数据
     * @param length  长度
     * @return
     */
    public static String getRadixValue(String value,int length) {
        Integer num = Integer.parseInt(value, 16);
        String binString = Integer.toBinaryString(num);
        //log.info(binString);
        while (binString.length() < length) {
            binString = "0" + binString;
        }
        return binString;
    }
}
