DataCountService.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. package com.skyversation.xjcy.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.skyversation.xjcy.bean.*;
  5. import com.skyversation.xjcy.dms.DMSQuery;
  6. import com.skyversation.xjcy.dms.DMSService;
  7. import com.skyversation.xjcy.enums.EnterpriseLevel;
  8. import com.skyversation.xjcy.util.SpecialisationStringCollector;
  9. import com.skyversation.xjcy.util.TimeUtil;
  10. import lombok.NoArgsConstructor;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.util.LinkedMultiValueMap;
  14. import org.springframework.util.MultiValueMap;
  15. import javax.annotation.PostConstruct;
  16. import javax.annotation.Resource;
  17. import java.math.BigDecimal;
  18. import java.time.LocalDate;
  19. import java.util.*;
  20. import java.util.concurrent.atomic.AtomicBoolean;
  21. import java.util.concurrent.atomic.AtomicLong;
  22. import java.util.stream.Collectors;
  23. import java.util.stream.Stream;
  24. @Service
  25. public class DataCountService {
  26. @Resource
  27. DMSService dmsService;
  28. private JSONObject cacheTownData;
  29. private Map<String, JSONObject> cacheParkData;
  30. private Map<String, JSONObject> cacheBuildData;
  31. private JSONObject tempCacheTownData;
  32. private Map<String, JSONObject> tempCacheParkData;
  33. private Map<String, JSONObject> tempCacheBuildData;
  34. Map<String, EnterpriseEconomicCount> economicCountBy12Month = new HashMap<>();
  35. Map<String, EnterpriseEconomicCount> economicCountByYtdThisYear = new HashMap<>();
  36. Map<String, EnterpriseEconomicCount> economicCountByYtdLastYear = new HashMap<>();
  37. Map<String, Enterprise> aliveEnterpriseMap = new HashMap<>();
  38. MultiValueMap<String, IndustrialPark> roomMap = new LinkedMultiValueMap<>();
  39. MultiValueMap<String, IndustrialPark> floorMap = new LinkedMultiValueMap<>();
  40. MultiValueMap<String, IndustrialPark> buildMap = new LinkedMultiValueMap<>();
  41. List<IndustrialPark> allPark = new ArrayList<>();
  42. private Map<String,String> nationalIndustryToMerged = new HashMap<>();
  43. private Map<String,String> nationalIndustryMap = new HashMap<>();
  44. private Map<String,String> regTypeMap = new HashMap<>();
  45. @PostConstruct
  46. public void init() {
  47. initNationalIndustryToMerged();
  48. initRegType();
  49. initNationalIndustry();
  50. }
  51. private void initNationalIndustryToMerged () {
  52. Map<String,String> map = new HashMap<>();
  53. map.put("F", "批发和零售业");
  54. map.put("I", "信息传输、软件和信息技术服务业");
  55. map.put("L", "租赁和商务服务业");
  56. map.put("M", "科学研究和技术服务业");
  57. map.put("C", "制造业");
  58. map.put("R", "文化、体育和娱乐业");
  59. nationalIndustryToMerged = map;
  60. }
  61. private void initRegType(){
  62. Map<String,String> map = new HashMap<>();
  63. map.put("1", "注册实体型");
  64. map.put("2", "注册非实体型");
  65. map.put("3", "实体非注册型");
  66. map.put("4", "临时流动");
  67. regTypeMap = map;
  68. }
  69. private void initNationalIndustry() {
  70. Map<String,String> map = new HashMap<>();
  71. map.put("A", "农、林、牧、渔业");
  72. map.put("B", "采矿业");
  73. map.put("C", "制造业");
  74. map.put("D", "电力、热力、燃气及水生产和供应业");
  75. map.put("E", "建筑业");
  76. map.put("F", "批发和零售业");
  77. map.put("G", "交通运输、仓储和邮政业");
  78. map.put("H", "住宿和餐饮业");
  79. map.put("I", "信息传输、软件和信息技术服务业");
  80. map.put("J", "金融业");
  81. map.put("K", "房地产业");
  82. map.put("L", "租赁和商务服务业");
  83. map.put("M", "科学研究和技术服务业");
  84. map.put("N", "水利、环境和公共设施管理业");
  85. map.put("O", "居民服务、修理和其他服务业");
  86. map.put("P", "教育");
  87. map.put("Q", "卫生和社会工作");
  88. map.put("R", "文化、体育和娱乐业");
  89. map.put("S", "公共管理、社会保障和社会组织");
  90. map.put("T", "国际组织");
  91. nationalIndustryMap = map;
  92. }
  93. //简单限制统计频率
  94. @Value("${app.count-cache-length}")
  95. private long INTERVAL_MILLIS ;
  96. private final AtomicLong lastStartTime = new AtomicLong(0);
  97. private final AtomicBoolean running = new AtomicBoolean(false);
  98. @Resource
  99. private AuthService authService;
  100. public void tryCount() {
  101. long now = System.currentTimeMillis();
  102. long last = lastStartTime.get();
  103. if (now - last < INTERVAL_MILLIS&&cacheBuildData!=null&&cacheParkData!=null||cacheTownData!=null) {
  104. return;
  105. }
  106. if (!running.compareAndSet(false, true)) {
  107. return;
  108. }
  109. if (now - lastStartTime.get() < INTERVAL_MILLIS) {
  110. running.set(false);
  111. return;
  112. }
  113. try {
  114. count(now);
  115. } finally {
  116. running.set(false);
  117. }
  118. }
  119. @NoArgsConstructor
  120. private static class EnterpriseEconomicCount {
  121. BigDecimal tax = BigDecimal.ZERO;
  122. BigDecimal product = BigDecimal.ZERO;
  123. BigDecimal revenue = BigDecimal.ZERO;
  124. public EnterpriseEconomicCount(List<EnterpriseEconomic> enterpriseEconomicList) {
  125. for (EnterpriseEconomic enterpriseEconomic : enterpriseEconomicList) {
  126. tax = tax.add(Optional.ofNullable(enterpriseEconomic.getCApprovedTax()).orElse(BigDecimal.ZERO));
  127. product = product.add(Optional.ofNullable(enterpriseEconomic.getCApprovedOutputValue()).orElse(BigDecimal.ZERO));
  128. revenue = revenue.add(Optional.ofNullable(enterpriseEconomic.getCApprovedRevenue()).orElse(BigDecimal.ZERO));
  129. }
  130. }
  131. }
  132. public void count(long startTime) {
  133. lastStartTime.set(startTime);
  134. tempCacheTownData = new JSONObject();
  135. tempCacheParkData = new HashMap<>();
  136. tempCacheBuildData = new HashMap<>();
  137. economicCountBy12Month = new HashMap<>();
  138. economicCountByYtdThisYear = new HashMap<>();
  139. economicCountByYtdLastYear = new HashMap<>();
  140. aliveEnterpriseMap = new HashMap<>();
  141. roomMap = new LinkedMultiValueMap<>();
  142. floorMap = new LinkedMultiValueMap<>();
  143. buildMap = new LinkedMultiValueMap<>();
  144. allPark = new ArrayList<>();
  145. LocalDate now = LocalDate.now();
  146. //数据量大不了所以不做流式
  147. List<IndustrialPark> allIndustrialPark = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.INDUSTRIAL_PARK, IndustrialPark.class, now);
  148. List<Enterprise> allEnterprise = dmsService.queryEnterpriseByComplex(authService.getTokenOfServiceAccount());
  149. List<EnterpriseEconomic> inDateEnterpriseEconomic = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.ECONOMIC_ALL_INDEED, EnterpriseEconomic.class, now);
  150. List<Order> allOrder = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.ORDER, Order.class, now);
  151. List<LeaseDetail> lastLeaseDetail = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.LAST_LEASE_DETAIL, LeaseDetail.class, now);
  152. List<LeaseDetail> lastYearLease = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.LAST_YEAR_LEASE_DETAIL, LeaseDetail.class, now);
  153. List<P18EnterpriseEconomic> needP18EnterpriseEconomic = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.THIS_YEAR_P18_ENTERPRISE_ECONOMIC, P18EnterpriseEconomic.class, now);
  154. //处理一下拿到的数据,建一建索引
  155. Map<String, List<EnterpriseEconomic>> enterpriseEconomicMap = new HashMap<>();
  156. inDateEnterpriseEconomic.forEach(e -> {
  157. String id = e.getCEnterpriseId();
  158. if (enterpriseEconomicMap.containsKey(id)) {
  159. enterpriseEconomicMap.get(id).add(e);
  160. } else {
  161. List<EnterpriseEconomic> list = new ArrayList<>();
  162. list.add(e);
  163. enterpriseEconomicMap.put(id, list);
  164. }
  165. });
  166. lastLeaseDetail = new ArrayList<>(lastLeaseDetail.stream()
  167. .collect(Collectors.toMap(
  168. LeaseDetail::getCRoomCode,
  169. p -> p,
  170. (existing, replacement) ->{
  171. LocalDate existingEnd = existing.getCEndDate();
  172. LocalDate replacementEnd = replacement.getCEndDate();
  173. if (existingEnd == null) return replacement;
  174. if (replacementEnd == null) return existing;
  175. return existingEnd.isAfter(replacementEnd) ? existing : replacement;
  176. }
  177. ))
  178. .values());
  179. Map<String,BigDecimal> p18EnterpriseEconomicMap = mergeP118EnterpriseEconomicMap(needP18EnterpriseEconomic);
  180. //楼宇/企业/企业经济数据遍历,全镇级/产业园级/楼栋级一并处理
  181. //遍历并计算企业相关的结果
  182. countEnterprise(allEnterprise, enterpriseEconomicMap, now);
  183. //遍历计算产业园相关
  184. countIndustrialPark(allIndustrialPark,p18EnterpriseEconomicMap);
  185. //企呼我应
  186. countOrder(allOrder);
  187. //租赁信息相关统计
  188. countLease(lastLeaseDetail,now);
  189. countRoomStatusByLease(lastLeaseDetail, now);
  190. countHistoryData(allEnterprise, allIndustrialPark, lastYearLease, now);
  191. cacheTownData = tempCacheTownData;
  192. cacheParkData = tempCacheParkData;
  193. cacheBuildData = tempCacheBuildData;
  194. }
  195. /**
  196. * 合并18项经济指标数据为营收数据,按企业合并
  197. * @param needP18EnterpriseEconomic 18项经济指标数据
  198. * @return 企业编码->营收数据
  199. */
  200. private Map<String, BigDecimal> mergeP118EnterpriseEconomicMap(List<P18EnterpriseEconomic> needP18EnterpriseEconomic) {
  201. return needP18EnterpriseEconomic.stream()
  202. .collect(Collectors.groupingBy(P18EnterpriseEconomic::getCEnterpriseCode))
  203. .entrySet()
  204. .stream()
  205. .collect(Collectors.toMap(
  206. Map.Entry::getKey,
  207. e -> e.getValue().stream()
  208. .map(this::getRevenue)
  209. .reduce(BigDecimal::add)
  210. .orElse(BigDecimal.ZERO)
  211. ));
  212. }
  213. /**
  214. * 获取营收数据
  215. */
  216. private BigDecimal getRevenue(P18EnterpriseEconomic p18){
  217. return mergeAll(
  218. p18.getCGsjyyysZf(),
  219. p18.getCGsrxyys(),
  220. p18.getCGsrlzyfwyys(),
  221. p18.getCGshzyys(),
  222. p18.getCGskjfwyys(),
  223. p18.getCGswhtyhylyys(),
  224. p18.getCQtfwy()
  225. );
  226. }
  227. private void countHistoryData(List<Enterprise> allEnterprise, List<IndustrialPark> allIndustrialPark, List<LeaseDetail> lastYearLease, LocalDate now) {
  228. //历史企业总数统计
  229. //历史数据,所以不论企业现状如何
  230. Map<String, List<Enterprise>> enterpriseMap = TimeUtil.split(allEnterprise, now);
  231. Map<String, Integer> enterpriseByMonth = enterpriseMap.entrySet()
  232. .stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size()));
  233. //历史异地企业统计
  234. //同上
  235. Map<String, List<Enterprise>> enterpriseInTownMap = TimeUtil.split(allEnterprise
  236. .stream()
  237. .filter(e -> Objects.equals(e.getCSsxqCode(), "#YIDIQIYE"))
  238. .collect(Collectors.toList()), now);
  239. Map<String, Integer> enterpriseInTownByMonth = enterpriseInTownMap.entrySet()
  240. .stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size()));
  241. //历史房间总数统计
  242. Map<String, List<IndustrialPark>> industrialParkMap = TimeUtil.split(allIndustrialPark
  243. .stream()
  244. .filter(e -> Objects.equals(e.getCResourceType(), "1"))
  245. .collect(Collectors.toList()),
  246. now);
  247. Map<String, Integer> roomByMonth = industrialParkMap.entrySet().stream().collect(
  248. Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size())
  249. );
  250. //历史已使用房间总数统计
  251. Map<String, List<LeaseDetail>> leaseMap = TimeUtil.split(lastYearLease, now);
  252. Map<String, Long> leaseByMonth = leaseMap.entrySet()
  253. .stream()
  254. .collect(
  255. Collectors.toMap(
  256. Map.Entry::getKey,
  257. e -> {
  258. Set<String> parkCodes = industrialParkMap.getOrDefault(e.getKey(), new ArrayList<>())
  259. .stream()
  260. .map(IndustrialPark::getCParkCode)
  261. .collect(Collectors.toSet());
  262. return e.getValue()
  263. .stream()
  264. .map(LeaseDetail::getCRoomCode)
  265. .filter(parkCodes::contains)
  266. .distinct()
  267. .count();
  268. }
  269. )
  270. );
  271. tempCacheTownData.put("enterpriseByMonth", enterpriseByMonth);
  272. tempCacheTownData.put("enterpriseInTownByMonth", enterpriseInTownByMonth);
  273. tempCacheTownData.put("leaseByMonth", leaseByMonth);
  274. tempCacheTownData.put("roomByMonth", roomByMonth);
  275. }
  276. private void countIndustrialPark(List<IndustrialPark> allIndustrialPark, Map<String, BigDecimal> p18EnterpriseEconomicMap) {
  277. //简单统计并按父级编码建映射
  278. simpleCountPark(allIndustrialPark);
  279. Map<EnterpriseLevel, Map<String, Integer>> enterPriceLevelCountByPark = new HashMap<>();
  280. for (EnterpriseLevel value : EnterpriseLevel.values()) {
  281. enterPriceLevelCountByPark.put(value, new HashMap<>());
  282. }
  283. //按楼宇父子关系统计
  284. for (IndustrialPark industrialPark : allPark) {
  285. String parkCode = industrialPark.getCParkCode();
  286. List<IndustrialPark> allBuild;
  287. List<IndustrialPark> allRoom = new ArrayList<>();
  288. BigDecimal allTaxByYtdThisYear = BigDecimal.ZERO;
  289. BigDecimal allGsProductByYtdThisYear = BigDecimal.ZERO;
  290. BigDecimal allTaxByYtdLastYear = BigDecimal.ZERO;
  291. BigDecimal allGsProductByYtdLastYear = BigDecimal.ZERO;
  292. int enterPriceCount = 0;
  293. int enterPriceGsCount = 0;
  294. Set<String> inParkEnterpriseCode = new HashSet<>();
  295. List<String> aliveEnterprise = new ArrayList<>();
  296. List<String> gsEnterprise = new ArrayList<>();
  297. int usedRoomCount = 0;
  298. allBuild = buildMap.get(parkCode);
  299. if (allBuild == null) {
  300. allBuild = new ArrayList<>();
  301. }
  302. //楼栋
  303. for (IndustrialPark build : allBuild) {
  304. String buildCode = build.getCParkCode();
  305. Set<String> buildEnterpriseCode = new HashSet<>();
  306. JSONObject buildData = new JSONObject();
  307. int buildRoomCount = 0;
  308. int buildUsedRoomCount = 0;
  309. JSONArray floors = new JSONArray();
  310. List<IndustrialPark> partFloor = floorMap.get(buildCode);
  311. if (partFloor == null) {
  312. partFloor = new ArrayList<>();
  313. }
  314. //楼层
  315. for (IndustrialPark floor : partFloor) {
  316. String floorCode = floor.getCParkCode();
  317. JSONObject floorData = new JSONObject();
  318. int floorRoomCount = 0;
  319. int floorUsedRoomCount = 0;
  320. List<IndustrialPark> partRoom = roomMap.get(floorCode);
  321. if (partRoom == null) {
  322. partRoom = new ArrayList<>();
  323. }
  324. //房间
  325. allRoom.addAll(partRoom);
  326. for (IndustrialPark room : partRoom) {
  327. buildRoomCount++;
  328. floorRoomCount++;
  329. String enterpriseCode = room.getCZlqydm();
  330. if (enterpriseCode != null) {
  331. usedRoomCount++;
  332. buildUsedRoomCount++;
  333. floorUsedRoomCount++;
  334. inParkEnterpriseCode.add(enterpriseCode);
  335. buildEnterpriseCode.add(enterpriseCode);
  336. }
  337. }
  338. floorData.put("floorCode", floorCode);
  339. floorData.put("roomCount", floorRoomCount);
  340. floorData.put("usedRoomCount", floorUsedRoomCount);
  341. floorData.put("buildingAreaCount", floor.getCBuildingArea());
  342. floorData.put("vacantAreaCount", floor.getCVacantArea());
  343. floorData.put("floorName", floor.getCResourceName());
  344. floors.add(floorData);
  345. }
  346. int buildEnterpriseCount = 0;
  347. int buildGsEnterpriseCount = 0;
  348. for (String enterpriseCode : buildEnterpriseCode) {
  349. boolean isGsqy = false;
  350. if (aliveEnterpriseMap.containsKey(enterpriseCode)) {
  351. Enterprise enterprise = aliveEnterpriseMap.get(enterpriseCode);
  352. if ("2".equals(enterprise.getIsGsqy())) {
  353. isGsqy = true;
  354. }
  355. }
  356. buildEnterpriseCount++;
  357. if (isGsqy) {
  358. buildGsEnterpriseCount++;
  359. }
  360. }
  361. buildData.put("enterpriseCount", buildEnterpriseCount);
  362. buildData.put("gsEnterpriseCount", buildGsEnterpriseCount);
  363. buildData.put("floorCount", partFloor.size());
  364. buildData.put("roomCount", buildRoomCount);
  365. buildData.put("usedRoomCount", buildUsedRoomCount);
  366. buildData.put("buildingAreaCount", build.getCBuildingArea());
  367. buildData.put("vacantAreaCount", build.getCVacantArea());
  368. buildData.put("floors", floors);
  369. tempCacheBuildData.put(buildCode, buildData);
  370. }
  371. for (EnterpriseLevel value : EnterpriseLevel.values()) {
  372. enterPriceLevelCountByPark.get(value).put(industrialPark.getCResourceName(), 0);
  373. }
  374. Map<String, Integer> enterPriceIndustryCount = new HashMap<>();
  375. for (String enterpriseCode : inParkEnterpriseCode) {
  376. boolean isGsqy = false;
  377. String nationalIndustryCode = null;
  378. if (aliveEnterpriseMap.containsKey(enterpriseCode)) {
  379. Enterprise enterprise = aliveEnterpriseMap.get(enterpriseCode);
  380. isGsqy = "2".equals(enterprise.getIsGsqy());
  381. nationalIndustryCode = enterprise.getCNationalIndustryCode();
  382. EnterpriseLevel.ofJsonValue(enterprise.getCompanyLevel()).ifPresent(level -> {
  383. Map<String, Integer> map = enterPriceLevelCountByPark.get(level);
  384. map.put(industrialPark.getCResourceName(), map.get(industrialPark.getCResourceName()) + 1);
  385. });
  386. EnterpriseEconomicCount ytdThisYear = economicCountByYtdThisYear.get(enterpriseCode);
  387. EnterpriseEconomicCount ytdLastYear = economicCountByYtdLastYear.get(enterpriseCode);
  388. allTaxByYtdThisYear = allTaxByYtdThisYear.add(ytdThisYear.tax);
  389. allTaxByYtdLastYear = allTaxByYtdLastYear.add(ytdLastYear.tax);
  390. if (isGsqy) {
  391. allGsProductByYtdThisYear = allGsProductByYtdThisYear.add(ytdThisYear.product);
  392. allGsProductByYtdLastYear = allGsProductByYtdLastYear.add(ytdLastYear.product);
  393. gsEnterprise.add(enterpriseCode);
  394. }
  395. aliveEnterprise.add(enterpriseCode);
  396. }
  397. enterPriceCount++;
  398. if (isGsqy) {
  399. enterPriceGsCount++;
  400. }
  401. if (nationalIndustryCode != null) {
  402. String prefix = nationalIndustryCode.substring(0, 1);
  403. String nationalIndustry = nationalIndustryMap.get(prefix);
  404. if (enterPriceIndustryCount.containsKey(nationalIndustry)) {
  405. enterPriceIndustryCount.put(nationalIndustry, enterPriceIndustryCount.get(nationalIndustry) + 1);
  406. } else {
  407. enterPriceIndustryCount.put(nationalIndustry, 1);
  408. }
  409. }
  410. }
  411. JSONObject parkDataCount = new JSONObject();
  412. parkDataCount.put("totalTax", allTaxByYtdThisYear);
  413. parkDataCount.put("totalGsOutput", allGsProductByYtdThisYear);
  414. parkDataCount.put("totalTaxLastYear", allTaxByYtdThisYear);
  415. parkDataCount.put("totalGsOutputLastYear", allGsProductByYtdThisYear);
  416. parkDataCount.put("buildCount", allBuild.size());
  417. parkDataCount.put("officeCount", allRoom.size());
  418. parkDataCount.put("usedRoomCount", usedRoomCount);
  419. parkDataCount.put("enterpriseCount", enterPriceCount);
  420. parkDataCount.put("enterpriseGsCount", enterPriceGsCount);
  421. parkDataCount.put("parkVacantArea", industrialPark.getCVacantArea());
  422. parkDataCount.put("enterPriseIndustry", enterPriceIndustryCount);
  423. JSONArray topRevenue = new JSONArray();
  424. List<String> sortedEnterprise = gsEnterprise
  425. .stream()
  426. .sorted(Comparator.comparing(p18EnterpriseEconomicMap::get,Comparator.nullsFirst(BigDecimal::compareTo)).reversed())
  427. .limit(10).collect(Collectors.toList());
  428. for (String enterPriseCode : sortedEnterprise) {
  429. JSONObject outPut = new JSONObject();
  430. Enterprise enterPrice = aliveEnterpriseMap.get(enterPriseCode);
  431. BigDecimal product =nullToZero( p18EnterpriseEconomicMap.get(enterPriseCode));
  432. outPut.put("enterPriceCode", enterPriseCode);
  433. outPut.put("enterPriceName", enterPrice.getCEnterpriseName());
  434. if (product.compareTo(BigDecimal.ZERO)==0){
  435. outPut.put("enterPriceRevenue","暂无");
  436. }else {
  437. outPut.put("enterPriceRevenue",product);
  438. }
  439. topRevenue.add(outPut);
  440. }
  441. parkDataCount.put("topRevenue", topRevenue);
  442. tempCacheParkData.put(parkCode, parkDataCount);
  443. for (EnterpriseLevel level : EnterpriseLevel.values()) {
  444. Map<String, Integer> map = enterPriceLevelCountByPark.get(level);
  445. Map<String, Integer> sortedDesc = map.entrySet()
  446. .stream()
  447. .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
  448. .limit(10)
  449. .collect(Collectors.toMap(
  450. Map.Entry::getKey,
  451. Map.Entry::getValue,
  452. (e1, e2) -> e1,
  453. LinkedHashMap::new
  454. ));
  455. enterPriceLevelCountByPark.put(level, sortedDesc);
  456. }
  457. tempCacheTownData.put("enterpriseLevelCountByPark", enterPriceLevelCountByPark);
  458. }
  459. }
  460. private void simpleCountPark(List<IndustrialPark> allIndustrialPark) {
  461. int parkCount = 0;
  462. Map<String, Integer> parkTypeCount = new HashMap<>();
  463. for (IndustrialPark industrialPark : allIndustrialPark) {
  464. try {
  465. int type = Integer.parseInt(industrialPark.getCResourceType());
  466. switch (type) {
  467. case 1: {
  468. String parentCode = industrialPark.getCParentCode();
  469. roomMap.add(parentCode, industrialPark);
  470. break;
  471. }
  472. case 2: {
  473. String parentCode = industrialPark.getCParentCode();
  474. floorMap.add(parentCode, industrialPark);
  475. break;
  476. }
  477. case 3: {
  478. String parentCode = industrialPark.getCParentCode();
  479. buildMap.add(parentCode, industrialPark);
  480. break;
  481. }
  482. case 4: {
  483. parkCount++;
  484. allPark.add(industrialPark);
  485. if (parkTypeCount.containsKey(industrialPark.getCLandUse())) {
  486. parkTypeCount.put(industrialPark.getCLandUse(), parkCount + 1);
  487. } else {
  488. parkTypeCount.put(industrialPark.getCLandUse(), 1);
  489. }
  490. }
  491. default: {
  492. //do nothing
  493. }
  494. }
  495. } catch (Exception e) {
  496. //nothing
  497. }
  498. }
  499. BigDecimal totalFloorArea = allPark.stream().map(IndustrialPark::getCFloorArea).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
  500. BigDecimal totalBuildArea = allPark.stream().map(IndustrialPark::getCBuildingArea).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
  501. BigDecimal totalVacantArea = allPark.stream().map(IndustrialPark::getCVacantArea).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
  502. tempCacheTownData.put("parkCount", parkCount);
  503. tempCacheTownData.put("parkType", parkTypeCount);
  504. tempCacheTownData.put("areaBuild", totalBuildArea);
  505. tempCacheTownData.put("areaFloor", totalFloorArea);
  506. tempCacheTownData.put("areaVacant", totalVacantArea);
  507. tempCacheTownData.put("areaUsed", totalBuildArea.subtract(totalVacantArea));
  508. }
  509. private void countEnterprise(List<Enterprise> allEnterprise, Map<String, List<EnterpriseEconomic>> enterpriseEconomicMap, LocalDate now) {
  510. int inTownCount = 0;
  511. int gsqyCount = 0;
  512. int aliveCount = 0;
  513. Map<String, Integer> industryCount = new HashMap<>();
  514. Map<String, Integer> nationalIndustryCount = new HashMap<>();
  515. Map<String, Integer> regTypeCount = new HashMap<>();
  516. for (Enterprise enterPrise : allEnterprise) {
  517. boolean isInTown =Arrays.asList("1","3","4").contains(Optional.ofNullable(enterPrise.getRegType()).orElse("1"));
  518. boolean isGsqy = "2".equals(Optional.ofNullable(enterPrise.getIsGsqy()).orElse("1"));
  519. boolean isAlive = true;
  520. if (isAlive) {
  521. aliveCount++;
  522. if (isInTown) {
  523. inTownCount++;
  524. }
  525. if (isGsqy) {
  526. gsqyCount++;
  527. }
  528. String nationalIndustryCode = enterPrise.getCNationalIndustryCode();
  529. String mergedNationalIndustry = null;
  530. if (nationalIndustryCode!=null&& !nationalIndustryCode.isEmpty()) {
  531. String prefix = nationalIndustryCode.substring(0, 1);
  532. String industry = nationalIndustryMap.get(prefix);
  533. if (industryCount.containsKey(industry)) {
  534. industryCount.put(industry, industryCount.get(industry) + 1);
  535. } else {
  536. industryCount.put(industry, 1);
  537. }
  538. mergedNationalIndustry = nationalIndustryToMerged.get(prefix);
  539. }
  540. if (mergedNationalIndustry==null)mergedNationalIndustry = "其他";
  541. if (nationalIndustryCount.containsKey(mergedNationalIndustry)) {
  542. nationalIndustryCount.put(mergedNationalIndustry, nationalIndustryCount.get(mergedNationalIndustry) + 1);
  543. } else {
  544. nationalIndustryCount.put(mergedNationalIndustry, 1);
  545. }
  546. String regType = enterPrise.getRegType();
  547. String regTypeChinese = null;
  548. if (regType!=null&& !regType.isEmpty()){
  549. regTypeChinese = regTypeMap.get(regType);
  550. }
  551. if (regTypeChinese == null) regTypeChinese = "其他";
  552. if (regTypeCount.containsKey(regTypeChinese)) {
  553. regTypeCount.put(regTypeChinese, regTypeCount.get(regTypeChinese) + 1);
  554. } else {
  555. regTypeCount.put(regTypeChinese, 1);
  556. }
  557. String enterPriceCode = enterPrise.getCUnifiedSocialCreditCode();
  558. if (enterPriceCode != null) {
  559. aliveEnterpriseMap.put(enterPriceCode, enterPrise);
  560. List<EnterpriseEconomic> enterpriseEconomicList = enterpriseEconomicMap.get(enterPriceCode);
  561. List<EnterpriseEconomic> economicBy12Month = new ArrayList<>();
  562. List<EnterpriseEconomic> economicByYtdThisYear = new ArrayList<>();
  563. List<EnterpriseEconomic> economicByYtdLastYear = new ArrayList<>();
  564. List<String> Last12Month = SpecialisationStringCollector.YearMonthByLast12Month(now);
  565. List<String> ytdThisYear = SpecialisationStringCollector.YearMonthByYtd(now);
  566. List<String> ytdLastYear = SpecialisationStringCollector.YearMonthByYtd(1, now);
  567. Optional.ofNullable(enterpriseEconomicList).orElse(new ArrayList<>()).stream()
  568. .filter(e -> !"4".equals(e.getCDataStatus()))
  569. .forEach(e -> {
  570. String time = e.getCYearMonth();
  571. if (Last12Month.contains(time)) {
  572. economicBy12Month.add(e);
  573. }
  574. if (ytdThisYear.contains(time)) {
  575. economicByYtdThisYear.add(e);
  576. }
  577. if (ytdLastYear.contains(time)) {
  578. economicByYtdLastYear.add(e);
  579. }
  580. });
  581. economicCountBy12Month.put(enterPriceCode, new EnterpriseEconomicCount(economicBy12Month));
  582. economicCountByYtdThisYear.put(enterPriceCode, new EnterpriseEconomicCount(economicByYtdThisYear));
  583. economicCountByYtdLastYear.put(enterPriceCode, new EnterpriseEconomicCount(economicByYtdLastYear));
  584. }
  585. }
  586. }
  587. tempCacheTownData.put("enterPriseCount", aliveCount);
  588. tempCacheTownData.put("enterPriseSdCount", inTownCount);
  589. tempCacheTownData.put("enterPriseGsCount", gsqyCount);
  590. tempCacheTownData.put("enterPriseIndustry", industryCount);
  591. tempCacheTownData.put("mergedEnterPriseIndustry", nationalIndustryCount);
  592. tempCacheTownData.put("enterPriseRegType", regTypeCount);
  593. }
  594. private void countOrder(List<Order> allOrder) {
  595. int orderComplete = 0;
  596. int orderCount = 0;
  597. for (Order order : allOrder) {
  598. String status = order.getStatus();
  599. switch (status) {
  600. case "3": {
  601. orderComplete++;
  602. //就是没有break,需要它自然下落
  603. }
  604. case "1":
  605. case "2": {
  606. orderCount++;
  607. }
  608. case "4":
  609. default: {
  610. //doNothing
  611. }
  612. }
  613. }
  614. tempCacheTownData.put("completeOrder", orderComplete);
  615. tempCacheTownData.put("orderCount", orderCount);
  616. }
  617. private void countLease(List<LeaseDetail> allLease,LocalDate now) {
  618. List<LeaseDetail> timeoutIn1Month = new ArrayList<>();
  619. List<LeaseDetail> timeoutIn3Month = new ArrayList<>();
  620. for (LeaseDetail lease : allLease) {
  621. if (lease.getCEndDate() != null) {
  622. if (lease.getCEndDate().isAfter(now)) {
  623. if (lease.getCEndDate().isBefore(now.plusMonths(1))) {
  624. timeoutIn1Month.add(lease);
  625. } else if (lease.getCEndDate().isBefore(now.plusMonths(3))) {
  626. timeoutIn3Month.add(lease);
  627. }
  628. }
  629. }
  630. }
  631. timeoutIn1Month.sort(Comparator.comparing(LeaseDetail::getCEndDate));
  632. timeoutIn3Month.sort(Comparator.comparing(LeaseDetail::getCEndDate));
  633. List<LeaseDetail> sortedLeaseTimeout = Stream.concat(timeoutIn1Month.stream(), timeoutIn3Month.stream())
  634. .sorted(Comparator.comparing(LeaseDetail::getCEndDate))
  635. .limit(25)
  636. .collect(Collectors.toList());
  637. tempCacheTownData.put("leaseTimeoutIn1Month", timeoutIn1Month.size());
  638. tempCacheTownData.put("leaseTimeoutIn3Month", timeoutIn3Month.size());
  639. tempCacheTownData.put("leaseTimeout", sortedLeaseTimeout);
  640. }
  641. private void countRoomStatusByLease(List<LeaseDetail> allLease, LocalDate now) {
  642. Map<String, LeaseDetail> leaseMap = allLease.stream().collect(
  643. Collectors.toMap(
  644. LeaseDetail::getCRoomCode,
  645. v -> v
  646. )
  647. );
  648. String vacant = "vacant";
  649. String expiringSoon = "expiringSoon";
  650. String utilized = "utilized";
  651. String reserved = "reserved";
  652. String terminated = "terminated";
  653. String[] buckets = new String[]{vacant, expiringSoon, utilized, reserved, terminated};
  654. Map<String, List<IndustrialPark>> roomBuckets = new HashMap<>();
  655. for (String bucket : buckets) {
  656. roomBuckets.put(bucket, new ArrayList<>());
  657. }
  658. for (List<IndustrialPark> roomList : roomMap.values()) {
  659. for (IndustrialPark room : roomList) {
  660. LeaseDetail lease = leaseMap.get(room.getCParkCode());
  661. if (lease == null) {
  662. roomBuckets.get(vacant).add(room);
  663. continue;
  664. }
  665. if (!lease.getCEndDate().isBefore(now) && !lease.getCEndDate().isAfter(now.plusMonths(3))) {
  666. roomBuckets.get(expiringSoon).add(room);
  667. continue;
  668. }
  669. if (!lease.getCEndDate().isBefore(now) && !lease.getCStartDate().isAfter(now)) {
  670. roomBuckets.get(utilized).add(room);
  671. continue;
  672. }
  673. if (lease.getCStartDate().isAfter(now)) {
  674. roomBuckets.get(reserved).add(room);
  675. continue;
  676. }
  677. if (!lease.getCEndDate().plusMonths(1).isBefore(now)) {
  678. roomBuckets.get(terminated).add(room);
  679. continue;
  680. }
  681. roomBuckets.get(vacant).add(room);
  682. }
  683. }
  684. tempCacheTownData.put("leaseVacantRoomCount", roomBuckets.get(vacant).size());
  685. tempCacheTownData.put("leaseVacantAreaCount", roomBuckets.get(vacant)
  686. .stream()
  687. .map(IndustrialPark::getCBuildingArea)
  688. .filter(Objects::nonNull)
  689. .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
  690. tempCacheTownData.put("leaseExpiringSoonRoomCount", roomBuckets.get(expiringSoon).size());
  691. tempCacheTownData.put("leaseExpiringSoonAreaCount", roomBuckets.get(expiringSoon)
  692. .stream()
  693. .map(IndustrialPark::getCBuildingArea)
  694. .filter(Objects::nonNull)
  695. .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
  696. tempCacheTownData.put("leaseUtilizedRoomCount", roomBuckets.get(utilized).size());
  697. tempCacheTownData.put("leaseUtilizedAreaCount", roomBuckets.get(utilized)
  698. .stream()
  699. .map(IndustrialPark::getCBuildingArea)
  700. .filter(Objects::nonNull)
  701. .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
  702. tempCacheTownData.put("leaseReservedRoomCount", roomBuckets.get(reserved).size());
  703. tempCacheTownData.put("leaseReservedAreaCount", roomBuckets.get(reserved)
  704. .stream()
  705. .map(IndustrialPark::getCBuildingArea)
  706. .filter(Objects::nonNull)
  707. .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
  708. tempCacheTownData.put("leaseTerminatedRoomCount", roomBuckets.get(terminated).size());
  709. tempCacheTownData.put("leaseTerminatedAreaCount", roomBuckets.get(terminated)
  710. .stream()
  711. .map(IndustrialPark::getCBuildingArea)
  712. .filter(Objects::nonNull)
  713. .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
  714. }
  715. public JSONObject getTownData() {
  716. tryCount();
  717. return cacheTownData;
  718. }
  719. public JSONObject getParkData(String parkCode) {
  720. tryCount();
  721. return cacheParkData.get(parkCode);
  722. }
  723. public JSONObject getBuildData(String buildCode) {
  724. tryCount();
  725. return cacheBuildData.get(buildCode);
  726. }
  727. private static BigDecimal nullToZero(BigDecimal value){
  728. return value == null ? BigDecimal.ZERO : value;
  729. }
  730. private static BigDecimal mergeAll(BigDecimal... values){
  731. return Arrays.stream(values)
  732. .map(DataCountService::nullToZero)
  733. .reduce(BigDecimal::add)
  734. .orElse(BigDecimal.ZERO);
  735. }
  736. }