package com.ruoyi.system.controller; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.SysTreeDict; import com.ruoyi.system.service.ISysTreeDictService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 树形字典Controller * * @author ruoyi * @date 2025-02-03 */ @RestController @RequestMapping("/system/dict") public class SysTreeDictController extends BaseController { @Autowired private ISysTreeDictService sysTreeDictService; @Autowired private SysUnitConvertController sysUnitConvertController; /** * 查询树形字典列表 */ @PreAuthorize("@ss.hasPermi('system:dict:list')") @GetMapping("/list") public TableDataInfo list(SysTreeDict sysTreeDict) { startPage(); List list = sysTreeDictService.selectSysTreeDictList(sysTreeDict); return getDataTable(list); } /** * 导出树形字典列表 */ @PreAuthorize("@ss.hasPermi('system:dict:export')") @Log(title = "树形字典", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, SysTreeDict sysTreeDict) { List list = sysTreeDictService.selectSysTreeDictList(sysTreeDict); ExcelUtil util = new ExcelUtil(SysTreeDict.class); util.exportExcel(response, list, "树形字典数据"); } /** * 获取树形字典详细信息 */ @PreAuthorize("@ss.hasPermi('system:dict:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return success(sysTreeDictService.selectSysTreeDictById(id)); } /** * 新增树形字典 */ @PreAuthorize("@ss.hasPermi('system:dict:add')") @Log(title = "树形字典", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody SysTreeDict sysTreeDict) { return toAjax(sysTreeDictService.insertSysTreeDict(sysTreeDict)); } /** * 修改树形字典 */ @PreAuthorize("@ss.hasPermi('system:dict:edit')") @Log(title = "树形字典", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody SysTreeDict sysTreeDict) { return toAjax(sysTreeDictService.updateSysTreeDict(sysTreeDict)); } /** * 删除树形字典 */ @PreAuthorize("@ss.hasPermi('system:dict:remove')") @Log(title = "树形字典", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(sysTreeDictService.deleteSysTreeDictByIds(ids)); } //生成树形菜单的形式 @PreAuthorize("@ss.hasPermi('system:dict:remove')") @Log(title = "树形字典", businessType = BusinessType.DELETE) @GetMapping("/getTree") public List buildDictTree(List list) { List returnList = new ArrayList<>(); List tempList = list.stream().map(SysTreeDict::getId).collect(Collectors.toList()); for (SysTreeDict dict : list) { if (!tempList.contains(dict.getPid())) { recursionFn(list, dict); returnList.add(dict); } } if (returnList.isEmpty()) { returnList = list; } return returnList; } private void recursionFn(List list, SysTreeDict dict) { List childList = getChildList(list, dict); dict.setChildren(childList); for (SysTreeDict child : childList) { if (hasChild(list, child)) { recursionFn(list, child); } } } private List getChildList(List list, SysTreeDict dict) { return list.stream() .filter(d -> d.getPid() != null && d.getPid().equals(dict.getId())) .sorted(Comparator.comparingInt(SysTreeDict::getDictSort)) .collect(Collectors.toList()); } private boolean hasChild(List list, SysTreeDict dict) { return list.stream().anyMatch(d -> d.getPid() != null && d.getPid().equals(dict.getId())); } }