package com.kingdee.eas.custom.esign.service;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.esign.FieldMappingFactory;
import com.kingdee.eas.custom.esign.FieldMappingInfo;
import com.kingdee.shr.base.syssetting.MSFServiceFacadeFactory;

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

public class GetESignConfigTableService implements IHRMsfService {
    @Override
    public Object process(Context context, Map map) throws EASBizException, BOSException {
        // 从参数映射中获取"number"参数值，用于定位配置信息
        Object o = map.get("number");
        // 从参数映射中获取"filter"参数值，用于构建查询过滤条件
        Object o1 = map.get("filter");

        Object otherParameters = map.get("otherParameters");
        // 若"number"参数为null，返回空字符串（无效请求）
        if(o==null){
            return "";
        }
        // 若"number"参数为null，返回空字符串（无效请求）
        if(o1==null){
            return "";
        }
        FieldMappingInfo fieldMappingInfo = FieldMappingFactory.getLocalInstance(context)
                .getFieldMappingInfo("where number = '" + o.toString() + "' ");

        String dataInterface = fieldMappingInfo.getDataInterface();
        if (dataInterface==null||dataInterface.equals("")){
            Map<String,Object> getSqlServiceMap = new HashMap<String,Object>();
            getSqlServiceMap.put("number",fieldMappingInfo.getNumber());
            getSqlServiceMap.put("filter",o1.toString());
            Object o2 = MSFServiceFacadeFactory.getLocalInstance(context)
                    .processService("getESignConfigDataService", getSqlServiceMap);
            return o2;
        }else {
            JSONObject jsonObject = JSONObject.parseObject(otherParameters.toString());
            Map<String,Object> osfMap = new HashMap<String, Object>();
            for (String key : jsonObject.keySet()) {
                osfMap.put(key,jsonObject.getString(key));
            }
            osfMap.put("number",fieldMappingInfo.getNumber());
            Object o2 = MSFServiceFacadeFactory.getLocalInstance(context)
                    .processService(dataInterface, osfMap);
            return o2;
        }
    }
}
