| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import liquibase.pro.packaged.Q; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.exception.JeecgBootException; |
| | | import org.jeecg.common.system.query.QueryGenerator; |
| | | import org.jeecg.common.system.vo.DictModel; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.common.util.ImportExcelUtil; |
| | | import org.jeecg.common.util.SqlInjectionUtil; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | import org.jeecg.modules.system.entity.SysCategory; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 分页列表查询 |
| | | * |
| | | * @param sysCategory |
| | | * @param pageNo |
| | | * @param pageSize |
| | |
| | | @GetMapping(value = "/childList") |
| | | public Result<List<SysCategory>> queryPageList(SysCategory sysCategory,HttpServletRequest req) { |
| | | Result<List<SysCategory>> result = new Result<List<SysCategory>>(); |
| | | QueryWrapper<SysCategory> queryWrapper = QueryGenerator.initQueryWrapper(sysCategory, req.getParameterMap()); |
| | | QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("pid", sysCategory.getPid()); |
| | | queryWrapper.orderByAsc("code"); |
| | | List<SysCategory> list = sysCategoryService.list(queryWrapper); |
| | | result.setSuccess(true); |
| | | result.setResult(list); |
| | |
| | | |
| | | /** |
| | | * 添加 |
| | | * |
| | | * @param sysCategory |
| | | * @return |
| | | */ |
| | | @PostMapping(value = "/add") |
| | | public Result<SysCategory> add(@RequestBody SysCategory sysCategory) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | try { |
| | | sysCategoryService.addSysCategory(sysCategory); |
| | | result.success("添加成功!"); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(),e); |
| | | result.error500("操作失败"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 编辑 |
| | | * |
| | | * @param sysCategory |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/edit", method = { RequestMethod.PUT,RequestMethod.POST }) |
| | | public Result<SysCategory> edit(@RequestBody SysCategory sysCategory) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | SysCategory sysCategoryEntity = sysCategoryService.getById(sysCategory.getId()); |
| | | if(sysCategoryEntity==null) { |
| | | result.error500("未找到对应实体"); |
| | | }else { |
| | | sysCategoryService.updateSysCategory(sysCategory); |
| | | result.success("修改成功!"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 通过id删除 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 批量删除 |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 通过id查询 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | |
| | | @RequestMapping(value = "/exportXls") |
| | | public ModelAndView exportXls(HttpServletRequest request, SysCategory sysCategory) { |
| | | // Step.1 组装查询条件查询数据 |
| | | QueryWrapper<SysCategory> queryWrapper = QueryGenerator.initQueryWrapper(sysCategory, request.getParameterMap()); |
| | | List<SysCategory> pageList = sysCategoryService.list(queryWrapper); |
| | | List<SysCategory> pageList = sysCategoryService.listByParams(sysCategory); |
| | | // Step.2 AutoPoi 导出Excel |
| | | ModelAndView mv = new ModelAndView(new JeecgEntityExcelView()); |
| | | // 过滤选中数据 |
| | |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | // 错误信息 |
| | | List<String> errorMessage = new ArrayList<>(); |
| | | int successLines = 0, errorLines = 0; |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | // 获取上传文件对象 |
| | | MultipartFile file = entity.getValue(); |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(2); |
| | | params.setTitleRows(0); |
| | | params.setHeadRows(1); |
| | | params.setNeedSave(true); |
| | | try { |
| | | List<SysCategory> listSysCategorys = ExcelImportUtil.importExcel(file.getInputStream(), SysCategory.class, params); |
| | | //按照编码长度排序 |
| | | Collections.sort(listSysCategorys); |
| | | log.info("排序后的list====>",listSysCategorys); |
| | | for (int i = 0; i < listSysCategorys.size(); i++) { |
| | | SysCategory sysCategoryExcel = listSysCategorys.get(i); |
| | | String code = sysCategoryExcel.getCode(); |
| | | if(code.length()>3){ |
| | | String pCode = sysCategoryExcel.getCode().substring(0,code.length()-3); |
| | | log.info("pCode====>",pCode); |
| | | String pId=sysCategoryService.queryIdByCode(pCode); |
| | | log.info("pId====>",pId); |
| | | if(StringUtils.isNotBlank(pId)){ |
| | | sysCategoryExcel.setPid(pId); |
| | | } |
| | | }else{ |
| | | sysCategoryExcel.setPid("0"); |
| | | } |
| | | try { |
| | | sysCategoryService.save(sysCategoryExcel); |
| | | successLines++; |
| | | } catch (Exception e) { |
| | | errorLines++; |
| | | String message = e.getMessage().toLowerCase(); |
| | | int lineNumber = i + 1; |
| | | // 通过索引名判断出错信息 |
| | | if (message.contains(CommonConstant.SQL_INDEX_UNIQ_CATEGORY_CODE)) { |
| | | errorMessage.add("第 " + lineNumber + " 行:分类编码已经存在,忽略导入。"); |
| | | } else { |
| | | errorMessage.add("第 " + lineNumber + " 行:未知错误,忽略导入"); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | //组装树结构 |
| | | List<SysCategory> treeNodes = buildTree(listSysCategorys); |
| | | for (SysCategory sysCategory : treeNodes) { |
| | | sysCategoryService.deepAdd(sysCategory, errorMessage); |
| | | } |
| | | } catch (Exception e) { |
| | | errorMessage.add("发生异常:" + e.getMessage()); |
| | | log.error(e.getMessage(), e); |
| | | } finally { |
| | | try { |
| | | file.getInputStream().close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException ignored) { |
| | | } |
| | | } |
| | | } |
| | | return ImportExcelUtil.imporReturnRes(errorLines,successLines,errorMessage); |
| | | return Result.ok("导入成功"); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * 校验编码 |
| | | * |
| | | * @param pid |
| | | * @param code |
| | | * @return |
| | |
| | | @GetMapping(value = "/checkCode") |
| | | public Result<?> checkCode(@RequestParam(name="pid",required = false) String pid,@RequestParam(name="code",required = false) String code) { |
| | | if(oConvertUtils.isEmpty(code)){ |
| | | return Result.error("错误,类型编码为空!"); |
| | | return Result.error("错误,分类编码为空!"); |
| | | } |
| | | if(oConvertUtils.isEmpty(pid)){ |
| | | SysCategory parent = sysCategoryService.getById(pid); |
| | | String rootCode = null; |
| | | if (parent != null) { |
| | | rootCode = parent.getRootCode(); |
| | | } |
| | | SysCategory original = sysCategoryService.getByCodeAndRootCode(code, rootCode); |
| | | if (original != null) { |
| | | return Result.error("编码已存在,请重新输入!"); |
| | | } |
| | | return Result.ok(); |
| | | } |
| | | SysCategory parent = this.sysCategoryService.getById(pid); |
| | | if(code.startsWith(parent.getCode())){ |
| | | return Result.ok(); |
| | | }else{ |
| | | return Result.error("编码不符合规范,须以\""+parent.getCode()+"\"开头!"); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分类字典树控件 加载节点 |
| | | * |
| | | * @param pid |
| | | * @param pcode |
| | | * @param condition |
| | |
| | | |
| | | /** |
| | | * [列表页面]加载分类字典数据 用于值的替换 |
| | | * |
| | | * @param code |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 根据父级id批量查询子节点 |
| | | * |
| | | * @param parentIds |
| | | * @return |
| | | */ |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 组装 树型结构 |
| | | * |
| | | * @param list |
| | | * @return |
| | | */ |
| | | private List<SysCategory> buildTree(List<SysCategory> list) { |
| | | //根节点 |
| | | List<SysCategory> rootList = new ArrayList<>(); |
| | | //节点 Map |
| | | Map<String, SysCategory> nodeMap = list.stream().collect(Collectors.toMap(SysCategory::getCode, node -> node)); |
| | | |
| | | for (SysCategory node : list) { |
| | | if (StringUtils.isBlank(node.getParentCode()) || ISysCategoryService.ROOT_PID_VALUE.equals(node.getParentCode())) { |
| | | rootList.add(node); |
| | | } else { |
| | | SysCategory parent = nodeMap.get(node.getParentCode()); |
| | | if (parent == null) { |
| | | throw new JeecgBootException("父节点不存在:" + node.getParentCode()); |
| | | } |
| | | parent.addChild(node); |
| | | } |
| | | } |
| | | |
| | | return rootList; |
| | | } |
| | | |
| | | |
| | | } |