consum-base/src/main/java/com/consum/base/controller/BaseCategoryController.java
@@ -1,7 +1,19 @@
package com.consum.base.controller;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.consum.base.pojo.*;
import com.consum.base.pojo.excel.ImportClassificationTemplate;
import com.consum.model.po.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -9,20 +21,16 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.pojo.BaseCategoryParam;
import com.consum.base.pojo.ProjectTreeResult;
import com.consum.base.service.BaseCategoryService;
import com.consum.base.service.BaseGoodsTemplateService;
import com.consum.model.po.BaseCategory;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;
/**
 * @Description 物品分类
@@ -70,7 +78,7 @@
        }
        // 判断同一父类id下分类名称是否重复
        BaseCategory category = this.baseCategoryService.getByCategoryNameAndFatherCategoryId(param.getCategoryName(),
            param.getFatherCategoryId());
                param.getFatherCategoryId());
        if (category != null) {
            return ResponseValue.error("分类名称已存在");
        }
@@ -214,4 +222,184 @@
        return ResponseValue.success(baseCategories);
    }
    /**
     * 导入物品分类
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/import")
    public ResponseValue importData(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
//        if (!".xls".endsWith(originalFilename)) {
//            return ResponseValue.error("文件格式有误!");
//        }
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("当前登录用户为空");
        }
        // 当前登录用户
        S_user_core currentUser = this.getCurrentUser();
        try {
            EasyExcelFactory.read(file.getInputStream(), ImportClassificationTemplate.class,
                    new AnalysisEventListener<ImportClassificationTemplate>() {
                        /**
                         * 每条存储的list
                         */
                        final List<ImportClassificationTemplate> importData = Lists.newArrayList();
                        /**
                         * 读取的行号
                         */
                        Integer rowIndex = 0;
                        /**
                         * 表头信息
                         * @param headMap
                         * @param context
                         */
                        @Override
                        public void invokeHeadMap(Map headMap, AnalysisContext context) {
                            // 验证表头数量
                            logger.info("解析分发单的表头长度: {}", headMap.size());
                            if (headMap.size() != 4) {
                                throw new ExcelAnalysisException("上传的文件不符!");
                            }
                        }
                        /**
                         * 数据处理
                         * @param data
                         * @param analysisContext
                         */
                        @Override
                        public void invoke(ImportClassificationTemplate data, AnalysisContext analysisContext) {
                            rowIndex++;
                            // 跳过
                            if (data.getCategoryOne().contains("说明:")) {
                                return;
                            }
                            if (StrUtil.isEmpty(data.getCategoryOne())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,一级分类不能为空:" + data.getCategoryOne());
                            }
                            if (StrUtil.isEmpty(data.getCategoryTwo())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,二级分类不能为空:" + data.getCategoryTwo());
                            }
                            if (StrUtil.isEmpty(data.getCategoryThree())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,品类名称不能为空:" + data.getCategoryThree());
                            }
                            if (StrUtil.isEmpty(data.getType())) {
                                throw new ExcelAnalysisException("第" + rowIndex + "条数据,管理分类不能为空:" + data.getType());
                            }
                            importData.add(data);
                        }
                        /**
                         * 数据处理导入主方法
                         * @param analysisContext
                         */
                        @Override
                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                            for (ImportClassificationTemplate data : importData) {
                                String h1 = data.getCategoryOne();
                                String h2 = data.getCategoryTwo();
                                String h3 = data.getCategoryThree();
                                String type = data.getType();
                                BaseCategoryParam baseCategoryParam1 = new BaseCategoryParam();
                                baseCategoryParam1.setFatherCategoryId(0L);
                                baseCategoryParam1.setCategoryName(h1);
                                GenericPager<BaseCategory> baseCategoryGenericPager = baseCategoryService.queryBaseCategoryList2(baseCategoryParam1);
                                List<BaseCategory> datas = baseCategoryGenericPager.getDatas();
                                // 一级分类
                                BaseCategory D1 = null;
                                if (!CollectionUtil.isEmpty(datas)) {
                                    D1 = datas.get(0);
                                } else {
                                    // 新增父级
                                    BaseCategoryParam param = new BaseCategoryParam();
                                    param.setCategoryName(h1);
                                    param.setStates(1);
                                    int getIndex = baseCategoryService.selIndexByPid(0L);
                                    param.setOrderNumber(getIndex + 1);
                                    baseCategoryService.add(param, currentUser);
                                    GenericPager<BaseCategory> baseCategoryGenericPager2 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam1);
                                    List<BaseCategory> datas2 = baseCategoryGenericPager2.getDatas();
                                    D1 = datas2.get(0);
                                }
                                // 2、二级分类 查询第二层数据
                                BaseCategoryParam baseCategoryParam2 = new BaseCategoryParam();
                                baseCategoryParam2.setCategoryName(h2);
                                baseCategoryParam2.setFatherCategoryId(D1.getId());
                                GenericPager<BaseCategory> baseCategoryGenericPager2 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam2);
                                List<BaseCategory> datas2 = baseCategoryGenericPager2.getDatas();
                                BaseCategory D2 = null;
                                if (!CollectionUtil.isEmpty(datas2)) {
                                    D2 = datas2.get(0);
                                } else {
                                    // 新增父2级
                                    BaseCategoryParam param = new BaseCategoryParam();
                                    param.setCategoryName(h2);
                                    param.setStates(1);
                                    param.setFatherCategoryId(D1.getId());
                                    int getIndex = baseCategoryService.selIndexByPid(D1.getId());
                                    param.setOrderNumber(getIndex + 1);
                                    baseCategoryService.add(param, currentUser);
                                    GenericPager<BaseCategory> baseCategoryGenericPager4 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam2);
                                    List<BaseCategory> datas4 = baseCategoryGenericPager4.getDatas();
                                    D2 = datas4.get(0);
                                }
                                // 3、三级分类(如果已经存在,那么不处理,不存在新增)
                                BaseCategoryParam baseCategoryParam3 = new BaseCategoryParam();
                                baseCategoryParam3.setCategoryName(h3);
                                baseCategoryParam3.setClassification(type);
                                baseCategoryParam3.setFatherCategoryId(D2.getId());
                                GenericPager<BaseCategory> baseCategoryGenericPager3 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam3);
                                List<BaseCategory> datas3 = baseCategoryGenericPager3.getDatas();
                                if (!CollectionUtil.isEmpty(datas3)) {
                                } else {
                                    // 新增父2级
                                    BaseCategoryParam param = new BaseCategoryParam();
                                    param.setCategoryName(h3);
                                    param.setStates(1);
                                    param.setFatherCategoryId(D2.getId());
                                    int getIndex = baseCategoryService.selIndexByPid(D2.getId());
                                    param.setOrderNumber(getIndex + 1);
                                    param.setClassification(type);
                                    baseCategoryService.add(param, currentUser);
                                    //GenericPager<BaseCategory> baseCategoryGenericPager4 = baseCategoryService.queryBaseCategoryList2(baseCategoryParam3);
                                    //List<BaseCategory> datas4 = baseCategoryGenericPager4.getDatas();
                                    //D3 = datas4.get(0);
                                }
                            }
                        }
                        @Override
                        public void onException(Exception exception, AnalysisContext analysisContext) throws Exception {
                            if (exception instanceof ExcelDataConvertException) {
                                ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
                                Integer row = excelDataConvertException.getRowIndex() + 1;
                                Integer column = excelDataConvertException.getColumnIndex() + 1;
                                throw new ExcelAnalysisException("第" + row + "行,第" + column + "列解析异常,请正确填写");
                            } else {
                                throw new ExcelAnalysisException(exception.getMessage());
                            }
                        }
                    }).sheet(0).doRead();
        } catch (ExcelAnalysisException e) {
            return ResponseValue.error(e.getMessage());
        }catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseValue.error("系统错误");
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseValue.error("系统错误");
        }
        return ResponseValue.success("导入成功!");
    }
}