json树数据

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;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值