package com.dayou.controller;

import com.dayou.bo.MenuTreeBO;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.controller.BaseController;
import com.dayou.service.IMenuService;
import com.dayou.entity.Menu;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
/**
 * 菜单
 *
 * @author wucl
 * @since 2022-11-29
 */
@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController extends BaseController {
    @Autowired
    private IMenuService menuService;

    /**
    * 菜单列表
    */
    @GetMapping("")
    public RestResponse<Page<Menu>> page(Menu menu, Page page){
        Page<Menu> pages=menuService.selectPage(page,menu);
        return RestResponse.data(pages);
    }

    /**
     * 菜单详情
     */
    @GetMapping("/{id}")
    public RestResponse<Menu> detail(@PathVariable Long id){
        Menu xMenu =menuService.detail(id);
        return RestResponse.data(xMenu);
     }

    /**
     * 菜单新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody Menu menu) {
        Boolean ret = menuService.add(menu);
        return RestResponse.data(ret);
    }

    /**
     * 菜单更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody Menu menu) {
        Boolean ret = menuService.update(menu);
        return RestResponse.data(ret);
    }

    /**
     * 菜单删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = menuService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 菜单下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<Menu>()
        .select(Menu::getId,Menu::getName)
        .eq(Menu::getDeleted,false);
        List<Menu> list= menuService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }
    /**
     * 菜单模板下载
     */
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = menuService.generateImportTemplate();

            String fileName = "菜单导入模板" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("生成菜单导入模板异常", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }
    }

    /**
     * 菜单导入
     */
    @PostMapping("/importExcel")
    public RestResponse<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        menuService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 菜单导出
     */
    @GetMapping(value = "/download/exportExcel")
    public void export(Menu menu) {
        try {
            XSSFWorkbook wb =  menuService.exportExcel(menu);

            String fileName = "菜单" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("导出菜单", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }

    }

    /**
     * 所有菜单tree
     *
     * @return
     */
    @GetMapping(value = "/tree" )
    public RestResponse<List<MenuTreeBO>> menuTree() {
        List<MenuTreeBO> list = menuService.menuTree();
        return RestResponse.data(list);
    }
}

