Kaynağa Gözat

1、导入模块调整
2、导出模块调整

dzx 1 yıl önce
ebeveyn
işleme
81045cd1eb

+ 24 - 26
src/main/java/com/xjrsoft/module/dataexpert/controller/DataExpertTemplateController.java

@@ -3,11 +3,7 @@ package com.xjrsoft.module.dataexpert.controller;
 import cn.dev33.satoken.annotation.SaCheckPermission;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.StrUtil;
-import com.alibaba.excel.EasyExcel;
-import com.alibaba.excel.ExcelWriter;
 import com.alibaba.excel.support.ExcelTypeEnum;
-import com.alibaba.excel.write.metadata.WriteSheet;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.github.yulichang.wrapper.MPJLambdaWrapper;
@@ -31,12 +27,14 @@ import com.xjrsoft.module.dataexpert.service.IDataExpertTemplateService;
 import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateFieldVo;
 import com.xjrsoft.module.dataexpert.vo.DataExpertTemplatePageVo;
 import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateVo;
-import com.xjrsoft.module.student.entity.BaseStudent;
-import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
-import org.apache.commons.lang3.function.FailablePredicate;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -49,6 +47,8 @@ import org.springframework.web.bind.annotation.RestController;
 
 import javax.validation.Valid;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -144,9 +144,9 @@ public class DataExpertTemplateController {
 
     @PostMapping("/export-query")
     @ApiOperation(value = "导出")
-    public ResponseEntity<byte[]> exportData(@Valid @RequestBody DataExpertDto dto) {
+    public ResponseEntity<byte[]> exportData(@Valid @RequestBody DataExpertDto dto) throws SQLException, IOException {
         //拼接字段
-        String fields = "";
+        List<String> fields = new ArrayList<>();
         Map<Integer, String> showNameMap = new HashMap<>();
         List<String> titleList = new ArrayList<>();
         List<DataExpertTemplateFieldVo> fieldList = dto.getFieldList();
@@ -156,10 +156,7 @@ public class DataExpertTemplateController {
             for (int i = 0; i < fieldList.size(); i ++){
                 DataExpertTemplateFieldVo fieldVo = fieldList.get(i);
                 showNameMap.put(i, fieldVo.getShowName());
-                if(i > 0){
-                    fields += ",";
-                }
-                fields += fieldVo.getFieldName();
+                fields.add("ifnull(" + fieldVo.getFieldName() + ", '')");
                 titleList.add(fieldVo.getShowName());
             }
         }else{
@@ -173,33 +170,34 @@ public class DataExpertTemplateController {
             for (JsonElement jsonElement : fieldJson) {
                 JsonObject jsonObject = jsonElement.getAsJsonObject();
                 showNameMap.put(i, jsonObject.get("name").getAsString());
-                if(i > 0){
-                    fields += ",";
-                }
-                fields += jsonObject.get("field").getAsString();
+                fields.add("ifnull(" + jsonObject.get("field").getAsString() + ", '')");
                 titleList.add(jsonObject.get("name").getAsString());
                 i ++;
             }
         }
 
         //查出导出的数据并进行组装
-        List<String[]> dataList = dataExpertTemplateService.getDataList(fields, expertSource.getViewName());
+        List<String[]> dataList = dataExpertTemplateService.getDataList(fields.toString().substring(1, fields.toString().length() - 1), expertSource.getViewName());
 
         List<String[]> allDataList = new ArrayList<>();
         allDataList.add(titleList.toArray(new String[titleList.size()]));
         allDataList.addAll(dataList);
 
         String sheetName = "数据";
+        Workbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet(sheetName);
+        for (int i = 0; i < allDataList.size(); i ++){
+            Row row = sheet.createRow(i);
+            String[] data = allDataList.get(i);
+            for (int j = 0; j < data.length; j ++ ){
+                Cell cell = row.createCell(j);
+                cell.setCellValue(data[j]);
+            }
+        }
+
         String fileName = "导出结果" + ExcelTypeEnum.XLSX.getValue();
         ByteArrayOutputStream bot = new ByteArrayOutputStream();
-
-        ExcelWriter excelWriter = EasyExcel.write(bot).excelType(ExcelTypeEnum.XLSX).build();
-        WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).needHead(Boolean.FALSE).build();
-
-        excelWriter.write(allDataList, writeSheet);
-
-        excelWriter.finish();
-
+        workbook.write(bot);
         return RT.fileStream(bot.toByteArray(), fileName);
     }
 

+ 2 - 1
src/main/java/com/xjrsoft/module/dataexpert/mapper/DataExpertTemplateMapper.java

@@ -3,6 +3,7 @@ package com.xjrsoft.module.dataexpert.mapper;
 import com.github.yulichang.base.MPJBaseMapper;
 import com.xjrsoft.module.dataexpert.entity.DataExpertTemplate;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 
 import java.util.List;
@@ -17,5 +18,5 @@ import java.util.List;
 public interface DataExpertTemplateMapper extends MPJBaseMapper<DataExpertTemplate> {
 
     @Select("select #{fields} from #{viewName}")
-    List<String[]> getDataList(String fields, String viewName);
+    List<String[]> getDataList(@Param("fields") String fields, @Param("viewName") String viewName);
 }

+ 2 - 1
src/main/java/com/xjrsoft/module/dataexpert/service/IDataExpertTemplateService.java

@@ -6,6 +6,7 @@ import com.xjrsoft.module.dataexpert.entity.DataExpertTemplate;
 import com.xjrsoft.module.dataexpert.vo.DataExpertSourceFieldVo;
 import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateFieldVo;
 
+import java.sql.SQLException;
 import java.util.List;
 
 /**
@@ -20,5 +21,5 @@ public interface IDataExpertTemplateService extends MPJBaseService<DataExpertTem
     List<DataExpertTemplateFieldVo> getFieldList(Long id);
 
 
-    List<String[]> getDataList(String fields, String viewName);
+    List<String[]> getDataList(String fields, String viewName) throws SQLException;
 }

+ 13 - 2
src/main/java/com/xjrsoft/module/dataexpert/service/impl/DataExpertTemplateServiceImpl.java

@@ -1,17 +1,24 @@
 package com.xjrsoft.module.dataexpert.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.db.Db;
+import cn.hutool.db.Entity;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.github.yulichang.base.MPJBaseServiceImpl;
+import com.xjrsoft.common.constant.GlobalConstant;
+import com.xjrsoft.common.utils.DatasourceUtil;
 import com.xjrsoft.module.dataexpert.entity.DataExpertTemplate;
 import com.xjrsoft.module.dataexpert.entity.DataExpertTemplateField;
 import com.xjrsoft.module.dataexpert.mapper.DataExpertTemplateFieldMapper;
 import com.xjrsoft.module.dataexpert.mapper.DataExpertTemplateMapper;
 import com.xjrsoft.module.dataexpert.service.IDataExpertTemplateService;
 import com.xjrsoft.module.dataexpert.vo.DataExpertTemplateFieldVo;
+import com.xjrsoft.module.schedule.entity.JianyueData;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
+import javax.sql.DataSource;
+import java.sql.SQLException;
 import java.util.List;
 
 /**
@@ -36,7 +43,11 @@ public class DataExpertTemplateServiceImpl extends MPJBaseServiceImpl<DataExpert
     }
 
     @Override
-    public List<String[]> getDataList(String fields, String viewName) {
-        return templateMapper.getDataList(fields, viewName);
+    public List<String[]> getDataList(String fields, String viewName) throws SQLException {
+        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
+        Db use = Db.use(datasource);
+        String sql = "SELECT " + fields + " FROM " + viewName + " WHERE 1 = 1";
+        List<String[]> list = use.query(sql, String[].class);
+        return list;
     }
 }