You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.3 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. package com.example.demo.serviceImpl.bean;
  2. import com.example.demo.Util.BaseDES2;
  3. import com.example.demo.domain.vo.bean.*;
  4. import com.example.demo.domain.vo.coin.*;
  5. import com.example.demo.mapper.bean.BeanConsumeMapper;
  6. import com.example.demo.mapper.bean.BeanUserMapper;
  7. import com.example.demo.mapper.coin.BeanRechargeMapper1;
  8. import com.example.demo.mapper.coin.UserMapper;
  9. import com.example.demo.mapper.live.LiveMapper;
  10. import com.example.demo.service.bean.BeanConsumeService;
  11. import com.github.pagehelper.PageHelper;
  12. import com.github.pagehelper.PageInfo;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.http.HttpEntity;
  16. import org.springframework.http.HttpHeaders;
  17. import org.springframework.http.MediaType;
  18. import org.springframework.http.ResponseEntity;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import org.springframework.web.client.RestClientException;
  22. import org.springframework.web.client.RestTemplate;
  23. import org.springframework.web.util.UriComponentsBuilder;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.UUID;
  28. /**
  29. * @program: gold-java
  30. * @ClassName BeanConsumeServiceImpl
  31. * @description:
  32. * @author: Ethan
  33. * @create: 202507-30 11:53
  34. * @Version 1.0
  35. **/
  36. @Service
  37. public class BeanConsumeServiceImpl implements BeanConsumeService {
  38. @Autowired
  39. private BeanConsumeMapper beanConsumeMapper;
  40. @Autowired
  41. private LiveMapper liveMapper;
  42. @Autowired
  43. private BeanRechargeMapper1 beanRechargeMapper1;
  44. @Autowired
  45. private UserMapper userMapper;
  46. @Autowired
  47. private RestTemplate restTemplate;
  48. // 构建接口URL及参数
  49. @Value("${bean.consume.url}")
  50. String apiUrl;
  51. @Autowired
  52. private BeanUserMapper beanUserMapper;
  53. //获取消费用户分部
  54. @Override
  55. public List<String> getDept() {
  56. List<String> deptList = beanConsumeMapper.getDept();
  57. return deptList;
  58. }
  59. //获取直播消费客户分部
  60. @Override
  61. public List<String> getLiveDept() {
  62. List<String> liveDeptList = liveMapper.getDept();
  63. return liveDeptList;
  64. }
  65. //减少金豆
  66. @Transactional
  67. @Override
  68. public Result reduce(BeanConsume consume) {
  69. // 验证精网号是否有效
  70. if (consume.getJwcode() == null || consume.getJwcode().isEmpty()) {
  71. return Result.error("精网号不能为空");
  72. }
  73. // 验证免费豆和永久豆是否为非负数
  74. if (consume.getFreeBean() < 0 || consume.getPermanentBean() < 0) {
  75. return Result.error("免费豆和永久豆不能为负数");
  76. }
  77. // 验证免费豆和永久豆是否为非负数
  78. if (consume.getFreeBean() > 999999 || consume.getPermanentBean() > 999999) {
  79. return Result.error("免费豆和永久豆不能超过999999");
  80. }
  81. // 验证免费豆和永久豆是否均为0
  82. if (consume.getFreeBean() == 0 && consume.getPermanentBean() == 0) {
  83. return Result.error("免费豆和永久豆不能同时为0");
  84. }
  85. //获取用户当前余额
  86. BeanUserCard user= beanUserMapper.userCard(consume.getJwcode());
  87. if (user.getFreeBean() < consume.getFreeBean()) {
  88. return Result.error("用户免费金豆余额不足");
  89. }
  90. if (user.getPermanentBean() < consume.getPermanentBean()) {
  91. return Result.error("用户永久金豆余额不足");
  92. }
  93. // 验证备注是否为空
  94. if (consume.getRemark() == null || consume.getRemark().isEmpty()) {
  95. return Result.error("备注不能为空");
  96. }
  97. String jwcode = consume.getJwcode();
  98. //String jwcode = "1bf7194c2dc63c45cd834d35e38faa71";
  99. try {
  100. BaseDES2 d = new BaseDES2();
  101. jwcode = d.encrypt(jwcode);
  102. } catch (Exception e) {
  103. return Result.error("加密失败");
  104. }
  105. String content = consume.getRemark(); // 备注作为content参数
  106. // 2. 创建请求参数对象(使用Map或自定义实体类)
  107. Map<String, String> params = new HashMap<>();
  108. params.put("jwcode", jwcode);
  109. int total = consume.getPermanentBean() + consume.getFreeBean();
  110. params.put("total", String.valueOf(total));
  111. params.put("buy", consume.getPermanentBean().toString());
  112. params.put("free", consume.getFreeBean().toString());
  113. params.put("content", content);
  114. // 3. 构建请求头,指定Content-Type为JSON
  115. HttpHeaders headers = new HttpHeaders();
  116. headers.setContentType(MediaType.APPLICATION_JSON); // 关键:设置为JSON格式
  117. // 4. 构建请求实体(参数+头信息)
  118. HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(params, headers);
  119. try {
  120. // 5. 发送POST请求
  121. ResponseEntity<String> response = restTemplate.postForEntity(
  122. apiUrl,
  123. requestEntity,
  124. String.class
  125. );
  126. System.out.println("请求参数:" + params);
  127. System.out.println("响应状态码:" + response.getStatusCodeValue());
  128. System.out.println("响应内容:" + response.getBody());
  129. if (!response.getStatusCode().is2xxSuccessful()) {
  130. return Result.error("远程接口接口调用失败,状态码:" + response.getStatusCodeValue() + ",响应:" + response.getBody());
  131. }
  132. } catch (RestClientException e) {
  133. e.printStackTrace();
  134. return Result.error("请求发送失败:" + e.getMessage());
  135. }
  136. return Result.success();
  137. }
  138. //筛选查询直播消费
  139. @Override
  140. public Object selectLiveBy(Integer pageNum, Integer pageSize, BeanConsumeLive beanConsumeLive) {
  141. PageHelper.startPage(pageNum, pageSize);
  142. List<BeanConsumeLive> beanConsumeLives = liveMapper.selectLiveBy(beanConsumeLive);
  143. //int total = liveMapper.selectLiveCount(beanConsumeLive);
  144. return new PageInfo<>(beanConsumeLives);
  145. }
  146. //筛选查询铁粉消费
  147. @Override
  148. public Object selectFanBy(Integer pageNum, Integer pageSize, BeanConsumeFan beanConsumeFan) {
  149. PageHelper.startPage(pageNum, pageSize);
  150. List<BeanConsumeFan> beanConsumeFans = beanConsumeMapper.selectFanBy(beanConsumeFan);
  151. return new PageInfo<>(beanConsumeFans);
  152. }
  153. //筛选查询文章消费
  154. @Override
  155. public Object selectArticleBy(Integer pageNum, Integer pageSize, BeanConsumeArticle beanConsumeArticle) {
  156. PageHelper.startPage(pageNum, pageSize);
  157. List<BeanConsumeArticle> beanConsumeArticles = beanConsumeMapper.selectArticleBy(beanConsumeArticle);
  158. return new PageInfo<>(beanConsumeArticles);
  159. }
  160. //筛选查询小黄车消费
  161. @Override
  162. public Object selectCartBy(Integer pageNum, Integer pageSize, BeanConsumeCart beanConsumeCart) {
  163. PageHelper.startPage(pageNum, pageSize);
  164. List<BeanConsumeCartVo> beanConsumeCarts = beanRechargeMapper1.selectCartBy(beanConsumeCart);
  165. return new PageInfo<>(beanConsumeCarts);
  166. }
  167. @Override
  168. public BeanConsumeGold sumConsumeGold(Page page) {
  169. BeanConsumeGold gold = new BeanConsumeGold();
  170. Integer payType = page.getPayType();
  171. if (payType==1) {
  172. //直播消费合计
  173. gold = liveMapper.selectSumLiveBy(page.getBeanConsumeLive());
  174. }else if (payType==7) {
  175. //充值消费合计
  176. gold = beanConsumeMapper.selectSumFansBy(page.getBeanConsumeFan());
  177. }else if (payType==8) {
  178. //文章消费合计
  179. gold = beanConsumeMapper.selectSumArticleBy(page.getBeanConsumeArticle());
  180. }
  181. return gold;
  182. }
  183. //查询直播礼物
  184. @Override
  185. public List<String> getLiveGift() {
  186. return liveMapper.getLiveGift();
  187. }
  188. //查询直播全部频道
  189. @Override
  190. public List<String> getLiveChannel() {
  191. return liveMapper.getLiveChannel();
  192. }
  193. }