感觉是个人写的最费力的接口,记录一下。

为什么写接口慢……
感觉是个人写的最费力的接口,还不是mybatis框架,手拼sql语句

  * SELECT A.gbcode, D.district_name as gbname, NVL(B.needCount, 0) as needCount, NVL(C.overCount, 0) as overCount
     * FROM 
     *     (SELECT RPAD(SUBSTR(T.pataddress_Gbcode, 1, 4), 9, '0') AS gbcode
     *     FROM QUERY_FOLLOWUP T 
     *     WHERE SUBSTR(T.pataddress_Gbcode, 1, 4) LIKE '32__'
     *     GROUP BY SUBSTR(T.pataddress_Gbcode, 1, 4)) A
     * LEFT JOIN
     *     (SELECT RPAD(SUBSTR(T.pataddress_Gbcode, 1, 4), 9, '0') AS gbcode, COUNT(*) as needCount
     *     FROM QUERY_FOLLOWUP T 
     *     WHERE SUBSTR(T.pataddress_Gbcode, 1, 4) LIKE '32__'
     *     AND T.Next_Time >= to_date('2023-01-01','YYYY-MM-DD') 
     *     AND T.Next_Time <= to_date('2023-12-31','YYYY-MM-DD') 
     *     GROUP BY SUBSTR(T.pataddress_Gbcode, 1, 4)) B
     * ON A.gbcode = B.gbcode
     * LEFT JOIN
     *     (SELECT RPAD(SUBSTR(T.pataddress_Gbcode, 1, 4), 9, '0') AS gbcode, COUNT(*) as overCount
     *     FROM QUERY_FOLLOWUP T 
     *     WHERE SUBSTR(T.pataddress_Gbcode, 1, 4) LIKE '32__'
     *     AND T.Next_Time < SYSDATE
     *     GROUP BY SUBSTR(T.pataddress_Gbcode, 1, 4)) C
     * ON A.gbcode = C.gbcode
     * LEFT JOIN
     *     (SELECT district_code, district_name
     *     FROM t_s_district) D
     * ON A.gbcode = D.district_code
@Override
    public PageBo findNeedToFollow(WorkbenchFlowModuleReqBody reqBody) {
        final StringBuffer buffer = new StringBuffer();

        int cPageNo = reqBody.getCurrentPage();
        int pageSize = reqBody.getPageSize();
        PageBo pageBo = new PageBo(cPageNo, pageSize, true);
        String pataddressGbcode=reqBody.getPataddressGbcode();
        String result = pataddressGbcode.substring(0, 2);
        buffer.append("SELECT A.gbcode" +
                ", D.district_name as gbname" +
                ", NVL(B.needCount, 0) as needCount"+
                ", NVL(C.overCount, 0) as overCount\n" +
                "FROM \n" +
                "    (SELECT RPAD(SUBSTR(T.pataddress_Gbcode, 1, 4), 9, '0') AS gbcode\n" +
                "    FROM QUERY_FOLLOWUP T \n" +
                "    WHERE SUBSTR(T.pataddress_Gbcode, 1, 4) LIKE '"+result+"__'\n" +
                "    GROUP BY SUBSTR(T.pataddress_Gbcode, 1, 4)) A\n" +
                "LEFT JOIN\n" +
                "    (SELECT RPAD(SUBSTR(T.pataddress_Gbcode, 1, 4), 9, '0') AS gbcode, COUNT(*) as needCount\n" +
                "    FROM QUERY_FOLLOWUP T \n" +
                "    WHERE SUBSTR(T.pataddress_Gbcode, 1, 4) LIKE '"+result+"__'\n");
        String currentDay = DateApi.getCurrentDate();//当前时间
        // 获取当前日期
        if (reqBody.getMoudle()!=null) {
            //当前日
            if(reqBody.getMoudle().equals("day")) {
                buffer.append(SymbolApi.TAB).append("and T.Next_Time =to_date('"+currentDay+"','YYYY-MM-DD')");
                System.out.println(buffer);
            //当前周
            } else if (reqBody.getMoudle().equals("week")) {
                String sevenDay = DateApi.getNextNDay(6);// 当前时间+6天
                buffer.append(SymbolApi.TAB).append("and T.Next_Time >= to_date('"+currentDay+"','YYYY-MM-DD') " +
                        "AND T.Next_Time <= to_date('"+sevenDay+"','YYYY-MM-DD')");
            //当前月
            } else if (reqBody.getMoudle().equals("month")) {
                LocalDate nowDate = LocalDate.now();
                //获取当月第一天和最后一天
                LocalDate lastDay = nowDate.with(TemporalAdjusters.lastDayOfMonth());
                LocalDate firstDay = nowDate.with(TemporalAdjusters.firstDayOfMonth());

                buffer.append(SymbolApi.TAB).append("and T.Next_Time >= to_date('"+firstDay+"','YYYY-MM-DD') " +
                        "AND T.Next_Time <= to_date('"+lastDay+"','YYYY-MM-DD')");
                //当前年
            } else if (reqBody.getMoudle().equals("year")) {
                LocalDate nowDate = LocalDate.now();
                // 获取当年第一天和最后一天
                 LocalDate lastYearDay = nowDate.with(TemporalAdjusters.lastDayOfYear());
                 LocalDate firstYearDay = nowDate.with(TemporalAdjusters.firstDayOfYear());
                // System.out.println("当年第一天:"+firstYearDay + ";当月最后一天:" + lastYearDay);
                buffer.append(SymbolApi.TAB).append("and T.Next_Time >= to_date('"+firstYearDay+"','YYYY-MM-DD') " +
                        "AND T.Next_Time <= to_date('"+lastYearDay+"','YYYY-MM-DD')");
            }
        }else{
            //指定时间
            buffer.append(SymbolApi.TAB).append("and T.Next_Time >= to_date('"+reqBody.getNextStartTimeRecently()+"','YYYY-MM-DD')"+
                    "AND T.Next_Time <= to_date('"+reqBody.getNextEndTimeRecently()+"','YYYY-MM-DD')");

        }
        buffer.append(SymbolApi.TAB).append(" GROUP BY SUBSTR(T.pataddress_Gbcode, 1, 4)) B\n" +
                " ON A.gbcode = B.gbcode ");
        buffer.append(SymbolApi.TAB).append("LEFT JOIN\n" +
                "    (SELECT RPAD(SUBSTR(T.pataddress_Gbcode, 1, 4), 9, '0') AS gbcode, COUNT(*) as overCount \n" +
                "    FROM QUERY_FOLLOWUP T \n" +
                "    WHERE SUBSTR(T.pataddress_Gbcode, 1, 4) LIKE'"+result+"__' \n" );
        // 获取当前日期
        if (reqBody.getMoudle()!=null) {
            //当前日
            if(reqBody.getMoudle().equals("day")) {
                buffer.append(SymbolApi.TAB).append("and T.Next_Time +currentDay+"','YYYY-MM-DD') ");
                System.out.println(buffer);
                //当前周
            } else if (reqBody.getMoudle().equals("week")) {
                buffer.append(SymbolApi.TAB).append("and T.Next_Time <= to_date('"+currentDay+"','YYYY-MM-DD') ");
                //当前月
            } else if (reqBody.getMoudle().equals("month")) {
                LocalDate nowDate = LocalDate.now();
                //获取当月第一天和最后一天
                LocalDate firstDay = nowDate.with(TemporalAdjusters.firstDayOfMonth());

                buffer.append(SymbolApi.TAB).append("and T.Next_Time <= to_date('"+firstDay+"','YYYY-MM-DD') ");
                //当前年
            } else if (reqBody.getMoudle().equals("year")) {
                LocalDate nowDate = LocalDate.now();
                // 获取当年第一天和最后一天
                LocalDate firstYearDay = nowDate.with(TemporalAdjusters.firstDayOfYear());
                // System.out.println("当年第一天:"+firstYearDay + ";当月最后一天:" + lastYearDay);
                buffer.append(SymbolApi.TAB).append("and T.Next_Time <= to_date('"+firstYearDay+"','YYYY-MM-DD') ");
            }
        }else{
            //指定时间
            buffer.append(SymbolApi.TAB).append("and T.Next_Time <= to_date('"+reqBody.getNextStartTimeRecently()+"','YYYY-MM-DD') ");

        }

        buffer.append(SymbolApi.TAB).append(" GROUP BY SUBSTR(T.pataddress_Gbcode, 1, 4)) C \n" +
                "ON A.gbcode = C.gbcode ");
        buffer.append(SymbolApi.TAB).append(" LEFT JOIN\n" +
                "    (SELECT district_code, district_name\n" +
                "    FROM t_s_district) D\n" +
                "ON A.gbcode = D.district_code ");
        Query nativeQuery = entityManager.createNativeQuery(new String(buffer));
        nativeQuery.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        final List<?> resultList = nativeQuery.getResultList();

        PageBo rPageBo = PageUtils.getInstance().buildPageBo(pageBo, 0, resultList);
        return rPageBo;
        }

你可能感兴趣的:(后端,java,sql)