| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846 |
- package com.skyversation.xjcy.service;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.skyversation.xjcy.bean.*;
- import com.skyversation.xjcy.dms.DMSQuery;
- import com.skyversation.xjcy.dms.DMSService;
- import com.skyversation.xjcy.enums.EnterpriseLevel;
- import com.skyversation.xjcy.util.SpecialisationStringCollector;
- import com.skyversation.xjcy.util.TimeUtil;
- import lombok.NoArgsConstructor;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.util.LinkedMultiValueMap;
- import org.springframework.util.MultiValueMap;
- import javax.annotation.PostConstruct;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.time.LocalDate;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicBoolean;
- import java.util.concurrent.atomic.AtomicLong;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- @Service
- public class DataCountService {
- @Resource
- DMSService dmsService;
- private JSONObject cacheTownData;
- private Map<String, JSONObject> cacheParkData;
- private Map<String, JSONObject> cacheBuildData;
- private JSONObject tempCacheTownData;
- private Map<String, JSONObject> tempCacheParkData;
- private Map<String, JSONObject> tempCacheBuildData;
- Map<String, EnterpriseEconomicCount> economicCountBy12Month = new HashMap<>();
- Map<String, EnterpriseEconomicCount> economicCountByYtdThisYear = new HashMap<>();
- Map<String, EnterpriseEconomicCount> economicCountByYtdLastYear = new HashMap<>();
- Map<String, Enterprise> aliveEnterpriseMap = new HashMap<>();
- MultiValueMap<String, IndustrialPark> roomMap = new LinkedMultiValueMap<>();
- MultiValueMap<String, IndustrialPark> floorMap = new LinkedMultiValueMap<>();
- MultiValueMap<String, IndustrialPark> buildMap = new LinkedMultiValueMap<>();
- List<IndustrialPark> allPark = new ArrayList<>();
- private Map<String,String> nationalIndustryToMerged = new HashMap<>();
- private Map<String,String> nationalIndustryMap = new HashMap<>();
- private Map<String,String> regTypeMap = new HashMap<>();
- @PostConstruct
- public void init() {
- initNationalIndustryToMerged();
- initRegType();
- initNationalIndustry();
- }
- private void initNationalIndustryToMerged () {
- Map<String,String> map = new HashMap<>();
- map.put("F", "批发和零售业");
- map.put("I", "信息传输、软件和信息技术服务业");
- map.put("L", "租赁和商务服务业");
- map.put("M", "科学研究和技术服务业");
- map.put("C", "制造业");
- map.put("R", "文化、体育和娱乐业");
- nationalIndustryToMerged = map;
- }
- private void initRegType(){
- Map<String,String> map = new HashMap<>();
- map.put("1", "注册实体型");
- map.put("2", "注册非实体型");
- map.put("3", "实体非注册型");
- map.put("4", "临时流动");
- regTypeMap = map;
- }
- private void initNationalIndustry() {
- Map<String,String> map = new HashMap<>();
- map.put("A", "农、林、牧、渔业");
- map.put("B", "采矿业");
- map.put("C", "制造业");
- map.put("D", "电力、热力、燃气及水生产和供应业");
- map.put("E", "建筑业");
- map.put("F", "批发和零售业");
- map.put("G", "交通运输、仓储和邮政业");
- map.put("H", "住宿和餐饮业");
- map.put("I", "信息传输、软件和信息技术服务业");
- map.put("J", "金融业");
- map.put("K", "房地产业");
- map.put("L", "租赁和商务服务业");
- map.put("M", "科学研究和技术服务业");
- map.put("N", "水利、环境和公共设施管理业");
- map.put("O", "居民服务、修理和其他服务业");
- map.put("P", "教育");
- map.put("Q", "卫生和社会工作");
- map.put("R", "文化、体育和娱乐业");
- map.put("S", "公共管理、社会保障和社会组织");
- map.put("T", "国际组织");
- nationalIndustryMap = map;
- }
- //简单限制统计频率
- @Value("${app.count-cache-length}")
- private long INTERVAL_MILLIS ;
- private final AtomicLong lastStartTime = new AtomicLong(0);
- private final AtomicBoolean running = new AtomicBoolean(false);
- @Resource
- private AuthService authService;
- public void tryCount() {
- long now = System.currentTimeMillis();
- long last = lastStartTime.get();
- if (now - last < INTERVAL_MILLIS&&cacheBuildData!=null&&cacheParkData!=null||cacheTownData!=null) {
- return;
- }
- if (!running.compareAndSet(false, true)) {
- return;
- }
- if (now - lastStartTime.get() < INTERVAL_MILLIS) {
- running.set(false);
- return;
- }
- try {
- count(now);
- } finally {
- running.set(false);
- }
- }
- @NoArgsConstructor
- private static class EnterpriseEconomicCount {
- BigDecimal tax = BigDecimal.ZERO;
- BigDecimal product = BigDecimal.ZERO;
- BigDecimal revenue = BigDecimal.ZERO;
- public EnterpriseEconomicCount(List<EnterpriseEconomic> enterpriseEconomicList) {
- for (EnterpriseEconomic enterpriseEconomic : enterpriseEconomicList) {
- tax = tax.add(Optional.ofNullable(enterpriseEconomic.getCApprovedTax()).orElse(BigDecimal.ZERO));
- product = product.add(Optional.ofNullable(enterpriseEconomic.getCApprovedOutputValue()).orElse(BigDecimal.ZERO));
- revenue = revenue.add(Optional.ofNullable(enterpriseEconomic.getCApprovedRevenue()).orElse(BigDecimal.ZERO));
- }
- }
- }
- public void count(long startTime) {
- lastStartTime.set(startTime);
- tempCacheTownData = new JSONObject();
- tempCacheParkData = new HashMap<>();
- tempCacheBuildData = new HashMap<>();
- economicCountBy12Month = new HashMap<>();
- economicCountByYtdThisYear = new HashMap<>();
- economicCountByYtdLastYear = new HashMap<>();
- aliveEnterpriseMap = new HashMap<>();
- roomMap = new LinkedMultiValueMap<>();
- floorMap = new LinkedMultiValueMap<>();
- buildMap = new LinkedMultiValueMap<>();
- allPark = new ArrayList<>();
- LocalDate now = LocalDate.now();
- //数据量大不了所以不做流式
- List<IndustrialPark> allIndustrialPark = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.INDUSTRIAL_PARK, IndustrialPark.class, now);
- List<Enterprise> allEnterprise = dmsService.queryEnterpriseByComplex(authService.getTokenOfServiceAccount());
- List<EnterpriseEconomic> inDateEnterpriseEconomic = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.ECONOMIC_ALL_INDEED, EnterpriseEconomic.class, now);
- List<Order> allOrder = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.ORDER, Order.class, now);
- List<LeaseDetail> lastLeaseDetail = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.LAST_LEASE_DETAIL, LeaseDetail.class, now);
- List<LeaseDetail> lastYearLease = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.LAST_YEAR_LEASE_DETAIL, LeaseDetail.class, now);
- List<P18EnterpriseEconomic> needP18EnterpriseEconomic = dmsService.query(authService.getTokenOfServiceAccount(), DMSQuery.THIS_YEAR_P18_ENTERPRISE_ECONOMIC, P18EnterpriseEconomic.class, now);
- //处理一下拿到的数据,建一建索引
- Map<String, List<EnterpriseEconomic>> enterpriseEconomicMap = new HashMap<>();
- inDateEnterpriseEconomic.forEach(e -> {
- String id = e.getCEnterpriseId();
- if (enterpriseEconomicMap.containsKey(id)) {
- enterpriseEconomicMap.get(id).add(e);
- } else {
- List<EnterpriseEconomic> list = new ArrayList<>();
- list.add(e);
- enterpriseEconomicMap.put(id, list);
- }
- });
- lastLeaseDetail = new ArrayList<>(lastLeaseDetail.stream()
- .collect(Collectors.toMap(
- LeaseDetail::getCRoomCode,
- p -> p,
- (existing, replacement) ->{
- LocalDate existingEnd = existing.getCEndDate();
- LocalDate replacementEnd = replacement.getCEndDate();
- if (existingEnd == null) return replacement;
- if (replacementEnd == null) return existing;
- return existingEnd.isAfter(replacementEnd) ? existing : replacement;
- }
- ))
- .values());
- Map<String,BigDecimal> p18EnterpriseEconomicMap = mergeP118EnterpriseEconomicMap(needP18EnterpriseEconomic);
- //楼宇/企业/企业经济数据遍历,全镇级/产业园级/楼栋级一并处理
- //遍历并计算企业相关的结果
- countEnterprise(allEnterprise, enterpriseEconomicMap, now);
- //遍历计算产业园相关
- countIndustrialPark(allIndustrialPark,p18EnterpriseEconomicMap);
- //企呼我应
- countOrder(allOrder);
- //租赁信息相关统计
- countLease(lastLeaseDetail,now);
- countRoomStatusByLease(lastLeaseDetail, now);
- countHistoryData(allEnterprise, allIndustrialPark, lastYearLease, now);
- cacheTownData = tempCacheTownData;
- cacheParkData = tempCacheParkData;
- cacheBuildData = tempCacheBuildData;
- }
- /**
- * 合并18项经济指标数据为营收数据,按企业合并
- * @param needP18EnterpriseEconomic 18项经济指标数据
- * @return 企业编码->营收数据
- */
- private Map<String, BigDecimal> mergeP118EnterpriseEconomicMap(List<P18EnterpriseEconomic> needP18EnterpriseEconomic) {
- return needP18EnterpriseEconomic.stream()
- .collect(Collectors.groupingBy(P18EnterpriseEconomic::getCEnterpriseCode))
- .entrySet()
- .stream()
- .collect(Collectors.toMap(
- Map.Entry::getKey,
- e -> e.getValue().stream()
- .map(this::getRevenue)
- .reduce(BigDecimal::add)
- .orElse(BigDecimal.ZERO)
- ));
- }
- /**
- * 获取营收数据
- */
- private BigDecimal getRevenue(P18EnterpriseEconomic p18){
- return mergeAll(
- p18.getCGsjyyysZf(),
- p18.getCGsrxyys(),
- p18.getCGsrlzyfwyys(),
- p18.getCGshzyys(),
- p18.getCGskjfwyys(),
- p18.getCGswhtyhylyys(),
- p18.getCQtfwy()
- );
- }
- private void countHistoryData(List<Enterprise> allEnterprise, List<IndustrialPark> allIndustrialPark, List<LeaseDetail> lastYearLease, LocalDate now) {
- //历史企业总数统计
- //历史数据,所以不论企业现状如何
- Map<String, List<Enterprise>> enterpriseMap = TimeUtil.split(allEnterprise, now);
- Map<String, Integer> enterpriseByMonth = enterpriseMap.entrySet()
- .stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size()));
- //历史异地企业统计
- //同上
- Map<String, List<Enterprise>> enterpriseInTownMap = TimeUtil.split(allEnterprise
- .stream()
- .filter(e -> Objects.equals(e.getCSsxqCode(), "#YIDIQIYE"))
- .collect(Collectors.toList()), now);
- Map<String, Integer> enterpriseInTownByMonth = enterpriseInTownMap.entrySet()
- .stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size()));
- //历史房间总数统计
- Map<String, List<IndustrialPark>> industrialParkMap = TimeUtil.split(allIndustrialPark
- .stream()
- .filter(e -> Objects.equals(e.getCResourceType(), "1"))
- .collect(Collectors.toList()),
- now);
- Map<String, Integer> roomByMonth = industrialParkMap.entrySet().stream().collect(
- Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size())
- );
- //历史已使用房间总数统计
- Map<String, List<LeaseDetail>> leaseMap = TimeUtil.split(lastYearLease, now);
- Map<String, Long> leaseByMonth = leaseMap.entrySet()
- .stream()
- .collect(
- Collectors.toMap(
- Map.Entry::getKey,
- e -> {
- Set<String> parkCodes = industrialParkMap.getOrDefault(e.getKey(), new ArrayList<>())
- .stream()
- .map(IndustrialPark::getCParkCode)
- .collect(Collectors.toSet());
- return e.getValue()
- .stream()
- .map(LeaseDetail::getCRoomCode)
- .filter(parkCodes::contains)
- .distinct()
- .count();
- }
- )
- );
- tempCacheTownData.put("enterpriseByMonth", enterpriseByMonth);
- tempCacheTownData.put("enterpriseInTownByMonth", enterpriseInTownByMonth);
- tempCacheTownData.put("leaseByMonth", leaseByMonth);
- tempCacheTownData.put("roomByMonth", roomByMonth);
- }
- private void countIndustrialPark(List<IndustrialPark> allIndustrialPark, Map<String, BigDecimal> p18EnterpriseEconomicMap) {
- //简单统计并按父级编码建映射
- simpleCountPark(allIndustrialPark);
- Map<EnterpriseLevel, Map<String, Integer>> enterPriceLevelCountByPark = new HashMap<>();
- for (EnterpriseLevel value : EnterpriseLevel.values()) {
- enterPriceLevelCountByPark.put(value, new HashMap<>());
- }
- //按楼宇父子关系统计
- for (IndustrialPark industrialPark : allPark) {
- String parkCode = industrialPark.getCParkCode();
- List<IndustrialPark> allBuild;
- List<IndustrialPark> allRoom = new ArrayList<>();
- BigDecimal allTaxByYtdThisYear = BigDecimal.ZERO;
- BigDecimal allGsProductByYtdThisYear = BigDecimal.ZERO;
- BigDecimal allTaxByYtdLastYear = BigDecimal.ZERO;
- BigDecimal allGsProductByYtdLastYear = BigDecimal.ZERO;
- int enterPriceCount = 0;
- int enterPriceGsCount = 0;
- Set<String> inParkEnterpriseCode = new HashSet<>();
- List<String> aliveEnterprise = new ArrayList<>();
- List<String> gsEnterprise = new ArrayList<>();
- int usedRoomCount = 0;
- allBuild = buildMap.get(parkCode);
- if (allBuild == null) {
- allBuild = new ArrayList<>();
- }
- //楼栋
- for (IndustrialPark build : allBuild) {
- String buildCode = build.getCParkCode();
- Set<String> buildEnterpriseCode = new HashSet<>();
- JSONObject buildData = new JSONObject();
- int buildRoomCount = 0;
- int buildUsedRoomCount = 0;
- JSONArray floors = new JSONArray();
- List<IndustrialPark> partFloor = floorMap.get(buildCode);
- if (partFloor == null) {
- partFloor = new ArrayList<>();
- }
- //楼层
- for (IndustrialPark floor : partFloor) {
- String floorCode = floor.getCParkCode();
- JSONObject floorData = new JSONObject();
- int floorRoomCount = 0;
- int floorUsedRoomCount = 0;
- List<IndustrialPark> partRoom = roomMap.get(floorCode);
- if (partRoom == null) {
- partRoom = new ArrayList<>();
- }
- //房间
- allRoom.addAll(partRoom);
- for (IndustrialPark room : partRoom) {
- buildRoomCount++;
- floorRoomCount++;
- String enterpriseCode = room.getCZlqydm();
- if (enterpriseCode != null) {
- usedRoomCount++;
- buildUsedRoomCount++;
- floorUsedRoomCount++;
- inParkEnterpriseCode.add(enterpriseCode);
- buildEnterpriseCode.add(enterpriseCode);
- }
- }
- floorData.put("floorCode", floorCode);
- floorData.put("roomCount", floorRoomCount);
- floorData.put("usedRoomCount", floorUsedRoomCount);
- floorData.put("buildingAreaCount", floor.getCBuildingArea());
- floorData.put("vacantAreaCount", floor.getCVacantArea());
- floorData.put("floorName", floor.getCResourceName());
- floors.add(floorData);
- }
- int buildEnterpriseCount = 0;
- int buildGsEnterpriseCount = 0;
- for (String enterpriseCode : buildEnterpriseCode) {
- boolean isGsqy = false;
- if (aliveEnterpriseMap.containsKey(enterpriseCode)) {
- Enterprise enterprise = aliveEnterpriseMap.get(enterpriseCode);
- if ("2".equals(enterprise.getIsGsqy())) {
- isGsqy = true;
- }
- }
- buildEnterpriseCount++;
- if (isGsqy) {
- buildGsEnterpriseCount++;
- }
- }
- buildData.put("enterpriseCount", buildEnterpriseCount);
- buildData.put("gsEnterpriseCount", buildGsEnterpriseCount);
- buildData.put("floorCount", partFloor.size());
- buildData.put("roomCount", buildRoomCount);
- buildData.put("usedRoomCount", buildUsedRoomCount);
- buildData.put("buildingAreaCount", build.getCBuildingArea());
- buildData.put("vacantAreaCount", build.getCVacantArea());
- buildData.put("floors", floors);
- tempCacheBuildData.put(buildCode, buildData);
- }
- for (EnterpriseLevel value : EnterpriseLevel.values()) {
- enterPriceLevelCountByPark.get(value).put(industrialPark.getCResourceName(), 0);
- }
- Map<String, Integer> enterPriceIndustryCount = new HashMap<>();
- for (String enterpriseCode : inParkEnterpriseCode) {
- boolean isGsqy = false;
- String nationalIndustryCode = null;
- if (aliveEnterpriseMap.containsKey(enterpriseCode)) {
- Enterprise enterprise = aliveEnterpriseMap.get(enterpriseCode);
- isGsqy = "2".equals(enterprise.getIsGsqy());
- nationalIndustryCode = enterprise.getCNationalIndustryCode();
- EnterpriseLevel.ofJsonValue(enterprise.getCompanyLevel()).ifPresent(level -> {
- Map<String, Integer> map = enterPriceLevelCountByPark.get(level);
- map.put(industrialPark.getCResourceName(), map.get(industrialPark.getCResourceName()) + 1);
- });
- EnterpriseEconomicCount ytdThisYear = economicCountByYtdThisYear.get(enterpriseCode);
- EnterpriseEconomicCount ytdLastYear = economicCountByYtdLastYear.get(enterpriseCode);
- allTaxByYtdThisYear = allTaxByYtdThisYear.add(ytdThisYear.tax);
- allTaxByYtdLastYear = allTaxByYtdLastYear.add(ytdLastYear.tax);
- if (isGsqy) {
- allGsProductByYtdThisYear = allGsProductByYtdThisYear.add(ytdThisYear.product);
- allGsProductByYtdLastYear = allGsProductByYtdLastYear.add(ytdLastYear.product);
- gsEnterprise.add(enterpriseCode);
- }
- aliveEnterprise.add(enterpriseCode);
- }
- enterPriceCount++;
- if (isGsqy) {
- enterPriceGsCount++;
- }
- if (nationalIndustryCode != null) {
- String prefix = nationalIndustryCode.substring(0, 1);
- String nationalIndustry = nationalIndustryMap.get(prefix);
- if (enterPriceIndustryCount.containsKey(nationalIndustry)) {
- enterPriceIndustryCount.put(nationalIndustry, enterPriceIndustryCount.get(nationalIndustry) + 1);
- } else {
- enterPriceIndustryCount.put(nationalIndustry, 1);
- }
- }
- }
- JSONObject parkDataCount = new JSONObject();
- parkDataCount.put("totalTax", allTaxByYtdThisYear);
- parkDataCount.put("totalGsOutput", allGsProductByYtdThisYear);
- parkDataCount.put("totalTaxLastYear", allTaxByYtdThisYear);
- parkDataCount.put("totalGsOutputLastYear", allGsProductByYtdThisYear);
- parkDataCount.put("buildCount", allBuild.size());
- parkDataCount.put("officeCount", allRoom.size());
- parkDataCount.put("usedRoomCount", usedRoomCount);
- parkDataCount.put("enterpriseCount", enterPriceCount);
- parkDataCount.put("enterpriseGsCount", enterPriceGsCount);
- parkDataCount.put("parkVacantArea", industrialPark.getCVacantArea());
- parkDataCount.put("enterPriseIndustry", enterPriceIndustryCount);
- JSONArray topRevenue = new JSONArray();
- List<String> sortedEnterprise = gsEnterprise
- .stream()
- .sorted(Comparator.comparing(p18EnterpriseEconomicMap::get,Comparator.nullsFirst(BigDecimal::compareTo)).reversed())
- .limit(10).collect(Collectors.toList());
- for (String enterPriseCode : sortedEnterprise) {
- JSONObject outPut = new JSONObject();
- Enterprise enterPrice = aliveEnterpriseMap.get(enterPriseCode);
- BigDecimal product =nullToZero( p18EnterpriseEconomicMap.get(enterPriseCode));
- outPut.put("enterPriceCode", enterPriseCode);
- outPut.put("enterPriceName", enterPrice.getCEnterpriseName());
- if (product.compareTo(BigDecimal.ZERO)==0){
- outPut.put("enterPriceRevenue","暂无");
- }else {
- outPut.put("enterPriceRevenue",product);
- }
- topRevenue.add(outPut);
- }
- parkDataCount.put("topRevenue", topRevenue);
- tempCacheParkData.put(parkCode, parkDataCount);
- for (EnterpriseLevel level : EnterpriseLevel.values()) {
- Map<String, Integer> map = enterPriceLevelCountByPark.get(level);
- Map<String, Integer> sortedDesc = map.entrySet()
- .stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
- .limit(10)
- .collect(Collectors.toMap(
- Map.Entry::getKey,
- Map.Entry::getValue,
- (e1, e2) -> e1,
- LinkedHashMap::new
- ));
- enterPriceLevelCountByPark.put(level, sortedDesc);
- }
- tempCacheTownData.put("enterpriseLevelCountByPark", enterPriceLevelCountByPark);
- }
- }
- private void simpleCountPark(List<IndustrialPark> allIndustrialPark) {
- int parkCount = 0;
- Map<String, Integer> parkTypeCount = new HashMap<>();
- for (IndustrialPark industrialPark : allIndustrialPark) {
- try {
- int type = Integer.parseInt(industrialPark.getCResourceType());
- switch (type) {
- case 1: {
- String parentCode = industrialPark.getCParentCode();
- roomMap.add(parentCode, industrialPark);
- break;
- }
- case 2: {
- String parentCode = industrialPark.getCParentCode();
- floorMap.add(parentCode, industrialPark);
- break;
- }
- case 3: {
- String parentCode = industrialPark.getCParentCode();
- buildMap.add(parentCode, industrialPark);
- break;
- }
- case 4: {
- parkCount++;
- allPark.add(industrialPark);
- if (parkTypeCount.containsKey(industrialPark.getCLandUse())) {
- parkTypeCount.put(industrialPark.getCLandUse(), parkCount + 1);
- } else {
- parkTypeCount.put(industrialPark.getCLandUse(), 1);
- }
- }
- default: {
- //do nothing
- }
- }
- } catch (Exception e) {
- //nothing
- }
- }
- BigDecimal totalFloorArea = allPark.stream().map(IndustrialPark::getCFloorArea).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
- BigDecimal totalBuildArea = allPark.stream().map(IndustrialPark::getCBuildingArea).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
- BigDecimal totalVacantArea = allPark.stream().map(IndustrialPark::getCVacantArea).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
- tempCacheTownData.put("parkCount", parkCount);
- tempCacheTownData.put("parkType", parkTypeCount);
- tempCacheTownData.put("areaBuild", totalBuildArea);
- tempCacheTownData.put("areaFloor", totalFloorArea);
- tempCacheTownData.put("areaVacant", totalVacantArea);
- tempCacheTownData.put("areaUsed", totalBuildArea.subtract(totalVacantArea));
- }
- private void countEnterprise(List<Enterprise> allEnterprise, Map<String, List<EnterpriseEconomic>> enterpriseEconomicMap, LocalDate now) {
- int inTownCount = 0;
- int gsqyCount = 0;
- int aliveCount = 0;
- Map<String, Integer> industryCount = new HashMap<>();
- Map<String, Integer> nationalIndustryCount = new HashMap<>();
- Map<String, Integer> regTypeCount = new HashMap<>();
- for (Enterprise enterPrise : allEnterprise) {
- boolean isInTown =Arrays.asList("1","3","4").contains(Optional.ofNullable(enterPrise.getRegType()).orElse("1"));
- boolean isGsqy = "2".equals(Optional.ofNullable(enterPrise.getIsGsqy()).orElse("1"));
- boolean isAlive = true;
- if (isAlive) {
- aliveCount++;
- if (isInTown) {
- inTownCount++;
- }
- if (isGsqy) {
- gsqyCount++;
- }
- String nationalIndustryCode = enterPrise.getCNationalIndustryCode();
- String mergedNationalIndustry = null;
- if (nationalIndustryCode!=null&& !nationalIndustryCode.isEmpty()) {
- String prefix = nationalIndustryCode.substring(0, 1);
- String industry = nationalIndustryMap.get(prefix);
- if (industryCount.containsKey(industry)) {
- industryCount.put(industry, industryCount.get(industry) + 1);
- } else {
- industryCount.put(industry, 1);
- }
- mergedNationalIndustry = nationalIndustryToMerged.get(prefix);
- }
- if (mergedNationalIndustry==null)mergedNationalIndustry = "其他";
- if (nationalIndustryCount.containsKey(mergedNationalIndustry)) {
- nationalIndustryCount.put(mergedNationalIndustry, nationalIndustryCount.get(mergedNationalIndustry) + 1);
- } else {
- nationalIndustryCount.put(mergedNationalIndustry, 1);
- }
- String regType = enterPrise.getRegType();
- String regTypeChinese = null;
- if (regType!=null&& !regType.isEmpty()){
- regTypeChinese = regTypeMap.get(regType);
- }
- if (regTypeChinese == null) regTypeChinese = "其他";
- if (regTypeCount.containsKey(regTypeChinese)) {
- regTypeCount.put(regTypeChinese, regTypeCount.get(regTypeChinese) + 1);
- } else {
- regTypeCount.put(regTypeChinese, 1);
- }
- String enterPriceCode = enterPrise.getCUnifiedSocialCreditCode();
- if (enterPriceCode != null) {
- aliveEnterpriseMap.put(enterPriceCode, enterPrise);
- List<EnterpriseEconomic> enterpriseEconomicList = enterpriseEconomicMap.get(enterPriceCode);
- List<EnterpriseEconomic> economicBy12Month = new ArrayList<>();
- List<EnterpriseEconomic> economicByYtdThisYear = new ArrayList<>();
- List<EnterpriseEconomic> economicByYtdLastYear = new ArrayList<>();
- List<String> Last12Month = SpecialisationStringCollector.YearMonthByLast12Month(now);
- List<String> ytdThisYear = SpecialisationStringCollector.YearMonthByYtd(now);
- List<String> ytdLastYear = SpecialisationStringCollector.YearMonthByYtd(1, now);
- Optional.ofNullable(enterpriseEconomicList).orElse(new ArrayList<>()).stream()
- .filter(e -> !"4".equals(e.getCDataStatus()))
- .forEach(e -> {
- String time = e.getCYearMonth();
- if (Last12Month.contains(time)) {
- economicBy12Month.add(e);
- }
- if (ytdThisYear.contains(time)) {
- economicByYtdThisYear.add(e);
- }
- if (ytdLastYear.contains(time)) {
- economicByYtdLastYear.add(e);
- }
- });
- economicCountBy12Month.put(enterPriceCode, new EnterpriseEconomicCount(economicBy12Month));
- economicCountByYtdThisYear.put(enterPriceCode, new EnterpriseEconomicCount(economicByYtdThisYear));
- economicCountByYtdLastYear.put(enterPriceCode, new EnterpriseEconomicCount(economicByYtdLastYear));
- }
- }
- }
- tempCacheTownData.put("enterPriseCount", aliveCount);
- tempCacheTownData.put("enterPriseSdCount", inTownCount);
- tempCacheTownData.put("enterPriseGsCount", gsqyCount);
- tempCacheTownData.put("enterPriseIndustry", industryCount);
- tempCacheTownData.put("mergedEnterPriseIndustry", nationalIndustryCount);
- tempCacheTownData.put("enterPriseRegType", regTypeCount);
- }
- private void countOrder(List<Order> allOrder) {
- int orderComplete = 0;
- int orderCount = 0;
- for (Order order : allOrder) {
- String status = order.getStatus();
- switch (status) {
- case "3": {
- orderComplete++;
- //就是没有break,需要它自然下落
- }
- case "1":
- case "2": {
- orderCount++;
- }
- case "4":
- default: {
- //doNothing
- }
- }
- }
- tempCacheTownData.put("completeOrder", orderComplete);
- tempCacheTownData.put("orderCount", orderCount);
- }
- private void countLease(List<LeaseDetail> allLease,LocalDate now) {
- List<LeaseDetail> timeoutIn1Month = new ArrayList<>();
- List<LeaseDetail> timeoutIn3Month = new ArrayList<>();
- for (LeaseDetail lease : allLease) {
- if (lease.getCEndDate() != null) {
- if (lease.getCEndDate().isAfter(now)) {
- if (lease.getCEndDate().isBefore(now.plusMonths(1))) {
- timeoutIn1Month.add(lease);
- } else if (lease.getCEndDate().isBefore(now.plusMonths(3))) {
- timeoutIn3Month.add(lease);
- }
- }
- }
- }
- timeoutIn1Month.sort(Comparator.comparing(LeaseDetail::getCEndDate));
- timeoutIn3Month.sort(Comparator.comparing(LeaseDetail::getCEndDate));
- List<LeaseDetail> sortedLeaseTimeout = Stream.concat(timeoutIn1Month.stream(), timeoutIn3Month.stream())
- .sorted(Comparator.comparing(LeaseDetail::getCEndDate))
- .limit(25)
- .collect(Collectors.toList());
- tempCacheTownData.put("leaseTimeoutIn1Month", timeoutIn1Month.size());
- tempCacheTownData.put("leaseTimeoutIn3Month", timeoutIn3Month.size());
- tempCacheTownData.put("leaseTimeout", sortedLeaseTimeout);
- }
- private void countRoomStatusByLease(List<LeaseDetail> allLease, LocalDate now) {
- Map<String, LeaseDetail> leaseMap = allLease.stream().collect(
- Collectors.toMap(
- LeaseDetail::getCRoomCode,
- v -> v
- )
- );
- String vacant = "vacant";
- String expiringSoon = "expiringSoon";
- String utilized = "utilized";
- String reserved = "reserved";
- String terminated = "terminated";
- String[] buckets = new String[]{vacant, expiringSoon, utilized, reserved, terminated};
- Map<String, List<IndustrialPark>> roomBuckets = new HashMap<>();
- for (String bucket : buckets) {
- roomBuckets.put(bucket, new ArrayList<>());
- }
- for (List<IndustrialPark> roomList : roomMap.values()) {
- for (IndustrialPark room : roomList) {
- LeaseDetail lease = leaseMap.get(room.getCParkCode());
- if (lease == null) {
- roomBuckets.get(vacant).add(room);
- continue;
- }
- if (!lease.getCEndDate().isBefore(now) && !lease.getCEndDate().isAfter(now.plusMonths(3))) {
- roomBuckets.get(expiringSoon).add(room);
- continue;
- }
- if (!lease.getCEndDate().isBefore(now) && !lease.getCStartDate().isAfter(now)) {
- roomBuckets.get(utilized).add(room);
- continue;
- }
- if (lease.getCStartDate().isAfter(now)) {
- roomBuckets.get(reserved).add(room);
- continue;
- }
- if (!lease.getCEndDate().plusMonths(1).isBefore(now)) {
- roomBuckets.get(terminated).add(room);
- continue;
- }
- roomBuckets.get(vacant).add(room);
- }
- }
- tempCacheTownData.put("leaseVacantRoomCount", roomBuckets.get(vacant).size());
- tempCacheTownData.put("leaseVacantAreaCount", roomBuckets.get(vacant)
- .stream()
- .map(IndustrialPark::getCBuildingArea)
- .filter(Objects::nonNull)
- .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
- tempCacheTownData.put("leaseExpiringSoonRoomCount", roomBuckets.get(expiringSoon).size());
- tempCacheTownData.put("leaseExpiringSoonAreaCount", roomBuckets.get(expiringSoon)
- .stream()
- .map(IndustrialPark::getCBuildingArea)
- .filter(Objects::nonNull)
- .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
- tempCacheTownData.put("leaseUtilizedRoomCount", roomBuckets.get(utilized).size());
- tempCacheTownData.put("leaseUtilizedAreaCount", roomBuckets.get(utilized)
- .stream()
- .map(IndustrialPark::getCBuildingArea)
- .filter(Objects::nonNull)
- .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
- tempCacheTownData.put("leaseReservedRoomCount", roomBuckets.get(reserved).size());
- tempCacheTownData.put("leaseReservedAreaCount", roomBuckets.get(reserved)
- .stream()
- .map(IndustrialPark::getCBuildingArea)
- .filter(Objects::nonNull)
- .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
- tempCacheTownData.put("leaseTerminatedRoomCount", roomBuckets.get(terminated).size());
- tempCacheTownData.put("leaseTerminatedAreaCount", roomBuckets.get(terminated)
- .stream()
- .map(IndustrialPark::getCBuildingArea)
- .filter(Objects::nonNull)
- .reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
- }
- public JSONObject getTownData() {
- tryCount();
- return cacheTownData;
- }
- public JSONObject getParkData(String parkCode) {
- tryCount();
- return cacheParkData.get(parkCode);
- }
- public JSONObject getBuildData(String buildCode) {
- tryCount();
- return cacheBuildData.get(buildCode);
- }
- private static BigDecimal nullToZero(BigDecimal value){
- return value == null ? BigDecimal.ZERO : value;
- }
- private static BigDecimal mergeAll(BigDecimal... values){
- return Arrays.stream(values)
- .map(DataCountService::nullToZero)
- .reduce(BigDecimal::add)
- .orElse(BigDecimal.ZERO);
- }
- }
|