package com.ningdatech.pmapi.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.log.annotation.WebLog;
import com.ningdatech.pmapi.common.model.entity.MenuTreeEntity;
import com.ningdatech.pmapi.common.util.TreeUtil;
import com.ningdatech.pmapi.sys.manage.MenuManage;
import com.ningdatech.pmapi.sys.model.dto.MenuQueryDTO;
import com.ningdatech.pmapi.sys.model.dto.MenuSaveDTO;
import com.ningdatech.pmapi.sys.model.dto.MenuUpdateDTO;
import com.ningdatech.pmapi.sys.model.entity.Menu;
import com.ningdatech.pmapi.sys.model.vo.MenuRoleVO;
import com.ningdatech.pmapi.sys.model.vo.MenuVO;
import com.ningdatech.pmapi.sys.service.IMenuService;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器-菜单
 * </p>
 *
 * @author WendyYang
 * @since 2022-09-30
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/menu")
@Api(value = "Menu", tags = "系统管理-菜单")
public class MenuController {

    private final IMenuService menuService;
    private final MenuManage menuManage;

    @ApiOperation("查询系统所有的菜单")
    @GetMapping("/list")
    public List<Menu> allTree(@Valid @RequestParam(required = false, value = "title") String title) {
        List<Menu> list = menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSort));
        return TreeUtil.buildTree(list, title);
    }

    @ApiOperation("查询系统所有数据权限的菜单")
    @GetMapping("/listByDataScope")
    public List<Menu> allTreeByDataScope() {
        LambdaQueryWrapper<Menu> dsQuery = Wrappers.lambdaQuery(Menu.class)
                .eq(Menu::getHasDataScope, Boolean.TRUE)
                .orderByAsc(Menu::getSort);
        List<Menu> list = menuService.list(dsQuery);
        if (!list.isEmpty()) {
            List<Long> pidList = CollUtils.fieldList(list, MenuTreeEntity::getPid);
            list.addAll(menuService.listByIds(pidList));
        }
        return TreeUtil.buildTree(list);
    }

    @ApiOperation(value = "查询当前登录用户的菜单", notes = "查询当前登录用户的菜单")
    @GetMapping("/myMenu")
    public List<MenuRoleVO> currentUserMenu() {
        List<Menu> list = menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSort));
        return menuManage.buildUserMenu(list, LoginUserUtil.loginUserDetail());
    }

    @ApiOperation(value = "查询菜单", notes = "查询菜单")
    @GetMapping("/details")
    public MenuQueryDTO getDetails(@RequestParam Long id) {
        Menu menu = menuService.getById(id);
        return BeanUtil.toBean(menu, MenuQueryDTO.class);
    }

    @ApiOperation(value = "保存新菜单", notes = "保存新菜单")
    @PostMapping("/save")
    @WebLog("保存菜单")
    public MenuVO handlerSave(@Valid @RequestBody MenuSaveDTO data) {
        menuService.save(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, MenuVO.class);
    }

    @ApiOperation(value = "编辑菜单", notes = "编辑菜单")
    @PostMapping("/modify")
    @WebLog("编辑菜单")
    public MenuVO handlerUpdate(@RequestBody MenuUpdateDTO data) {
        menuService.update(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, MenuVO.class);
    }

    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @PostMapping("/remove")
    @WebLog("删除菜单")
    public Boolean handlerDelete(@RequestBody List<Long> ids) {
        return menuService.removeByIdWithCache(ids);
    }

}
