描述; 动态表单导入 本来是用的阿里的easyexcle 这是是真好用 无奈 读取出来的数据不能用一个对象接收 ,所以本人尝试了 解析出来表头然后反向生成实体 但是也不现实 ,easyExcle可以用map接收 但是如果直接保存的数据库 属性就会变成汉字 查询返回也是汉字
解析出来的也是一个linkHashMap 遍历映射取不到 关系没办法映射
我这里的业务是 所有需要的表单是前台拖拽的 后面保存一个json 我需要从json中取出所有的key
废话不说了 上代码:
首先引入糊涂的工具包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
hutool官网地址
参考json;
{
"name":"[{\"title\":\"姓名\",\"id\":\"text\",\"key\":\"name\"},{\"title\":\"性别\",\"id\":\"radio\",\"key\":\"sex\"},{\"title\":\"出生日期\",\"id\":\"date\",\"key\":\"birth\"}]"
}
核心代码:
//用来存储取出的key
List<String> keyList=new ArrayList<>();
//模板json
JSONArray otherArr = JSONObject.parseArray(temJson);
for(int i=0;i<otherArr.size();i++){
String key = otherArr.getJSONObject(i).getString("key");
keyList.add(key);
}
List<Map<String,String>> mapList=new ArrayList<>();
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
List<List<Object>> read = reader.read(1);
System.out.println(read);
for(List<Object> objectList:read){
Map<String,String> map2=new HashMap<>();
for(int i=0;i<objectList.size();i++){
Object o = objectList.get(i); //o表示当前的值 下图
System.out.println(o);
//添加非空判断 有个能某一列为空值
//注意 keyList的长度一定要和objectList的大小一样 位置是定死的
map2.put(keyList.get(i),StrUtil.isEmptyOrUndefined(String.valueOf(o))?"":String.valueOf(o));
}
map2.put("creatTime",DateUtil.format(new Date(),"yyyy-MM-dd"));
map2.put("uuId",UUID.randomUUID().toString());
map2.put("homeId",homeId);
mapList.add(map2);
}
//保存数据库 我这里是保存到mongodb里面
MongodbUtil.saveAll(tableName,mapList);
结果:
后补充 添加判断重复
@PostMapping("/importTopicPat")
@Transactional(rollbackFor = Exception.class)
public BaseResult importTopicPat(@RequestParam("file") MultipartFile file,
@RequestParam("homeId")String homeId,
@RequestParam("tableName")String tableName,
@RequestParam(value = "indexValue",required = false)String indexValue,//前台设置唯一索引
@RequestParam("docId")Integer docId) throws IOException {
try {
//课题导入之前先判断是否创建了模板表单
ExcleTempleExample ex=new ExcleTempleExample();
ExcleTempleExample.Criteria criteria = ex.createCriteria();
criteria.andHomeIdEqualTo(Integer.valueOf(homeId));
List<ExcleTemple> excleTemplesList = excleTempleMapper.selectByExample(ex);
if(excleTemplesList.size()<0||excleTemplesList==null) {
redisUtil.del(tableName+":"+homeId);
return BaseResult.build(StatusCode.SERVICEERROR,"请先创建表单模板");
}
//根据索引值判断
String[] split = indexValue.split(",");
//查出已经存在的患者
List<String> indexBool = this.findIndex(homeId, tableName, split);
//取出模板json的key
ExcleTemple excleTemple = excleTemplesList.get(0);
String temJson = excleTemple.getTemJson();
System.out.println(temJson);
List<String> keyList=new ArrayList<>();
JSONArray otherArr = JSONObject.parseArray(temJson);
for(int i=0;i<otherArr.size();i++){
String key = otherArr.getJSONObject(i).getString("key");
keyList.add(key);
}
List<Map<String,String>> mapList=new ArrayList<>();
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
//默认从第二行开始
List<List<Object>> read = reader.read(1);
System.out.println(read);
StringBuffer bufferExcle=null;
for(List<Object> objectList:read){
bufferExcle=new StringBuffer();
Map<String,String> map2=new HashMap<>();
for(int i=0;i<objectList.size();i++){
Object o = objectList.get(i);
System.out.println(o);
map2.put(keyList.get(i),StrUtil.isEmptyOrUndefined(String.valueOf(o))?"无":String.valueOf(o));
}
map2.put("creatTime",DateUtil.format(new Date(),"yyyy-MM-dd"));
map2.put("uuId",UUID.randomUUID().toString());
map2.put("homeId",homeId);
map2.put("delTag","0");
for(String indexV:split){
String name = map2.get(indexV);
bufferExcle.append(name);
}
String md5 = MD5Utils.getMd5(bufferExcle.toString());
if(!indexBool.contains(md5)){
mapList.add(map2);
}
}
MongodbUtil.saveAll(tableName,mapList);
}catch (Exception e){
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return BaseResult.build(StatusCode.SERVICEERROR,"导入失败");
}finally{
return BaseResult.ok();
}
}
/**
* md5加密筛选
* @param homeId
* @param tableName
* @param split
* @return 数据库原始值
*/
private List<String> findIndex(String homeId,String tableName,String[] split){
//根据homeId以及表名查出来已经存在的患者
Map<String,Object> findMap=new HashMap<>();
findMap.put("homeId",homeId);
Map map = new HashMap();
//返回结果 是为List<Map>
List<?> all = MongodbUtil.findSortByParamNotDesc(map, tableName, findMap);
StringBuffer buffer=null;
List<String> indexBool=new ArrayList<>();
for(int i=0;i<all.size();i++){
buffer=new StringBuffer();
Map<String,String> o = (Map<String,String>)all.get(i);
System.out.println(o);
for(String index:split){
String name = o.get(index);
buffer.append(name);
}
indexBool.add(MD5Utils.getMd5(buffer.toString()));
}
System.out.println(indexBool);
return indexBool;
}