package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.base.entity.Menu;
import com.tofly.base.service.MenuService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 移动端相关接口
 * @author tuonbed
 * @date 2020/3/26  9:05
 */
@RestController
@RequestMapping("/ms")
@Api(tags="移动端接口")
@Slf4j
@AllArgsConstructor
public class MsController {
    private final MenuService menuService;

    @ApiOperation(value = "获取用户移动端菜单信息")
    @GetMapping("/getUserMenus")
    public ResultRespone getUserMenu(){
        List menuList= menuService.listMaps(Wrappers.<Menu>lambdaQuery()
                .select(Menu::getId,Menu::getName,Menu::getParentId,Menu::getSort,Menu::getType,Menu::getIcon,Menu::getCode)
                .orderByAsc(Menu::getSort)
                .apply("exists\n" +
                        " (select 1\n" +
                        "          from TF_SMPN_USERROLE_X, TF_SMPN_ROLEMENU_X\n" +
                        "         where TF_SMPN_USERROLE_X.role_id = TF_SMPN_ROLEMENU_X.role_id\n" +
                        "           and TF_SMPN_ROLEMENU_X.menu_id = TF_SMPN_MENU_X.id\n" +
                        "           and TF_SMPN_USERROLE_X.user_id = {0})", SecurityUtils.getUserId())
                .apply("exists (select 1 from TF_SMPN_SYSTEM_X where TF_SMPN_SYSTEM_X.id=TF_SMPN_MENU_X.sys_id and code = 'ms' )")
        );
        return ResultRespone.success(menuList);
    }
}
