LocalSysFileServiceImpl.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.RemoteAttendanceService;
  16. import com.boman.web.core.api.RemoteObjService;
  17. import org.apache.commons.lang3.BooleanUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.cloud.context.config.annotation.RefreshScope;
  22. import org.springframework.context.annotation.Primary;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import javax.annotation.Resource;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.io.IOException;
  28. import java.util.*;
  29. import static com.boman.common.core.utils.obj.ObjectUtils.map;
  30. /**
  31. * 本地文件存储
  32. *
  33. * @author ruoyi
  34. */
  35. @Primary
  36. @Service
  37. @RefreshScope
  38. public class LocalSysFileServiceImpl implements ISysFileService
  39. {
  40. private static final Logger LOGGER = LoggerFactory.getLogger(LocalSysFileServiceImpl.class);
  41. @Resource
  42. private RemoteObjService remoteObjService;
  43. @Resource
  44. private RedisService redisService;
  45. @Resource
  46. private RemoteAttendanceService remoteAttendanceService;
  47. /**
  48. * 资源映射路径 前缀
  49. */
  50. @Value("${file.prefix}")
  51. public String localFilePrefix;
  52. /**
  53. * 域名或本机访问地址
  54. */
  55. @Value("${file.domain}")
  56. public String domain;
  57. /**
  58. * 上传文件存储在本地的根路径
  59. */
  60. @Value("${file.path}")
  61. private String localFilePath;
  62. /******************************* 考勤表中的常量 *******************************/
  63. public static final String USERNAME= "userName";
  64. public static final String DEPT_NAME= "deptName";
  65. public static final String ATTENDANCE_TABLE_LEAVE_OR_SUM= "attendanceTableLeaveOrSum";
  66. public static final String ATTENDANCE_TABLE_LATE_SUM= "attendanceTableLateSum";
  67. public static final String ATTENDANCE_TABLE_LEAVE_SUM= "attendanceTableLeaveSum";
  68. public static final String DATE= "date";
  69. /**
  70. * 本地文件上传接口
  71. *
  72. * @param file 上传的文件
  73. * @return 访问地址
  74. * @throws Exception
  75. */
  76. @Override
  77. public String uploadFile(MultipartFile file) throws Exception
  78. {
  79. String name = FileUploadUtils.upload(localFilePath, file);
  80. String url = domain + localFilePrefix + name;
  81. LOGGER.info("上传的路径为: {}", url);
  82. return url;
  83. }
  84. /**
  85. * 功能描述: 上传base64
  86. *
  87. * @param base64 base64
  88. * @return java.lang.String
  89. */
  90. @Override
  91. public String uploadFileBase64(String base64) throws IOException {
  92. MultipartFile multipartFile = FileUploadUtils.base64ToMultipart(base64);
  93. String name = FileUploadUtils.upload(localFilePath, multipartFile);
  94. String path = domain + localFilePrefix + name;
  95. LOGGER.info("上传的路径为: {}", path);
  96. return path;
  97. }
  98. /**
  99. * 功能描述: 通用的导入接口
  100. *
  101. * @param multipartFile multipartFile
  102. * @param tableName tableName
  103. * @return java.util.List<com.alibaba.fastjson.JSONObject>
  104. */
  105. @Override
  106. public AjaxResult importExcelCommon(MultipartFile multipartFile, String tableName) throws Exception {
  107. Objects.requireNonNull(multipartFile, "multipartFile is empty");
  108. ObjectUtils.requireNonNull(tableName, "tableName is empty");
  109. GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  110. List<GenTableColumn> columns = genTable.getColumns();
  111. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  112. List<JSONObject> list = util.importCommonExcel("", multipartFile.getInputStream(), columns);
  113. ImportExcelDto dto = new ImportExcelDto();
  114. dto.setDataList(list);
  115. dto.setTableName(tableName);
  116. return remoteObjService.importCommonData(dto);
  117. }
  118. /**
  119. * 功能描述: 通用的导出接口
  120. *
  121. * @param response response
  122. * @param dto tableName
  123. * empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
  124. * condition 查询条件
  125. * @return com.boman.domain.dto.AjaxResult
  126. */
  127. @Override
  128. public AjaxResult exportExcelCommon(HttpServletResponse response, ExportExcelDto dto) {
  129. String tableName = dto.getTableName();
  130. Boolean empty = dto.getEmpty();
  131. ObjectUtils.requireNonNull(tableName, "exportExcelCommon tableName is empty");
  132. ObjectUtils.requireNonNull(empty, "exportExcelCommon empty is empty");
  133. GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  134. List<GenTableColumn> columns = genTable.getColumns();
  135. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  136. List<Map<String, Object>> list = null;
  137. if (BooleanUtils.isTrue(empty)) {
  138. list = new ArrayList<>(0);
  139. } else {
  140. FormDataDto condition = new FormDataDto();
  141. condition.setTable(tableName);
  142. condition.setFixedData(new JSONObject(dto.getCondition()));
  143. AjaxResult ajaxResult = remoteObjService.getByMap(condition);
  144. if (AjaxResult.checkSuccess(ajaxResult)) {
  145. list = ((List<Map<String, Object>>) ajaxResult.get(AjaxResult.DATA_TAG));
  146. columns = ExcelUtil.filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
  147. handleNullColumnValue(list, map(columns, GenTableColumn::getColumnName));
  148. }
  149. }
  150. try {
  151. util.exportExcelCommon(response, list, "sheet1", columns, empty, false);
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. return null;
  156. }
  157. /**
  158. * 功能描述: 导出数据,只是sql不一样,其余的都是通用接口的内容
  159. *
  160. * @param response response
  161. * @param dto tableName
  162. * empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
  163. * condition 查询条件
  164. * @return com.boman.domain.dto.AjaxResult
  165. */
  166. @Override
  167. public AjaxResult statisticsByMonth(HttpServletResponse response, ExportExcelDto dto) {
  168. String tableName = dto.getTableName();
  169. Boolean empty = dto.getEmpty();
  170. ObjectUtils.requireNonNull(tableName, "exportExcelCommon tableName is empty");
  171. ObjectUtils.requireNonNull(empty, "exportExcelCommon empty is empty");
  172. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  173. List<Map<String, Object>> list = null;
  174. if (BooleanUtils.isTrue(empty)) {
  175. list = new ArrayList<>(0);
  176. } else {
  177. AjaxResult ajaxResult = remoteAttendanceService.statisticsByMonth(dto.getCondition());
  178. if (AjaxResult.checkSuccess(ajaxResult)) {
  179. list = ((List<Map<String, Object>>) ajaxResult.get(AjaxResult.DATA_TAG));
  180. // 导出不需要user_id, 同时把map中的value换成int
  181. List<Map<String, Object>> newList = new ArrayList<>(list.size());
  182. for (Map<String, Object> map : list) {
  183. map.remove("user_id");
  184. for (Map.Entry<String, Object> entry : map.entrySet()) {
  185. Object value = entry.getValue();
  186. if (value instanceof Double) {
  187. Double value1 = (Double) value;
  188. entry.setValue(value1.intValue());
  189. }
  190. }
  191. Map<String, Object> newMap = new LinkedHashMap<>(map.size());
  192. newMap.put(USERNAME, map.get(USERNAME));
  193. newMap.put(DEPT_NAME, map.get(DEPT_NAME));
  194. newMap.put(ATTENDANCE_TABLE_LATE_SUM, map.get(ATTENDANCE_TABLE_LATE_SUM));
  195. newMap.put(ATTENDANCE_TABLE_LEAVE_SUM, map.get(ATTENDANCE_TABLE_LEAVE_SUM));
  196. newMap.put(ATTENDANCE_TABLE_LEAVE_OR_SUM, map.get(ATTENDANCE_TABLE_LEAVE_OR_SUM));
  197. newMap.put(DATE, map.get(DATE));
  198. newList.add(newMap);
  199. }
  200. list = newList;
  201. }
  202. }
  203. try {
  204. util.exportExcelCommon(response, list, "sheet1", null, empty, true);
  205. } catch (IOException e) {
  206. LOGGER.error("考勤统计导出失败,导出数据为:{}", list);
  207. e.printStackTrace();
  208. }
  209. return null;
  210. }
  211. /**
  212. * 功能描述: 通用的导入接口
  213. *
  214. * @param dto@return java.util.List<com.alibaba.fastjson.JSONObject>
  215. */
  216. @Override
  217. public List<JSONObject> importExcelCommon(ImportExcelDto dto) throws Exception {
  218. return null;
  219. }
  220. public void handleNullColumnValue(List<Map<String, Object>> result, List<String> showData) {
  221. for (Map<String, Object> map : result) {
  222. Set<String> resultKeySet = map.keySet();
  223. for (String columnName : showData) {
  224. if (!resultKeySet.contains(columnName)) {
  225. map.put(columnName, "");
  226. }
  227. }
  228. }
  229. }
  230. }