package com.itstyle.mdm.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.itstyle.mdm.entity.HttpClientResult; import com.itstyle.quartz.entity.SysConfigEntity; import com.itstyle.mdm.enumwrap.MdmEnum; import com.itstyle.mdm.service.MdmService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; /** * 主数据获取 */ @Service("mdmUtils") public class MdmUtils { @Value("${MDM_ADRESS}") private static String MDM_ADRESS; @Autowired private MdmService mdmService; /** * MDM局域物料名称编码数据获取 * @return */ public String queryMaterial(){ StringBuffer returnJson = new StringBuffer(); Map params = new HashMap<>(); params.put("code","1560000581165.1560000576507.a94a779e9e6d3ecbb646e39477dad6ec");//授权码 params.put("current","1");//页码 params.put("size","1000");//每页记录数(最大1000) params.put("queryValue","");//模糊查询 MdmEnum anEnum = MdmEnum.getMdmEnumByAddress("局域物料名称编码数据获取"); String address = anEnum.getAddress(); try { HttpClientResult httpClientResult = MdmApiUtils.doPost(MDM_ADRESS+address, null, params); int code = httpClientResult.getCode(); if(200 == code){ //成功 String records = httpClientResult.getContent(); /*String records = "{\n" + " \"current\": 1,\n" + " \"orders\": [],\n" + " \"pages\": 303,\n" + " \"records\": [\n" + " {\n" + " \"aliascode\": \"000\",\n" + " \"appfrom\": \"1\",\n" + " \"applicationaccountid\": \"D15600005811650001\",\n" + " \"applicationname\": \"审核管理员\",\n" + " \"attri1\": \"\",\n" + " \"attri10\": \"\",\n" + " \"attri11\": \"\",\n" + " \"attri12\": \"产品\",\n" + " \"attri13\": \"1667957805944\",\n" + " \"attri14\": \"\",\n" + " \"attri15\": \"0003.000\",\n" + " \"attri2\": \"\",\n" + " \"attri3\": \"\",\n" + " \"attri4\": \"\",\n" + " \"attri5\": \"\",\n" + " \"attri6\": \"\",\n" + " \"attri7\": \"\",\n" + " \"attri8\": \"\",\n" + " \"attri9\": \"\",\n" + " \"companyid\": \"1560000581165\",\n" + " \"employeeid\": \"\",\n" + " \"globtgccode\": \"\",\n" + " \"id\": 1668994782539,\n" + " \"industrycode\": \"517\",\n" + " \"industryname\": \"医药卫生保健化妆品业\",\n" + " \"name\": \"感冒药\",\n" + " \"nameen\": \"abc\",\n" + " \"note\": \"\",\n" + " \"ontologynamecode\": \"9000000003\",\n" + " \"pym\": \"gmy\",\n" + " \"rebacklockflag\": \"\",\n" + " \"regioncode\": \"\",\n" + " \"regionlevel\": \"\",\n" + " \"repeatflag\": \"\",\n" + " \"status\": \"200\",\n" + " \"tgcoriginalcode\": \"9000000003.000\",\n" + " \"updatedat\": 1668995367562,\n" + " \"updatetime\": 1668995367562\n" + " }\n" + " ],\n" + " \"searchCount\": true,\n" + " \"size\": 1,\n" + " \"total\": 303\n" + "}";*/ records = records.replaceAll("\\p{Z}", ""); JSONObject jsonObj = JSON.parseObject(records); records = jsonObj.getString("records"); JSONArray recordArray = JSON.parseArray(records);//物料数据信息 //将数据保存至数据库 JSONArray newJson = mdmService.saveMdmData(recordArray); if(newJson.size()!=0){ //定义返回json格式 returnJson.append("{\n" + " \"current\": 1,\n" + " \"orders\": [],\n" + " \"pages\": 303,"); returnJson.append("\"records\": "+newJson.toJSONString()); returnJson.append(",\n" + " \"searchCount\": true,\n" + " \"size\": 1,\n" + " \"total\": 303\n" + "}"); //返回json格式参数 String toString = returnJson.toString(); System.out.println(toString); //分发增量数据至异构系统 distributeDataToSys(toString,"局域物料名称编码数据获取"); } } } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 分发主数据数据至异构系统 * @param jsonParam */ public void distributeDataToSys(String jsonParam,String interfaceName){ List sysConfigEntities = mdmService.querySysConfigs(interfaceName); if (sysConfigEntities.size()!=0){ for (int i = 0; i < sysConfigEntities.size(); i++) { SysConfigEntity sysConfigEntity = sysConfigEntities.get(i); String sysName = sysConfigEntity.getSysName();//系统名称 String address = sysConfigEntity.getAddress();//分发地址 HttpClientResult httpClientResult = null; String uuid = UUID.randomUUID().toString(); try { //数据分发 httpClientResult = MdmApiUtils.doPostJson(address, jsonParam); //响应结果 String content = httpClientResult.getContent(); int code = httpClientResult.getCode(); if(200 == code){ //成功 this.saveInterfaceLog(uuid,interfaceName,address,jsonParam,content); }else { //异常 this.saveInterfaceLog(uuid,interfaceName,address,jsonParam,content); } } catch (Exception e) { e.printStackTrace(); this.saveInterfaceLog(uuid,interfaceName,address,jsonParam,"{\"batchId\":\""+uuid+"\",\"msg\":\" 接口调用网络异常!!! \"}"); } } } } /** * 保存接口分发日志 * @param interfaceName * @param url * @param param * @param result */ public void saveInterfaceLog(String uuid,String interfaceName,String url,String param,String result){ mdmService.saveInterfaceLog(uuid,interfaceName, url, param, result); } }