WfAssetManageServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.xjrsoft.module.asset.service.impl;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.support.ExcelTypeEnum;
  4. import com.alibaba.excel.util.ListUtils;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.fasterxml.jackson.core.JsonProcessingException;
  7. import com.fasterxml.jackson.core.type.TypeReference;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.github.yulichang.base.MPJBaseServiceImpl;
  10. import com.xjrsoft.common.page.ConventPage;
  11. import com.xjrsoft.module.asset.dto.WfAssetManageConditionalSearchQueryDto;
  12. import com.xjrsoft.module.asset.dto.WfAssetManagePageDto;
  13. import com.xjrsoft.module.asset.dto.WfAssetManageSelectRecordQueryDto;
  14. import com.xjrsoft.module.asset.entity.WfAssetManage;
  15. import com.xjrsoft.module.asset.mapper.WfAssetManageMapper;
  16. import com.xjrsoft.module.asset.service.IWfAssetManageService;
  17. import com.xjrsoft.module.asset.vo.WfAssetManagePageVo;
  18. import com.xjrsoft.module.asset.vo.WfAssetManageQueryVo;
  19. import lombok.AllArgsConstructor;
  20. import org.springframework.stereotype.Service;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * @title: 资产管理
  29. * @Author dzx
  30. * @Date: 2024-03-28
  31. * @Version 1.0
  32. */
  33. @Service
  34. @AllArgsConstructor
  35. public class WfAssetManageServiceImpl extends MPJBaseServiceImpl<WfAssetManageMapper, WfAssetManage> implements IWfAssetManageService {
  36. private final WfAssetManageMapper wfAssetManageMapper;
  37. @Override
  38. public IPage<WfAssetManagePageVo> getPage(WfAssetManagePageDto dto) {
  39. IPage<WfAssetManagePageVo> page = wfAssetManageMapper.getPage(ConventPage.getPage(dto), dto);
  40. return page;
  41. }
  42. @Override
  43. public ByteArrayOutputStream listWfAssetManageSelectRecordQuery(WfAssetManageSelectRecordQueryDto dto) {
  44. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  45. List<WfAssetManageQueryVo> result = wfAssetManageMapper.listWfAssetManageSelectRecordQuery(dto);
  46. if(!result.isEmpty()){
  47. //以第一条数据中的其他信息的key作为整个excel的表头,后面的数据根据填充
  48. List<List<String>> headList = head(result.get(0));
  49. List<List<Object>> dataList = dataList(headList, result);
  50. EasyExcel.write(bot).head(headList).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);
  51. return bot;
  52. }
  53. return bot;
  54. }
  55. @Override
  56. public ByteArrayOutputStream listWfAssetManageConditionalSearchQuery(WfAssetManageConditionalSearchQueryDto dto) {
  57. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  58. List<WfAssetManageQueryVo> result = wfAssetManageMapper.listWfAssetManageConditionalSearchQuery(dto);
  59. if(!result.isEmpty()){
  60. //以第一条数据中的其他信息的key作为整个excel的表头,后面的数据根据填充
  61. List<List<String>> headList = head(result.get(0));
  62. List<List<Object>> dataList = dataList(headList, result);
  63. EasyExcel.write(bot).head(headList).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);
  64. return bot;
  65. }
  66. return bot;
  67. }
  68. private List<List<String>> head(WfAssetManageQueryVo firstRecord) {
  69. List<List<String>> list = ListUtils.newArrayList();
  70. ObjectMapper objectMapper = new ObjectMapper();
  71. Map<String, Object> extendJsonMap = new HashMap<>();
  72. try {
  73. extendJsonMap = objectMapper.readValue(firstRecord.getExtendJson(), new TypeReference<Map<String, Object>>(){});
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. // 获取对象的所有属性
  78. String[] fields = {"资产类型", "资产类别", "资产种类", "名称", "所属部门", "使用人", "金额"};
  79. for (String field : fields) {
  80. list.add(new ArrayList<String>(){{
  81. add(field);
  82. }});
  83. }
  84. for (Map.Entry<String, Object> entry : extendJsonMap.entrySet()){
  85. String key = entry.getKey();
  86. list.add(new ArrayList<String>(){{
  87. add(key);
  88. }});
  89. }
  90. return list;
  91. }
  92. private List<List<Object>> dataList(List<List<String>> headList, List<WfAssetManageQueryVo> result) {
  93. List<List<Object>> list = ListUtils.newArrayList();
  94. ObjectMapper objectMapper = new ObjectMapper();
  95. for (WfAssetManageQueryVo wfAssetManageQueryVo : result) {
  96. List<Object> oneRecord = ListUtils.newArrayList();
  97. //先处理固定字段
  98. oneRecord.add(wfAssetManageQueryVo.getAssetTypeCn());
  99. oneRecord.add(wfAssetManageQueryVo.getAssetCategoryCn());
  100. oneRecord.add(wfAssetManageQueryVo.getAssetSpeciesCn());
  101. oneRecord.add(wfAssetManageQueryVo.getName());
  102. oneRecord.add(wfAssetManageQueryVo.getDeptIdCn());
  103. oneRecord.add(wfAssetManageQueryVo.getUserIdCn());
  104. oneRecord.add(wfAssetManageQueryVo.getAmount());
  105. String extendJson = wfAssetManageQueryVo.getExtendJson();
  106. if(!extendJson.isEmpty()){
  107. try {
  108. Map<String, Object> data = objectMapper.readValue(extendJson, new TypeReference<Map<String, Object>>(){});
  109. for (List<String> head : headList){
  110. if(data.get(head.get(0)) != null){
  111. oneRecord.add(data.get(head.get(0)));
  112. }
  113. }
  114. } catch (JsonProcessingException e) {
  115. throw new RuntimeException(e);
  116. }
  117. }
  118. list.add(oneRecord);
  119. }
  120. return list;
  121. }
  122. }