¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.modules.system.controller; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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.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.SqlInjectionUtil; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | import org.jeecg.modules.system.entity.SysCategory; |
| | | import org.jeecg.modules.system.model.TreeSelectModel; |
| | | import org.jeecg.modules.system.service.ISysCategoryService; |
| | | import org.jeecgframework.poi.excel.ExcelImportUtil; |
| | | import org.jeecgframework.poi.excel.def.NormalExcelConstants; |
| | | import org.jeecgframework.poi.excel.entity.ExportParams; |
| | | import org.jeecgframework.poi.excel.entity.ImportParams; |
| | | import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Description: åç±»åå
¸ |
| | | * @Author: jeecg-boot |
| | | * @Date: 2019-05-29 |
| | | * @Version: V1.0 |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/sys/category") |
| | | @Slf4j |
| | | public class SysCategoryController { |
| | | @Autowired |
| | | private ISysCategoryService sysCategoryService; |
| | | |
| | | /** |
| | | * åç±»ç¼ç 0 |
| | | */ |
| | | private static final String CATEGORY_ROOT_CODE = "0"; |
| | | |
| | | /** |
| | | * å页å表æ¥è¯¢ |
| | | * |
| | | * @param sysCategory |
| | | * @param pageNo |
| | | * @param pageSize |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @GetMapping(value = "/rootList") |
| | | public Result<IPage<SysCategory>> queryPageList(SysCategory sysCategory, |
| | | @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, |
| | | @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, |
| | | HttpServletRequest req) { |
| | | if (oConvertUtils.isEmpty(sysCategory.getPid())) { |
| | | sysCategory.setPid("0"); |
| | | } |
| | | Result<IPage<SysCategory>> result = new Result<IPage<SysCategory>>(); |
| | | |
| | | //--author:os_chengtgen---date:20190804 -----for: åç±»åå
¸é¡µé¢æ¾ç¤ºé误,issues:377--------start |
| | | //--author:liusq---date:20211119 -----for: ãvue3ãåç±»åå
¸é¡µé¢æ¥è¯¢æ¡ä»¶é
ç½®--------start |
| | | QueryWrapper<SysCategory> queryWrapper = QueryGenerator.initQueryWrapper(sysCategory, req.getParameterMap()); |
| | | String name = sysCategory.getName(); |
| | | String code = sysCategory.getCode(); |
| | | //QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<SysCategory>(); |
| | | if (StringUtils.isBlank(name) && StringUtils.isBlank(code)) { |
| | | queryWrapper.eq("pid", sysCategory.getPid()); |
| | | } |
| | | //--author:liusq---date:20211119 -----for: åç±»åå
¸é¡µé¢æ¥è¯¢æ¡ä»¶é
ç½®--------end |
| | | //--author:os_chengtgen---date:20190804 -----for:ãvue3ã åç±»åå
¸é¡µé¢æ¾ç¤ºé误,issues:377--------end |
| | | |
| | | Page<SysCategory> page = new Page<SysCategory>(pageNo, pageSize); |
| | | IPage<SysCategory> pageList = sysCategoryService.page(page, queryWrapper); |
| | | result.setSuccess(true); |
| | | result.setResult(pageList); |
| | | return result; |
| | | } |
| | | |
| | | @GetMapping(value = "/childList") |
| | | public Result<List<SysCategory>> queryPageList(SysCategory sysCategory, HttpServletRequest req) { |
| | | Result<List<SysCategory>> result = new Result<List<SysCategory>>(); |
| | | 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); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ·»å |
| | | * |
| | | * @param sysCategory |
| | | * @return |
| | | */ |
| | | @PostMapping(value = "/add") |
| | | public Result<SysCategory> add(@RequestBody SysCategory sysCategory) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | sysCategoryService.addSysCategory(sysCategory); |
| | | result.success("æ·»å æåï¼"); |
| | | 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>(); |
| | | sysCategoryService.updateSysCategory(sysCategory); |
| | | result.success("ä¿®æ¹æå!"); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * éè¿idå é¤ |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @DeleteMapping(value = "/delete") |
| | | public Result<SysCategory> delete(@RequestParam(name = "id", required = true) String id) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | SysCategory sysCategory = sysCategoryService.getById(id); |
| | | if (sysCategory == null) { |
| | | result.error500("æªæ¾å°å¯¹åºå®ä½"); |
| | | } else { |
| | | this.sysCategoryService.deleteSysCategory(id); |
| | | result.success("å 餿å!"); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * æ¹éå é¤ |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @DeleteMapping(value = "/deleteBatch") |
| | | public Result<SysCategory> deleteBatch(@RequestParam(name = "ids", required = true) String ids) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | if (ids == null || "".equals(ids.trim())) { |
| | | result.error500("åæ°ä¸è¯å«ï¼"); |
| | | } else { |
| | | this.sysCategoryService.deleteSysCategory(ids); |
| | | result.success("å 餿å!"); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * éè¿idæ¥è¯¢ |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @GetMapping(value = "/queryById") |
| | | public Result<SysCategory> queryById(@RequestParam(name = "id", required = true) String id) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | SysCategory sysCategory = sysCategoryService.getById(id); |
| | | if (sysCategory == null) { |
| | | result.error500("æªæ¾å°å¯¹åºå®ä½"); |
| | | } else { |
| | | result.setResult(sysCategory); |
| | | result.setSuccess(true); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 导åºexcel |
| | | * |
| | | * @param request |
| | | */ |
| | | @RequestMapping(value = "/exportXls") |
| | | public ModelAndView exportXls(HttpServletRequest request, SysCategory sysCategory) { |
| | | // Step.1 ç»è£
æ¥è¯¢æ¡ä»¶æ¥è¯¢æ°æ® |
| | | List<SysCategory> pageList = sysCategoryService.listByParams(sysCategory); |
| | | // Step.2 AutoPoi 导åºExcel |
| | | ModelAndView mv = new ModelAndView(new JeecgEntityExcelView()); |
| | | // è¿æ»¤é䏿°æ® |
| | | String selections = request.getParameter("selections"); |
| | | if (oConvertUtils.isEmpty(selections)) { |
| | | mv.addObject(NormalExcelConstants.DATA_LIST, pageList); |
| | | } else { |
| | | List<String> selectionList = Arrays.asList(selections.split(",")); |
| | | List<SysCategory> exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList()); |
| | | mv.addObject(NormalExcelConstants.DATA_LIST, exportList); |
| | | } |
| | | //å¯¼åºæä»¶åç§° |
| | | mv.addObject(NormalExcelConstants.FILE_NAME, "åç±»åå
¸å表"); |
| | | mv.addObject(NormalExcelConstants.CLASS, SysCategory.class); |
| | | LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("åç±»åå
¸åè¡¨æ°æ®", "导åºäºº:" + user.getRealname(), "导åºä¿¡æ¯")); |
| | | return mv; |
| | | } |
| | | |
| | | /** |
| | | * éè¿excel导å
¥æ°æ® |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/importExcel", method = RequestMethod.POST) |
| | | public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | | Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); |
| | | // éè¯¯ä¿¡æ¯ |
| | | List<String> errorMessage = new ArrayList<>(); |
| | | for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { |
| | | // è·åä¸ä¼ æä»¶å¯¹è±¡ |
| | | MultipartFile file = entity.getValue(); |
| | | ImportParams params = new ImportParams(); |
| | | params.setTitleRows(0); |
| | | params.setHeadRows(1); |
| | | params.setNeedSave(true); |
| | | try { |
| | | List<SysCategory> listSysCategorys = ExcelImportUtil.importExcel(file.getInputStream(), SysCategory.class, params); |
| | | //ç»è£
æ ç»æ |
| | | List<SysCategory> treeNodes = buildTree(listSysCategorys); |
| | | for (SysCategory sysCategory : treeNodes) { |
| | | sysCategoryService.deepAdd(sysCategory, errorMessage); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } finally { |
| | | try { |
| | | file.getInputStream().close(); |
| | | } catch (IOException ignored) { |
| | | } |
| | | } |
| | | } |
| | | return Result.ok("导å
¥æå"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * å è½½åä¸ªæ°æ® ç¨äºåæ¾ |
| | | */ |
| | | @RequestMapping(value = "/loadOne", method = RequestMethod.GET) |
| | | public Result<SysCategory> loadOne(@RequestParam(name = "field") String field, @RequestParam(name = "val") String val) { |
| | | Result<SysCategory> result = new Result<SysCategory>(); |
| | | try { |
| | | //update-begin-author:taoyan date:2022-5-6 for: issues/3663 sql注å
¥é®é¢ |
| | | boolean isClassField = SqlInjectionUtil.isClassField(field, SysCategory.class); |
| | | if (!isClassField) { |
| | | return Result.error("åæ®µæ æï¼è¯·æ£æ¥!"); |
| | | } |
| | | //update-end-author:taoyan date:2022-5-6 for: issues/3663 sql注å
¥é®é¢ |
| | | QueryWrapper<SysCategory> query = new QueryWrapper<SysCategory>(); |
| | | query.eq(field, val); |
| | | List<SysCategory> ls = this.sysCategoryService.list(query); |
| | | if (ls == null || ls.size() == 0) { |
| | | result.setMessage("æ¥è¯¢æ æ"); |
| | | result.setSuccess(false); |
| | | } else if (ls.size() > 1) { |
| | | result.setMessage("æ¥è¯¢æ°æ®å¼å¸¸,[" + field + "]åå¨å¤ä¸ªå¼:" + val); |
| | | result.setSuccess(false); |
| | | } else { |
| | | result.setSuccess(true); |
| | | result.setResult(ls.get(0)); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | result.setMessage(e.getMessage()); |
| | | result.setSuccess(false); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * å è½½èç¹çåæ°æ® |
| | | */ |
| | | @RequestMapping(value = "/loadTreeChildren", method = RequestMethod.GET) |
| | | public Result<List<TreeSelectModel>> loadTreeChildren(@RequestParam(name = "pid") String pid) { |
| | | Result<List<TreeSelectModel>> result = new Result<List<TreeSelectModel>>(); |
| | | try { |
| | | List<TreeSelectModel> ls = this.sysCategoryService.queryListByPid(pid); |
| | | result.setResult(ls); |
| | | result.setSuccess(true); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | result.setMessage(e.getMessage()); |
| | | result.setSuccess(false); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * å è½½ä¸çº§èç¹/妿æ¯åæ¥ åæææ°æ® |
| | | */ |
| | | @RequestMapping(value = "/loadTreeRoot", method = RequestMethod.GET) |
| | | public Result<List<TreeSelectModel>> loadTreeRoot(@RequestParam(name = "async") Boolean async, @RequestParam(name = "pcode") String pcode) { |
| | | Result<List<TreeSelectModel>> result = new Result<List<TreeSelectModel>>(); |
| | | try { |
| | | List<TreeSelectModel> ls = this.sysCategoryService.queryListByCode(pcode); |
| | | if (!async) { |
| | | loadAllCategoryChildren(ls); |
| | | } |
| | | result.setResult(ls); |
| | | result.setSuccess(true); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | result.setMessage(e.getMessage()); |
| | | result.setSuccess(false); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * é彿±åèç¹ åæ¥å è½½ç¨å° |
| | | */ |
| | | private void loadAllCategoryChildren(List<TreeSelectModel> ls) { |
| | | for (TreeSelectModel tsm : ls) { |
| | | List<TreeSelectModel> temp = this.sysCategoryService.queryListByPid(tsm.getKey()); |
| | | if (temp != null && temp.size() > 0) { |
| | | tsm.setChildren(temp); |
| | | loadAllCategoryChildren(temp); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªç¼ç |
| | | * |
| | | * @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("é误,åç±»ç¼ç 为空!"); |
| | | } |
| | | 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(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * åç±»åå
¸æ æ§ä»¶ å è½½èç¹ |
| | | * |
| | | * @param pid |
| | | * @param pcode |
| | | * @param condition |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/loadTreeData", method = RequestMethod.GET) |
| | | public Result<List<TreeSelectModel>> loadDict(@RequestParam(name = "pid", required = false) String pid, @RequestParam(name = "pcode", required = false) String pcode, @RequestParam(name = "condition", required = false) String condition) { |
| | | Result<List<TreeSelectModel>> result = new Result<List<TreeSelectModel>>(); |
| | | //pidå¦æä¼ å¼äº 就忽ç¥pcodeçä½ç¨ |
| | | if (oConvertUtils.isEmpty(pid)) { |
| | | if (oConvertUtils.isEmpty(pcode)) { |
| | | result.setSuccess(false); |
| | | result.setMessage("å è½½åç±»åå
¸æ åæ°æè¯¯.[null]!"); |
| | | return result; |
| | | } else { |
| | | if (ISysCategoryService.ROOT_PID_VALUE.equals(pcode)) { |
| | | pid = ISysCategoryService.ROOT_PID_VALUE; |
| | | } else { |
| | | pid = this.sysCategoryService.queryIdByCode(pcode); |
| | | } |
| | | if (oConvertUtils.isEmpty(pid)) { |
| | | result.setSuccess(false); |
| | | result.setMessage("å è½½åç±»åå
¸æ åæ°æè¯¯.[code]!"); |
| | | return result; |
| | | } |
| | | } |
| | | } |
| | | Map<String, String> query = null; |
| | | if (oConvertUtils.isNotEmpty(condition)) { |
| | | query = JSON.parseObject(condition, Map.class); |
| | | } |
| | | List<TreeSelectModel> ls = sysCategoryService.queryListByPid(pid, query); |
| | | result.setSuccess(true); |
| | | result.setResult(ls); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * åç±»åå
¸æ§ä»¶æ°æ®åæ¾[表å页é¢] |
| | | * |
| | | * @param ids |
| | | * @param delNotExist æ¯å¦ç§»é¤ä¸åå¨ç项ï¼é»è®¤ä¸ºtrueï¼è®¾ä¸ºfalse妿æä¸ªkeyä¸å卿°æ®åºä¸ï¼åç´æ¥è¿åkeyæ¬èº« |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/loadDictItem", method = RequestMethod.GET) |
| | | public Result<List<String>> loadDictItem(@RequestParam(name = "ids") String ids, @RequestParam(name = "delNotExist", required = false, defaultValue = "true") boolean delNotExist) { |
| | | Result<List<String>> result = new Result<>(); |
| | | // éç©ºå¤æ |
| | | if (StringUtils.isBlank(ids)) { |
| | | result.setSuccess(false); |
| | | result.setMessage("ids ä¸è½ä¸ºç©º"); |
| | | return result; |
| | | } |
| | | // æ¥è¯¢æ°æ® |
| | | List<String> textList = sysCategoryService.loadDictItem(ids, delNotExist); |
| | | result.setSuccess(true); |
| | | result.setResult(textList); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * [å表页é¢]å è½½åç±»åå
¸æ°æ® ç¨äºå¼çæ¿æ¢ |
| | | * |
| | | * @param code |
| | | * @return |
| | | */ |
| | | @RequestMapping(value = "/loadAllData", method = RequestMethod.GET) |
| | | public Result<List<DictModel>> loadAllData(@RequestParam(name = "code", required = true) String code) { |
| | | Result<List<DictModel>> result = new Result<List<DictModel>>(); |
| | | LambdaQueryWrapper<SysCategory> query = new LambdaQueryWrapper<SysCategory>(); |
| | | if (oConvertUtils.isNotEmpty(code) && !CATEGORY_ROOT_CODE.equals(code)) { |
| | | query.likeRight(SysCategory::getCode, code); |
| | | } |
| | | List<SysCategory> list = this.sysCategoryService.list(query); |
| | | if (list == null || list.size() == 0) { |
| | | result.setMessage("æ æ°æ®,åæ°æè¯¯.[code]"); |
| | | result.setSuccess(false); |
| | | return result; |
| | | } |
| | | List<DictModel> rdList = new ArrayList<DictModel>(); |
| | | for (SysCategory c : list) { |
| | | rdList.add(new DictModel(c.getId(), c.getName())); |
| | | } |
| | | result.setSuccess(true); |
| | | result.setResult(rdList); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®ç¶çº§idæ¹éæ¥è¯¢åèç¹ |
| | | * |
| | | * @param parentIds |
| | | * @return |
| | | */ |
| | | @GetMapping("/getChildListBatch") |
| | | public Result getChildListBatch(@RequestParam("parentIds") String parentIds) { |
| | | try { |
| | | QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>(); |
| | | List<String> parentIdList = Arrays.asList(parentIds.split(",")); |
| | | queryWrapper.in("pid", parentIdList); |
| | | List<SysCategory> list = sysCategoryService.list(queryWrapper); |
| | | IPage<SysCategory> pageList = new Page<>(1, 10, list.size()); |
| | | pageList.setRecords(list); |
| | | return Result.OK(pageList); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | return Result.error("æ¹éæ¥è¯¢åèç¹å¤±è´¥ï¼" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * ç»è£
æ åç»æ |
| | | * |
| | | * @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; |
| | | } |
| | | |
| | | |
| | | } |