彤医通项目之数据字典的列表、导出、导入功能

controller层

/**
 * @Author Weton Li
 * @Date 2021/4/13 17:40
 */
@Api("数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin // 解决跨域问题方法一
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation("根据id查找子节点数据")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id){

        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    /**
     * 导出 下载 数据
     * @param resp
     */
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse resp){
        dictService.exportDictData(resp);
    }

    @PostMapping("importData")
    public Result importDict(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }
}

service层

	@Service
	public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    /**
     * 根据id查询孩子数据
     *
     * @param id
     * @return
     */
    @Override
    public List<Dict> findChildData(Long id) {

        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id); // 将数据库中的id,和参数id对比
        List<Dict> list = baseMapper.selectList(wrapper);


        for (Dict dict : list) {
            Long dictId = dict.getId(); // 此id非彼参数id
            boolean isHave = isChild(dictId); // 调用isChild()方法
            dict.setHasChildren(isHave);
        }
        return list;
    }

    /**
     * 导出数据到磁盘,即下载
     *
     * @param resp
     */
    @Override
    public void exportDictData(HttpServletResponse resp) {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");
        String fileName = "dict";
        resp.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
// 查询数据库
        List<Dict> list = baseMapper.selectList(null);
        // 将Dict转换为DictEeVo
        ArrayList<DictEeVo> dictEeVoList = new ArrayList<>();
        for (Dict dict : list) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict, dictEeVo);
            dictEeVoList.add(dictEeVo);
        }

        try {
            EasyExcel.write(resp.getOutputStream(), DictEeVo.class)
                    .sheet("dict")
                    .doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 需要监听器 去上传导入 往内存写数据
     * @param file
     */
    @Override
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListencer(baseMapper))
            .sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断是否有孩子节点数据
     *
     * @param id
     * @return
     */
    private boolean isChild(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0;
    }
}

model层

/**
 * @Author Weton Li
 * @Date 2021/4/13 17:25
 */
@Data
@ApiModel(description = "数据字典")
@TableName("dict") // 数据库表名称
public class Dict {

    /*
    这里不继承BaseEntity,因为在dict中,主键不能设置为自增属性,
    因为导入的时候,很可能主键是自己制定的值
     */
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("create_time")
    private Date createTime;

    @ApiModelProperty(value = "更新时间")
    @TableField("update_time")
    private Date updateTime;

    @ApiModelProperty(value = "逻辑删除(1:已删除,0:未删除)")
    @TableLogic
    @TableField("is_deleted")
    private Integer isDeleted;

    @ApiModelProperty(value = "其他参数")
    @TableField(exist = false)
    private Map<String,Object> param = new HashMap<>();


// dict真实体
    @ApiModelProperty(value = "上级id")
    @TableField("parent_id")
    private Long parentId;

    @ApiModelProperty(value = "名称")
    @TableField("name")
    private String name;

    @ApiModelProperty(value = "值")
    @TableField("value")
    private String value;

    @ApiModelProperty(value = "编码")
    @TableField("dict_code")
    private String dictCode;

    @ApiModelProperty(value = "是否包含子节点")
    @TableField(exist = false) // 表中不存在的字段
    private boolean hasChildren; // element-ui中层级表格中需要此属性

}
/**
 * 导入导出的 表格excel实体类
 * @Author Weton Li
 * @Date 2021/4/15 21:28
 */
@Data
public class DictEeVo {

    @ExcelProperty(value = "id" ,index = 0)
    private Long id;

    @ExcelProperty(value = "上级id" ,index = 1)
    private Long parentId;

    @ExcelProperty(value = "名称" ,index = 2)
    private String name;

    @ExcelProperty(value = "值" ,index = 3)
    private String value;

    @ExcelProperty(value = "编码" ,index = 4)
    private String dictCode;

}

EasyExcel.read(file.getInputStream(),DictEeVo.class,监听器new DictListencer(baseMapper))
.sheet().doRead();

excel监听器

/**
 * @Author Weton Li
 * @Date 2021/4/16 21:53
 */
public class DictListencer extends AnalysisEventListener<DictEeVo> {

    private DictMapper dictMapper;
    public DictListencer(DictMapper dictMapper){
        this.dictMapper = dictMapper;
    }

    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo, dict);
        dictMapper.insert(dict);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值