|
|
|
@ -7,6 +7,8 @@ import com.example.demo.domain.vo.Red; |
|
|
|
import com.example.demo.exception.SystemException; |
|
|
|
import com.example.demo.mapper.Temporary.RedMapper; |
|
|
|
import com.example.demo.service.Temporary.RedService; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
@ -39,6 +41,8 @@ public class RedServiceImpl implements RedService { |
|
|
|
private String BASE_URLProd ; |
|
|
|
private static final String BASE_URLDev = "http://gf977328.natappfree.cc"; |
|
|
|
private static final String PATH = "/api/coupon/IssueRechargeRedPacket"; |
|
|
|
private static final String PATHRED = "/api/coupon/getTotalDiscount"; |
|
|
|
|
|
|
|
private static final String CONSUME_PATH = "/api/coupon/updateCostRedPacket"; |
|
|
|
private static final HttpClient CLIENT = HttpClient.newHttpClient(); |
|
|
|
|
|
|
|
@ -130,6 +134,41 @@ public class RedServiceImpl implements RedService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public BigDecimal selectRedSum(Integer jwcode) { |
|
|
|
try { |
|
|
|
String body = JSON.toJSONString(java.util.Map.of("jwcode", jwcode)); |
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder() |
|
|
|
.uri(URI.create("http://sb29dfa2.natappfree.cc" + PATHRED)) //URL记得换 |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(body)) |
|
|
|
.build(); |
|
|
|
|
|
|
|
HttpResponse<String> resp = CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); |
|
|
|
// 解析 JSON |
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
JsonNode rootNode = mapper.readTree(resp.body()); |
|
|
|
|
|
|
|
// 提取 data 字段 |
|
|
|
if (rootNode.has("data")) { |
|
|
|
JsonNode dataNode = rootNode.get("data"); |
|
|
|
System.out.println("Data: " + dataNode); |
|
|
|
return dataNode.decimalValue(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (resp.statusCode() != 200) { |
|
|
|
log.warn("网络异常", resp.statusCode(), resp.body()); |
|
|
|
} |
|
|
|
return BigDecimal.valueOf(0); |
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
log.error("网络异常", jwcode, e); |
|
|
|
return BigDecimal.valueOf(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// --- 校验方法 --- |
|
|
|
private void validateParams(Integer jwcode, Integer type, BigDecimal delta) { |
|
|
|
if (jwcode == null || jwcode <= 0) { |
|
|
|
@ -159,9 +198,6 @@ public class RedServiceImpl implements RedService { |
|
|
|
} |
|
|
|
else if (type == 2) { |
|
|
|
// 消费:必须 delta < 0(或你设计为正数+方向,此处按负数扣款) |
|
|
|
if (delta.compareTo(BigDecimal.ZERO) > 0) { |
|
|
|
throw new BusinessException("消费金额应为负数(表示扣款)"); |
|
|
|
} |
|
|
|
if (now.isBefore(timeRuleConfig.getConsumeStartTime())) { |
|
|
|
String openTime = timeRuleConfig.getConsumeStartTime() |
|
|
|
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); |
|
|
|
|