package com.feima.xxhglbaseservice.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.feima.entity.base.TFunction; import com.feima.entity.base.TFunctionExample; import com.feima.utils.IdWorker; import com.feima.utils.ZtreeDto; import com.feima.xxhglbaseservice.dao.TFunctionMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import sun.util.calendar.LocalGregorianCalendar; import javax.xml.crypto.Data; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class TFunctionService { @Autowired private TFunctionMapper tFunctionMapper; @Autowired private IdWorker idWorker; public List<ZtreeDto> selectAll(){ /* TFunctionExample tFunctionExample = new TFunctionExample(); tFunctionExample.createCriteria().andParentFunctionIdEqualTo(id);*/ return null; }; /** * 根据id 生成的树结构数据 * @param id * @return */ public List<ZtreeDto> getZtreeById(long id){ List<TFunction> list = getTFunctionById(id); List<ZtreeDto> zlist= transformTreePos(list); List<ZtreeDto> ztreeDtos = buildZtreeDto(zlist); if(ztreeDtos!=null&&ztreeDtos.size()>0){ return ztreeDtos; }else { return null; } } /** * 获取全部目录 * @return */ public List<ZtreeDto> getAllZtreeDto(){ List<Long> ids=getToolNumber(); List<ZtreeDto> ztreeDtos = new ArrayList<>(); for (Long id: ids) { List<ZtreeDto> list=getZtreeById(id); ztreeDtos.addAll(list); } return ztreeDtos; } /** * 分组树结构 * @param list * @return */ public List<ZtreeDto> buildZtreeDto(List<ZtreeDto> list){ for(int i = 0;i<list.size();i++){ List<TFunction> childrenTF=getTFunctionByParentId(list.get(i).getId()); List<ZtreeDto> children = transformTreePos(childrenTF); buildZtreeDto(children); list.get(i).setChildren(children); } return list; } /** * 根据父节点查找实体 * @return */ public List<TFunction> getTFunctionByParentId(Long id){ TFunctionExample tFunctionExample = new TFunctionExample(); tFunctionExample.createCriteria().andParentFunctionIdEqualTo(id); return tFunctionMapper.selectByExample(tFunctionExample); } /** * 根据id查找实体 * @return */ public List<TFunction> getTFunctionById(Long id){ TFunctionExample tFunctionExample = new TFunctionExample(); tFunctionExample.createCriteria().andIdEqualTo(id); return tFunctionMapper.selectByExample(tFunctionExample); } /** * 将实体类转成树结构 */ public List<ZtreeDto> transformTreePos(List<TFunction> list){ List<ZtreeDto> ztreeDtos = new ArrayList<>(); for (int i=0;i<list.size();i++){ TFunction tFunction = list.get(i); ZtreeDto ztreeDto = transformTreePo(tFunction); ztreeDtos.add(ztreeDto); } return ztreeDtos; } /** * 将实体类转成树实体 * @param tFunction * @return */ public ZtreeDto transformTreePo(TFunction tFunction){ ZtreeDto ztreeDto = new ZtreeDto(); ztreeDto.setpId(tFunction.getParentFunctionId()); ztreeDto.setId(tFunction.getId()); ztreeDto.setName(tFunction.getFunctionName()); return ztreeDto; } /** * 根据主键删除目录 * @param id * @return */ public int deleteMenu(Long id){ int i=tFunctionMapper.deleteByPrimaryKey(id); if(i==1){ return 1; }else{ return 0; } } /** * 根据主键更新目录 * @return */ public int updataMenu(TFunction tFunction){ tFunction.setCreateTime(new Date()); int i=tFunctionMapper.updateByPrimaryKeySelective(tFunction); if(i==1){ return 1; }else { return 0; } } /** * 添加目录 * @return */ public int insertMenu(TFunction tFunction){ tFunction.setCreateTime(new Date()); tFunction.setId(idWorker.nextId()); int resulet=tFunctionMapper.insertSelective(tFunction); if(resulet==1){ return 1; }else { return 0; } } /** * 查询根目录的目录个数 */ public List<Long> getToolNumber(){ TFunctionExample tFunctionExample = new TFunctionExample(); Short s = 1; tFunctionExample.createCriteria().andFunctionLevelEqualTo(s); List<TFunction> tFunctions =tFunctionMapper.selectByExample(tFunctionExample); List<Long> list = new ArrayList<>(); for (TFunction tFunction: tFunctions) { list.add(tFunction.getId()); } return list; } }
//
package com.feima.utils; import java.io.Serializable; import java.util.List; public class ZtreeDto implements Serializable { private Long id; private Long pId; private String name; private boolean open; private List<ZtreeDto> children; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getpId() { return pId; } public void setpId(Long pId) { this.pId = pId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isOpen() { return open; } public void setOpen(boolean open) { this.open = open; } public List<ZtreeDto> getChildren() { return children; } public void setChildren(List<ZtreeDto> children) { this.children = children; } }