package com.sptg.permissions.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.util.TreeUtil;
import com.sptg.permissions.api.dto.MenuDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Menu;
import com.sptg.permissions.api.feigns.MenuFeign;
import com.sptg.permissions.biz.service.MenuServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * title: MenuController
 * package: com.sptg.permissions.biz.controller
 * description: 菜单控制器
 *
 *                  
 *                 
 */
@RestController
@RequiredArgsConstructor
public class MenuController implements MenuFeign {

    private final MenuServiceInterface menuServiceInterface;

    @Override
    @GetMapping(value = "/menu/getMenuTreeList")
    public ResultBean<Menu> getMenuTreeList() {
        //  构建查询体检
//        Wrapper<Menu> wrapper = new EntityWrapper<>();
        //  过滤已删除的菜单
//        wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
        //  获取菜单列表
//        List<Menu> menuList = this.menuServiceInterface.selectList(wrapper);
        //  树形排序
//        menuList = TreeUtil.tree(menuList);
        List<Menu> menuTree = this.menuServiceInterface.getMenuWithTree(1L);
        return new ResultBean<>(ResultEnum.SUCCESS, menuTree);
    }

    @Override
    @PostMapping(value = "/menu/insertMenu")
    public ResultBean<Boolean> insertMenu(@RequestBody MenuDto menu) {
        Account onLineUser = menu.getOnLineUser();
        //  设置创建人Id
        menu.setCreateAccountId(onLineUser.getId());
        //  设置创建时间
        menu.setCreateDate(new Date());
        //  创建菜单
        return this.menuServiceInterface.insertMenu(menu);
    }

    @Override
    public ResultBean<Boolean> updateMenu(@RequestBody MenuDto menuDto) {
        return this.menuServiceInterface.updateMenu(menuDto);
    }

    @Override
    public ResultBean<Boolean> deleteMenu(@RequestBody MenuDto menuDto) {
        return this.menuServiceInterface.deleteMenu(menuDto);
    }

    @Override
    @GetMapping(value = "/menu/selectById")
    public Menu selectById(@RequestBody Long id) {
        return this.menuServiceInterface.selectById(id);
    }

    @Override
    public ResultBean<Menu> getMenuTreeWithRoot() {
        List<Menu> menuTree = this.menuServiceInterface.getMenuWithTree(0L);
        return new ResultBean<>(ResultEnum.SUCCESS, menuTree);
    }
}
