|
1 | 1 | package com.example.helper.service; |
2 | 2 |
|
3 | 3 | import com.example.helper.constant.Messages; |
| 4 | +import com.example.helper.constant.SpecMealInputsEng; |
4 | 5 | import com.example.helper.constant.SpecMealInputsKor; |
5 | 6 | import com.example.helper.constant.Types; |
6 | 7 | import com.example.helper.dto.DateMealDto; |
|
11 | 12 | import com.fasterxml.jackson.databind.ObjectMapper; |
12 | 13 | import lombok.extern.slf4j.Slf4j; |
13 | 14 |
|
| 15 | +import java.text.ParseException; |
| 16 | +import java.text.SimpleDateFormat; |
14 | 17 | import java.util.List; |
15 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
16 | 19 | import org.springframework.stereotype.Service; |
@@ -120,12 +123,128 @@ else if((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsK |
120 | 123 |
|
121 | 124 | return "2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n"; |
122 | 125 | } |
| 126 | + public Boolean dateFormatValidation(String date) { |
| 127 | + Boolean ret = true; |
| 128 | + try { |
| 129 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| 130 | + sdf.setLenient(false); |
| 131 | + sdf.parse(date); |
123 | 132 |
|
| 133 | + } catch(ParseException e) { |
| 134 | + ret = false; |
| 135 | + } |
| 136 | + return ret; |
| 137 | + } |
124 | 138 | public String getSpecEngMeal(String dateCustom, String bld) { |
125 | 139 | if (!specInputValidation(dateCustom, bld)) { |
126 | 140 | return Messages.NO_MEAL_ENG.getMessages(); |
127 | 141 | } |
128 | | - return "2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n"; |
| 142 | + LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul")); |
| 143 | + |
| 144 | + Integer year = currentDateTime.getYear(); |
| 145 | + Integer month = currentDateTime.getMonth().getValue(); |
| 146 | + Integer day = currentDateTime.getDayOfMonth(); |
| 147 | + Integer currentDateNumber = currentDateTime.getDayOfWeek().getValue(); |
| 148 | + |
| 149 | + Integer dateCustomLen = dateCustom.length(); |
| 150 | + if(dateCustom.equals(SpecMealInputsEng.TODAY.getInputs())) { |
| 151 | + // 오늘 |
| 152 | + } |
| 153 | + else if(dateCustom.equals(SpecMealInputsEng.TOMORROW.getInputs())) { |
| 154 | + // 내일 |
| 155 | + currentDateTime = currentDateTime.plusDays(1); |
| 156 | + year = currentDateTime.getYear(); |
| 157 | + month = currentDateTime.getMonth().getValue(); |
| 158 | + day = currentDateTime.getDayOfMonth(); |
| 159 | + } |
| 160 | + else if(dateCustomLen > 2) { |
| 161 | + String dateCustomPostfix = dateCustom.substring(dateCustomLen - 2, dateCustomLen); |
| 162 | + if( dateCustomPostfix.equals(SpecMealInputsEng.DAY_1.getInputs()) || |
| 163 | + dateCustomPostfix.equals(SpecMealInputsEng.DAY_2.getInputs()) || |
| 164 | + dateCustomPostfix.equals(SpecMealInputsEng.DAY_3.getInputs()) || |
| 165 | + dateCustomPostfix.equals(SpecMealInputsEng.DAY_OTHER.getInputs())) { |
| 166 | + // 특정날짜 |
| 167 | + String dateCustomPrefix = (dateCustomLen == 3) ? dateCustom.substring(0, 1) : dateCustom.substring(0, 2); |
| 168 | + |
| 169 | + try{ |
| 170 | + day = Integer.valueOf(dateCustomPrefix); |
| 171 | + } |
| 172 | + catch (NumberFormatException ex){ |
| 173 | + day = 0; |
| 174 | + } |
| 175 | + } |
| 176 | + else { |
| 177 | + //요일 |
| 178 | + Integer dateNumber = 0; |
| 179 | + if(dateCustom.equals(SpecMealInputsEng.MON.getInputs())) { |
| 180 | + dateNumber = SpecMealInputsEng.MON.getInputValue(); |
| 181 | + } |
| 182 | + else if(dateCustom.equals(SpecMealInputsEng.TUE.getInputs())) { |
| 183 | + dateNumber = SpecMealInputsEng.TUE.getInputValue(); |
| 184 | + } |
| 185 | + else if(dateCustom.equals(SpecMealInputsEng.WED.getInputs())) { |
| 186 | + dateNumber = SpecMealInputsEng.WED.getInputValue(); |
| 187 | + } |
| 188 | + else if(dateCustom.equals(SpecMealInputsEng.THR.getInputs())) { |
| 189 | + dateNumber = SpecMealInputsEng.THR.getInputValue(); |
| 190 | + } |
| 191 | + else if(dateCustom.equals(SpecMealInputsEng.FRI.getInputs())) { |
| 192 | + dateNumber = SpecMealInputsEng.FRI.getInputValue(); |
| 193 | + } |
| 194 | + else if(dateCustom.equals(SpecMealInputsEng.SAT.getInputs())) { |
| 195 | + dateNumber = SpecMealInputsEng.SAT.getInputValue(); |
| 196 | + } |
| 197 | + else if(dateCustom.equals(SpecMealInputsEng.SUN.getInputs())) { |
| 198 | + dateNumber = SpecMealInputsEng.SUN.getInputValue(); |
| 199 | + } |
| 200 | + |
| 201 | + if(dateNumber == 0) { |
| 202 | + day = 0; |
| 203 | + } |
| 204 | + else { |
| 205 | + Integer dateDiff = dateNumber - currentDateNumber; |
| 206 | + Integer dateDiffAbs = (dateDiff > 0) ? dateDiff : dateDiff * -1; |
| 207 | + currentDateTime = (dateDiff > 0) ? currentDateTime.plusDays(dateDiffAbs) : currentDateTime.minusDays(dateDiffAbs); |
| 208 | + year = currentDateTime.getYear(); |
| 209 | + month = currentDateTime.getMonth().getValue(); |
| 210 | + day = currentDateTime.getDayOfMonth(); |
| 211 | + } |
| 212 | + |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + Integer kindType = -1; |
| 217 | + if(bld.equals(SpecMealInputsEng.BREAKFAST.getInputs())) { |
| 218 | + kindType = SpecMealInputsEng.BREAKFAST.getInputValue(); |
| 219 | + } |
| 220 | + else if(bld.equals(SpecMealInputsEng.LUNCH.getInputs())) { |
| 221 | + kindType = SpecMealInputsEng.LUNCH.getInputValue(); |
| 222 | + } |
| 223 | + else if(bld.equals(SpecMealInputsEng.DINNER.getInputs())) { |
| 224 | + kindType = SpecMealInputsEng.DINNER.getInputValue(); |
| 225 | + } |
| 226 | + |
| 227 | + if( (day < 1 && day > 31) || (kindType < 0 || kindType > 2)) { |
| 228 | + return Messages.NO_MEAL_ENG.getMessages(); |
| 229 | + } |
| 230 | + |
| 231 | + String date = year + "-"; |
| 232 | + date += String.format("%02d", month) + "-"; |
| 233 | + date += String.format("%02d", day) + ""; |
| 234 | + |
| 235 | + //형식이 잘못되면 Query 에서 NULL 을 반환한다. |
| 236 | + //if(!dateFormatValidation(date)) { |
| 237 | + // return Messages.NO_MEAL_ENG.getMessages(); |
| 238 | + //} |
| 239 | + |
| 240 | + Optional<Meal> result = sqlMealRepository.findByDate(Types.BLDG2_1ST.getType(), Types.LANG_ENG.getType(), kindType, date); |
| 241 | + |
| 242 | + if(result.isEmpty()) { |
| 243 | + //throw new IllegalStateException(Messages.EXIST_MEAL_ERROR.getMessages()); |
| 244 | + return Messages.NO_MEAL_ENG.getMessages(); |
| 245 | + } |
| 246 | + Meal meal = result.get(); |
| 247 | + return meal.generateMenu(); |
129 | 248 | } |
130 | 249 |
|
131 | 250 | public Map<String, Object> responseMeal(String menu) { |
|
0 commit comments