Browse Source

定时任务

Jxing 6 năm trước cách đây
mục cha
commit
c31e8282ab

+ 162 - 16
src/main/java/com/sagacloud/route/processors/PeriodTask/StatisticsProcessor.java

@@ -4,38 +4,184 @@ package com.sagacloud.route.processors.PeriodTask;
  * Create Time: 2018/7/13
  */
 
+import com.alibaba.fastjson.JSONObject;
 import com.sagacloud.pojos.DPSelectPropertyResult;
+import com.sagacloud.pojos.ProjectStatistics;
 import com.sagacloud.pojos.VenderStatistics;
+import com.sagacloud.route.InitEnvRoute;
+import com.sagacloud.route.processors.Insurance.GetPropertyUnderWarrantyProcessor;
+import com.sagacloud.utils.Const;
 import com.sagacloud.utils.VendersUtil;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 public class StatisticsProcessor implements Processor {
-    public static final Set<String> manufacturerNameSign = VendersUtil.createSet("Manufacturer", "Brand", "Specification");
-    public static final Set<String> manufacturerIdSign = VendersUtil.createSet("DPManufacturerID", "DPSpecificationID");
-    public static final Set<String> supplierNameSign = VendersUtil.createSet("Supplier");
-    public static final Set<String> supplierIdSign = VendersUtil.createSet("DPSupplierID");
-    public static final Set<String> insuranceNameSign = VendersUtil.createSet("Insurer", "InsuranceNum");
-    public static final Set<String> insuranceIdSign = VendersUtil.createSet("DPInsurerID");
-    public static final Set<String> maintainanceNameSign = VendersUtil.createSet("Maintainer");
-    public static final Set<String> maintainanceIdSign = VendersUtil.createSet("DPMaintainerID");
+    public static final Set<String> manufacturerSign = VendersUtil.createSet("Manufacturer", "Brand", "Specification", "DPManufacturerID", "DPSpecificationID");
+    public static final Set<String> supplierSign = VendersUtil.createSet("Supplier", "DPSupplierID");
+    public static final Set<String> insuranceSign = VendersUtil.createSet("Insurer", "InsuranceNum", "DPInsurerID");
+    public static final Set<String> maintainanceSign = VendersUtil.createSet("Maintainer", "DPMaintainerID");
     @Override
     public void process(Exchange exchange) throws Exception {
         String projectId = exchange.getIn().getBody(String.class);
         DPSelectPropertyResult propertyData = (DPSelectPropertyResult) exchange.getProperty("propertyData");
-        Map<String, String> idMap = (Map<String, String>) exchange.getProperty("idMap");
-        Map<String, Map<String, String>> nameMap = (Map<String, Map<String, String>>) exchange.getProperty("nameMap");
+        Map<String, Map<String, String>> infoValueMap = (Map<String, Map<String, String>>) exchange.getProperty("map");  // 信息点Code --> (厂商库在该信息点的值value --> vender DPID)
         List<Map<String, Object>> propertyList = propertyData.getContent();
-
+        Map<String, VenderStatistics> venderStatisticsMap = new HashMap<>();
+        Map<String, Set<String>> contracts = new HashMap<>(); // 保险商统计供应合同数目使用
+        Map<String, Set<String>> warranties = new HashMap<>(); // 保险商统计供应合同数目使用
         for(int i = 0; i < propertyList.size(); ++i){
             Map<String, Object> singleProperty = propertyList.get(i);
-            //Map<String, VenderStatistics>
-            //compareCount(idMap, nameMap, manufacturerIdSign, manufacturerNameSign);
+            matchCountManufacturer(singleProperty, infoValueMap, manufacturerSign, venderStatisticsMap);
+            matchCountSupplier(singleProperty, infoValueMap, supplierSign, venderStatisticsMap, contracts);
+            matchCountInsurer(singleProperty, infoValueMap, insuranceSign, venderStatisticsMap, warranties);
+            matchCountMaintainance(singleProperty, infoValueMap, maintainanceSign, venderStatisticsMap);
+        }
+        addContractCount(venderStatisticsMap, contracts);
+        addContractCount(venderStatisticsMap, warranties);
+        ProjectStatistics ps = new ProjectStatistics();
+        ps.setCriterias((List<VenderStatistics>) venderStatisticsMap.values());
+        ps.setProjectId(projectId);
+        exchange.getIn().setBody(JSONObject.toJSONString(ps));
+        exchange.getIn().setHeader(Exchange.HTTP_URI, String.join("", InitEnvRoute.venders, "/auxiliary/property/create_by_project?method=POST"));
+    }
+
+    private void addContractCount(Map<String, VenderStatistics> venderStatisticsMap, Map<String, Set<String>> contracts) {
+        for(String venderId : contracts.keySet()){
+            updateCount(venderStatisticsMap, venderId, 0, contracts.get(venderId).size(), 0);
         }
+    }
+
+    private void matchCountMaintainance(Map<String, Object> singleProperty, Map<String, Map<String, String>> infoValueMap, Set<String> maintainanceSign, Map<String, VenderStatistics> venderStatisticsMap) {
+        try {
+            Map<String, Object> propertyInfos = (Map<String, Object>) singleProperty.get("infos");
+            for(String idInfoCode : maintainanceSign){
+                Map<String, String> valueMap = infoValueMap.get(idInfoCode);
+                if(propertyInfos.get(idInfoCode) == null || valueMap == null)
+                    continue;
+                String infoValue = propertyInfos.get(idInfoCode).toString();   //当前信息点的值
+                if(valueMap.containsKey(infoValue)){
+                    // 匹配由此保险商保的资产
+                    String venderId = valueMap.get(infoValue);
+                    if(propertyInfos.get(Const.MTN_DEADLINE) != null) {
+                        String deadline = propertyInfos.get(Const.MTN_DEADLINE).toString();
+                        try{  // 比较deadline 是否是快过期的
+                            Calendar c = Calendar.getInstance();
+                            c.add(Calendar.MONTH, 6);
+                            Date now = new Date();
+                            Date upcomingDate = c.getTime();
+                            Date expireDate = GetPropertyUnderWarrantyProcessor.sdf.parse(deadline);
+                            if(expireDate.compareTo(upcomingDate) >= 0){
+                                updateCount(venderStatisticsMap, venderId, 1, 0, 0);
+                            }else if(expireDate.compareTo(now) >= 0){
+                                updateCount(venderStatisticsMap, venderId, 0, 0, 1);
+                            }
+                        }catch (Exception ignored){
+                            updateCount(venderStatisticsMap, venderId, 0, 0, 1);
+                        }
+                        return;
+                    }else    // 如果没有填时间, 则统计在即将超出维保时间的资产中
+                    {
+                        updateCount(venderStatisticsMap, venderId, 0, 0, 1);
+                        return;
+                    }
+                }
+            }
+        }catch (Exception ignored){}
+    }
+
+    // InsuranceNum 在infoValueMap里面保存的已经是所有的在保保单号, 因此匹配到资产之后必须验证资产保单号是否属于在保保单
+    private void matchCountInsurer(Map<String, Object> singleProperty, Map<String, Map<String, String>> infoValueMap, Set<String> insuranceSign, Map<String, VenderStatistics> venderStatisticsMap, Map<String, Set<String>> warranties) {
+        try {
+            Map<String, Object> propertyInfos = (Map<String, Object>) singleProperty.get("infos");
+            for(String idInfoCode : insuranceSign){
+                Map<String, String> valueMap = infoValueMap.get(idInfoCode);
+                if(propertyInfos.get(idInfoCode) == null || valueMap == null)
+                    continue;
+                String infoValue = propertyInfos.get(idInfoCode).toString();   //当前信息点的值
+                if(valueMap.containsKey(infoValue)){
+                    // 匹配由此保险商保的资产
+                    if(propertyInfos.get(Const.INFO_INSURANCENUM) != null) {
+                        String insuranceNum = propertyInfos.get(Const.INFO_INSURANCENUM).toString();
+                        Map<String, String> insuranceNumMap = infoValueMap.get(Const.INFO_INSURANCENUM);
+                        if (insuranceNumMap.containsKey(insuranceNum)) {
+                            String venderId = insuranceNumMap.get(insuranceNum);
+                            Set<String> insuranceSet = null;
+                            if (warranties.get(venderId) == null) {
+                                insuranceSet = new HashSet<>();
+                                warranties.put(venderId, insuranceSet);
+                            } else
+                                insuranceSet = warranties.get(venderId);
+                            if (!insuranceSet.contains(insuranceNum))
+                                insuranceSet.add(insuranceNum);
+                            updateCount(venderStatisticsMap, venderId, 1, 0, 0);
+                        }
+                        return;
+                    }
+                }
+            }
+        }catch (Exception ignored){}
+    }
 
+    private void matchCountSupplier(Map<String, Object> singleProperty, Map<String, Map<String, String>> infoValueMap, Set<String> supplierSign, Map<String, VenderStatistics> venderStatisticsMap, Map<String, Set<String>> contracts) {
+        try {
+            Map<String, Object> propertyInfos = (Map<String, Object>) singleProperty.get("infos");
+            for(String idInfoCode : supplierSign){
+                Map<String, String> valueMap = infoValueMap.get(idInfoCode);
+                if(propertyInfos.get(idInfoCode) == null || valueMap == null)
+                    continue;
+                String infoValue = propertyInfos.get(idInfoCode).toString();
+                if(valueMap.containsKey(infoValue)){
+                    // 匹配由此供应商提供的资产
+                    String venderId = valueMap.get(infoValue);
+                    if(propertyInfos.get(Const.INFO_SUPPLIERCONTRACTID) != null){
+                        String contractNo = propertyInfos.get(Const.INFO_SUPPLIERCONTRACTID).toString();
+                        Set<String> contractSet = null;
+                        if(contracts.get(venderId) == null){
+                            contractSet = new HashSet<>();
+                            contracts.put(venderId, contractSet);
+                        }else
+                            contractSet = contracts.get(venderId);
+                        if(!contractSet.contains(contractNo))
+                            contractSet.add(contractNo);
+                    }
+                    updateCount(venderStatisticsMap, venderId, 1, 0, 0);
+                    return;
+                }
+            }
+        }catch (Exception ignored){}
+    }
+
+    private void matchCountManufacturer(Map<String, Object> singleProperty, Map<String, Map<String, String>> infoValueMap, Set<String> venderSign, Map<String, VenderStatistics> venderStatisticsMap) {
+        try {
+            Map<String, Object> propertyInfos = (Map<String, Object>) singleProperty.get("infos");
+            for(String idInfoCode : venderSign){
+                Map<String, String> valueMap = infoValueMap.get(idInfoCode);
+                if(propertyInfos.get(idInfoCode) == null || valueMap == null)
+                    continue;
+                String infoValue = propertyInfos.get(idInfoCode).toString();
+                if(valueMap.containsKey(infoValue)){
+                    // 匹配成功, 由此生产商生产
+                    String venderId = valueMap.get(infoValue);
+                    updateCount(venderStatisticsMap, venderId, 1, 0, 0);
+                    return;
+                }
+            }
+        }catch (Exception ignored){}
+    }
+    private void updateCount(Map<String, VenderStatistics> venderStatisticsMap, String venderId, int totalCount, int contractCount, int nearlyExpireCount){
+        VenderStatistics statistics = null;
+        if(venderStatisticsMap.get(venderId) == null){
+            statistics = new VenderStatistics();
+            statistics.setVenderId(venderId);
+            venderStatisticsMap.put(venderId, statistics);
+        }else
+            statistics = venderStatisticsMap.get(venderId);
+        if(statistics == null)
+            return;
+        statistics.setContractCount(statistics.getContractCount() + contractCount);
+        statistics.setNearlyExpireCount(statistics.getNearlyExpireCount() + nearlyExpireCount);
+        statistics.setTotalCount(statistics.getTotalCount() + totalCount);
     }
 }