Java 8マルチフィールドグループ統計によると、インスタンスを求めます。

19673 ワード

1.この実例は簡単なsqlで実現できます。  私達のプロジェクトのデータベースには時間が13桁のタイムスタンプが保存されています。    したがって、日付書式に変換しなければなりません。  グループ化することができます   以下のとおりです
SELECT    count(1)シンプルNumber、    SUM(penaltyual mount)AS simplePenaltyAmount、    Handling_department、    クリアードタイム、LEFT(FROMUUNIXTIME(LEFT(createmutime,10))、FROM    tう。case_シンプル_case WHERE    1=1 GROUTP BY Handling_department、LEFT(FROMUUNIXTIME(LEFT(createmutime、10)、7)
以上のものが多フィールドの求めや統計などの機能を実現できます。  でも、うちのボスは速度を調べたり、在庫を換えたりすることを考えています。    FROMを使わないように提案します。UNIXTIME()関数なので、一番愚かな方法で一歩ずつ実現するしかないです。   以下はJava 8方式で実現したものです。  コードは以下の通りです
@Override
public  List queryFirstCase(String startTime,String condition,String caseType) {
    List statisticalAnalyses = null;
    //    
    if (caseType.equals(CaseTypeEnum.SIMPLETYPE.code())) {
        statisticalAnalyses = statisticalAnalysisDao.querySimpleData();
    }
    //    
    if (caseType.equals(CaseTypeEnum.NORMALTYPE.code())) {
        statisticalAnalyses = statisticalAnalysisDao.queryNormalData();
    }
    for (StatisticalAnalysis analysis : statisticalAnalyses) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String newCreateTime = sdf.format(new Date(Long.valueOf(analysis.getCreateTime())));
            analysis.setCreateTime(newCreateTime);
            SysOrg sysOrg = commonSearchDao.findByOrgId(analysis.getHandlingDepartment());
            if (sysOrg != null) {
                analysis.setHandlingDepartmentName(sysOrg.getOrgName());
            }
            if(analysis.getSimplePenaltyAmount()==null){
                analysis.setSimplePenaltyAmount(0.0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    List caseTotalList = new ArrayList<>();
    //              
    Map> caseTotal = statisticalAnalyses.stream().collect(Collectors.groupingBy(StatisticalAnalysis::getCreateTime, Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.counting())));
    //              
    Map> sumCase = statisticalAnalyses.stream().collect(Collectors.groupingBy(StatisticalAnalysis::getCreateTime, Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.summingDouble(StatisticalAnalysis::getSimplePenaltyAmount))));
    //                   
    for (Map.Entry> entry : caseTotal.entrySet()) {
        if (entry.getKey().indexOf(startTime) > -1) {
            StatisticalAnalysis statisticalAnalysis = new StatisticalAnalysis();
            statisticalAnalysis.setCreateTime(entry.getKey());
            //   -                    
            String key = entry.getKey().replace("-", "");
            Integer sortNum = Integer.parseInt(key);
            statisticalAnalysis.setSortNum(sortNum);
            Map map = entry.getValue();
            for (Map.Entry entr : map.entrySet()) {
                statisticalAnalysis.setHandlingDepartmentName(entr.getKey());
                statisticalAnalysis.setSimpleNumber(entr.getValue());
            }
            caseTotalList.add(statisticalAnalysis);
        }
    }
    caseTotalList.sort((a, b) -> a.getSortNum() - b.getSortNum());
    //                   
    List analyses = new ArrayList<>();
    for (Map.Entry> entry : sumCase.entrySet()) {
        if (entry.getKey().indexOf(startTime) > -1) {
            StatisticalAnalysis statisticalAnalysis = new StatisticalAnalysis();
            statisticalAnalysis.setCreateTime(entry.getKey());
            //   -                    
            String key = entry.getKey().replace("-", "");
            Integer sortNum = Integer.parseInt(key);
            statisticalAnalysis.setSortNum(sortNum);
            Map map = entry.getValue();
            for (Map.Entry entr : map.entrySet()) {
                statisticalAnalysis.setHandlingDepartmentName(entr.getKey());
                statisticalAnalysis.setSimplePenaltyAmount(entr.getValue());
            }
            analyses.add(statisticalAnalysis);
        }
    }

    analyses.sort((a, b) -> a.getSortNum() - b.getSortNum());
    //                   
    List analysisList = new ArrayList<>();
    for (StatisticalAnalysis analys : caseTotalList) {
        for (StatisticalAnalysis analys2 : analyses) {
            StatisticalAnalysis statisticalAnalysis = new StatisticalAnalysis();
            if (analys.getSortNum().intValue() == analys2.getSortNum().intValue()) {
                statisticalAnalysis.setSimpleNumber(analys.getSimpleNumber());
                statisticalAnalysis.setSimplePenaltyAmount(analys2.getSimplePenaltyAmount());
                statisticalAnalysis.setCreateTime(analys.getCreateTime());
                statisticalAnalysis.setHandlingDepartmentName(analys.getHandlingDepartmentName());
                analysisList.add(statisticalAnalysis);
            }
        }
    }
    List newAnalysisList = new ArrayList<>();
    if (analysisList.size() > 0) {
        //      
        if (condition.equals(YearEnum.FIRST.code())) {
            for (StatisticalAnalysis analysis : analysisList) {
                StatisticalAnalysis analysis1 = new StatisticalAnalysis();
                if (analysis.getCreateTime().indexOf(startTime + "-01") > -1) {
                    BeanUtils.copyProperties(analysis, analysis1);
                    if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                        analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"       ");
                    }else{
                        analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"       ");
                    }
                    newAnalysisList.add(analysis1);
                }
                if (analysis.getCreateTime().indexOf(startTime + "-02") > -1) {
                    BeanUtils.copyProperties(analysis, analysis1);
                    if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                        analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"       ");
                    }else{
                        analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"       ");
                    }
                    newAnalysisList.add(analysis1);
                }
                if (analysis.getCreateTime().indexOf(startTime + "-03") > -1) {
                    BeanUtils.copyProperties(analysis, analysis1);
                    if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                        analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"       ");
                    }else{
                        analysis1.setTitle(startTime+YearEnum.FIRST.desc()+"       ");
                    }
                    newAnalysisList.add(analysis1);
                }
            }
            return newAnalysisList;
    }
    //      
    if (condition.equals(YearEnum.SECOND.code())) {
        for (StatisticalAnalysis analysis : analysisList) {
            StatisticalAnalysis analysis1 = new StatisticalAnalysis();
            if (analysis.getCreateTime().indexOf(startTime + "-04") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-05") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-06") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.SECOND.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
        }
        return newAnalysisList;
    }
    //      
    if (condition.equals(YearEnum.THREE.code())) {
        for (StatisticalAnalysis analysis : analysisList) {
            StatisticalAnalysis analysis1 = new StatisticalAnalysis();
            if (analysis.getCreateTime().indexOf(startTime + "-07") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.THREE.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.THREE.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-08") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.THREE.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.THREE.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-09") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.THREE.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.THREE.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
        }
        return newAnalysisList;
    }
    //      
    if (condition.equals(YearEnum.FOUR.code())) {
        for (StatisticalAnalysis analysis : analysisList) {
            StatisticalAnalysis analysis1 = new StatisticalAnalysis();
            if (analysis.getCreateTime().indexOf(startTime + "-10") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-11") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-12") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.FOUR.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
        }
        return newAnalysisList;
    }
    //            
    if (condition.equals(YearEnum.HALF.code())) {
        for (StatisticalAnalysis analysis : analysisList) {
            StatisticalAnalysis analysis1 = new StatisticalAnalysis();
            if (analysis.getCreateTime().indexOf(startTime + "-01") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-02") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-03") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-04") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-05") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-06") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.HALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
        }
        return newAnalysisList;
    }
    //            
    if (condition.equals(YearEnum.LASTHALF.code())) {
        for (StatisticalAnalysis analysis : analysisList) {
            StatisticalAnalysis analysis1 = new StatisticalAnalysis();
            if (analysis.getCreateTime().indexOf(startTime + "-07") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-08") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-09") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-10") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-11") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
            if (analysis.getCreateTime().indexOf(startTime + "-12") > -1) {
                BeanUtils.copyProperties(analysis, analysis1);
                if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }else{
                    analysis1.setTitle(startTime+YearEnum.LASTHALF.desc()+"       ");
                }
                newAnalysisList.add(analysis1);
            }
        }
        return newAnalysisList;
    }
    if (condition.equals(YearEnum.FULLYEAR.code())) {
        for (StatisticalAnalysis analysis : analysisList) {
            if(caseType.equals(CaseTypeEnum.SIMPLETYPE.code())){
                analysis.setTitle(startTime+YearEnum.FULLYEAR.desc()+"       ");
            }else{
                analysis.setTitle(startTime+YearEnum.FULLYEAR.desc()+"       ");
            }
        }
        return analysisList;
    }
}
    return null;
}