package cn.com.ty.lift.ud.appmenu.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.com.ty.lift.ud.appmenu.mapper.AppmenuMapper;
import cn.com.ty.lift.ud.appmenu.mapper.entity.Appmenu;
import cn.com.ty.lift.ud.appmenu.mapper.entity.AppmenuTree;
import cn.com.ty.lift.ud.appmenu.service.IAppmenuService;
import cn.com.xwy.util.collection.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * app权限-ServiceImpl
 *
 * @author Xson
 * @date 2020-06-04
 */
@Service
@Transactional
public class AppmenuServiceImpl extends ServiceImpl<AppmenuMapper, Appmenu> implements IAppmenuService {

    @Autowired
    private AppmenuMapper appmenuMapper;

    @Override
    public Appmenu findByRouter(String router) {
        return appmenuMapper.findByRouter(router);
    }

    @Override
    public RestResponse queryAll() {
        List<Appmenu> queryAll = appmenuMapper.queryAll();
        return RestResponse.success(queryAll, "success");
    }

    /**
     * 查询所有菜单-树形结构
     * 
     * @return
     */
    @Override
    public RestResponse queryAllTree() {
        List<AppmenuTree> queryAllTreeList = appmenuMapper.queryAllTree();
        List<AppmenuTree> queryTreeList = new ArrayList<>();
        if (ListUtil.isNotEmpty(queryAllTreeList)) {
            for (AppmenuTree appmenuTree : queryAllTreeList) {
                Appmenu appmenu = appmenuMapper.selectById(appmenuTree.getParentId());
                if (null != appmenu) {
                    appmenuTree.setAppmenu(appmenu);
                }
                //顶级菜单过滤掉，页面只显示2级
                if(appmenuTree.getId() != 1){
                    queryTreeList.add(appmenuTree);
                }
            }
        }

        return RestResponse.success(queryTreeList, "查询成功");
    }

    /**
     * 查询所有父级菜单
     * 
     * @return
     */
    @Override
    public RestResponse queryAllParent() {
        List<Appmenu> appmenuList = appmenuMapper.queryAllParent();
        return RestResponse.success(appmenuList, "查询成功");
    }

}
