package com.example.demo.serviceImpl.bean; import com.example.demo.Util.BaseDES2; import com.example.demo.domain.vo.bean.BeanAuditInfo; import com.example.demo.domain.vo.bean.BeanRechargeInfo; import com.example.demo.domain.vo.bean.GoldBean; import com.example.demo.domain.vo.coin.Result; import com.example.demo.mapper.coin.BeanAuditMapper; import com.example.demo.service.bean.BeanAuditService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 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; /** * @program: gold-java * @ClassName BeanAuditServiceImpl * @description: * @author: Double * @create: 2025−08-01 11:37 * @Version 1.0 **/ @Service @RequiredArgsConstructor public class BeanAuditServiceImpl implements BeanAuditService { @Autowired private BeanAuditMapper beanAuditMapper; @Autowired private RestTemplate restTemplate; // 2. 构建接口URL及参数 @Value("${bean.recharge.url}") String apiUrl; //查找审核信息 @Override public PageInfo selectBy(Integer pageNum, Integer pageSize, BeanAuditInfo beanAuditInfo) { PageHelper.startPage(pageNum, pageSize); List beanAuditInfos = beanAuditMapper.selectBy(beanAuditInfo); return new PageInfo<>(beanAuditInfos); } @Transactional @Override public Result updateStatus1(BeanAuditInfo info) { BeanAuditInfo beanAuditInfo = beanAuditMapper.selectById(info.getId()); String jwcode = beanAuditInfo.getJwcode().toString(); // String jwcode = "2e35cadd48a15cc4cd834d35e38faa71"; try { BaseDES2 d = new BaseDES2(); jwcode = d.encrypt(jwcode); } catch (Exception e) { return Result.error("加密失败"); } String content = beanAuditInfo.getRemark(); // 备注作为content参数 String orderNo = UUID.randomUUID().toString().replace("-", ""); // 生成唯一订单号(去除横线) // 1. 创建请求参数对象(使用Map或自定义实体类) Map params = new HashMap<>(); params.put("jwcode", jwcode); params.put("gold_buy", beanAuditInfo.getPermanentBean().toString()); params.put("gold_free", beanAuditInfo.getFreeBean().toString()); params.put("content", content); params.put("order_no", orderNo); // 2. 构建请求头,指定Content-Type为JSON HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // 关键:设置为JSON格式 // 3. 构建请求实体(参数+头信息) HttpEntity> requestEntity = new HttpEntity<>(params, headers); try { // 4. 发送POST请求 ResponseEntity response = restTemplate.postForEntity( apiUrl, requestEntity, String.class ); System.out.println("请求参数:" + params); System.out.println("响应状态码:" + response.getStatusCodeValue()); System.out.println("响应内容:" + response.getBody()); if (!response.getStatusCode().is2xxSuccessful()) { return Result.error("远程接口接口调用失败,状态码:" + response.getStatusCodeValue() + ",响应:" + response.getBody()); } } catch (RestClientException e) { e.printStackTrace(); return Result.error("请求发送失败:" + e.getMessage()); } beanAuditMapper.updateStatus1(info); return Result.success(); } @Override public void updateStatus2(BeanAuditInfo beanAuditInfo) { beanAuditMapper.updateStatus2(beanAuditInfo); } @Override public GoldBean statsBean(BeanAuditInfo beanAuditInfo) { GoldBean gold = new GoldBean(); List beanAuditInfos = beanAuditMapper.selectBy(beanAuditInfo); // 初始化累加器 int permanentGoldSum = 0; int freeGoldSum = 0; int num = 0; // 遍历充值记录并累加金币 for (BeanAuditInfo info : beanAuditInfos) { // 累加永久金币 if (info.getPermanentBean() != null) { permanentGoldSum += info.getPermanentBean(); } // 累加免费金币 if (info.getFreeBean() != null) { freeGoldSum += info.getFreeBean(); } num++; } // 将累加结果设置到Gold对象 gold.setPermanentBean(permanentGoldSum); gold.setFreeBean(freeGoldSum); gold.setNum(num); gold.setBeanNum(permanentGoldSum + freeGoldSum); return gold; } }