|
|
@@ -1,294 +0,0 @@
|
|
|
-package com.skyversation.xjcy.service;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.skyversation.xjcy.bean.*;
|
|
|
-import com.skyversation.xjcy.util.HttpUtil;
|
|
|
-import com.skyversation.xjcy.util.SpecialisationStringCollector;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
-import lombok.Getter;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.LinkedMultiValueMap;
|
|
|
-import org.springframework.util.MultiValueMap;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-import org.springframework.web.client.RestClientException;
|
|
|
-
|
|
|
-import java.time.LocalDate;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-@Service
|
|
|
-public class DMSService {
|
|
|
- @Value("${app.dms.path}")
|
|
|
- private String path;
|
|
|
- @Value("${app.oauth.login-name}")
|
|
|
- private String loginName;
|
|
|
- @Value("${app.oauth.password}")
|
|
|
- private String password;
|
|
|
- @Value("${app.oauth.path}")
|
|
|
- private String oauthPath;
|
|
|
-
|
|
|
- @AllArgsConstructor
|
|
|
- @Getter
|
|
|
- public enum DMSQuery {
|
|
|
-
|
|
|
- INDUSTRIAL_PARK("1580", IndustrialPark.class) {
|
|
|
- @Override
|
|
|
- public DMSRequest preRequest(LocalDate now) {
|
|
|
- return new DMSRequest();
|
|
|
- }
|
|
|
- },
|
|
|
- ENTERPRISE("1593", Enterprise.class) {
|
|
|
- @Override
|
|
|
- public DMSRequest preRequest(LocalDate now) {
|
|
|
- return new DMSRequest();
|
|
|
- }
|
|
|
- },
|
|
|
- ECONOMIC_ALL_INDEED("1594", EnterpriseEconomic.class) {
|
|
|
- @Override
|
|
|
- public DMSRequest preRequest(LocalDate now) {
|
|
|
- DMSRequest request = new DMSRequest();
|
|
|
- Set<String> monthInDeed = new HashSet<>();
|
|
|
- monthInDeed.addAll(SpecialisationStringCollector.YearMonthByLast12Month(now));
|
|
|
- monthInDeed.addAll(SpecialisationStringCollector.YearMonthByYtd(now));
|
|
|
- monthInDeed.addAll(SpecialisationStringCollector.YearMonthByYtd(1, now));
|
|
|
- request.addWhere("c_year_month", "5", monthInDeed);
|
|
|
- return request;
|
|
|
- }
|
|
|
- },
|
|
|
- ORDER("1587", Order.class) {
|
|
|
- @Override
|
|
|
- public DMSRequest preRequest(LocalDate now) {
|
|
|
- return new DMSRequest();
|
|
|
- }
|
|
|
- },
|
|
|
- LAST_LEASE_DETAIL("1574", LeaseDetail.class) {
|
|
|
- @Override
|
|
|
- public DMSRequest preRequest(LocalDate now) {
|
|
|
- DMSRequest request = new DMSRequest();
|
|
|
- request.addWhere("c_is_latest_lease", "1", "2");
|
|
|
- return request;
|
|
|
- }
|
|
|
- },
|
|
|
- LAST_YEAR_LEASE_DETAIL("1574", LeaseDetail.class) {
|
|
|
- @Override
|
|
|
- public DMSRequest preRequest(LocalDate now) {
|
|
|
- DMSRequest request = new DMSRequest();
|
|
|
- LocalDate indeedStartTime = now.minusMonths(13).withDayOfMonth(1); // 2024-06-01
|
|
|
- LocalDate indeedEndTime = now.minusMonths(1) // 上个月
|
|
|
- .withDayOfMonth(
|
|
|
- now.minusMonths(1)
|
|
|
- .lengthOfMonth());
|
|
|
-
|
|
|
- LocalDate timeOfFarBefore = LocalDate.of(1970, 1, 1);
|
|
|
- LocalDate timeOfFarAfter = indeedEndTime.plusYears(100);
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
- request.addWhere("c_start_date", "3", timeOfFarBefore.format(formatter), indeedEndTime.format(formatter));
|
|
|
- request.addWhere("c_end_date", "3", indeedStartTime.format(formatter), timeOfFarAfter.format(formatter));
|
|
|
- return request;
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- private final String columnId;
|
|
|
- private final Class<? extends FromJSON> resultType;
|
|
|
-
|
|
|
- abstract DMSRequest preRequest(LocalDate now);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static class DMSRequest {
|
|
|
- DMSRequestType type;
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- String token;
|
|
|
- JSONArray where = new JSONArray();
|
|
|
- JSONArray order = new JSONArray();
|
|
|
-
|
|
|
- private void addWhere(String field, String type, String content) {
|
|
|
- this.where.add(summonWhere(field, type, content));
|
|
|
- }
|
|
|
-
|
|
|
- private void addWhere(String field, String type, Set<String> content) {
|
|
|
- this.where.add(summonWhere(field, type, content));
|
|
|
- }
|
|
|
-
|
|
|
- private JSONObject summonWhere(String field, String type, Object content) {
|
|
|
- JSONObject where = new JSONObject();
|
|
|
- where.put("field", field);
|
|
|
- where.put("searchType", type);
|
|
|
- JSONObject contents = new JSONObject();
|
|
|
- contents.put("value", content);
|
|
|
- where.put("content", contents);
|
|
|
- return where;
|
|
|
- }
|
|
|
-
|
|
|
- private void addWhere(String field, String type, String start, String end) {
|
|
|
- JSONObject where = new JSONObject();
|
|
|
- where.put("field", field);
|
|
|
- where.put("searchType", type);
|
|
|
- JSONObject contents = new JSONObject();
|
|
|
- contents.put("start", start);
|
|
|
- contents.put("end", end);
|
|
|
- where.put("content", contents);
|
|
|
- this.where.add(where);
|
|
|
- }
|
|
|
-
|
|
|
- private void addOrder(DMSRequest request) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private void setPage(String page, String pageSize) {
|
|
|
- this.params.put("page", page);
|
|
|
- this.params.put("pageSize", pageSize);
|
|
|
- }
|
|
|
-
|
|
|
- private void setState() {
|
|
|
- this.params.put("states", "0,1,2,3");
|
|
|
- }
|
|
|
-
|
|
|
- private void setColumnId(String columnId) {
|
|
|
- this.params.put("columnId", columnId);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private enum DMSRequestType {
|
|
|
- List, JOIN_LIST
|
|
|
- }
|
|
|
-
|
|
|
- public String loginForToken() throws Exception {
|
|
|
- if (!StringUtils.hasText(loginName) || !StringUtils.hasText(password)) {
|
|
|
- throw new Exception("请提供用于访问DMS的账号密码");
|
|
|
- }
|
|
|
- MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
|
|
- params.add("userName", loginName);
|
|
|
- params.add("password", password);
|
|
|
- params.add("clientId", "2");
|
|
|
- String response = HttpUtil.requestPost(oauthPath, params, new HashMap<>());
|
|
|
- JSONObject jsonObject = JSON.parseObject(response);
|
|
|
- if ("密码错误".equals(jsonObject.getString("message"))) {
|
|
|
- loginName = null;
|
|
|
- password = null;
|
|
|
- throw new Exception("请检查使用的账户和密码是否正确,已停止使用该账号密码以避免锁定账号");
|
|
|
- }
|
|
|
- if (!Objects.equals(jsonObject.getString("code"), "200")) {
|
|
|
- throw new Exception(jsonObject.getString("message"));
|
|
|
- }
|
|
|
- return jsonObject.getString("message");
|
|
|
- }
|
|
|
-
|
|
|
- private JSONObject sendToDms(DMSRequest request, int depth) {
|
|
|
- JSONObject jsonObject;
|
|
|
- MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
|
|
- request.params.forEach(params::add);
|
|
|
- params.add("search", request.where.toJSONString());
|
|
|
- try {
|
|
|
- String response;
|
|
|
- switch (request.type) {
|
|
|
- case List: {
|
|
|
- response = HttpUtil.requestPost(path + "/content/selectContentListInfo", params,
|
|
|
- Collections.singletonMap("token", request.token));
|
|
|
- break;
|
|
|
- }
|
|
|
- case JOIN_LIST: {
|
|
|
- response = "";
|
|
|
- break;
|
|
|
- }
|
|
|
- default: {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
- jsonObject = JSON.parseObject(response);
|
|
|
- } catch (RestClientException e) {
|
|
|
- if (depth < 3) {
|
|
|
- return sendToDms(request, depth + 1);
|
|
|
- }
|
|
|
- return null;
|
|
|
- } catch (Exception e) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- if (Objects.equals(jsonObject.getString("message"), "无效token") && depth < 3) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-
|
|
|
- private JSONObject sendToDms(DMSRequest request) {
|
|
|
- return sendToDms(request, 0);
|
|
|
- }
|
|
|
-
|
|
|
- private List<JSONObject> queryDmsList(DMSRequest request, String token, String columnId) {
|
|
|
- request.token = token;
|
|
|
- request.type = DMSRequestType.List;
|
|
|
- request.setColumnId(columnId);
|
|
|
- request.setState();
|
|
|
- int page = 0;
|
|
|
- int pageSize = 2000;
|
|
|
- List<JSONObject> list = new ArrayList<>();
|
|
|
- do {
|
|
|
- try {
|
|
|
- request.setPage(page + "", pageSize + "");
|
|
|
- JSONObject jsonObject = sendToDms(request);
|
|
|
- if ("成功".equals(jsonObject.getString("message"))) {
|
|
|
- JSONArray content = jsonObject.getJSONArray("content");
|
|
|
- if (content.isEmpty()) {
|
|
|
- break;
|
|
|
- }
|
|
|
- for (int i = 0; i < content.size(); i++) {
|
|
|
- list.add(content.getJSONObject(i));
|
|
|
- }
|
|
|
- } else {
|
|
|
- break;
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- break;
|
|
|
- } finally {
|
|
|
- page++;
|
|
|
- }
|
|
|
- } while (true);
|
|
|
-
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public List<JSONObject> simpleQuery(String token, String columId) {
|
|
|
- return queryDmsList(new DMSRequest(), token, columId);
|
|
|
- }
|
|
|
-
|
|
|
- public int getLargestCode(String token,String columId,String codeColumn,String today){
|
|
|
- DMSRequest request = new DMSRequest();
|
|
|
- request.addWhere(codeColumn,"2","%"+today+"%");
|
|
|
- List<JSONObject> list = null;
|
|
|
- try {
|
|
|
- list = queryDmsList(request,token,columId);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException("无法获取流水号:无法获取DMS中流水号对应的表。");
|
|
|
- }
|
|
|
- String pattern = ".*"+today;
|
|
|
- int max = 0;
|
|
|
- for (JSONObject jsonObject : list) {
|
|
|
- try {
|
|
|
- int code = Integer.parseInt(jsonObject.get(codeColumn).toString().replaceAll(pattern,""));
|
|
|
- if (code > max) {
|
|
|
- max = code;
|
|
|
- }
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException("无法获取流水号:出现了未预期的流水号");
|
|
|
- }
|
|
|
- }
|
|
|
- return max;
|
|
|
- }
|
|
|
-
|
|
|
- public <T extends FromJSON> List<T> query(String token, DMSQuery query, Class<T> clazz, LocalDate now) {
|
|
|
- if (query.getResultType() != clazz) {
|
|
|
- throw new RuntimeException("类型与枚举提供的类型不符,请确认枚举中的类型");
|
|
|
- }
|
|
|
- List<JSONObject> dmsResult = queryDmsList(query.preRequest(now), token, query.getColumnId());
|
|
|
-
|
|
|
- return dmsResult.stream().map(jsonObject -> JSON.toJavaObject(jsonObject, clazz)).collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|