๊ฐœ๋ฐœ์ผ์ง€-24 [๐Ÿ’ฐ๋งค์ถœ ๋„๋ฉ”์ธ : ์กฐํšŒ's & Calendarํด๋ž˜์Šค ํ•œ ์ฃผ ๊ตฌํ•˜๊ธฐ]

๊น€์„ฑ์ธยท2023๋…„ 9์›” 18์ผ
0

๋งค์ถœ ๋„๋ฉ”์ธ ๊ธฐ๋Šฅ ์ •์˜ ํฌ์ŠคํŒ…


๋งค์ถœ ํ™•์ธ ํ™”๋ฉด


1. [GET] /jat/sales/today

๋งจ ์ƒ๋‹จ "์‚ฌ์žฅ๋‹˜, ์˜ค๋Š˜ ๋งค์ถœ์€ n์› ์ž…๋‹ˆ๋‹ค."

  • ๋ชจ๋“  ๋งค์ถœ ์กฐํšŒ ๊ธฐ์ค€์€, DB์— ์ €์žฅ๋œ TimeStamp ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰๋˜์—ˆ๋‹ค.
  • ์˜ค๋Š˜ ๋‚ ์งœ์—์„œ ๊ฐ ๊ฐ€๊ฒŒ์˜ ์‹œ๊ฐ„๋ณ„ ๋งค์ถœ์„ ์ง‘๊ณ„ํ•˜์—ฌ ์‘๋‹ต์— ํฌํ•จํ•˜์˜€๊ณ ,
    ํ”„๋ก ํŠธ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ทธ๋ž˜ํ”„ ํ‘œ๊ธฐ๋ฅผ ์ง„ํ–‰ํ•˜์˜€๋‹ค.
  • DB์—ฐ๊ด€๊ด€๊ณ„๋Š” ์ฃผ๋ฌธ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€๊ฒŒ Idx๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‚ ์งœ ๋ฒ”์œ„ ๋‚ด์— ์กด์žฌํ•˜๋Š” ์‹œ๊ฐ„์„ ์ง‘๊ณ„ํ•˜์˜€๋‹ค.
  • ์‘๋‹ต ๋ฐ”๋””
  • Dao ์ฟผ๋ฆฌ
    public TodayTotalSalesRes getTodayTotalSales(int storeIdx) {
        String query = "SELECT\n" +
                "    DATE_FORMAT(O.created, '%Y-%c-%d') as today,\n" +
                "    SUM(TM.price * OL.cnt) as total_price \n" +
                "FROM Orders O\n" +
                "LEFT JOIN OrderLists OL on O.orderIdx = OL.orderIdx\n" +
                "LEFT JOIN TodayMenu TM on OL.todaymenuIdx = TM.todaymenuIdx\n" +
                "WHERE O.storeIdx = ?\n" +
                "  AND O.created >= CONCAT(DATE_FORMAT(NOW(), '%Y-%c-%d'), ' 00:00:00')\n" +
                "  AND O.created <= CONCAT(DATE_FORMAT(NOW(), '%Y-%c-%d'), ' 23:59:59')\n" +
                "  AND O.status LIKE '%A%'";

        return this.jdbcTemplate.queryForObject(query,
                (rs, rowNum) -> new TodayTotalSalesRes(
                        storeIdx,
                        rs.getString("today"),
                        rs.getInt("total_price")
                ), storeIdx);
    }

2. [GET] /jat/sales/yesterday

์–ด์ œ ์˜ค๋Š˜ ์‹œ๊ฐ„๋Œ€๋ณ„ ๋งค์ถœ๋Ÿ‰

  • ์˜ค๋Š˜, ์–ด์ œ ๋‚ ์งœ + ๊ฐ€๊ฒŒ ์˜คํ”ˆ/๋งˆ๊ฐ ์‹œ๊ฐ„ ๋ฒ”์œ„ ๋‚ด์— ์กด์žฌํ•˜๋Š” ๋งค์ถœ์„ ๊ฐ๊ฐ ์กฐํšŒํ•˜๊ณ , ์ง‘๊ณ„ํ•˜์˜€๋‹ค.
  • ์˜ค๋Š˜, ์–ด์ œ ๋‚ ์งœ๋Š” ์„œ๋น„์Šค์—์„œ SimpleDateFormat, Date ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ๊ตฌํ•˜์˜€๋‹ค.
  • ์–ด์ œ์™€ ์˜ค๋Š˜ ์‘๋‹ต์œผ๋กœ ๊ฐ ์‹œ๊ฐ„๋Œ€๋ณ„ ์ˆœ์œผ๋กœ ๋งค์ถœ์„ ๋‚˜๋ˆ ์„œ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์‘๋‹ต ๋ฐ”๋””
  • Dao ์ฟผ๋ฆฌ
//์‹œ๊ฐ„๋Œ€๋ณ„ ์ง‘๊ณ„, ๋ฐ ์‹œ๊ฐ„๋Œ€๋ณ„ ์ˆœ์œผ๋กœ ์กฐํšŒ, 
// ์ฃผ๋ฌธ์— ์—ฐ๊ด€๋œ ์ฃผ๋ฌธ ๋ฉ”๋‰ด์˜ ๋–จ์ด๋ฉ”๋‰ด ๊ธˆ์•ก์„ ์ง‘๊ณ„
    public List<SalesByTime> getFromYtoTdaySales(int storeIdx, String dayStart, String dayFinish) {
        String query = "SELECT\n" +
                "    DATE_FORMAT(O.created, '%Y-%c-%d %H') as dayTime,\n" +
                "    SUM(TM.price * OL.cnt) as total_price_of_the_day\n" +
                "FROM Orders O\n" +
                "LEFT JOIN OrderLists OL on O.orderIdx = OL.orderIdx\n" +
                "LEFT JOIN TodayMenu TM on OL.todaymenuIdx = TM.todaymenuIdx\n" +
                "WHERE O.storeIdx = ? \n" +
                "  AND O.created >= ? \n" +
                "  AND O.created <= ? \n" +
                "AND O.status LIKE '%A%'\n" +
                "GROUP BY dayTime \n" +
                "ORDER BY dayTime";

        Object[] params = new Object[]{
                storeIdx, dayStart, dayFinish
        };

        return this.jdbcTemplate.query(query,
                (rs, rowNum) -> new SalesByTime(
                        rs.getString("dayTime"),
                        rs.getInt("total_price_of_the_day")
                ), params);
    }

3. [GET] /jat/sales/weekday

์ฃผ๊ฐ„ ๊ฐ€๊ฒŒ ํŒ๋งค ๋งค์ถœ๋Ÿ‰ (์›”~์ผ)

  • ์ž๋ฐ”์˜ Calendar ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ์„œ ์ด๋ฒˆ์ฃผ์˜ ๋‚ ์งœ๋“ค์„ ๊ตฌํ•˜์˜€๋‹ค.
  • ํ™”๋ฉด์—๋Š” ์›”์š”์ผ์„ ์‹œ์ž‘๊ธฐ์ค€์œผ๋กœ ํ•œ ์ฃผ๋ฅผ ์žก์ง€๋งŒ, Calendarํด๋ž˜์Šค๋Š” ์ผ์š”์ผ๋ถ€ํ„ฐ ํ•œ ์ฃผ๋ฅผ ์žก๊ฒŒ ๋˜์–ด์„œ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ์— ํ•œ ์ฃผ์”ฉ ๋‚ ์งœ๊ฐ€ ๋ฐ€๋ ค์„œ ์•ฝ๊ฐ„์˜ ์ฒจ๊ฐ€๊ฐ€ ํ•„์š”ํ•˜์˜€์Œ..
  • ์„œ๋น„์Šค ํด๋ž˜์Šค ๋‚ด ์ด๋ฒˆ ์ฃผ/ ์ €๋ฒˆ ์ฃผ ๋‚ ์งœ ์กฐํšŒ ํ•จ์ˆ˜
    public String[] getLtoTWeekMontoSun(){ // ์ด๋ฒˆ์ฃผ ์ €๋ฒˆ์ฃผ ์‹œ์ž‘(์›”)/์ข…๋ฃŒ(์ผ) ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ
        // 1) ์ด๋ฒˆ์ฃผ ์‹œ์ž‘ ๋‚ ์งœ, ์ €๋ฒˆ์ฃผ ์‹œ์ž‘๋‚ ์งœ ๊ตฌํ•˜๊ธฐ๊ธฐ
        // 1-1) ์˜ค๋Š˜ ๋‚ ์งœ ๋‹ฌ๋ ฅ์— ์ ์šฉ
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance(Locale.KOREA);
        calendar.setTime(new Date());
  1. Calendar.getTime().getDay() ๋ฐ˜ํ™˜๊ฐ’ -> 0(์ผ์š”์ผ) ~ 6(์›”์š”์ผ)
    0(์ผ์š”์ผ) ์ด๋ฉด ๋‹ค์Œ ์ฃผ๊นŒ์ง€ ๋ฒ”์œ„๊ฐ€ ๋„˜์–ด๊ฐ€๋ฒ„๋ฆฌ๋ฏ€๋กœ, ํ•œ ์ฃผ ์•ž๋‹น๊ฒจ์„œ ๊ธฐ์ค€์„ ์žก์Œ.
    ์•„๋‹ˆ๋ผ๋ฉด, ์ด๋ฒˆ ์ฃผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ
        // ๊ธฐ์ค€์ด ์ผ์š”์ผ๋ถ€ํ„ฐ ์ด๋ฒˆ ์ฃผ๋กœ ์‹œ์ž‘ํ•ด์„œ ์›”์š”์ผ๋กœ ๋ณ€ํ™˜๋˜๋„๋ก ๋กœ์ง์ฒ˜๋ฆฌ
        if(calendar.getTime().getDay() == 0){
            calendar.add(Calendar.DATE, -7);
        }

  1. ์•ž์„  ๊ธฐ์ค€์„ ์ ์šฉํ•˜์—ฌ ์ด๋ฒˆ์ฃผ ์›”์š”์ผ์„ ์ ์šฉํ•˜๊ณ , ํ•ด๋‹น ์›”์š”์ผ๋ถ€ํ„ฐ +6์ผ ๋˜๋Š” ์ด๋ฒˆ ์ฃผ ์ผ์š”์ผ ๋‚ ์งœ๋„ ๊ตฌํ•จ
        // 1-2) ์ด๋ฒˆ์ฃผ ์›”์š”์ผ
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // Monday = 1
        String thisweekMon = sdf.format(calendar.getTime());
        calendar.add(Calendar.DATE, 6);  // Monday + 6 = Sunday(7)
        String thisweekSun = sdf.format(calendar.getTime());

  1. ์ด๋ฒˆ ์ฃผ Calendar๊ธฐ์ค€ ์ผ์š”์ผ์—์„œ ํ•œ ์ฃผ ๋‹น๊ธฐ๋ฉด ๋‹ค์‹œ ์ด๋ฒˆ ์ฃผ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒŒ ๋˜์–ด์„œ -1์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ €๋ฒˆ์ฃผ ํ† ์š”์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์žก์Œ.
        // 1-3) ์ €๋ฒˆ์ฃผ ์›”์š”์ผ
        calendar.add(Calendar.DATE, -8); // Sunday(7) - 8 = -1 = Saturday(6)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // Monday = 1
        String lastweekMon = sdf.format(calendar.getTime());
        calendar.add(Calendar.DATE, 6);  // Monday + 6 = Sunday(7)
        String lastweekSun = sdf.format(calendar.getTime());

        return new String[] {thisweekMon, thisweekSun, lastweekMon, lastweekSun};
    }

  • Dao ์ฟผ๋ฆฌ
// weekDayId๋ฅผ ํ†ตํ•ด ์š”์ผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๋งค์ถœ์˜ ์ง‘๊ณ„๋ฅผ ๊ตฌํ•˜์˜€๋‹ค.
    public List<SalesByWeekDay> getFromLtoTWeekSales(int storeIdx, String dayStart, String dayFinish){
        String query = "SELECT\n" +
                "    DATE_FORMAT(O.created, '%w') as weekDayId,\n" +
                "    DATE_FORMAT(O.created, '%Y-%c-%d') as weekDate,\n" +
                "    SUM(TM.price * OL.cnt) as total_price_of_the_weekDay\n" +
                "FROM Orders O\n" +
                "LEFT JOIN OrderLists OL on O.orderIdx = OL.orderIdx\n" +
                "LEFT JOIN TodayMenu TM on OL.todaymenuIdx = TM.todaymenuIdx\n" +
                "WHERE  O.storeIdx = ?\n" +
                "  AND O.created >= ?\n" +
                "  AND O.created <= ?\n" +
                "  AND O.status LIKE '%A%'\n" +
                "GROUP BY weekDayId\n" +
                "ORDER BY weekDayId";

        Object[] params = new Object[]{
                storeIdx, dayStart, dayFinish
        };

        return this.jdbcTemplate.query(query,
                (rs, rowNum) -> new SalesByWeekDay(
                        rs.getString("weekDate"),
                        rs.getInt("weekDayId"),
                        rs.getInt("total_price_of_the_weekDay")
                ), params);

    }

4. [GET] /jat/sales/monthly


5. [GET] /jat/sales/monthly-ratio

profile
๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฟˆ์ธ 25์‚ด ๋Œ€ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€