|
|
@@ -0,0 +1,172 @@
|
|
|
+package com.uas.erp.schedular.listen;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.uas.erp.schedular.database.RestJdbcTemplate;
|
|
|
+import com.uas.erp.schedular.entity.Master;
|
|
|
+import com.uas.erp.schedular.entity.MasterTaskConfigEntity;
|
|
|
+import com.uas.erp.schedular.entity.MasterTaskConfigId;
|
|
|
+import com.uas.erp.schedular.service.ScheduledTaskService;
|
|
|
+import com.uas.erp.schedular.service.SettingService;
|
|
|
+import com.uas.erp.schedular.util.ContextHolder;
|
|
|
+import com.uas.erp.schedular.web.ResultListWrap;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: huj
|
|
|
+ * @Date: Created in 14:41 2018/11/27.
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class ListenTask {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ScheduledTaskService scheduledTaskService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SettingService settingService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestJdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ private String getUrl() {
|
|
|
+ return settingService.getValue("api.database.url");
|
|
|
+ }
|
|
|
+
|
|
|
+ private final static String MACADDRESS = DiskUtil.getAllMacAddress();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按账套环境,使用不同接口地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getApiDomain() {
|
|
|
+ return settingService.getValue("api.b2b.url");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每十分钟发送一次
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 */10 * * * *")
|
|
|
+ public void listen() {
|
|
|
+ List<Master> masters = new ArrayList<>();
|
|
|
+ List<Master> all = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ String allStr = restTemplate.getForObject(getUrl() + "/v1/master/list", String.class);
|
|
|
+ ResultListWrap<Master> allList = JSON.parseObject(allStr, new TypeReference<ResultListWrap<Master>>(Master.class) {
|
|
|
+ });
|
|
|
+ if (allList.isSuccess()) {
|
|
|
+ all = allList.getContent();
|
|
|
+ }
|
|
|
+ String resultStr = restTemplate.getForObject(getUrl() + "/v1/master/list?cloudEnabled=true", String.class);
|
|
|
+ ResultListWrap<Master> result = JSON.parseObject(resultStr, new TypeReference<ResultListWrap<Master>>(Master.class) {
|
|
|
+ });
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ masters = result.getContent();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (CollectionUtils.isEmpty(masters)) {
|
|
|
+ StringBuilder str = new StringBuilder();
|
|
|
+ if (!CollectionUtils.isEmpty(all)) {
|
|
|
+ for (Master master : all) {
|
|
|
+ if ("".equals(str.toString())) {
|
|
|
+ str.append(StringUtils.isEmpty(master.getMa_function()) ? master.getMa_name() : master.getMa_function());
|
|
|
+ } else {
|
|
|
+ str.append("、" + (StringUtils.isEmpty(master.getMa_function()) ? master.getMa_name() : master.getMa_function()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ListenDomain listenDomain = new ListenDomain(MACADDRESS, str == null ? "" : str.toString());
|
|
|
+ List<ListenDomain> listenDomains = new ArrayList<>();
|
|
|
+ if (!"".equals(str.toString())) {
|
|
|
+ listenDomain.setDescription("所有账套都未启用轮询");
|
|
|
+ } else {
|
|
|
+ listenDomain.setDescription("h2数据库查询异常或oracle数据库设置异常");
|
|
|
+ }
|
|
|
+ listenDomains.add(listenDomain);
|
|
|
+ postForList(getApiDomain() + "/erp/listen", ListenDomain.class, dataWrap(listenDomains));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(masters)) {
|
|
|
+ ContextHolder.setMaster(masters.get(0));
|
|
|
+ int dual = jdbcTemplate.getInt("select 1 from dual");
|
|
|
+ if (dual == 1) {
|
|
|
+ StringBuilder str = new StringBuilder();
|
|
|
+ for (Master master : masters) {
|
|
|
+ if ("".equals(str.toString())) {
|
|
|
+ str.append(StringUtils.isEmpty(master.getMa_function()) ? master.getMa_name() : master.getMa_function());
|
|
|
+ } else {
|
|
|
+ str.append("、" + (StringUtils.isEmpty(master.getMa_function()) ? master.getMa_name() : master.getMa_function()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ MasterTaskConfigId configId = new MasterTaskConfigId(null, null, masters.get(0).getMa_name());
|
|
|
+ List<MasterTaskConfigEntity> taskConfigs = scheduledTaskService.getTaskConfigs(configId, "b2b", "BUYER");
|
|
|
+ if (!CollectionUtils.isEmpty(taskConfigs)) {
|
|
|
+ ListenDomain listenDomain = new ListenDomain(MACADDRESS,
|
|
|
+ str == null ? "" : str.toString());
|
|
|
+ List<ListenDomain> listenDomains = new ArrayList<>();
|
|
|
+ listenDomains.add(listenDomain);
|
|
|
+ postForList(getApiDomain() + "/erp/listen", ListenDomain.class, dataWrap(listenDomains));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ListenDomain listenDomain = new ListenDomain(MACADDRESS, "" );
|
|
|
+ List<ListenDomain> listenDomains = new ArrayList<>();
|
|
|
+ listenDomain.setDescription("oracle查询异常");
|
|
|
+ listenDomains.add(listenDomain);
|
|
|
+ postForList(getApiDomain() + "/erp/listen", ListenDomain.class, dataWrap(listenDomains));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ContextHolder.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 封装成平台接口数据格式
|
|
|
+ * @param data
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected static MultiValueMap<String, String> dataWrap(Object data) {
|
|
|
+ MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
|
+ String dataStr = JSON.toJSONString(data);
|
|
|
+ try {
|
|
|
+ dataStr = URLEncoder.encode(dataStr, "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ map.set("data", dataStr);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ResponseEntity postForEntity(String url, MultiValueMap<String, String> vars) {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(vars, headers);
|
|
|
+ return restTemplate.postForEntity(url, request, String.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected <T> List<T> postForList(String url, Class<T> objectClass, MultiValueMap<String, String> vars) {
|
|
|
+ ResponseEntity<String> resultEntity = postForEntity(url, vars);
|
|
|
+ return JSON.parseArray(resultEntity.getBody(), objectClass);
|
|
|
+ }
|
|
|
+}
|