package com.youchain.utils;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.youchain.basicdata.service.dto.TableConfigDto;
import com.youchain.basicdata.service.dto.TableConfigQueryCriteria;
import com.youchain.basicdata.service.impl.TableConfigServiceImpl;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.system.domain.DictDetail;
import com.youchain.modules.system.service.impl.DictDetailServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import java.util.*;
@Slf4j
public  class ExcelDownUtils {
    public static  List<Map<String, Object>> CreateMap(List  all,String tableType) throws Exception{
        Long stTime=System.currentTimeMillis();

        //查询配置表
        TableConfigServiceImpl tableConfigService = SpringContextHolder.getBean(TableConfigServiceImpl.class);
        TableConfigQueryCriteria tbble_criteria =new TableConfigQueryCriteria();
        tbble_criteria.setTableType(tableType.trim());
        List<TableConfigDto> tableConfigDtoList=tableConfigService.queryAll(tbble_criteria);
        if(tableConfigDtoList.size()<=0){
            throw new BadRequestException("请先保存配置");
        }
        TableConfigDto tableConfigDto=tableConfigDtoList.get(0);
        //解析配置内容
        JSONArray tableConfigArr;
        try{
            tableConfigArr= JSONUtil.parseArray(tableConfigDto.getValue());
        }catch (Exception e){
            throw new BadRequestException("请保存配置");
        }
        log.error("导出配置数据："+tableConfigArr+"");

        //创建空的字典集合
        Map<String, Map<String, DictDetail>> dictDetailMaps =new HashMap<>();
        //加载字典表
        DictDetailServiceImpl dictDetailService = SpringContextHolder.getBean(DictDetailServiceImpl.class);
        //查找所需要用到的字典
        tableConfigArr.forEach(tableConfigBean->{
            //遍历配置属性
            JSONObject jsonObject=(JSONObject) tableConfigBean;
            String property=jsonObject.getStr("property");
            if(property.indexOf("#")>-1){
                //如果带# 代表调用了字典
                try{
                    String  dictStr=property.split("#")[0];
                    Map<String, DictDetail> dictDetailMap=dictDetailService.getDictDetailByNameForValue(dictStr);
                    dictDetailMaps.put(dictStr,dictDetailMap);
                }catch (Exception e){
                    log.error(e.toString());
                    log.error("获取字典配置"+property+"异常");
                }

            }
        });

        List<Map<String, Object>> list = new ArrayList<>();

        for (Object obj : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            JSON json=JSONUtil.parse(obj);
            tableConfigArr.forEach(tableConfig->{
                JSONObject jsonObject=(JSONObject) tableConfig;
                if(jsonObject.getBool("visible")){
                    String property=jsonObject.getStr("property");

                    if(property.indexOf("#")>-1){
                        try{
                            String  dictStr=property.split("#")[0];
                            String  propertyStr=property.split("#")[1];

                            Map<String, DictDetail> dictDetailMap=dictDetailMaps.get(dictStr);
                            //log.info("dictDetailMap："+dictDetailMap.size());
                            //for (String key: dictDetailMap.keySet()) {
                                //log.info("dictDetailMap："+key);
                            //}
                            //log.info(dictStr+"获取value："+json.getByPath(propertyStr)+"");

                            DictDetail dictDetail=dictDetailMap.get(json.getByPath(propertyStr)+"");

                            //log.info(jsonObject.getStr("label")+"--"+dictDetail.getLabel()+"");
                            String value=dictDetail.getLabel()+"";
                            map.put(jsonObject.getStr("label"), value.trim());
                        }catch (Exception e){
                            log.error(e.toString());
                            log.error("匹配字典："+property+"异常");
                        }


                    }else{
                        try{
                            Object value= json.getByPath(jsonObject.getStr("property")+"");
                            if(value instanceof String){
                                value=((String) value).trim();
                            }
                            map.put(jsonObject.getStr("label"),value);

                        }catch (Exception e){
                            log.error(e.toString());
                            log.error("匹配数据"+property+"异常");
                        }

                    }

                }

            });


            list.add(map);


            //Object value2 = jsonObj.eval("$.address.city");

        }
        return  list;
    }
}
