LocalSysFileServiceImpl.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.boman.file.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.boman.common.core.utils.obj.ObjectUtils;
  4. import com.boman.common.core.utils.poi.ExcelUtil;
  5. import com.boman.domain.dto.AjaxResult;
  6. import com.boman.common.redis.RedisKey;
  7. import com.boman.common.redis.service.RedisService;
  8. import com.boman.domain.GenTable;
  9. import com.boman.domain.GenTableColumn;
  10. import com.boman.domain.constant.MaskConstant;
  11. import com.boman.domain.dto.ExportExcelDto;
  12. import com.boman.domain.dto.FormDataDto;
  13. import com.boman.domain.dto.ImportExcelDto;
  14. import com.boman.file.utils.FileUploadUtils;
  15. import com.boman.web.core.api.RemoteObjService;
  16. import org.apache.commons.lang3.BooleanUtils;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.cloud.context.config.annotation.RefreshScope;
  19. import org.springframework.context.annotation.Primary;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.annotation.Resource;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.IOException;
  25. import java.util.*;
  26. import static com.boman.common.core.utils.obj.ObjectUtils.map;
  27. /**
  28. * 本地文件存储
  29. *
  30. * @author ruoyi
  31. */
  32. @Primary
  33. @Service
  34. @RefreshScope
  35. public class LocalSysFileServiceImpl implements ISysFileService
  36. {
  37. @Resource
  38. private RemoteObjService remoteObjService;
  39. @Resource
  40. private RedisService redisService;
  41. /**
  42. * 资源映射路径 前缀
  43. */
  44. @Value("${file.prefix}")
  45. public String localFilePrefix;
  46. /**
  47. * 域名或本机访问地址
  48. */
  49. @Value("${file.domain}")
  50. public String domain;
  51. /**
  52. * 上传文件存储在本地的根路径
  53. */
  54. @Value("${file.path}")
  55. private String localFilePath;
  56. /**
  57. * 本地文件上传接口
  58. *
  59. * @param file 上传的文件
  60. * @return 访问地址
  61. * @throws Exception
  62. */
  63. @Override
  64. public String uploadFile(MultipartFile file) throws Exception
  65. {
  66. String name = FileUploadUtils.upload(localFilePath, file);
  67. String url = domain + localFilePrefix + name;
  68. return url;
  69. }
  70. /**
  71. * 功能描述: 上传base64
  72. *
  73. * @param base64 base64
  74. * @return java.lang.String
  75. */
  76. @Override
  77. public String uploadFileBase64(String base64) throws IOException {
  78. MultipartFile multipartFile = FileUploadUtils.base64ToMultipart(base64);
  79. String name = FileUploadUtils.upload(localFilePath, multipartFile);
  80. return domain + localFilePrefix + name;
  81. }
  82. /**
  83. * 功能描述: 通用的导入接口
  84. *
  85. * @param multipartFile multipartFile
  86. * @param tableName tableName
  87. * @return java.util.List<com.alibaba.fastjson.JSONObject>
  88. */
  89. @Override
  90. public AjaxResult importExcelCommon(MultipartFile multipartFile, String tableName) throws Exception {
  91. Objects.requireNonNull(multipartFile, "multipartFile is empty");
  92. ObjectUtils.requireNonNull(tableName, "tableName is empty");
  93. GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  94. List<GenTableColumn> columns = genTable.getColumns();
  95. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  96. List<JSONObject> list = util.importCommonExcel("", multipartFile.getInputStream(), columns);
  97. ImportExcelDto dto = new ImportExcelDto();
  98. dto.setDataList(list);
  99. dto.setTableName(tableName);
  100. return remoteObjService.importCommonData(dto);
  101. }
  102. /**
  103. * 功能描述: 通用的导出接口
  104. *
  105. * @param response response
  106. * @param dto tableName
  107. * empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
  108. * condition 查询条件
  109. * @return com.boman.domain.dto.AjaxResult
  110. */
  111. @Override
  112. public AjaxResult exportExcelCommon(HttpServletResponse response, ExportExcelDto dto) {
  113. String tableName = dto.getTableName();
  114. Boolean empty = dto.getEmpty();
  115. ObjectUtils.requireNonNull(tableName, "exportExcelCommon tableName is empty");
  116. ObjectUtils.requireNonNull(empty, "exportExcelCommon empty is empty");
  117. GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  118. List<GenTableColumn> columns = genTable.getColumns();
  119. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  120. List<Map<String, Object>> list = null;
  121. if (BooleanUtils.isTrue(empty)) {
  122. list = new ArrayList<>(0);
  123. } else {
  124. FormDataDto condition = new FormDataDto();
  125. condition.setTable(tableName);
  126. condition.setFixedData(new JSONObject(dto.getCondition()));
  127. AjaxResult ajaxResult = remoteObjService.getByMap(condition);
  128. if (AjaxResult.checkSuccess(ajaxResult)) {
  129. list = ((List<Map<String, Object>>) ajaxResult.get(AjaxResult.DATA_TAG));
  130. columns = ExcelUtil.filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
  131. handleNullColumnValue(list, map(columns, GenTableColumn::getColumnName));
  132. }
  133. }
  134. try {
  135. util.exportExcelCommon(response, list, "sheet1", columns, empty);
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. return null;
  140. }
  141. /**f
  142. * 功能描述: 通用的导入接口
  143. *
  144. * @param dto@return java.util.List<com.alibaba.fastjson.JSONObject>
  145. */
  146. @Override
  147. public List<JSONObject> importExcelCommon(ImportExcelDto dto) throws Exception {
  148. return null;
  149. }
  150. public void handleNullColumnValue(List<Map<String, Object>> result, List<String> showData) {
  151. for (Map<String, Object> map : result) {
  152. Set<String> resultKeySet = map.keySet();
  153. for (String columnName : showData) {
  154. if (!resultKeySet.contains(columnName)) {
  155. map.put(columnName, "");
  156. }
  157. }
  158. }
  159. }
  160. }