๋งค์ถ ํ์ธ ํ๋ฉด
1. [GET] /jat/sales/today
๋งจ ์๋จ "์ฌ์ฅ๋, ์ค๋ ๋งค์ถ์ n์ ์
๋๋ค."
- ๋ชจ๋ ๋งค์ถ ์กฐํ ๊ธฐ์ค์, DB์ ์ ์ฅ๋ TimeStamp ๊ธฐ์ค์ผ๋ก ์งํ๋์๋ค.
- ์ค๋ ๋ ์ง์์ ๊ฐ ๊ฐ๊ฒ์ ์๊ฐ๋ณ ๋งค์ถ์ ์ง๊ณํ์ฌ ์๋ต์ ํฌํจํ์๊ณ ,
ํ๋ก ํธ์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ ๊ทธ๋ํ ํ๊ธฐ๋ฅผ ์งํํ์๋ค.
- DB์ฐ๊ด๊ด๊ณ๋ ์ฃผ๋ฌธ ํ
์ด๋ธ์์ ๊ฐ๊ฒ Idx๋ฅผ ์ด์ฉํ์ฌ ๋ ์ง ๋ฒ์ ๋ด์ ์กด์ฌํ๋ ์๊ฐ์ ์ง๊ณํ์๋ค.
- ์๋ต ๋ฐ๋
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 ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ ๊ตฌํ์๋ค.
- ์ด์ ์ ์ค๋ ์๋ต์ผ๋ก ๊ฐ ์๊ฐ๋๋ณ ์์ผ๋ก ๋งค์ถ์ ๋๋ ์ ๋ฆฌ์คํธ ํํ๋ก ๋ฐํํด์ฃผ์๋ค.
- ์๋ต ๋ฐ๋
//์๊ฐ๋๋ณ ์ง๊ณ, ๋ฐ ์๊ฐ๋๋ณ ์์ผ๋ก ์กฐํ,
// ์ฃผ๋ฌธ์ ์ฐ๊ด๋ ์ฃผ๋ฌธ ๋ฉ๋ด์ ๋จ์ด๋ฉ๋ด ๊ธ์ก์ ์ง๊ณ
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());
- Calendar.getTime().getDay() ๋ฐํ๊ฐ -> 0(์ผ์์ผ) ~ 6(์์์ผ)
0(์ผ์์ผ) ์ด๋ฉด ๋ค์ ์ฃผ๊น์ง ๋ฒ์๊ฐ ๋์ด๊ฐ๋ฒ๋ฆฌ๋ฏ๋ก, ํ ์ฃผ ์๋น๊ฒจ์ ๊ธฐ์ค์ ์ก์.
์๋๋ผ๋ฉด, ์ด๋ฒ ์ฃผ ๊ทธ๋๋ก ์ฌ์ฉ๊ฐ๋ฅ
// ๊ธฐ์ค์ด ์ผ์์ผ๋ถํฐ ์ด๋ฒ ์ฃผ๋ก ์์ํด์ ์์์ผ๋ก ๋ณํ๋๋๋ก ๋ก์ง์ฒ๋ฆฌ
if(calendar.getTime().getDay() == 0){
calendar.add(Calendar.DATE, -7);
}
- ์์ ๊ธฐ์ค์ ์ ์ฉํ์ฌ ์ด๋ฒ์ฃผ ์์์ผ์ ์ ์ฉํ๊ณ , ํด๋น ์์์ผ๋ถํฐ +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());
- ์ด๋ฒ ์ฃผ 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};
}
// 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