|
|
@ -1,5 +1,6 @@ |
|
|
|
package com.example.demo.serviceImpl.bean; |
|
|
|
|
|
|
|
import com.example.demo.Util.BaseDES2; |
|
|
|
import com.example.demo.domain.vo.bean.*; |
|
|
|
import com.example.demo.domain.vo.coin.*; |
|
|
|
import com.example.demo.mapper.bean.BeanConsumeMapper; |
|
|
@ -9,12 +10,20 @@ import com.example.demo.service.bean.BeanConsumeService; |
|
|
|
import com.github.pagehelper.PageHelper; |
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.http.HttpEntity; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.client.RestClientException; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.springframework.web.util.UriComponentsBuilder; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
/** |
|
|
@ -35,7 +44,9 @@ public class BeanConsumeServiceImpl implements BeanConsumeService { |
|
|
|
private UserMapper userMapper; |
|
|
|
@Autowired |
|
|
|
private RestTemplate restTemplate; |
|
|
|
|
|
|
|
// 构建接口URL及参数 |
|
|
|
@Value("${bean.consume.url}") |
|
|
|
String apiUrl; |
|
|
|
|
|
|
|
//获取消费用户分部 |
|
|
|
@Override |
|
|
@ -50,9 +61,10 @@ public class BeanConsumeServiceImpl implements BeanConsumeService { |
|
|
|
return liveDeptList; |
|
|
|
} |
|
|
|
//减少金豆 |
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public Result reduce(BeanConsume consume) { |
|
|
|
try { |
|
|
|
|
|
|
|
// 验证精网号是否有效 |
|
|
|
if (consume.getJwcode() == null || consume.getJwcode().isEmpty()) { |
|
|
|
return Result.error("精网号不能为空"); |
|
|
@ -71,43 +83,60 @@ public class BeanConsumeServiceImpl implements BeanConsumeService { |
|
|
|
return Result.error("免费豆和永久豆不能同时为0"); |
|
|
|
} |
|
|
|
// 验证备注是否为空 |
|
|
|
if (consume.getRemark() == null || consume.getRemark().toString().isEmpty()) { |
|
|
|
if (consume.getRemark() == null || consume.getRemark().isEmpty()) { |
|
|
|
return Result.error("备注不能为空"); |
|
|
|
} |
|
|
|
if(consume.getAdminName() == null || consume.getAdminName().toString().isEmpty()){ |
|
|
|
return Result.error("管理员名称不能为空"); |
|
|
|
} |
|
|
|
GoldUser goldUser = userMapper.selectUser(consume.getJwcode().toString()); |
|
|
|
if(goldUser==null){ |
|
|
|
return Result.error("用户不存在"); |
|
|
|
|
|
|
|
String jwcode = consume.getJwcode(); |
|
|
|
//String jwcode = "1bf7194c2dc63c45cd834d35e38faa71"; |
|
|
|
try { |
|
|
|
BaseDES2 d = new BaseDES2(); |
|
|
|
jwcode = d.encrypt(jwcode); |
|
|
|
} catch (Exception e) { |
|
|
|
return Result.error("加密失败"); |
|
|
|
} |
|
|
|
// String jwcode = consume.getJwcode(); |
|
|
|
String jwcode = "2e35cadd48a15cc4cd834d35e38faa71"; |
|
|
|
String op = "cost"; // 操作类型(根据实际业务定义,例如"recharge"表示充值) |
|
|
|
String content = consume.getRemark(); // 备注作为content参数 |
|
|
|
String orderNo = UUID.randomUUID().toString().replace("-", ""); // 生成唯一订单号(去除横线) |
|
|
|
|
|
|
|
// 2. 构建接口URL及参数 |
|
|
|
String apiUrl = "http://39.101.133.168:8828/mock/61/hljw/api/user/gold"; |
|
|
|
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(apiUrl) |
|
|
|
.queryParam("jwcode", jwcode) |
|
|
|
.queryParam("op", op) |
|
|
|
.queryParam("gold", consume.getPermanentBean()) |
|
|
|
.queryParam("content", content) |
|
|
|
.queryParam("order_no", orderNo); |
|
|
|
|
|
|
|
// 3. 发送GET请求 |
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(urlBuilder.toUriString(), String.class); |
|
|
|
// 2. 创建请求参数对象(使用Map或自定义实体类) |
|
|
|
Map<String, String> params = new HashMap<>(); |
|
|
|
params.put("jwcode", jwcode); |
|
|
|
int total = consume.getPermanentBean() + consume.getFreeBean(); |
|
|
|
params.put("total", String.valueOf(total)); |
|
|
|
params.put("buy", consume.getPermanentBean().toString()); |
|
|
|
params.put("free", consume.getFreeBean().toString()); |
|
|
|
params.put("content", content); |
|
|
|
|
|
|
|
// 3. 构建请求头,指定Content-Type为JSON |
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON); // 关键:设置为JSON格式 |
|
|
|
|
|
|
|
// 4. 构建请求实体(参数+头信息) |
|
|
|
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(params, headers); |
|
|
|
|
|
|
|
try { |
|
|
|
// 5. 发送POST请求 |
|
|
|
ResponseEntity<String> response = restTemplate.postForEntity( |
|
|
|
apiUrl, |
|
|
|
requestEntity, |
|
|
|
String.class |
|
|
|
); |
|
|
|
|
|
|
|
System.out.println("请求参数:" + params); |
|
|
|
System.out.println("响应状态码:" + response.getStatusCodeValue()); |
|
|
|
System.out.println("响应内容:" + response.getBody()); |
|
|
|
|
|
|
|
// 4. 处理响应结果 |
|
|
|
if (!response.getStatusCode().is2xxSuccessful()) { |
|
|
|
return Result.error("远程接口调用失败,状态码:" + response.getStatusCodeValue()); |
|
|
|
return Result.error("远程接口接口调用失败,状态码:" + response.getStatusCodeValue() + ",响应:" + response.getBody()); |
|
|
|
} |
|
|
|
return Result.success("减少成功"); |
|
|
|
} catch (Exception e) { |
|
|
|
return Result.error("系统异常:" + e.getMessage()); |
|
|
|
} catch (RestClientException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return Result.error("请求发送失败:" + e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
return Result.success(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
//筛选查询直播消费 |
|
|
|
@Override |
|
|
|