package com.sys.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.shr.api.OSFWSClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShrOsf {
    static Logger logger = LoggerFactory.getLogger(ShrOsf.class);
    public static Object callOSF(String url,String serviceName, Map<String, Object> param){
        try {
            OSFWSClient client = new OSFWSClient();
            String str = client.proceedOSF(url,serviceName,param);
            return str;
        } catch (Exception e) {
            logger.error("shr接口"+serviceName+"调用失败",e);
        }
        return null;
    }

    public static String callOSFFindNameById(String url, String Id){
        String serviceName = "getValueByUuid";
        try {
            OSFWSClient client = new OSFWSClient();
            Map<String, Object> param  = new HashMap<String,Object>();
            param.put("id", Id);
            param.put("selector", "name");
            String str = client.proceedOSF(url,serviceName,param);
            if(!StringUtils.isEmpty(str) && str.indexOf("name")>0){
                JSONObject jsonObject = JSONObject.parseObject(str);
                return jsonObject.get("name").toString();
            }
        } catch (Exception e) {
            logger.error("shr接口"+serviceName+"获取数据失败："+Id,e);
        }
        return null;
    }
    public static String callOSFFindFieldById(String url, String Id,String field){
        String serviceName = "getValueByUuid";
        try {
            OSFWSClient client = new OSFWSClient();
            Map<String, Object> param  = new HashMap<String,Object>();
            param.put("id", Id);
            param.put("selector", field);
            String str = client.proceedOSF(url,serviceName,param);
            if(!StringUtils.isEmpty(str) && str.indexOf(field)>0){
                JSONObject jsonObject = JSONObject.parseObject(str);
                return jsonObject.get(field).toString();
            }
        } catch (Exception e) {
            logger.error("shr接口"+serviceName+"获取数据失败："+Id,e);
        }
        return null;
    }

    public static Map<String,String> callOSFFindDataById(String url, String Id, List<String> list){
        String serviceName = "getValueByUuid";
        String selector = null;
        Map<String,String> map = new HashMap<>();
        try {
            OSFWSClient client = new OSFWSClient();
            selector = list.get(0);
            for(int i=1;i<list.size();i++){
                selector +=","+list.get(i);
            }
            Map<String, Object> param  = new HashMap<String,Object>();
            param.put("id", Id);
            param.put("selector", selector);
            String str = client.proceedOSF(url,serviceName,param);
            if(!StringUtils.isEmpty(str) && str.indexOf("id")>0){
                JSONObject jsonObject = JSONObject.parseObject(str);
                for(String key:jsonObject.keySet()){
                    Object obj = jsonObject.get(key);
                    if(obj instanceof String){
                        map.put(key,obj.toString());
                    }else if(obj instanceof Integer || obj instanceof Double ||obj instanceof Float){
                        map.put(key,obj.toString());
                    }else if(obj instanceof JSONObject){
                        if(!((JSONObject)obj).containsKey("id")){
                            throw new Exception("解析数据异常："+str);
                        }
                        map.put(key+":id",((JSONObject)obj).get("id").toString());
                    }else{
                        throw new Exception("解析数据异常,不匹配的类型："+str+"》"+obj.getClass());
                    }
                }
                return map;
            }
        } catch (Exception e) {
            logger.error("shr接口"+serviceName+"获取数据失败："+Id+",selector:"+selector,e);
        }
        return null;
    }

//    serviceName = "getValueByUuid";
//        param1.put("id", "NNoAAAAADtqA733t");
//        param1.put("selector", "field37,field35,field30,field31");
//    {"field31":"0","field37":{"id":"NNoAAAAAF1FFi5MO"},"id":"NNoAAAAADtqA733t","field30":"0"}

}
