package shop.alien.store.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeSysMenu;
import shop.alien.store.service.LifeSysMenuService;

import java.util.List;

/**
 * 菜单权限表 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"数据中台-菜单管理"})
@ApiSort(3)
@CrossOrigin
@RestController
@RequestMapping("/sys/menu")
@RequiredArgsConstructor
public class LifeSysMenuController {

    private final LifeSysMenuService lifeSysMenuService;

    @ApiOperation("新增菜单")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名称", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "parentId", value = "父菜单ID（0表示根菜单）", dataType = "Long", paramType = "body"),
            @ApiImplicitParam(name = "menuType", value = "菜单类型（M目录 C菜单 F按钮）", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "menuSort", value = "显示顺序", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "path", value = "路由地址", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "component", value = "组件路径", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "perms", value = "权限标识（如：sys:user:list）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "icon", value = "菜单图标", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "status", value = "菜单状态（0正常 1停用）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "visible", value = "是否显示（0显示 1隐藏）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "isFrame", value = "是否为外链（0是 1否）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "isCache", value = "是否缓存（0缓存 1不缓存）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "body"),
    })
    @PostMapping(value = "/addMenu")
    public R<LifeSysMenu> addMenu(@RequestBody LifeSysMenu menu) {
        log.info("LifeSysMenuController.addMenu => menuName={}, parentId={}, menuType={}, menuSort={}, path={}, component={}, perms={}, icon={}, status={}, visible={}, isFrame={}, isCache={}, remark={}",
                menu.getMenuName(), menu.getParentId(), menu.getMenuType(), menu.getMenuSort(),
                menu.getPath(), menu.getComponent(), menu.getPerms(), menu.getIcon(),
                menu.getStatus(), menu.getVisible(), menu.getIsFrame(), menu.getIsCache(), menu.getRemark());
        return lifeSysMenuService.addMenu(menu);
    }

    @ApiOperation("编辑菜单")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "body", required = true),
            @ApiImplicitParam(name = "menuName", value = "菜单名称", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "menuType", value = "菜单类型（M目录 C菜单 F按钮）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "menuSort", value = "显示顺序", dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "path", value = "路由地址", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "component", value = "组件路径", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "perms", value = "权限标识（如：sys:user:list）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "icon", value = "菜单图标", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "status", value = "菜单状态（0正常 1停用）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "visible", value = "是否显示（0显示 1隐藏）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "isFrame", value = "是否为外链（0是 1否）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "isCache", value = "是否缓存（0缓存 1不缓存）", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "body"),
    })
    @PostMapping(value = "/updateMenu")
    public R<LifeSysMenu> updateMenu(@RequestBody LifeSysMenu menu) {
        log.info("LifeSysMenuController.updateMenu => menuId={}, menuName={}, menuType={}, menuSort={}, path={}, component={}, perms={}, icon={}, status={}, visible={}, isFrame={}, isCache={}, remark={}",
                menu.getMenuId(), menu.getMenuName(), menu.getMenuType(), menu.getMenuSort(),
                menu.getPath(), menu.getComponent(), menu.getPerms(), menu.getIcon(),
                menu.getStatus(), menu.getVisible(), menu.getIsFrame(), menu.getIsCache(), menu.getRemark());
        return lifeSysMenuService.updateMenu(menu);
    }

    @ApiOperation("删除菜单")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true),
    })
    @DeleteMapping(value = "/deleteMenu")
    public R<Boolean> deleteMenu(@RequestParam Long menuId) {
        log.info("LifeSysMenuController.deleteMenu => menuId={}", menuId);
        return lifeSysMenuService.deleteMenu(menuId);
    }

    @ApiOperation("根据ID查询菜单")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true),
    })
    @GetMapping(value = "/getMenuById")
    public R<LifeSysMenu> getMenuById(@RequestParam Long menuId) {
        log.info("LifeSysMenuController.getMenuById => menuId={}", menuId);
        return lifeSysMenuService.getMenuById(menuId);
    }

    @ApiOperation("查询菜单树形结构")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名称（模糊查询）", dataType = "String", paramType = "query", required = false),
    })
    @GetMapping(value = "/getMenuTree")
    public R<List<LifeSysMenu>> getMenuTree(@RequestParam(value = "menuName",required = false)  String menuName) {
        log.info("LifeSysMenuController.getMenuTree => menuName={}", menuName);
        return lifeSysMenuService.getMenuTree(menuName);
    }

    @ApiOperation("根据父菜单ID查询子菜单列表")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父菜单ID（0表示根菜单，为空时默认查询根菜单）", dataType = "Long", paramType = "query", required = false),
    })
    @GetMapping(value = "/getMenuListByParentId")
    public R<List<LifeSysMenu>> getMenuListByParentId(@RequestParam(required = false) Long parentId) {
        log.info("LifeSysMenuController.getMenuListByParentId => parentId={}", parentId);
        return lifeSysMenuService.getMenuListByParentId(parentId);
    }

    @ApiOperation("根据用户ID查询用户菜单树形结构")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "ifTree", value = "是否返回树形结构（0否 1是）", dataType = "Long", paramType = "query")
    })
    @GetMapping(value = "/getMenuByUserId")
    public R<List<LifeSysMenu>> getMenuByUserId(@RequestParam Long userId, @RequestParam(required = false, defaultValue = "0") Long ifTree) {
        log.info("LifeSysMenuController.getMenuByUserId => userId={}, ifTree={}", userId, ifTree);
        return lifeSysMenuService.getMenuByUserId(userId,ifTree);
    }
}

