ExportService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.dk.mdm.service.common;
  2. import com.dk.common.infrastructure.config.ApplicationContextUtil;
  3. import com.dk.common.infrastructure.constant.Constant;
  4. import com.dk.common.model.pojo.PageList;
  5. import com.dk.common.response.ResponseResultUtil;
  6. import com.dk.common.response.ResponseResultVO;
  7. import com.dk.common.util.ExcelPoiUtils;
  8. import com.dk.mdm.config.MesProxy;
  9. import com.dk.mdm.mapper.core.JobMapper;
  10. import com.dk.mdm.mapper.core.PostMapper;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.ibatis.session.SqlSession;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.lang.reflect.Method;
  16. import java.lang.reflect.Proxy;
  17. import java.time.LocalDateTime;
  18. import java.time.format.DateTimeFormatter;
  19. import java.util.*;
  20. /**
  21. * @author : 周兴
  22. * @desc : 控件数据源API
  23. * @date : 2023/1/3 17:21
  24. */
  25. @Service
  26. @Slf4j
  27. public class ExportService {
  28. @Autowired
  29. private JobMapper jobMapper;
  30. @Autowired
  31. private PostMapper postMapper;
  32. @Autowired
  33. private SqlSession sqlSession;
  34. /**
  35. * @desc : 导出excel
  36. * @author : 周兴
  37. * @date : 2023/2/22 15:13
  38. */
  39. // public void exportList(HttpServletResponse response, Map<String, Object> param,Map<String, Object> condition) {
  40. public ResponseResultVO<String> exportList(Map<String, Object> param) {
  41. Map<String, Object> condition = (Map<String, Object>) param.get("condition");
  42. Constant.FunUuidConstant constant = Constant.FunUuidConstant.get(param.get("name").toString());
  43. String title = param.get("title").toString();
  44. // 分页参数赋值
  45. if(param.get("pageSize") != null){
  46. param = this.getLimit(param);
  47. }
  48. List list = null;
  49. try {
  50. Class interfz = Class.forName(constant.getInterfz());
  51. Object instance = Proxy.newProxyInstance(interfz.getClassLoader(),new Class[]{interfz} ,new MesProxy(sqlSession.getMapper(interfz)));
  52. Method selectMethod;
  53. // 查询条件是map
  54. if(constant.getMapFlag()){
  55. selectMethod = instance.getClass().getDeclaredMethod(constant.getMethod(),Map.class);
  56. }else{
  57. selectMethod = instance.getClass().getDeclaredMethod(constant.getMethod(),Object.class);
  58. }
  59. list = (List)selectMethod.invoke(instance,param);
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. if (list != null && list.size() > 0) {
  64. // 处理数据
  65. List<Map<String, Object>> mapList = filterExportColumns(condition, list);
  66. String path = ExcelPoiUtils.createExcel(mapList, title + "_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + ".xlsx", title);
  67. return ResponseResultUtil.success(path);
  68. }
  69. return ResponseResultUtil.success("");
  70. }
  71. /**
  72. * @desc : 把list转成List<Map>,根据前台的传入的列进行过滤
  73. * @author : 周兴
  74. * @date : 2023/2/22 15:27
  75. */
  76. private List<Map<String, Object>> filterExportColumns(Map<String, Object> param, List list) {
  77. List<Map<String, Object>> mapList = new ArrayList<>();
  78. list.forEach(it -> {
  79. Map<String, Object> map = new LinkedHashMap<>();
  80. Set<String> mapKeys = param.keySet();
  81. for (String key : mapKeys) {
  82. map.put(param.get(key).toString(), getFieldValueByName(key, it));
  83. }
  84. // for (int i = 0; i < fields.length; i++) {
  85. // if (param.containsKey(fields[i].getName())) {
  86. // map.put(param.get(fields[i].getName()).toString(), getFieldValueByName(fields[i].getName(), it));
  87. // }
  88. // }
  89. mapList.add(map);
  90. });
  91. return mapList;
  92. }
  93. /**
  94. * @desc : 获取实体值
  95. * @author : 周兴
  96. * @date : 2023/2/22 15:46
  97. */
  98. private Object getFieldValueByName(String fieldName, Object o) {
  99. try {
  100. Object value;
  101. if (o instanceof Map) {
  102. value = ((Map<?, ?>) o).get(fieldName);
  103. } else {
  104. String firstLetter = fieldName.substring(0, 1).toUpperCase();
  105. String getter = "get" + firstLetter + fieldName.substring(1);
  106. Method method = o.getClass().getMethod(getter, new Class[]{});
  107. value = method.invoke(o, new Object[]{});
  108. }
  109. return value;
  110. } catch (Exception e) {
  111. return null;
  112. }
  113. }
  114. /**
  115. * @desc : 设置分页参数
  116. * @author : 姜宁
  117. * @date : 2023/2/1 14:00
  118. */
  119. private Map<String, Object> getLimit(Map<String, Object> param) {
  120. if (param.get("limit") != null) {
  121. param.put("currentPage", 1);
  122. param.put("pageSize", param.get("limit"));
  123. }
  124. param.put("start", ((int) param.get("currentPage") - 1) * (int) param.get("pageSize"));
  125. param.put("end", param.get("pageSize"));
  126. return param;
  127. }
  128. }