导入导出逻辑梳理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yanwenwennihao/article/details/84777305

导入后端逻辑

导入需要下载模板,目的是导入表格规范

Controller层

/**
     * 下载上课班信息模板,批量导入使用
     *
     * @param response
     * @return
     * @author 严文文
     * @since 2.0.0 2018-11-25 16:51:36
     */
    @ApiOperation(value = "下载上课班信息模板,批量导入使用", notes = "下载上课班信息模板,批量导入使用")
    @GetMapping(value = {"/downLoadTeachClassTemplate"})
    public ItooResult downLoadTeachClassTemplate(HttpServletResponse response) {
        if (teachClassService.downLoadTeachClassTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载上课班信息模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载上课班信息模板失败");
        }
    }

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。
request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象就行了。

Servicelmpl层

 @Override
    public boolean downLoadTeachClassTemplate(HttpServletResponse response){
        Map<Serializable, Serializable> map = new HashMap<>(16);
        List<TeachClassExportTemplate> templateList = new ArrayList<>();
        TeachClassExportTemplate teachClassExportTemplate = new TeachClassExportTemplate();

        //添加下载模板范例
        teachClassExportTemplate.setCode("388539");
        teachClassExportTemplate.setName("中国近代历史人物评说1班");
        teachClassExportTemplate.setSchoolYear("2017-2018学年第二学期");
        teachClassExportTemplate.setCapacity("99");
        templateList.add(teachClassExportTemplate);

        //sheet的名字
        map.put(SHEET_NAME, "上课班信息");
        //需要导入的字段
        map.put(COLUMNS, new String[]{"code","name","schoolYear","capacity"});
        //表格的标题
        map.put(TITLE, "上课班信息");
        map.put(NAME, "上课班信息模板");
        map.put(DATA_LIST, (Serializable) templateList);
        try {
            ExcelUtil.exportExcel(map, response);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

JAVA中Map和HashMap

1、首先Map是一个接口,HashMap实现了Map接口的类;HashMap是类,Map是接口

2、Map是存储键和值这样的双列数据集合,但存储的数据是没有顺序的,其键不能重复,但其值是可以重复的,可以通过每一个键找到每一个对应的值;HashMap线程不同步的,即线程不安全的,但只有一个线程访问时效率较高;

3、Map是接口,HashMap是接口Map的实现类,体现了面向接口编程

4、HashMap实现了接口Map,就是说HashMap实现了Map所有的方法。

深入分析HashMap:https://blog.csdn.net/lianhuazy167/article/details/66967698

导入过程

 /**
     * 批量导入上课班信息
     * @param multipartFile
     * @param request
     * @param response
     * @return
     * @author 严文文
     * @since 2.0.0 2018-11-25 16:51:36
     */
    @Override
    public ItooResult importTeachClassByTemplate(MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response){
        response.setContentType("UTF-8");
        try {
            //校验文件是否存在
            String fileName = multipartFile.getOriginalFilename();
            if (fileName == null) {
                log.error("导入失败,fileName is null!");
                return ItooResult.build(ItooResult.FAIL, "传入的文件为空!");
            }
            //EXCEL解析成List
            Map<Serializable, Serializable> map = new HashMap<>(4);
            map.put(SHEET_NAME, "上课班信息");
            map.put(CLASS, TeachClassExportTemplate.class);
            List<TeachClassExportTemplate> teachClassExportTemplateList = ExcelUtil.importExcel(Objects.requireNonNull(fileName), map, request, response);

            if (teachClassExportTemplateList.size() == 0) {
                return ItooResult.build(ItooResult.FAIL, "导入的数据为空,请填写!");
            }
            //定义导入的错误数据集合并存redis中形式的<String,TeachClassExportTemplate>
            List<TeachClassExportTemplate> errorTPTList = new ArrayList<>();
            String errorTPTListId = IdWorker.getIdStr();
            Long size = redisTemplate.opsForZSet().size(errorTPTListId);
            size = size == null ? 0L : size;

            for (TeachClassExportTemplate excelTPT : teachClassExportTemplateList) {

                if (!this.verify(excelTPT)) {
                    TeachClassExportTemplate teachClassExportTemplate = new TeachClassExportTemplate();
                    BeanUtils.copyProperties(excelTPT, teachClassExportTemplate);

                    errorTPTList.add(teachClassExportTemplate);
                    redisTemplate.opsForZSet().add(errorTPTListId, teachClassExportTemplate, size + 1);
                    continue;
                }
                    TeachClassEntity teachClassEntity=insertValue(excelTPT);
                this.save(teachClassEntity);

            }
            //不符合条件的信息返回
            if (errorTPTList.size() > 0) {
                return ItooResult.build("0001","部分导入失败",errorTPTListId);
            }
        }
        catch(Exception e)
        {
            return ItooResult.build(ItooResult.FAIL, "导入数据异常!",e);
        }
        return ItooResult.build(ItooResult.SUCCESS, "导入数据成功!");
    }

    /**
     * 将模板的值插入实体中-导入专用
     *
     * @param teachClassExportTemplate
     * @return 严文文
     * @since 2.0.0 2018-12-2 16:49:41
     */
    private TeachClassEntity insertValue(TeachClassExportTemplate teachClassExportTemplate){
        TeachClassEntity teachClassEntity = new TeachClassEntity();
        //将导入内容放到entity中对应的属性中去
        BeanUtils.copyProperties(teachClassExportTemplate, teachClassEntity);

        //上课班ID
        teachClassEntity.setId(IdWorker.getIdStr());
        //可用
        teachClassEntity.setIsDelete(0);
        //上课班名称
        teachClassEntity.setName(teachClassExportTemplate.getName());
        teachClassEntity.setSchoolYear(teachClassExportTemplate.getSchoolYear());
        teachClassEntity.setCapacity(Integer.parseInt(teachClassExportTemplate.getCapacity()));

        //上课班代码
        teachClassEntity.setCode(teachClassExportTemplate.getCode());

        //选填数据
        //操作者
        teachClassEntity.setOperator("大米时代");
        return   teachClassEntity;

    }
            /**
             * 判断导入的信息是否为空
             *
             * @param excelTPT 需要导入的信息
             * @return boolean 导入的字段是否为空
             * @author 严文文
             * @since 2.0.0 2018-11-25 17:41:30
             */
            private boolean verify(TeachClassExportTemplate excelTPT )
            {
                if (StringUtils.isEmpty(excelTPT.getCode())) {
                    excelTPT.setFailReason("上课班代码未填写");
                    log.warn("导入失败,上课班代码未填写,excelFooEntity-{}", excelTPT);
                    return false;
                }
                if (StringUtils.isEmpty(excelTPT.getName())) {
                    excelTPT.setFailReason("上课班名称未填写");
                    log.warn("导入失败,上课班名称未填写,excelFooEntity-{}", excelTPT);
                    return false;
                }
                if (StringUtils.isEmpty(excelTPT.getCapacity())) {
                    excelTPT.setFailReason("上课班容量未填写");
                    log.warn("导入失败,上课班容量未填写,excelFooEntity-{}", excelTPT);
                    return false;
                }
                if (StringUtils.isEmpty(excelTPT.getSchoolYear())) {
                    excelTPT.setFailReason("学期学年未填写");
                    log.warn("导入失败,学期学年未填写,excelFooEntity-{}", excelTPT);
                    return false;
                }

                //通过上课班code查询上课班信息
                List<TeachClassModel> teachClassModel = teachClassService.findteachClassByCode(excelTPT.getCode());
                //判断是否存在该上课班code,存在记录下错误原因
                if (!CollectionUtils.isEmpty(teachClassModel)) {
                    excelTPT.setFailReason("已存在该上课班code");
                    log.warn("已存在上课班Code-",excelTPT.getCode());
                    return false;
                }

                TeachClassEntity teachClassEntity = new TeachClassEntity();
                //将导入内容放到entity中对应的属性中去
                BeanUtils.copyProperties(excelTPT, teachClassEntity);
//不存在即将专业code存入

                teachClassEntity.setCode(excelTPT.getCode());
                teachClassEntity.setName(excelTPT.getName());
                teachClassEntity.setSchoolYear(excelTPT.getSchoolYear());
                teachClassEntity.setCapacity(Integer.parseInt(excelTPT.getCapacity()));
                return true;
            }
response.setContentType("UTF-8"); 防止中文乱码

技术点:一条一条导入表格数据,并且把错误数据导出,显示错误原因

代码思路:1 校验文件是否存在  2 excel解析成List   3  定义导入错误数据集合并存redis中形式  4显示结果

 导出全部信息思路

Controller

 /**
     * 根据ID导出上课班信息
     *
     * @param response
     * @param idList
     * @return
     * @author 严文文
     * @since 2.0.0 2018-11-25 16:51:36
     */
    @ApiOperation(value = "根据ID导出上课班信息")
    @GetMapping(value = {"/exportAllByIds"})
    public ItooResult exportAllByIds(HttpServletResponse response,@RequestParam(required = false,defaultValue = "") List<String> idList) {
        return teachClassService.exportAllByIds(response, idList);
    }

ServiceImpl层

 @Override
    public ItooResult exportAllByIds(HttpServletResponse response, List<String> idList){
        Map<Serializable, Serializable> map = new HashMap<>(16);

        //sheet的名字
        map.put(SHEET_NAME, "上课班信息");
        //需要导入的字段
        map.put(COLUMNS, new String[]{"code","name","schoolYear","capacity"});
        //表格的标题
        map.put(TITLE, "上课班信息");
        map.put(NAME, "上课班信息模板");
        //要导出的数据
        List<TeachClassExportTemplate> teachClassExportTemplateList = new ArrayList<>();
        List<TeachClassModel> teachClassModelList = teachClassService.queryTeachClassInfoByIds(idList);
        for (TeachClassModel teachClassModel : teachClassModelList) {
            TeachClassExportTemplate teachClassExportTemplate = new TeachClassExportTemplate();
            BeanUtils.copyProperties(teachClassModel, teachClassExportTemplate);
            teachClassExportTemplate.setCapacity(String.valueOf(teachClassModel.getCapacity()));
            teachClassExportTemplateList.add(teachClassExportTemplate);
        }
        //为空不进行导出
        if (teachClassExportTemplateList.size() == 0) {
            return ItooResult.build(ItooResult.FAIL, "没有要导出的数据!");
        }
        map.put(DATA_LIST, (Serializable) teachClassExportTemplateList);

        //导出
        try {
            ExcelUtil.exportExcel(map, response);
        } catch (Exception e) {
            log.error("导出失败,未知的异常--" + e);
            return ItooResult.build(ItooResult.FAIL, "导出信息失败!",e);
        }
        return ItooResult.build(ItooResult.SUCCESS, "导出信息成功!");
    }

猜你喜欢

转载自blog.csdn.net/yanwenwennihao/article/details/84777305