|
|
|
@ -7,9 +7,13 @@ 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; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
@ -33,10 +37,13 @@ import java.time.format.DateTimeFormatter; |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
public class RedServiceImpl implements RedService { |
|
|
|
private static final String BASE_URLProd = "http://39.101.133.168:8828/scms"; |
|
|
|
@Value("${red.url}") |
|
|
|
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(); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@ -49,9 +56,9 @@ public class RedServiceImpl implements RedService { |
|
|
|
throw new BusinessException("未接受到精网号"); |
|
|
|
} |
|
|
|
if (redMapper.selectJwcode(jwcode)==false){ |
|
|
|
throw new BusinessException("未找到该精网号"); |
|
|
|
return BigDecimal.valueOf(0); |
|
|
|
} |
|
|
|
if(redMapper.selectJwcode(jwcode)) |
|
|
|
|
|
|
|
if (type == null){ |
|
|
|
throw new BusinessException("未接受到类型"); |
|
|
|
} |
|
|
|
@ -62,7 +69,8 @@ public class RedServiceImpl implements RedService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
// @Transactional(rollbackFor = Exception.class) |
|
|
|
@Transactional(propagation = Propagation.REQUIRES_NEW) |
|
|
|
public void addAmount(Integer jwcode, BigDecimal sum, Integer type) { |
|
|
|
// 1. 基础参数校验 |
|
|
|
validateParams(jwcode, type, sum); |
|
|
|
@ -88,7 +96,7 @@ public class RedServiceImpl implements RedService { |
|
|
|
String body = JSON.toJSONString(java.util.Map.of("jwcode", jwcode)); |
|
|
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder() |
|
|
|
.uri(URI.create(BASE_URLDev + PATH)) //URL记得换 |
|
|
|
.uri(URI.create(BASE_URLProd + PATH)) //URL记得换 |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(body)) |
|
|
|
.build(); |
|
|
|
@ -104,6 +112,61 @@ public class RedServiceImpl implements RedService { |
|
|
|
return "{\"success\":false,\"msg\":\"网络异常\"}"; |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
public String sendConsumeJwcode(Integer jwcode, BigDecimal price) { |
|
|
|
try { |
|
|
|
String body = JSON.toJSONString(java.util.Map.of("jwcode", jwcode,"discount",price)); |
|
|
|
HttpRequest request = HttpRequest.newBuilder() |
|
|
|
.uri(URI.create(BASE_URLProd + CONSUME_PATH)) //URL记得换 |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(body)) |
|
|
|
.build(); |
|
|
|
|
|
|
|
HttpResponse<String> resp = CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); |
|
|
|
|
|
|
|
if (resp.statusCode() != 200) { |
|
|
|
log.warn("红包接口异常,status:{},body:{}", resp.statusCode(), resp.body()); |
|
|
|
} |
|
|
|
return resp.body(); |
|
|
|
} catch (IOException | InterruptedException e) { |
|
|
|
log.error("调用红包接口失败,jwcode:{}", jwcode, e); |
|
|
|
return "{\"success\":false,\"msg\":\"网络异常\"}"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// --- 校验方法 --- |
|
|
|
@ -135,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")); |
|
|
|
|