diff --git a/.idea/.cache/.Apifox_Helper/.toolWindow.db b/.idea/.cache/.Apifox_Helper/.toolWindow.db index 80e1644..97c283a 100644 Binary files a/.idea/.cache/.Apifox_Helper/.toolWindow.db and b/.idea/.cache/.Apifox_Helper/.toolWindow.db differ diff --git a/src/main/java/vvpkassistant/pk/model/DTO/PkResultPointsDTO.java b/src/main/java/vvpkassistant/pk/model/DTO/PkResultPointsDTO.java index ed061c7..fdd6318 100644 --- a/src/main/java/vvpkassistant/pk/model/DTO/PkResultPointsDTO.java +++ b/src/main/java/vvpkassistant/pk/model/DTO/PkResultPointsDTO.java @@ -6,4 +6,5 @@ import lombok.Data; public class PkResultPointsDTO { private Integer winnerUserId; private Integer loserUserId; + private Boolean draw; } diff --git a/src/main/java/vvpkassistant/pk/service/PkResultPointServiceImpl.java b/src/main/java/vvpkassistant/pk/service/PkResultPointServiceImpl.java index 12528d3..4c4af40 100644 --- a/src/main/java/vvpkassistant/pk/service/PkResultPointServiceImpl.java +++ b/src/main/java/vvpkassistant/pk/service/PkResultPointServiceImpl.java @@ -16,6 +16,7 @@ public class PkResultPointServiceImpl implements PkResultPointService { private static final String PK_WIN_COIN_CONFIG_NAME = "PK胜利增加积分"; private static final String PK_LOSE_COIN_CONFIG_NAME = "PK失败增加积分"; + private static final String PK_DRAW_COIN_CONFIG_NAME = "PK平局增加积分"; private static final int COIN_RECORD_ADD = 1; private final UserDao userDao; @@ -40,6 +41,9 @@ public class PkResultPointServiceImpl implements PkResultPointService { public String grantPkResultPoints(PkResultPointsDTO request) { validateRequest(request); int now = (int) epochSecondProvider.nowEpochSecond(); + if (isDraw(request)) { + return grantDrawPoints(request, now); + } int winPoints = parsePositivePoints(PK_WIN_COIN_CONFIG_NAME); int losePoints = parsePositivePoints(PK_LOSE_COIN_CONFIG_NAME); @@ -48,6 +52,13 @@ public class PkResultPointServiceImpl implements PkResultPointService { return String.format("操作成功,胜利方增加%d积分,失败方增加%d积分", winPoints, losePoints); } + private String grantDrawPoints(PkResultPointsDTO request, int now) { + int drawPoints = parsePositivePoints(PK_DRAW_COIN_CONFIG_NAME); + grantPoints(request.getWinnerUserId(), drawPoints, PK_DRAW_COIN_CONFIG_NAME, now); + grantPoints(request.getLoserUserId(), drawPoints, PK_DRAW_COIN_CONFIG_NAME, now); + return String.format("操作成功,平局双方各增加%d积分", drawPoints); + } + private static void validateRequest(PkResultPointsDTO request) { if (request == null || request.getWinnerUserId() == null || request.getLoserUserId() == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空"); @@ -57,6 +68,10 @@ public class PkResultPointServiceImpl implements PkResultPointService { } } + private static boolean isDraw(PkResultPointsDTO request) { + return Boolean.TRUE.equals(request.getDraw()); + } + private int parsePositivePoints(String configName) { String value = functionConfigProvider.getValue(configName); if (value == null) { @@ -64,7 +79,7 @@ public class PkResultPointServiceImpl implements PkResultPointService { } try { int points = Integer.parseInt(value); - if (points <= 0) { + if (points < 0) { throw new BusinessException(ErrorCode.SYSTEM_ERROR, configName + "配置错误"); } return points; diff --git a/src/test/java/vvpkassistant/pk/service/PkResultPointServiceImplTests.java b/src/test/java/vvpkassistant/pk/service/PkResultPointServiceImplTests.java index b81fc3a..de3d9fb 100644 --- a/src/test/java/vvpkassistant/pk/service/PkResultPointServiceImplTests.java +++ b/src/test/java/vvpkassistant/pk/service/PkResultPointServiceImplTests.java @@ -51,6 +51,41 @@ class PkResultPointServiceImplTests { Assertions.assertEquals(Integer.valueOf(1234), captor.getAllValues().get(1).getTime()); } + @Test + void shouldGrantDrawPointsToBothUsers() { + UserDao userDao = mock(UserDao.class); + CoinRecordsDao coinRecordsDao = mock(CoinRecordsDao.class); + FunctionConfigProvider functionConfigProvider = mock(FunctionConfigProvider.class); + EpochSecondProvider epochSecondProvider = () -> 1_234L; + + when(functionConfigProvider.getValue("PK平局增加积分")).thenReturn("5"); + when(userDao.increasePoints(1001, 5)).thenReturn(1); + when(userDao.increasePoints(1002, 5)).thenReturn(1); + when(coinRecordsDao.insert(any())).thenReturn(1); + + PkResultPointServiceImpl service = new PkResultPointServiceImpl( + userDao, coinRecordsDao, functionConfigProvider, epochSecondProvider + ); + PkResultPointsDTO request = new PkResultPointsDTO(); + request.setWinnerUserId(1001); + request.setLoserUserId(1002); + request.setDraw(true); + + String result = service.grantPkResultPoints(request); + + Assertions.assertEquals("操作成功,平局双方各增加5积分", result); + ArgumentCaptor captor = ArgumentCaptor.forClass(CoinRecords.class); + verify(coinRecordsDao, times(2)).insert(captor.capture()); + Assertions.assertEquals("PK平局增加积分", captor.getAllValues().get(0).getInfo()); + Assertions.assertEquals(Integer.valueOf(1001), captor.getAllValues().get(0).getUserId()); + Assertions.assertEquals(Integer.valueOf(5), captor.getAllValues().get(0).getNumber()); + Assertions.assertEquals("PK平局增加积分", captor.getAllValues().get(1).getInfo()); + Assertions.assertEquals(Integer.valueOf(1002), captor.getAllValues().get(1).getUserId()); + Assertions.assertEquals(Integer.valueOf(5), captor.getAllValues().get(1).getNumber()); + Assertions.assertEquals(Integer.valueOf(1234), captor.getAllValues().get(0).getTime()); + Assertions.assertEquals(Integer.valueOf(1234), captor.getAllValues().get(1).getTime()); + } + @Test void shouldFailWhenWinnerAndLoserAreSameUser() { UserDao userDao = mock(UserDao.class);