DataExpertTemplateController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.xjrsoft.module.dataexpert.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.alibaba.excel.support.ExcelTypeEnum;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  9. import com.google.gson.JsonArray;
  10. import com.google.gson.JsonElement;
  11. import com.google.gson.JsonObject;
  12. import com.google.gson.JsonParser;
  13. import com.xjrsoft.common.model.result.RT;
  14. import com.xjrsoft.common.page.ConventPage;
  15. import com.xjrsoft.common.page.PageOutput;
  16. import com.xjrsoft.common.utils.VoToColumnUtil;
  17. import com.xjrsoft.module.dataexpert.dto.AddDataExpertTemplateDto;
  18. import com.xjrsoft.module.dataexpert.dto.DataExpertConditionsDto;
  19. import com.xjrsoft.module.dataexpert.dto.DataExpertDto;
  20. import com.xjrsoft.module.dataexpert.dto.DataExpertTemplateListDto;
  21. import com.xjrsoft.module.dataexpert.dto.DataExpertTemplatePageDto;
  22. import com.xjrsoft.module.dataexpert.dto.UpdateDataExpertTemplateDto;
  23. import com.xjrsoft.module.dataexpert.entity.DataExpertSource;
  24. import com.xjrsoft.module.dataexpert.entity.DataExpertTemplate;
  25. import com.xjrsoft.module.dataexpert.service.IDataExpertSourceService;
  26. import com.xjrsoft.module.dataexpert.service.IDataExpertTemplateService;
  27. import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateFieldVo;
  28. import com.xjrsoft.module.dataexpert.vo.DataExpertTemplatePageVo;
  29. import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateVo;
  30. import io.swagger.annotations.Api;
  31. import io.swagger.annotations.ApiOperation;
  32. import lombok.AllArgsConstructor;
  33. import org.apache.poi.ss.usermodel.Cell;
  34. import org.apache.poi.ss.usermodel.Row;
  35. import org.apache.poi.ss.usermodel.Sheet;
  36. import org.apache.poi.ss.usermodel.Workbook;
  37. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  38. import org.springframework.http.ResponseEntity;
  39. import org.springframework.web.bind.annotation.DeleteMapping;
  40. import org.springframework.web.bind.annotation.GetMapping;
  41. import org.springframework.web.bind.annotation.PostMapping;
  42. import org.springframework.web.bind.annotation.PutMapping;
  43. import org.springframework.web.bind.annotation.RequestBody;
  44. import org.springframework.web.bind.annotation.RequestMapping;
  45. import org.springframework.web.bind.annotation.RequestParam;
  46. import org.springframework.web.bind.annotation.RestController;
  47. import javax.validation.Valid;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.IOException;
  50. import java.sql.SQLException;
  51. import java.util.ArrayList;
  52. import java.util.HashMap;
  53. import java.util.List;
  54. import java.util.Map;
  55. /**
  56. * @title: 数据导出-数据模板
  57. * @Author dzx
  58. * @Date: 2024-04-19
  59. * @Version 1.0
  60. */
  61. @RestController
  62. @RequestMapping("/dataexpert" + "/dataExpertTemplate")
  63. @Api(value = "/dataexpert" + "/dataExpertTemplate",tags = "数据导出-数据模板代码")
  64. @AllArgsConstructor
  65. public class DataExpertTemplateController {
  66. private final IDataExpertSourceService dataExpertSourceService;
  67. private final IDataExpertTemplateService dataExpertTemplateService;
  68. @GetMapping(value = "/page")
  69. @ApiOperation(value="数据导出-数据模板列表(分页)")
  70. @SaCheckPermission("dataexperttemplate:detail")
  71. public RT<PageOutput<DataExpertTemplatePageVo>> page(@Valid DataExpertTemplatePageDto dto){
  72. MPJLambdaWrapper<DataExpertTemplate> select = new MPJLambdaWrapper<DataExpertTemplate>()
  73. .leftJoin(DataExpertSource.class, DataExpertSource::getId, DataExpertTemplate::getDataExpertSourceId)
  74. .orderByDesc(DataExpertTemplate::getId)
  75. .like(StrUtil.isNotEmpty(dto.getName()), DataExpertTemplate::getName, dto.getName())
  76. .selectAs(DataExpertSource::getName, DataExpertTemplatePageVo::getDataExpertSourceName)
  77. .selectAs(DataExpertTemplate::getId, DataExpertTemplatePageVo::getId)
  78. .select(DataExpertTemplate.class, x -> VoToColumnUtil.fieldsToColumns(DataExpertTemplatePageVo.class).contains(x.getProperty()));
  79. IPage<DataExpertTemplatePageVo> page = dataExpertTemplateService.selectJoinListPage(ConventPage.getPage(dto), DataExpertTemplatePageVo.class, select);
  80. PageOutput<DataExpertTemplatePageVo> pageOutput = ConventPage.getPageOutput(page, DataExpertTemplatePageVo.class);
  81. return RT.ok(pageOutput);
  82. }
  83. @GetMapping(value = "/info")
  84. @ApiOperation(value="根据id查询数据导出-数据模板信息")
  85. @SaCheckPermission("dataexperttemplate:detail")
  86. public RT<DataExpertTemplateVo> info(@RequestParam Long id){
  87. DataExpertTemplate dataExpertTemplate = dataExpertTemplateService.getById(id);
  88. if (dataExpertTemplate == null) {
  89. return RT.error("找不到此数据!");
  90. }
  91. return RT.ok(BeanUtil.toBean(dataExpertTemplate, DataExpertTemplateVo.class));
  92. }
  93. @GetMapping(value = "/list")
  94. @ApiOperation(value="根据数据源id查询数据导出-数据模板信息")
  95. @SaCheckPermission("dataexperttemplate:detail")
  96. public RT<List<DataExpertTemplateVo>> list(@Valid DataExpertTemplateListDto dto){
  97. List<DataExpertTemplate> list = dataExpertTemplateService.list(
  98. new QueryWrapper<DataExpertTemplate>().lambda()
  99. .eq(DataExpertTemplate::getDataExpertSourceId, dto.getDataExpertSourceId())
  100. );
  101. return RT.ok(BeanUtil.copyToList(list, DataExpertTemplateVo.class));
  102. }
  103. @PostMapping
  104. @ApiOperation(value = "新增数据导出-数据模板")
  105. @SaCheckPermission("dataexperttemplate:add")
  106. public RT<Boolean> add(@Valid @RequestBody AddDataExpertTemplateDto dto){
  107. DataExpertTemplate dataExpertTemplate = BeanUtil.toBean(dto, DataExpertTemplate.class);
  108. boolean isSuccess = dataExpertTemplateService.save(dataExpertTemplate);
  109. return RT.ok(isSuccess);
  110. }
  111. @PutMapping
  112. @ApiOperation(value = "修改数据导出-数据模板")
  113. @SaCheckPermission("dataexperttemplate:edit")
  114. public RT<Boolean> update(@Valid @RequestBody UpdateDataExpertTemplateDto dto){
  115. DataExpertTemplate dataExpertTemplate = BeanUtil.toBean(dto, DataExpertTemplate.class);
  116. return RT.ok(dataExpertTemplateService.updateById(dataExpertTemplate));
  117. }
  118. @DeleteMapping
  119. @ApiOperation(value = "删除数据导出-数据模板")
  120. @SaCheckPermission("dataexperttemplate:delete")
  121. public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
  122. return RT.ok(dataExpertTemplateService.removeBatchByIds(ids));
  123. }
  124. @GetMapping(value = "/field-info")
  125. @ApiOperation(value="根据id查询数据导出-数据模板信息")
  126. @SaCheckPermission("dataexperttemplate:detail")
  127. public RT<List<DataExpertTemplateFieldVo>> getFieldInfo(@RequestParam Long id){
  128. return RT.ok(dataExpertTemplateService.getFieldList(id));
  129. }
  130. @PostMapping("/export-query")
  131. @ApiOperation(value = "导出")
  132. public ResponseEntity<byte[]> exportData(@Valid @RequestBody DataExpertDto dto) throws SQLException, IOException {
  133. //拼接字段
  134. List<String> fields = new ArrayList<>();
  135. Map<Integer, String> showNameMap = new HashMap<>();
  136. List<String> titleList = new ArrayList<>();
  137. List<DataExpertTemplateFieldVo> fieldList = dto.getFieldList();
  138. DataExpertSource expertSource;
  139. String conditions = "";
  140. if(dto.getDataExpertSourceId() != null){
  141. expertSource = dataExpertSourceService.getById(dto.getDataExpertSourceId());
  142. for (int i = 0; i < fieldList.size(); i ++){
  143. DataExpertTemplateFieldVo fieldVo = fieldList.get(i);
  144. showNameMap.put(i, fieldVo.getShowName());
  145. fields.add("ifnull(" + fieldVo.getFieldName() + ", '')");
  146. titleList.add(fieldVo.getShowName());
  147. }
  148. }else{
  149. //查出想要的数据
  150. DataExpertTemplate template = dataExpertTemplateService.getById(dto.getDataExpertTemplateId());
  151. expertSource = dataExpertSourceService.getById(template.getDataExpertSourceId());
  152. String fieldJsonStr = template.getFieldJson();
  153. JsonParser parser = new JsonParser();
  154. JsonArray fieldJson = parser.parse(fieldJsonStr).getAsJsonArray();
  155. int i = 0;
  156. for (JsonElement jsonElement : fieldJson) {
  157. JsonObject jsonObject = jsonElement.getAsJsonObject();
  158. showNameMap.put(i, jsonObject.get("name").getAsString());
  159. fields.add("ifnull(" + jsonObject.get("field").getAsString() + ", '')");
  160. titleList.add(jsonObject.get("name").getAsString());
  161. i ++;
  162. }
  163. }
  164. if(dto.getIds() != null && !dto.getIds().isEmpty()){
  165. String ids = dto.getIds().toString().substring(1, dto.getIds().toString().length() - 1).replaceAll(",","','");
  166. conditions += " and id in ('" + ids + "')";
  167. }
  168. if(dto.getConditions() != null && dto.getConditions().size() != 0){
  169. for (DataExpertConditionsDto condition : dto.getConditions()) {
  170. if(condition.getKeyType() == 1){
  171. conditions += " and " + condition.getKeyName() + " like '%" + condition.getKeyValue() + "%'";
  172. }else if(condition.getKeyType() == 2 && condition.getKeyValue() != null && !"".equals(condition.getKeyValue())){
  173. conditions += " and " + condition.getKeyName() + " in (";
  174. String[] keyValues = condition.getKeyValue().split(",");
  175. for(int k = 0; k < keyValues.length; k ++){
  176. if(k > 0){
  177. conditions += ",";
  178. }
  179. conditions += "'" + keyValues[k] + "'";
  180. }
  181. conditions += ")";
  182. }
  183. }
  184. }
  185. //查出导出的数据并进行组装
  186. List<String[]> dataList = dataExpertTemplateService.getDataList(fields.toString().substring(1, fields.toString().length() - 1), expertSource.getViewName(), conditions);
  187. List<String[]> allDataList = new ArrayList<>();
  188. allDataList.add(titleList.toArray(new String[titleList.size()]));
  189. allDataList.addAll(dataList);
  190. String sheetName = "数据";
  191. Workbook workbook = new XSSFWorkbook();
  192. Sheet sheet = workbook.createSheet(sheetName);
  193. sheet.createFreezePane(0, 1, 0, 1);
  194. for (int i = 0; i < allDataList.size(); i ++){
  195. Row row = sheet.createRow(i);
  196. String[] data = allDataList.get(i);
  197. for (int j = 0; j < data.length; j ++ ){
  198. Cell cell = row.createCell(j);
  199. cell.setCellValue(data[j]);
  200. }
  201. }
  202. String fileName = "导出结果" + ExcelTypeEnum.XLSX.getValue();
  203. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  204. workbook.write(bot);
  205. return RT.fileStream(bot.toByteArray(), fileName);
  206. }
  207. }