Browse Source

excel动态数据表初始化

v1.0
Yen 2 years ago
parent
commit
60cd01895a
  1. 86
      src/main/java/com/qs/serve/modules/exl/controller/ExlTableConfController.java
  2. 99
      src/main/java/com/qs/serve/modules/exl/controller/ExlTableDataController.java
  3. 15
      src/main/java/com/qs/serve/modules/exl/entity/ExlColumnConf.java
  4. 19
      src/main/java/com/qs/serve/modules/exl/entity/ExlTableConf.java
  5. 24
      src/main/java/com/qs/serve/modules/exl/entity/dto/ExlQueryFieldDto.java
  6. 3
      src/main/java/com/qs/serve/modules/exl/service/ExlTableConfService.java
  7. 46
      src/main/java/com/qs/serve/modules/exl/service/impl/ExlTableConfServiceImpl.java

86
src/main/java/com/qs/serve/modules/exl/controller/ExlTableConfController.java

@ -9,6 +9,11 @@ import com.qs.serve.common.model.enums.SystemModule;
import com.qs.serve.common.util.PageUtil;
import com.qs.serve.common.util.CopierUtil;
import com.qs.serve.common.util.StringUtils;
import com.qs.serve.common.util.WordUtil;
import com.qs.serve.modules.exl.entity.ExlColumnConf;
import com.qs.serve.modules.exl.entity.dto.ExlQueryFieldDto;
import com.qs.serve.modules.exl.entity.dto.ExlTableBo;
import com.qs.serve.modules.exl.service.ExlColumnConfService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
@ -18,7 +23,11 @@ import com.qs.serve.modules.exl.entity.ExlTableConf;
import com.qs.serve.modules.exl.service.ExlTableConfService;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 动态Excel表 表配置
@ -32,6 +41,7 @@ import java.util.List;
public class ExlTableConfController {
private ExlTableConfService exlTableConfService;
private ExlColumnConfService exlColumnConfService;
/**
* 列表
@ -45,6 +55,12 @@ public class ExlTableConfController {
return R.ok(list);
}
//测试传参
@GetMapping("/test")
public R<?> test(@RequestParam Map<String,String> map){
return R.ok(map);
}
/**
* 翻页
* @param param
@ -67,6 +83,34 @@ public class ExlTableConfController {
@SysLog(module = SystemModule.Excel, title = "表配置", biz = BizType.QUERY)
public R<ExlTableConf> getById(@PathVariable("id") String id){
ExlTableConf exlTableConf = exlTableConfService.getById(id);
LambdaQueryWrapper<ExlColumnConf> columnLqw = new LambdaQueryWrapper<>();
columnLqw.eq(ExlColumnConf::getTableConfId,id);
List<ExlColumnConf> columnConfList = exlColumnConfService.list(columnLqw);
exlTableConf.setColumnList(columnConfList);
//组装查询参数列表
List<ExlColumnConf> queryColumn = columnConfList.stream()
.filter(a->a.getConditionFlag().equals(1)).collect(Collectors.toList());
List<ExlQueryFieldDto> queryFieldList = new ArrayList<>();
for (ExlColumnConf columnConf : queryColumn) {
if(columnConf.getColumnType().contains("date")){
ExlQueryFieldDto startDto = new ExlQueryFieldDto();
startDto.setField("START--"+columnConf.getColumnName());
startDto.setType(columnConf.getColumnType());
startDto.setLabel(columnConf.getColumnHeader());
queryFieldList.add(startDto);
ExlQueryFieldDto fieldDto = new ExlQueryFieldDto();
fieldDto.setField("END--"+columnConf.getColumnName());
fieldDto.setType(columnConf.getColumnType());
fieldDto.setLabel(columnConf.getColumnHeader());
queryFieldList.add(fieldDto);
}else {
ExlQueryFieldDto fieldDto = new ExlQueryFieldDto();
fieldDto.setField(columnConf.getColumnName());
fieldDto.setType(columnConf.getColumnType());
fieldDto.setLabel(columnConf.getColumnHeader());
queryFieldList.add(fieldDto);
}
}
return R.ok(exlTableConf);
}
@ -79,9 +123,12 @@ public class ExlTableConfController {
*/
@PostMapping("/updateById")
@SysLog(module = SystemModule.Excel, title = "表配置", biz = BizType.UPDATE)
public R<?> updateById(@RequestBody @Valid ExlTableConf param){
boolean result = exlTableConfService.updateById(param);
return R.isTrue(result);
public R<?> updateById(@RequestBody @Valid ExlTableBo param){
if(param.getId()==null){
return R.error("id is null");
}
ExlTableConf tableConf = exlTableConfService.modify(param);
return R.ok(tableConf);
}
/**
@ -91,9 +138,29 @@ public class ExlTableConfController {
*/
@PostMapping("/save")
@SysLog(module = SystemModule.Excel, title = "表配置", biz = BizType.INSERT)
public R<?> save(@RequestBody @Valid ExlTableConf param){
boolean result = exlTableConfService.save(param);
return R.isTrue(result);
public R<?> save(@RequestBody @Valid ExlTableBo param){
if(param.getId()!=null){
return R.error("id should be null");
}
ExlTableConf tableConf = exlTableConfService.modify(param);
return R.ok(tableConf);
}
/**
* 启用配置
* @param tableId
* @return
*/
@PostMapping("/enable/{tableId}")
@SysLog(module = SystemModule.Excel, title = "表配置", biz = BizType.INSERT)
public R<?> enable(@PathVariable("tableId") String tableId){
ExlTableConf tableConf = exlTableConfService.getById(tableId);
if(tableConf.getEnableFlag().equals(0)){
tableConf.setEnableFlag(1);
tableConf.setEnableTime(LocalDateTime.now());
exlTableConfService.updateById(tableConf);
}
return R.ok();
}
/**
@ -105,6 +172,13 @@ public class ExlTableConfController {
@SysLog(module = SystemModule.Excel, title = "表配置", biz = BizType.DELETE)
public R<?> deleteById(@PathVariable("ids") String ids){
List<Long> idsLong = StringUtils.splitIdLong(ids);
LambdaQueryWrapper<ExlTableConf> lqw = new LambdaQueryWrapper<>();
lqw.in(ExlTableConf::getId,ids);
lqw.eq(ExlTableConf::getEnableFlag,1);
Long count = exlTableConfService.count(lqw);
if(count>0){
return R.error("已启用的配置不可删除");
}
boolean result = exlTableConfService.removeByIds(idsLong);
return R.isTrue(result);
}

99
src/main/java/com/qs/serve/modules/exl/controller/ExlTableDataController.java

@ -0,0 +1,99 @@
package com.qs.serve.modules.exl.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.serve.common.model.annotation.SysLog;
import com.qs.serve.common.model.dto.PageVo;
import com.qs.serve.common.model.dto.R;
import com.qs.serve.common.model.enums.BizType;
import com.qs.serve.common.model.enums.SystemModule;
import com.qs.serve.common.util.PageUtil;
import com.qs.serve.common.util.StringUtils;
import com.qs.serve.modules.exl.entity.ExlColumnConf;
import com.qs.serve.modules.exl.entity.ExlTableConf;
import com.qs.serve.modules.exl.entity.dto.ExlQueryFieldDto;
import com.qs.serve.modules.exl.entity.dto.ExlTableBo;
import com.qs.serve.modules.exl.service.ExlColumnConfService;
import com.qs.serve.modules.exl.service.ExlTableConfService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 动态Excel表 表配置
* @author YenHex
* @since 2023-08-11
*/
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("exl/tableData")
public class ExlTableDataController {
private ExlTableConfService exlTableConfService;
private ExlColumnConfService exlColumnConfService;
/**
* 翻页
* @param param
* @return
*/
@GetMapping("/page")
public R<PageVo<ExlTableConf>> getPage(ExlTableConf param){
LambdaQueryWrapper<ExlTableConf> lqw = new LambdaQueryWrapper<>(param);
PageUtil.startPage();
List<ExlTableConf> list = exlTableConfService.list(lqw);
return R.byPageHelperList(list);
}
/**
* 更新
* @param param
* @return
*/
@PostMapping("/updateById")
public R<?> updateById(@RequestBody @Valid ExlTableBo param){
return R.ok();
}
/**
* 新增
* @param param
* @return
*/
@PostMapping("/save")
public R<?> save(@RequestBody @Valid ExlTableBo param){
return R.ok();
}
/**
* 删除
* @param ids
* @return
*/
@DeleteMapping("/deleteById/{ids}")
@SysLog(module = SystemModule.Excel, title = "表配置", biz = BizType.DELETE)
public R<?> deleteById(@PathVariable("ids") String ids){
List<Long> idsLong = StringUtils.splitIdLong(ids);
LambdaQueryWrapper<ExlTableConf> lqw = new LambdaQueryWrapper<>();
lqw.in(ExlTableConf::getId,ids);
lqw.eq(ExlTableConf::getEnableFlag,1);
Long count = exlTableConfService.count(lqw);
if(count>0){
return R.error("已启用的配置不可删除");
}
boolean result = exlTableConfService.removeByIds(idsLong);
return R.isTrue(result);
}
}

15
src/main/java/com/qs/serve/modules/exl/entity/ExlColumnConf.java

@ -32,9 +32,11 @@ public class ExlColumnConf implements Serializable {
@TableId(type = IdType.AUTO)
private Long id;
/** 表名 */
@NotBlank(message = "表名不能为空")
@Length(max = 255,message = "表名长度不能超过255字")
private Long tableConfId;
/** 列名 */
@NotBlank(message = "列名不能为空")
@Length(max = 255,message = "列名长度不能超过255字")
private String columnName;
/** excel头部 */
@ -43,12 +45,11 @@ public class ExlColumnConf implements Serializable {
private String columnHeader;
/** 数据类型:string;int;money;date;datetime; */
@Length(max = 255,message = "数据类型:string;int;money;date;datetime;长度不能超过255字")
@Length(max = 20,message = "数据类型:string;int;money;date;datetime;长度不能超过255字")
private String columnType;
@JsonIgnore
/** 作为查询条件标识 string-like;int-eq;date-between;datetime-between */
@NotNull(message = "作为查询条件标识 string-like;int-eq;date-between;datetime-between不能为空")
//string-like;int-eq;date-between;datetime-between
/** 作为查询条件标识 */
private Integer conditionFlag;
/** 创建时间 */

19
src/main/java/com/qs/serve/modules/exl/entity/ExlTableConf.java

@ -4,6 +4,7 @@ import java.time.LocalDate;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
@ -46,7 +47,12 @@ public class ExlTableConf implements Serializable {
private String remark;
/** 启用标识 (0/1) */
private String enableFlag;
private Integer enableFlag;
/** 启用时间 */
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime enableTime;
/** 创建时间 */
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ -78,6 +84,17 @@ public class ExlTableConf implements Serializable {
@JsonProperty
private String delFlag;
/**
* 列配置的列表
*/
@TableField(exist = false)
private List<ExlColumnConf> columnList;
/**
* 查询参数列表
*/
@TableField(exist = false)
private List<ExlColumnConf> queryList;
public static ExlTableConf toNewObject(ExlTableConf source){
ExlTableConf tableConf = new ExlTableConf();

24
src/main/java/com/qs/serve/modules/exl/entity/dto/ExlQueryFieldDto.java

@ -0,0 +1,24 @@
package com.qs.serve.modules.exl.entity.dto;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
/**
* @author YenHex
* @since 2023/8/14
*/
@Data
public class ExlQueryFieldDto {
/** 字段名 */
private String field;
/** 数据类型 */
private String type;
/** 显示标签 */
private String label;
}

3
src/main/java/com/qs/serve/modules/exl/service/ExlTableConfService.java

@ -14,8 +14,9 @@ public interface ExlTableConfService extends IService<ExlTableConf> {
/**
* 编辑
* @param param
* @return
*/
void modify(ExlTableBo param);
ExlTableConf modify(ExlTableBo param);
}

46
src/main/java/com/qs/serve/modules/exl/service/impl/ExlTableConfServiceImpl.java

@ -1,6 +1,9 @@
package com.qs.serve.modules.exl.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.serve.common.util.Assert;
import com.qs.serve.modules.exl.entity.ExlColumnConf;
import com.qs.serve.modules.exl.entity.dto.ExlTableBo;
import com.qs.serve.modules.exl.service.ExlColumnConfService;
import lombok.AllArgsConstructor;
@ -10,6 +13,8 @@ import com.qs.serve.modules.exl.entity.ExlTableConf;
import com.qs.serve.modules.exl.service.ExlTableConfService;
import com.qs.serve.modules.exl.mapper.ExlTableConfMapper;
import java.util.List;
/**
* 表配置 服务实现类
* @author YenHex
@ -23,8 +28,45 @@ public class ExlTableConfServiceImpl extends ServiceImpl<ExlTableConfMapper,ExlT
private final ExlColumnConfService columnConfService;
@Override
public void modify(ExlTableBo param) {
public ExlTableConf modify(ExlTableBo param) {
ExlTableConf tableConf = new ExlTableConf();
if(param.getId()==null){
//save
tableConf.setTableName(param.getTableName());
tableConf.setExcelTitle(param.getExcelTitle());
tableConf.setRemark(param.getRemark());
this.save(tableConf);
final Long tableId = tableConf.getId();
List<ExlColumnConf> columnList = param.getColumnList();
columnList.forEach(a->{
a.setTableConfId(tableId);
a.setId(null);
});
columnConfService.saveBatch(columnList);
return tableConf;
}else {
//update
final Long tableId = param.getId();
ExlTableConf dbData = this.getById(tableId);
if(dbData.getEnableFlag().equals(1)){
Assert.throwEx("已启用的配置不可修改");
}
tableConf.setId(tableId);
tableConf.setTableName(param.getTableName());
tableConf.setExcelTitle(param.getExcelTitle());
tableConf.setRemark(param.getRemark());
this.updateById(tableConf);
LambdaQueryWrapper<ExlColumnConf> columnLqw = new LambdaQueryWrapper<>();
columnLqw.eq(ExlColumnConf::getTableConfId,param.getId());
columnConfService.remove(columnLqw);
List<ExlColumnConf> columnList = param.getColumnList();
columnList.forEach(a->{
a.setTableConfId(tableId);
a.setId(null);
});
columnConfService.saveBatch(columnList);
}
return tableConf;
}
}

Loading…
Cancel
Save