MatchmakerController.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.zhentao.controller;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.zhentao.vo.MatchmakerExcelVO;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.jdbc.core.JdbcTemplate;
  6. import org.springframework.web.bind.annotation.*;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.net.URLEncoder;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.stream.Collectors;
  13. /**
  14. * 红娘管理控制器(管理端)
  15. */
  16. @RestController
  17. @RequestMapping("/admin/matchmaker")
  18. @CrossOrigin(origins = "*")
  19. public class MatchmakerController {
  20. @Autowired
  21. private JdbcTemplate jdbcTemplate;
  22. /**
  23. * 导出红娘数据为Excel
  24. */
  25. @GetMapping("/export")
  26. public void exportMatchmakerList(
  27. HttpServletResponse response,
  28. @RequestParam(value = "matchmakerType", required = false) Integer matchmakerType,
  29. @RequestParam(value = "level", required = false) Integer level,
  30. @RequestParam(value = "keyword", required = false) String keyword) throws IOException {
  31. try {
  32. // 构建SQL查询
  33. StringBuilder sql = new StringBuilder();
  34. sql.append("SELECT m.matchmaker_id, m.real_name, m.phone, m.matchmaker_type, ");
  35. sql.append("m.level, m.success_couples, m.status, m.created_at, ");
  36. sql.append("c.name as city_name ");
  37. sql.append("FROM matchmakers m ");
  38. sql.append("LEFT JOIN area c ON m.city_id = c.id ");
  39. sql.append("WHERE 1=1 ");
  40. // 类型筛选
  41. if (matchmakerType != null) {
  42. sql.append("AND m.matchmaker_type = ").append(matchmakerType).append(" ");
  43. }
  44. // 等级筛选
  45. if (level != null) {
  46. sql.append("AND m.level = ").append(level).append(" ");
  47. }
  48. // 关键词搜索
  49. if (keyword != null && !keyword.trim().isEmpty()) {
  50. String kw = keyword.trim().replace("'", "''");
  51. sql.append("AND (m.real_name LIKE '%").append(kw).append("%' ");
  52. sql.append("OR m.phone LIKE '%").append(kw).append("%') ");
  53. }
  54. sql.append("ORDER BY m.created_at DESC");
  55. // 执行查询
  56. List<Map<String, Object>> results = jdbcTemplate.queryForList(sql.toString());
  57. // 转换为Excel VO
  58. List<MatchmakerExcelVO> excelData = results.stream()
  59. .map(MatchmakerExcelVO::fromMap)
  60. .collect(Collectors.toList());
  61. // 设置响应头
  62. String fileName = "红娘列表_" + System.currentTimeMillis() + ".xlsx";
  63. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  64. response.setCharacterEncoding("utf-8");
  65. response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
  66. // 写入Excel
  67. EasyExcel.write(response.getOutputStream(), MatchmakerExcelVO.class)
  68. .sheet("红娘列表")
  69. .doWrite(excelData);
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  73. response.setContentType("application/json;charset=UTF-8");
  74. response.getWriter().write("{\"code\":500,\"msg\":\"导出失败:" + e.getMessage() + "\"}");
  75. }
  76. }
  77. }