package com.fs.company.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyMenu;
import com.fs.company.service.ICompanyMenuService;
import com.fs.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.fs.common.core.domain.entity.SysMenu;
import com.fs.common.utils.ServletUtils;

/**
 * 菜单权限Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/companyMenu")
public class CompanyMenuController extends BaseController
{
    @Autowired
    private ICompanyMenuService companyMenuService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyMenu:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyMenu companyMenu)
    {
        startPage();
        List<CompanyMenu> list = companyMenuService.selectCompanyMenuList(companyMenu);
        return getDataTable(list);
    }

    /**
     * 导出菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyMenu:export')")
    @Log(title = "菜单权限", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyMenu companyMenu)
    {
        List<CompanyMenu> list = companyMenuService.selectCompanyMenuList(companyMenu);
        ExcelUtil<CompanyMenu> util = new ExcelUtil<CompanyMenu>(CompanyMenu.class);
        return util.exportExcel(list, "companyMenu");
    }

    /**
     * 获取菜单权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyMenu:query')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId)
    {
        return AjaxResult.success(companyMenuService.selectCompanyMenuById(menuId));
    }

    /**
     * 新增菜单权限
     */
    @PreAuthorize("@ss.hasPermi('company:companyMenu:add')")
    @Log(title = "菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyMenu companyMenu)
    {
        return toAjax(companyMenuService.insertCompanyMenu(companyMenu));
    }

    /**
     * 修改菜单权限
     */
    @PreAuthorize("@ss.hasPermi('company:companyMenu:edit')")
    @Log(title = "菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyMenu companyMenu)
    {
        return toAjax(companyMenuService.updateCompanyMenu(companyMenu));
    }

    /**
     * 删除菜单权限
     */
    @PreAuthorize("@ss.hasPermi('company:companyMenu:remove')")
    @Log(title = "菜单权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{menuIds}")
    public AjaxResult remove(@PathVariable Long[] menuIds)
    {
        return toAjax(companyMenuService.deleteCompanyMenuByIds(menuIds));
    }


    /**
     * 获取菜单下拉树列表
     */
    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(CompanyMenu menu)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<CompanyMenu> menus = companyMenuService.selectMenuList(menu, userId,"00");
        return AjaxResult.success(companyMenuService.buildMenuTreeSelect(menus));
    }

}
