niying
2024-01-24 8effe71359ed782ad82c8d6f0e3ce83aae9b88c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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<String,String> 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<SysConfigEntity> 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);
    }
}