package com.example.demo.serviceImpl.coin; import com.example.demo.Util.JWTUtil; import com.example.demo.Util.RedisUtil; import com.example.demo.domain.DTO.GoldDetailDTO; import com.example.demo.domain.DTO.GoldUserDTO; import com.example.demo.domain.entity.Admin; import com.example.demo.domain.entity.User; import com.example.demo.exception.SystemException; import com.example.demo.mapper.coin.GoldDetailMapper; import com.example.demo.service.coin.AdminService; import com.example.demo.service.coin.GoldDetailService; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.example.demo.domain.vo.coin.*; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @program: GOLD * @ClassName GoldDetailServiceImpl * @description: * @author: huangqizhen * @create: 2025−06-23 13:44 * @Version 1.0 **/ @Service public class GoldDetailServiceImpl implements GoldDetailService { @Autowired private GoldDetailMapper goldDetailMapper; @Autowired private RedisUtil redisUtil; @Autowired private AdminService adminService; @Override public PageInfo getGoldDetail(Integer pageNum, Integer pageSize, GoldDetail goldDetail) { PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper // System.out.println(goldDetail.getMarkets()); List list = goldDetailMapper.getGoldDetail(goldDetail); return new PageInfo<>(list); } @Override public Total getTotal(GoldDetail goldDetail) { return goldDetailMapper.getTotal(goldDetail); } @Override public PageInfo getGold(Integer pageNum, Integer pageSize, User user) { PageHelper.startPage(pageNum, pageSize); //必须要直接跟mapper List list = goldDetailMapper.getGold(user); return new PageInfo<>(list); } @Override public Total GoldTotal(User user) { return goldDetailMapper.GoldTotal(user); } /* 用户金币合计数 */ @Override public Gold sumGold(Integer pageNum, Integer pageSize, GoldDetail goldDetail) { Gold gold=goldDetailMapper.sumGold(goldDetail); return gold; } @Override public Result addExportRecord(GoldDetailDTO dto) { // 获取操作者 jwcode HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String token = request.getHeader("token"); try { UserDetails token1= JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class); dto.setAccount(Integer.valueOf(token1.getUsername())); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } String admin = adminService.getName(String.valueOf(dto.getAccount())); // 生成文件名 String fileName = String.format("%s_%s_%s.xlsx", "客户金币明细", admin, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); System.out.println(fileName); dto.setUrl(""); dto.setFileName(fileName); dto.setDataNum(0); try{ // 调用方式 GoldDetailMapper.ExportRecordIdHolder idHolder = new GoldDetailMapper.ExportRecordIdHolder(); goldDetailMapper.insertExportRecord( idHolder, // 用于接收主键 dto.getAccount(), dto.getType(), dto.getState(), dto.getUrl(), dto.getFileName(), dto.getDataNum() ); // 获取主键 Long recordId = idHolder.getId(); // 2. 构造完整的 JSON 数据(包含所有请求参数) Map exportData = new HashMap<>(); exportData.put("recordId", recordId); // 手动构造请求数据(避免 toString() 只返回部分字段) Map headers = new HashMap<>(); Map requestData = new HashMap<>(); requestData.put("token", token); requestData.put("goldDetail", dto.getGoldDetail()); exportData.put("requestData", requestData); // 3. 发送到 Redis 消息队列 String jsonData = new ObjectMapper().writeValueAsString(exportData); redisUtil.sendMessage("hwgold:queue:export_queue", jsonData); }catch (Exception e){ e.printStackTrace(); throw new SystemException("导出数据异常,请稍后重试", e); } return Result.success(); } @Override public Result addExportRecordGold(GoldUserDTO dto) { // 获取操作者 jwcode HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String token = request.getHeader("token"); try { UserDetails token1= JWTUtil.getUserDetailsList(String.valueOf(token), Admin.class); dto.setAccount(Integer.valueOf(token1.getUsername())); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } String admin = adminService.getName(String.valueOf(dto.getAccount())); // 生成文件名 String fileName = String.format("%s_%s_%s.xlsx", "金币余额明细", admin, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); System.out.println(fileName); dto.setUrl(""); dto.setFileName(fileName); dto.setDataNum(0); try{ // 调用方式 GoldDetailMapper.ExportRecordIdHolder idHolder = new GoldDetailMapper.ExportRecordIdHolder(); goldDetailMapper.insertExportRecord( idHolder, // 用于接收主键 dto.getAccount(), dto.getType(), dto.getState(), dto.getUrl(), dto.getFileName(), dto.getDataNum() ); // 获取主键 Long recordId = idHolder.getId(); // 2. 构造完整的 JSON 数据(包含所有请求参数) Map exportData = new HashMap<>(); exportData.put("recordId", recordId); // 手动构造请求数据(避免 toString() 只返回部分字段) Map requestData = new HashMap<>(); requestData.put("token", token); requestData.put("user", dto.getUser()); exportData.put("requestData", requestData); // 3. 发送到 Redis 消息队列 String jsonData = new ObjectMapper().writeValueAsString(exportData); redisUtil.sendMessage("gold:queue:export_queue", jsonData); }catch (Exception e){ e.printStackTrace(); throw new SystemException("导出数据异常,请稍后重试", e); } return Result.success(); } //更新用户消费次数 @Override public Result updateConsumeNum() { return goldDetailMapper.updateConsumeNum(); } }