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.

205 lines
7.7 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. package com.example.demo.serviceImpl.coin;
  2. import com.example.demo.Util.JWTUtil;
  3. import com.example.demo.Util.RedisUtil;
  4. import com.example.demo.domain.DTO.GoldDetailDTO;
  5. import com.example.demo.domain.DTO.GoldUserDTO;
  6. import com.example.demo.domain.entity.Admin;
  7. import com.example.demo.domain.entity.User;
  8. import com.example.demo.exception.SystemException;
  9. import com.example.demo.mapper.coin.GoldDetailMapper;
  10. import com.example.demo.service.coin.AdminService;
  11. import com.example.demo.service.coin.GoldDetailService;
  12. import com.fasterxml.jackson.databind.ObjectMapper;
  13. import com.github.pagehelper.PageHelper;
  14. import com.github.pagehelper.PageInfo;
  15. import jakarta.servlet.http.HttpServletRequest;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.security.core.userdetails.UserDetails;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.web.context.request.RequestContextHolder;
  20. import org.springframework.web.context.request.ServletRequestAttributes;
  21. import com.example.demo.domain.vo.coin.*;
  22. import java.math.BigDecimal;
  23. import java.time.LocalDateTime;
  24. import java.time.format.DateTimeFormatter;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * @program: GOLD
  30. * @ClassName GoldDetailServiceImpl
  31. * @description:
  32. * @author: huangqizhen
  33. * @create: 202506-23 13:44
  34. * @Version 1.0
  35. **/
  36. @Service
  37. public class GoldDetailServiceImpl implements GoldDetailService {
  38. @Autowired
  39. private GoldDetailMapper goldDetailMapper;
  40. @Autowired
  41. private RedisUtil redisUtil;
  42. @Autowired
  43. private AdminService adminService;
  44. @Override
  45. public PageInfo<GoldDetail> getGoldDetail(Integer pageNum, Integer pageSize, GoldDetail goldDetail) {
  46. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  47. // System.out.println(goldDetail.getMarkets());
  48. List<GoldDetail> list = goldDetailMapper.getGoldDetail(goldDetail);
  49. return new PageInfo<>(list);
  50. }
  51. @Override
  52. public Total getTotal(GoldDetail goldDetail) {
  53. return goldDetailMapper.getTotal(goldDetail);
  54. }
  55. @Override
  56. public PageInfo<User> getGold(Integer pageNum, Integer pageSize, User user) {
  57. PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper
  58. List<User> list = goldDetailMapper.getGold(user);
  59. return new PageInfo<>(list);
  60. }
  61. @Override
  62. public Total GoldTotal(User user) {
  63. return goldDetailMapper.GoldTotal(user);
  64. }
  65. /*
  66. 用户金币合计数
  67. */
  68. @Override
  69. public Gold sumGold(Integer pageNum, Integer pageSize, GoldDetail goldDetail) {
  70. Gold gold=goldDetailMapper.sumGold(goldDetail);
  71. return gold;
  72. }
  73. @Override
  74. public Result addExportRecord(GoldDetailDTO dto) {
  75. // 获取操作者 jwcode
  76. HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  77. String token = request.getHeader("token");
  78. try {
  79. UserDetails token1= JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class);
  80. dto.setAccount(Integer.valueOf(token1.getUsername()));
  81. } catch (Exception e) {
  82. throw new RuntimeException(e.getMessage());
  83. }
  84. String admin = adminService.getName(String.valueOf(dto.getAccount()));
  85. // 生成文件名
  86. String fileName = String.format("%s_%s_%s.xlsx",
  87. "客户金币明细",
  88. admin,
  89. LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
  90. System.out.println(fileName);
  91. dto.setUrl("");
  92. dto.setFileName(fileName);
  93. dto.setDataNum(0);
  94. try{
  95. // 调用方式
  96. GoldDetailMapper.ExportRecordIdHolder idHolder = new GoldDetailMapper.ExportRecordIdHolder();
  97. goldDetailMapper.insertExportRecord(
  98. idHolder, // 用于接收主键
  99. dto.getAccount(),
  100. dto.getType(),
  101. dto.getState(),
  102. dto.getUrl(),
  103. dto.getFileName(),
  104. dto.getDataNum()
  105. );
  106. // 获取主键
  107. Long recordId = idHolder.getId();
  108. // 2. 构造完整的 JSON 数据(包含所有请求参数)
  109. Map<String, Object> exportData = new HashMap<>();
  110. exportData.put("recordId", recordId);
  111. // 手动构造请求数据(避免 toString() 只返回部分字段)
  112. Map<String, String> headers = new HashMap<>();
  113. Map<String, Object> requestData = new HashMap<>();
  114. requestData.put("token", token);
  115. requestData.put("goldDetail", dto.getGoldDetail());
  116. exportData.put("requestData", requestData);
  117. // 3. 发送到 Redis 消息队列
  118. String jsonData = new ObjectMapper().writeValueAsString(exportData);
  119. redisUtil.sendMessage("hwgold:queue:export_queue", jsonData);
  120. }catch (Exception e){
  121. e.printStackTrace();
  122. throw new SystemException("导出数据异常,请稍后重试", e);
  123. }
  124. return Result.success();
  125. }
  126. @Override
  127. public Result addExportRecordGold(GoldUserDTO dto) {
  128. // 获取操作者 jwcode
  129. HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  130. String token = request.getHeader("token");
  131. try {
  132. UserDetails token1= JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class);
  133. dto.setAccount(Integer.valueOf(token1.getUsername()));
  134. } catch (Exception e) {
  135. throw new RuntimeException(e.getMessage());
  136. }
  137. String admin = adminService.getName(String.valueOf(dto.getAccount()));
  138. // 生成文件名
  139. String fileName = String.format("%s_%s_%s.xlsx",
  140. "金币余额明细",
  141. admin,
  142. LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
  143. System.out.println(fileName);
  144. dto.setUrl("");
  145. dto.setFileName(fileName);
  146. dto.setDataNum(0);
  147. try{
  148. // 调用方式
  149. GoldDetailMapper.ExportRecordIdHolder idHolder = new GoldDetailMapper.ExportRecordIdHolder();
  150. goldDetailMapper.insertExportRecord(
  151. idHolder, // 用于接收主键
  152. dto.getAccount(),
  153. dto.getType(),
  154. dto.getState(),
  155. dto.getUrl(),
  156. dto.getFileName(),
  157. dto.getDataNum()
  158. );
  159. // 获取主键
  160. Long recordId = idHolder.getId();
  161. // 2. 构造完整的 JSON 数据(包含所有请求参数)
  162. Map<String, Object> exportData = new HashMap<>();
  163. exportData.put("recordId", recordId);
  164. // 手动构造请求数据(避免 toString() 只返回部分字段)
  165. Map<String, Object> requestData = new HashMap<>();
  166. requestData.put("token", token);
  167. requestData.put("user", dto.getUser());
  168. exportData.put("requestData", requestData);
  169. // 3. 发送到 Redis 消息队列
  170. String jsonData = new ObjectMapper().writeValueAsString(exportData);
  171. redisUtil.sendMessage("gold:queue:export_queue", jsonData);
  172. }catch (Exception e){
  173. e.printStackTrace();
  174. throw new SystemException("导出数据异常,请稍后重试", e);
  175. }
  176. return Result.success();
  177. }
  178. //更新用户消费次数
  179. @Override
  180. public Result updateConsumeNum() {
  181. return goldDetailMapper.updateConsumeNum();
  182. }
  183. }