|
|
@ -1,6 +1,16 @@ |
|
|
|
package com.example.demo.serviceImpl.bean; |
|
|
|
|
|
|
|
import com.example.demo.domain.vo.bean.BeanRecharge; |
|
|
|
import com.example.demo.domain.vo.coin.Result; |
|
|
|
import com.example.demo.service.bean.BeanRechargeService; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.springframework.web.util.UriComponentsBuilder; |
|
|
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
/** |
|
|
|
* @program: gold-java |
|
|
@ -10,7 +20,57 @@ import org.springframework.stereotype.Service; |
|
|
|
* @create: 2025−07-29 16:47 |
|
|
|
* @Version 1.0 |
|
|
|
**/ |
|
|
|
|
|
|
|
@Service |
|
|
|
public class BeanRechargeServiceImpl { |
|
|
|
} |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class BeanRechargeServiceImpl implements BeanRechargeService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RestTemplate restTemplate; |
|
|
|
|
|
|
|
@Override |
|
|
|
public Result add(BeanRecharge recharge) { |
|
|
|
try { |
|
|
|
// 验证精网号是否有效 |
|
|
|
if (recharge.getJwcode() == null || recharge.getJwcode().toString().isEmpty()) { |
|
|
|
return Result.error("精网号不能为空"); |
|
|
|
} |
|
|
|
|
|
|
|
// 验证免费豆和永久豆是否为非负数 |
|
|
|
if (recharge.getFreeBean() < 0 || recharge.getPermanentBean() < 0) { |
|
|
|
return Result.error("免费豆和永久豆不能为负数"); |
|
|
|
} |
|
|
|
// 验证免费豆和永久豆是否均为0 |
|
|
|
if (recharge.getFreeBean() == 0 && recharge.getPermanentBean() == 0) { |
|
|
|
return Result.error("免费豆和永久豆不能同时为0"); |
|
|
|
} |
|
|
|
// 验证备注是否为空 |
|
|
|
if (recharge.getRemark() == null || recharge.getRemark().toString().isEmpty()) { |
|
|
|
return Result.error("备注不能为空"); |
|
|
|
} |
|
|
|
String jwcode = recharge.getJwcode().toString(); |
|
|
|
String op = "recharge"; // 操作类型(根据实际业务定义,例如"recharge"表示充值) |
|
|
|
String content = recharge.getRemark(); // 备注作为content参数 |
|
|
|
String orderNo = UUID.randomUUID().toString().replace("-", ""); // 生成唯一订单号(去除横线) |
|
|
|
|
|
|
|
// 2. 构建接口URL及参数 |
|
|
|
String apiUrl = "http://47.92.148.30:3003/mock/61/hljw/api/user/gold"; |
|
|
|
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(apiUrl) |
|
|
|
.queryParam("jwcode", jwcode) |
|
|
|
.queryParam("op", op) |
|
|
|
.queryParam("gold", recharge.getPermanentBean()) |
|
|
|
.queryParam("content", content) |
|
|
|
.queryParam("order_no", orderNo); |
|
|
|
|
|
|
|
// 3. 发送GET请求 |
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(urlBuilder.toUriString(), String.class); |
|
|
|
|
|
|
|
// 4. 处理响应结果 |
|
|
|
if (!response.getStatusCode().is2xxSuccessful()) { |
|
|
|
return Result.error("远程接口调用失败,状态码:" + response.getStatusCodeValue()); |
|
|
|
} |
|
|
|
return Result.success("添加成功"); |
|
|
|
} catch (Exception e) { |
|
|
|
return Result.error("系统异常:" + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |