Skip to content

Commit

Permalink
#261 fix(*): 절약금액 계산시에는 double로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuturn committed Feb 23, 2023
1 parent b3e7b47 commit 4b9d9f0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import com.kaspi.backend.service.UserRecordService;
import com.kaspi.backend.util.response.CommonResponseDto;
import com.kaspi.backend.util.response.code.DefaultCode;
import com.kaspi.backend.util.response.code.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
Expand All @@ -37,12 +40,15 @@ public class UserGasRecordController {

@PostMapping("/user/gas-record")
public ResponseEntity<CommonResponseDto> postUserGasRecord(@RequestBody UserGasRecordReqDto userGasRecordReqDto) {

// if (bindingResult.hasErrors()) {
// return ResponseEntity.status(HttpStatus.BAD_REQUEST)
// .body(CommonResponseDto.toResponse(ErrorCode.PARAMETER_ERROR));
// }
//주유소 객체 얻기
GasStation gasStation = gasStationService.getGasStationByNo(userGasRecordReqDto.getGasStationNo());

//유저의 주유량
Long userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation);
double userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation);

//전국 유가 평균 받기
Long nationalAvgOilPrice = opinetService.nationalAvgOilPrice(userGasRecordReqDto.getGasType());
Expand All @@ -57,7 +63,7 @@ public ResponseEntity<CommonResponseDto> postUserGasRecord(@RequestBody UserGasR
//유저 최종 저장
userRecordService.saveUserGasRecord(userGasRecordReqDto,
gasStation,
userGasAmount,
(long) userGasAmount,
usersSavingPrice);

return ResponseEntity.status(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.kaspi.backend.enums.GasType;
import lombok.Builder;
import lombok.Getter;
import org.checkerframework.checker.index.qual.Positive;

import java.util.Date;

Expand All @@ -11,6 +12,7 @@
public class UserGasRecordReqDto {

private GasType gasType;
@Positive
private Long refuelingPrice;
private Long gasStationNo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,25 @@ public class UserRecordService {
/**
* 사용자로 부터 받은 주유금액, 유종과 주유소 정보로 얼마만큼 넣었는지 계산하는 로직
*/
public Long calTodayUserGasAmount(UserGasRecordReqDto userGasRecordReqDto, GasStation gasStation) {
public double calTodayUserGasAmount(UserGasRecordReqDto userGasRecordReqDto, GasStation gasStation) {
Optional<Long> todayGasPrice = gasDetailDao.findTodayGasPrice(gasStation.getStationNo(),
userGasRecordReqDto.getGasType().name(),
GasDetail.getNowDateToStr());//오늘 날짜로 계산
validTodayGasPrice(userGasRecordReqDto, gasStation, todayGasPrice);
double userGasAmount = (double)userGasRecordReqDto.getRefuelingPrice() / todayGasPrice.get();
log.info("사용자가 주유한 가스타입:{}, 주유량:{}", userGasRecordReqDto.getGasType().name(), userGasAmount);
return userGasAmount;
}

private static void validTodayGasPrice(UserGasRecordReqDto userGasRecordReqDto, GasStation gasStation, Optional<Long> todayGasPrice) {
if (todayGasPrice.isEmpty() || todayGasPrice.get() == 0) {
log.error("DB에 오늘날짜에 해당되는 주유 가격 정보가 존재하지 않음 가스타입:{}, 주유소PK:{}", userGasRecordReqDto.getGasType().name(), gasStation.getStationNo());
throw new SqlNotFoundException(ErrorCode.SQL_NOT_FOUND);
}
Long userGasAmount = (long) Math.round(userGasRecordReqDto.getRefuelingPrice() / todayGasPrice.get());
log.info("사용자가 주유한 가스타입:{}, 주유량:{}", userGasRecordReqDto.getGasType().name(), userGasAmount);
return userGasAmount;
}

public Long calUserSavingAmount(Long userRefuelingPrice, Long userGasAmount, Long nationalAvgOilPrice) {
return nationalAvgOilPrice * userGasAmount-userRefuelingPrice;
public Long calUserSavingAmount(Long userRefuelingPrice, double userGasAmount, Long nationalAvgOilPrice) {
return (long) (nationalAvgOilPrice * userGasAmount-userRefuelingPrice);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void postUserGasRecord() throws Exception {

when(gasStationService.getGasStationByNo(userGasRecordReqDto.getGasStationNo())).thenReturn(gasStation);
when(userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation))
.thenReturn(userGasAmount);// 유저가 총 넣은 가스 리터 수
.thenReturn(Double.valueOf(userGasAmount));// 유저가 총 넣은 가스 리터 수
when(opinetService.nationalAvgOilPrice(userGasRecordReqDto.getGasType())).thenReturn(
nationalGasAvg);//전국 리터당 평균 가격
when(userRecordService.calUserSavingAmount(userGasRecordReqDto.getRefuelingPrice(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void calTodayUserGasAmount() {
GasDetail.getNowDateToStr())) // 항상 오늘날짜로 기준
.thenReturn(Optional.of(gasolinePerLiterPrice));//가솔린 1L당 1000원
//when
Long userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation);
double userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation);
//then
assertEquals(5L, userGasAmount);
}
Expand Down

0 comments on commit 4b9d9f0

Please sign in to comment.