package com.yc.ship.module.miniapplet.controller.admin.menu;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.module.miniapplet.controller.admin.menu.vo.AppletMenuPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.menu.vo.AppletMenuRespVO;
import com.yc.ship.module.miniapplet.controller.admin.menu.vo.AppletMenuSaveReqVO;
import com.yc.ship.module.miniapplet.dal.dataobject.menu.AppletMenuDO;
import com.yc.ship.module.miniapplet.service.menu.AppletMenuService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 小程序菜单")
@RestController
@RequestMapping("/applet/menu")
@Validated
public class AppletMenuController {

    @Resource
    private AppletMenuService menuService;

    @PostMapping("/create")
    @Operation(summary = "创建小程序菜单")
    @PreAuthorize("@ss.hasPermission('applet:menu:create')")
    public CommonResult<Long> createMenu(@Valid @RequestBody AppletMenuSaveReqVO createReqVO) {
        return success(menuService.createMenu(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序菜单")
    @PreAuthorize("@ss.hasPermission('applet:menu:update')")
    public CommonResult<Boolean> updateMenu(@Valid @RequestBody AppletMenuSaveReqVO updateReqVO) {
        menuService.updateMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序菜单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('applet:menu:delete')")
    public CommonResult<Boolean> deleteMenu(@RequestParam("id") Long id) {
        menuService.deleteMenu(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序菜单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:menu:query')")
    public CommonResult<AppletMenuRespVO> getMenu(@RequestParam("id") Long id) {
        AppletMenuDO menu = menuService.getMenu(id);
        return success(BeanUtils.toBean(menu, AppletMenuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序菜单分页")
    @PreAuthorize("@ss.hasPermission('applet:menu:query')")
    public CommonResult<PageResult<AppletMenuRespVO>> getMenuPage(@Valid AppletMenuPageReqVO pageReqVO) {
        PageResult<AppletMenuDO> pageResult = menuService.getMenuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppletMenuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小程序菜单 Excel")
    @PreAuthorize("@ss.hasPermission('applet:menu:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMenuExcel(@Valid AppletMenuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppletMenuDO> list = menuService.getMenuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "小程序菜单.xls", "数据", AppletMenuRespVO.class,
                        BeanUtils.toBean(list, AppletMenuRespVO.class));
    }

}