Browse Source

fix:划款导出sql优化

ly 10 months ago
parent
commit
67cef45f78

+ 76 - 2
src/main/java/cn/ezhizao/common/utils/poi/ExcelUtil.java

@@ -150,6 +150,8 @@ public class ExcelUtil<T>
      */
     public String[] excludeFields;
 
+    public String[] includeFields;
+
     public ExcelUtil(Class<T> clazz)
     {
         this.clazz = clazz;
@@ -168,6 +170,10 @@ public class ExcelUtil<T>
 
     public void init(List<T> list, String sheetName, String title, Type type)
     {
+        this.init(list, sheetName, title, type, new String[0]);
+    }
+
+    public void init(List<T> list, String sheetName, String title, Type type, String[] exportField) {
         if (list == null)
         {
             list = new ArrayList<T>();
@@ -176,6 +182,8 @@ public class ExcelUtil<T>
         this.sheetName = sheetName;
         this.type = type;
         this.title = title;
+        this.includeFields = exportField;
+
         createExcelField();
         createWorkbook();
         createTitle();
@@ -201,7 +209,9 @@ public class ExcelUtil<T>
             Cell titleCell = titleRow.createCell(0);
             titleCell.setCellStyle(styles.get("title"));
             titleCell.setCellValue(title);
-            sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
+            if (titleLastCol != 0) {
+                sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
+            }
         }
     }
 
@@ -492,6 +502,13 @@ public class ExcelUtil<T>
         this.init(list, sheetName, title, Type.EXPORT);
         exportExcel(response);
     }
+    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title, String [] columns)
+    {
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        this.init(list, sheetName, title, Type.EXPORT, columns);
+        exportExcel(response);
+    }
 
     /**
      * 对list数据源将其里面的数据导入到excel表单
@@ -543,6 +560,13 @@ public class ExcelUtil<T>
         exportExcel(response);
     }
 
+    public void importTemplateExcel(HttpServletResponse response, String sheetName, String title, String []exportFields) {
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        this.init(null, sheetName, title, Type.IMPORT, exportFields);
+        exportExcel(response);
+    }
+
     /**
      * 对list数据源将其里面的数据导入到excel表单
      *
@@ -557,6 +581,7 @@ public class ExcelUtil<T>
         }
         catch (Exception e)
         {
+            e.printStackTrace();
             log.error("导出Excel异常{}", e.getMessage());
         }
         finally
@@ -651,6 +676,7 @@ public class ExcelUtil<T>
             row = sheet.createRow(rowNo);
             // 得到导出对象.
             T vo = (T) list.get(i);
+//            System.out.println("i: " + i + ", vo: " + vo.toString());
             Collection<?> subList = null;
             if (isSubList())
             {
@@ -666,8 +692,10 @@ public class ExcelUtil<T>
                 }
             }
             int column = 0;
+            int columnNum = 0;
             for (Object[] os : fields)
             {
+                columnNum++;
                 Field field = (Field) os[0];
                 Excel excel = (Excel) os[1];
                 if (Collection.class.isAssignableFrom(field.getType()) && StringUtils.isNotNull(subList))
@@ -700,6 +728,8 @@ public class ExcelUtil<T>
                 {
                     this.addCell(excel, row, vo, field, column++);
                 }
+
+               // setSizeColumn(sheet,columnNum-1);
             }
         }
     }
@@ -844,14 +874,53 @@ public class ExcelUtil<T>
         {
             // 填充默认样式,防止合并单元格样式失效
             sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
+
             if (attr.needMerge())
             {
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
+
             }
         }
         return cell;
     }
 
+    /**
+     * 自适应宽度(中文支持)
+     *
+     * @param sheet sheet
+     * @param size  因为for循环从0开始,size值为 列数-1
+     */
+    public  void setSizeColumn(Sheet sheet, int size) {
+        for (int columnNum = 0; columnNum <= size; columnNum++) {
+            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
+            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
+                Row currentRow;
+                //当前行未被使用过
+                if (sheet.getRow(rowNum) == null) {
+                    currentRow = sheet.createRow(rowNum);
+                } else {
+                    currentRow = sheet.getRow(rowNum);
+                }
+
+                if (currentRow.getCell(columnNum) != null) {
+                    Cell currentCell = currentRow.getCell(columnNum);
+                    if (currentCell.getCellType() == CellType.STRING) {
+                        int length = currentCell.getStringCellValue().getBytes().length;
+                        if (columnWidth < length) {
+                            columnWidth = length;
+                        }
+                    }
+                }
+            }
+            if(30 * 256 * 3 > columnWidth * 256){
+                sheet.setColumnWidth(columnNum, columnWidth * 256);
+            }else{
+                sheet.setColumnWidth(columnNum, 30 * 256 * 3);
+            }
+
+        }
+    }
+
     /**
      * 设置单元格信息
      *
@@ -1028,7 +1097,7 @@ public class ExcelUtil<T>
      * @param endCol 结束列
      */
     public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
-                                      int firstCol, int endCol)
+            int firstCol, int endCol)
     {
         DataValidationHelper helper = sheet.getDataValidationHelper();
         DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
@@ -1356,6 +1425,9 @@ public class ExcelUtil<T>
     {
         this.fields = getFields();
         this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
+        if (this.includeFields.length > 0) {
+            this.fields = this.fields.stream().filter(v -> Arrays.stream(this.includeFields).anyMatch(e -> e.equals(((Field)v[0]).getName()))).collect(Collectors.toList());
+        }
         this.maxHeight = getRowHeight();
     }
 
@@ -1688,6 +1760,7 @@ public class ExcelUtil<T>
         }
         return method;
     }
+
     //读取表头
     public List<String> readExcelHeaders(InputStream inputStream) throws IOException {
         List<String> headers = new ArrayList<>();
@@ -1702,6 +1775,7 @@ public class ExcelUtil<T>
             Cell cell = cellIterator.next();
             headers.add(cell.getStringCellValue());
         }
+
         return headers;
     }
 }

+ 1 - 1
src/main/java/cn/ezhizao/project/business/deduction/controller/DeductionController.java

@@ -216,7 +216,7 @@ public class DeductionController extends BaseController {
     public void export(HttpServletResponse response, BizDeduction bizDeduction) throws NoSuchFieldException, IllegalAccessException {
         setTenantId(bizDeduction);
         startOrderBy();
-        List<BizDeduction> list = deductionService.getListByBean(bizDeduction);
+        List<BizDeduction> list = deductionService.getExportList(bizDeduction);
         for (BizDeduction l : list) {
             l.setCurrentMonth(l.getYear() + "-" + l.getMonth());
             //设置状态

+ 2 - 3
src/main/java/cn/ezhizao/project/business/deduction/domain/BizDeduction.java

@@ -28,10 +28,9 @@ public class BizDeduction extends BaseEntity implements Serializable {
     private BigDecimal amount;
 
     @ApiModelProperty(value = "状态 0-待确认扣款,1可扣款 3已扣款 4-驳回待处理")
+    @Excel(name = "状态",sort = 9, readConverterExp = "1=可扣款,0=待确认扣款,3=已扣款,4=扣款失败,5=已申报")
     private Integer status;
 
-
-    @Excel(name = "状态",sort = 9)
     @TableField(exist = false)
     private String statusType;
 
@@ -41,7 +40,7 @@ public class BizDeduction extends BaseEntity implements Serializable {
     @ApiModelProperty(value = "客户id")
     private Long companyId;
 
-    @Excel(name = "客户名称",sort = 1)
+    @Excel(name = "客户名称",sort = 1,width = 31)
     @ApiModelProperty(value = "客户名")
     @TableField(exist = false)
     private String companyName;

+ 1 - 0
src/main/java/cn/ezhizao/project/business/deduction/mapper/BizDeductionMapper.java

@@ -21,6 +21,7 @@ public interface BizDeductionMapper extends BaseMapper<BizDeduction> {
      */
     List<BizDeduction> getList(Map<String, Object> conditions);
 
+    List<BizDeduction> getExportList(BizDeduction deduction);
     /**
      * 根据Bean查询 列表
      *

+ 1 - 1
src/main/java/cn/ezhizao/project/business/deduction/service/IBizDeductionService.java

@@ -27,7 +27,7 @@ public interface IBizDeductionService extends IService<BizDeduction> {
      * @return List 集合
      */
     List<BizDeduction> getList(Map<String, Object> conditions);
-
+    List<BizDeduction> getExportList(BizDeduction deduction);
     /**
      * 带条件的查询总数
      *

+ 4 - 0
src/main/java/cn/ezhizao/project/business/deduction/service/impl/BizDeductionServiceImpl.java

@@ -33,6 +33,10 @@ public class BizDeductionServiceImpl extends ServiceImpl<BizDeductionMapper, Biz
     public List<BizDeduction> getListByBean(BizDeduction deduction) {
         return deductionMapper.getList(deduction);
     }
+    @Override
+    public List<BizDeduction> getExportList(BizDeduction deduction) {
+        return deductionMapper.getExportList(deduction);
+    }
 
     @Override
     public int getTotal(Map<String,Object> conditions) {

+ 16 - 1
src/main/resources/mybatis/business/BizDeductionMapper.xml

@@ -15,7 +15,16 @@
         <collection property="files" ofType="cn.ezhizao.project.system.domain.SysFileStorage" select="getFiles"
                     column="id"/>
     </resultMap>
-
+    <resultMap id="expotyResultMap" type="cn.ezhizao.project.business.deduction.domain.BizDeduction">
+        <id column="id" jdbcType="BIGINT" property="id"/>
+        <result column="id" jdbcType="BIGINT" property="id"/>
+        <result column="confirm_id" jdbcType="BIGINT" property="confirmId"/>
+        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
+        <result column="operation_id" jdbcType="BIGINT" property="operationId"/>
+        <association property="confirmName" javaType="java.lang.String" select="getConfirmName" column="confirm_id"/>
+        <association property="operationName" javaType="java.lang.String" select="getOperationName"
+                     column="operation_id"/>
+    </resultMap>
     <resultMap type="cn.ezhizao.project.system.domain.SysFileStorage" id="FileStorageResult">
         <id column="id" property="id"/>
     </resultMap>
@@ -46,6 +55,10 @@
     <select id="getList" resultMap="BaseResultMap">
         <include refid="getListSql"/>
     </select>
+    <!--带条件的、查询-->
+    <select id="getExportList" resultMap="expotyResultMap">
+        <include refid="getListSql"/>
+    </select>
 
     <!--带条件的List查询 数据总条数-->
     <select id="getTotal" resultType="java.lang.Integer">
@@ -54,10 +67,12 @@
 
     <sql id="getListSql">
         SELECT t1.*, t2.tax_type,
+        t11.name as companyName,
         t3.account_name as from_company_name,t8.account_name as toTenantName, t3.id from_tenant_id FROM biz_deduction t1 left join biz_company t2 on t2.id = t1.company_id
         left join biz_tenant t3 on t3.id = t1.tenant_id
         left join biz_entrust t9 on t9.work_order_id = t1.work_order_id and t9.deleted = 0 and  date_format(t9.current_month, '%Y%m') = concat(t1.year, t1.month)
         left join biz_tenant t8 on t8.id = t9.to_tenant_id
+        left join biz_company t11 on t11.id = t1.company_id and t11.deleted = 0
         <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
             t1.deleted = 0 AND
             <if test="id != null  ">