package org.jeecg.modules.tms.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import liquibase.pro.packaged.Q; import org.apache.poi.ss.usermodel.*; import org.apache.shiro.SecurityUtils; import org.jeecg.common.api.vo.CommonGenericTree; import org.jeecg.common.api.vo.Result; import org.jeecg.common.constant.CommonConstant; 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.StrUtils; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.system.mapper.SysDictItemMapper; import org.jeecg.modules.system.mapper.SysDictMapper; import org.jeecg.modules.tms.entity.ToolsClassify; import org.jeecg.modules.tms.mapper.ToolsClassifyMapper; import org.jeecg.modules.tms.service.IToolsClassifyService; import org.jeecgframework.poi.excel.def.NormalExcelConstants; import org.jeecgframework.poi.excel.entity.ExportParams; import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 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 javax.tools.Tool; import java.io.IOException; import java.io.InputStream; import java.util.*; /** * @Description: tms_tools_classify * @Author: jeecg-boot * @Date: 2025-05-06 * @Version: V1.0 */ @Service public class ToolsClassifyServiceImpl extends ServiceImpl implements IToolsClassifyService { @Value("${jeecg.path.upload}") private String upLoadPath; @Autowired private SysDictMapper sysDictMapper; @Autowired private SysDictItemMapper sysDictItemMapper; @Override public IPage queryPageList(Page page, Map parameterMap) { QueryWrapper queryWrapper = Wrappers.query(); String[] classifyIds = parameterMap.get("classifyId"); if (classifyIds != null && classifyIds.length > 0) { queryWrapper.like("t.classify_id", classifyIds[0]); } String[] typeNames = parameterMap.get("typeName"); if (typeNames != null && typeNames.length > 0) { queryWrapper.like("t.type_name", typeNames[0]); } String[] parentIds = parameterMap.get("parentId"); if (parentIds != null && parentIds.length > 0) { queryWrapper.eq("t.parent_id", parentIds[0]); } queryWrapper.orderByAsc("t.seq"); return this.baseMapper.queryPageList(page, queryWrapper); } public List loadTree() { List toolsClassifyList = this.list(); List commonGenericTreeList = loadTree(toolsClassifyList); return commonGenericTreeList; } public List loadTree(List toolsClassifyList) { @SuppressWarnings("unused") ToolsClassify toolsClassify = new ToolsClassify(); List list = new ArrayList<>(); Map map = new HashMap<>(); CommonGenericTree node = new CommonGenericTree<>(); node.setKey("-1"); node.setTitle("工具分类"); node.setRField1(""); node.setRField2(""); node.setEntity(new ToolsClassify().setId("-1").setClassifyId("0").setTypeName("工具分类").setLeafFlag("2")); list.add(node); if (CollectionUtils.isNotEmpty(toolsClassifyList)) { CommonGenericTree tcNode; for (ToolsClassify tc : toolsClassifyList) { if (StrUtils.isBlankOrNull(tc.getParentId()) || tc.getParentId().equals("-1")) { tcNode = new CommonGenericTree<>(); tcNode.setKey(tc.getId()); tcNode.setTitle(tc.getClassifyId() + "/" + tc.getTypeName()); tcNode.setParentId(node.getKey()); tcNode.setIcon(""); tcNode.setType(1); tcNode.setValue(tc.getId()); tcNode.setDisabled(CommonConstant.STATUS_0.equals(tc.getStatus()) ? true : false); tcNode.setRField1(tc.getClassifyId()); tcNode.setRField2(getBaseParent(tc.getId(), 0).getClassifyId()); tcNode.setEntity(tc); node.addChildren(tcNode); map.put(tc.getId(), tcNode); } } CommonGenericTree childNode; for (ToolsClassify tc : toolsClassifyList) { ToolsClassify child = tc; if (map.containsKey(child.getParentId())) { if (StrUtils.isBlankOrNull(tc.getParentId()) || tc.getParentId().equals("-1")) { toolsClassify = tc; } else { tcNode = map.get(child.getParentId()); childNode = new CommonGenericTree<>(); childNode.setKey(tc.getId()); childNode.setTitle(tc.getClassifyId() + "/" + tc.getTypeName()); childNode.setParentId(tc.getParentId()); childNode.setIcon(""); childNode.setType(0); childNode.setValue(tc.getId()); childNode.setDisabled(CommonConstant.STATUS_0.equals(tc.getStatus()) ? true : false); childNode.setRField1(tc.getClassifyId()); childNode.setRField2(getBaseParent(tc.getId(), 0).getClassifyId()); childNode.setEntity(tc); tcNode.addChildren(childNode); map.put(child.getId(), childNode); } } } } return list; } public ToolsClassify getBaseParent(String id, int index) { ToolsClassify toolsClassify = null; if (index < 99) { toolsClassify = getById(id); if (StrUtils.isNotBlankOrNull(toolsClassify.getParentId())) { if (!toolsClassify.getParentId().equals("-1")) { toolsClassify = getBaseParent(toolsClassify.getParentId(), index++); } } } return toolsClassify; } @Override public ModelAndView toolClassifyExport(HttpServletRequest request, ToolsClassify toolsClassify, Class clazz, String title) { // Step.1 组装查询条件 QueryWrapper queryWrapper = new QueryWrapper<>(); LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); // 过滤选中数据 String selections = request.getParameter("selections"); if (oConvertUtils.isNotEmpty(selections)) { List selectionList = Arrays.asList(selections.split(",")); queryWrapper.in("t.id",selectionList); } // Step.2 获取导出数据 List exportList = this.baseMapper.queryExportList(queryWrapper); // Step.3 AutoPoi 导出Excel ModelAndView mv = new ModelAndView(new JeecgEntityExcelView()); //此处设置的filename无效 ,前端会重更新设置一下 mv.addObject(NormalExcelConstants.FILE_NAME, title); mv.addObject(NormalExcelConstants.CLASS, clazz); //update-begin--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置-------------------- ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title); exportParams.setImageBasePath(upLoadPath); //update-end--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置---------------------- mv.addObject(NormalExcelConstants.PARAMS,exportParams); mv.addObject(NormalExcelConstants.DATA_LIST, exportList); return mv; } @Override public Result toolClassifyImportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // 获取上传文件对象 MultipartFile file = multipartRequest.getFile("file"); //错误信息 List errorMessage = new ArrayList<>(); int successLines = 0, errorLines = 0; try (InputStream fis = file.getInputStream()) { Workbook workbook = WorkbookFactory.create(fis); // 获取第一个工作表 Sheet sheet = workbook.getSheetAt(0); for(Row row : sheet) { int rowNum = row.getRowNum()+1; ToolsClassify newToolsClassify = new ToolsClassify(); if (row.getRowNum() == 0) continue; Cell numCell = row.getCell(0); String num = numCell.getStringCellValue(); Cell nameCell = row.getCell(1); String name = nameCell.getStringCellValue(); Cell parentNumCell = row.getCell(2); String parentNum = parentNumCell.getStringCellValue(); Cell seqCell = row.getCell(3); String seq = seqCell.getStringCellValue(); Cell leafFlagCell = row.getCell(4); String leafFlag = leafFlagCell.getStringCellValue(); Cell paraTypeFlagCell = row.getCell(5); String paraTypeFlag = paraTypeFlagCell.getStringCellValue(); //转义是否为叶子节点 if("是".equals(leafFlag)){ leafFlag = "1"; }else if("否".equals(leafFlag)){ leafFlag = "2"; }else { errorMessage.add("第"+rowNum+"行,是否为叶子节点填写错误,请填写‘是’或’否‘"); errorLines++; continue; } if(StringUtils.isBlank(num)){ errorMessage.add("第"+rowNum+"行,分类编码不能为空"); errorLines++; continue; } if(StringUtils.isBlank(name)){ errorMessage.add("第"+rowNum+"行,分类名称不能为空"); errorLines++; continue; //判断是否有上级分类 } //判断参数模板是否存在 if(StringUtils.isNotBlank(paraTypeFlag)){ List dictList = sysDictMapper.queryDictItemValueByCodeAndText("tool_para_type",paraTypeFlag); if(CollectionUtils.isNotEmpty(dictList)){ paraTypeFlag = dictList.get(0).getValue(); }else { errorMessage.add("第"+rowNum+"行,参数模板不存在"); errorLines++; continue; } } if(StringUtils.isNotBlank(parentNum)){ ToolsClassify toolsClassify = this.lambdaQuery() .eq(ToolsClassify::getClassifyId,parentNum) .eq(ToolsClassify::getLeafFlag,"2").one(); if(toolsClassify == null){ errorMessage.add("第"+rowNum+"行,分类编码为"+num+"的数据的上级分类编号不存在或为叶子节点"); errorLines++; }else { //判断编码是否重复,重复的进行更新 ToolsClassify tools = this.lambdaQuery().eq(ToolsClassify::getClassifyId,num).one(); if(tools != null){ tools.setClassifyId(num) .setTypeName(name) .setStatus("1") .setLeafFlag(leafFlag) .setSeq(seq) .setParentId(toolsClassify.getId()) .setParaTypeFlag(paraTypeFlag); this.updateById(tools); }else{ newToolsClassify.setClassifyId(num) .setTypeName(name) .setStatus("1") .setLeafFlag(leafFlag) .setSeq(seq) .setParentId(toolsClassify.getId()) .setParaTypeFlag(paraTypeFlag); this.save(newToolsClassify); } successLines++; } //如果没填写上级分类编号,则新增到根节点 }else { //判断编码是否重复,重复的进行更新 ToolsClassify tools = this.lambdaQuery().eq(ToolsClassify::getClassifyId,num).one(); if(tools != null){ tools.setClassifyId(num) .setTypeName(name) .setStatus("1") .setLeafFlag(leafFlag) .setSeq(seq) .setParentId("-1") .setParaTypeFlag(paraTypeFlag); this.updateById(tools); }else { newToolsClassify.setClassifyId(num) .setTypeName(name) .setStatus("1") .setParentId("-1") .setSeq(seq) .setLeafFlag(leafFlag) .setParaTypeFlag(paraTypeFlag); this.save(newToolsClassify); } successLines++; } } }catch (IOException e) { e.printStackTrace(); } return ImportExcelUtil.imporReturnRes(errorLines,successLines,errorMessage); } }