package com.engine.htsc.fwnrTokmbh.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.fwnrTokmbh.service.GetkmbhService;
import com.engine.htsc.payment.util.HttpUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class GetkmbhServiceImpl extends Service implements GetkmbhService {
    @Override
    public Map<String, Object> getKmbh(String fwmc) {
        BaseBean bb = new BaseBean();
        
        PropBean pp = new PropBean();
        String requestUrl = pp.getPropName("getKmbhByFwnr");//interface_Url
        String appSysId = pp.getPropName("yskmAiActionsysid");//appSysId
        String token = pp.getPropName("yskmAiActiontoken");//token


        Map<String, Object> paramsMap = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add(fwmc);
        Map<String, Object> data = new HashMap<>();
        data.put("NAME", list);

        paramsMap.put("systemId", "1");
        paramsMap.put("user", "2");
        paramsMap.put("functionId", "3");
        paramsMap.put("traceId", "4");
        paramsMap.put("data", data);

        Map<String, String> appHeader = new HashMap<>();
        appHeader.put("appSysId", appSysId);
        appHeader.put("token", token);
        appHeader.put("Content-Type", "application/json");
        bb.writeLog("======"+JSONObject.toJSONString(appHeader));

        Map<String, Object> returnBack = new HashMap<>();
        try {
            bb.writeLog("=======传给接口的参数是："+JSON.toJSONString(paramsMap));
            String backData = HttpUtil.postApplicationJson(requestUrl, JSON.toJSONString(paramsMap), appHeader);
            bb.writeLog("接口返回的参数是："+backData);
            JSONObject object = JSON.parseObject(backData);
            if (object.getString("code").equals("200")) {
                String kmmc = "";
                String kmcodeList = JSON.parseObject(object.getString("data")).getString("label");
                String[] codeList = kmcodeList.split("\"");
                bb.writeLog("========接口返回的科目编码是" + codeList[1]);
                String code = codeList[1];
                RecordSet rs = new RecordSet();
                String sql = "select yskmmc from uf_kmpz k where k.yskmbm = '" + code + "'";
                bb.writeLog("=====即将执行根据科目编码获取科目名称：" + sql);
                rs.executeQuery(sql);
                if (rs.next()) {
                    kmmc = Util.null2String(rs.getString("yskmmc"));
                }
                bb.writeLog("=========获取的科目名称是：" + kmmc);
                returnBack.put("code", "200");
                Map<String, Object> datatxt = new HashMap<>();
                datatxt.put("kmbm", code);
                datatxt.put("kmmc", kmmc);
                returnBack.put("data", datatxt);
            }
        } catch (Exception e) {
            e.printStackTrace();
            bb.writeLog("=========" + e.getMessage());
            returnBack.put("code", "500");
            returnBack.put("data", "获取时发生错误");
        }
        return returnBack;
    }
}
