package com.rj.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rj.entity.sys.Menu;
import com.rj.service.sys.IMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 系统生成
 * @since 2025-08-07
 */
@RestController
@RequestMapping("/api/sys/menu")
@Tag(name = "菜单管理", description = "菜单管理相关接口")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 新增菜单
     */
    @PostMapping("/add")
    @Operation(summary = "新增菜单", description = "添加新的菜单信息")
    public ResponseEntity<Map<String, Object>> addMenu(
            @Parameter(description = "菜单信息", required = true)
            @RequestBody Menu menu) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = menuService.save(menu);
            if (success) {
                result.put("success", true);
                result.put("message", "菜单添加成功");
                result.put("data", menu);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "菜单添加失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "菜单添加异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据ID查询菜单
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询菜单", description = "根据菜单ID获取菜单详细信息")
    public ResponseEntity<Map<String, Object>> getMenuById(
            @Parameter(description = "菜单ID", required = true)
            @PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Menu menu = menuService.getById(id);
            if (menu != null) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", menu);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "菜单不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据菜单名查询菜单
     */
    @GetMapping("/getByName")
    @Operation(summary = "根据菜单名查询菜单", description = "根据菜单名查询菜单信息")
    public ResponseEntity<Map<String, Object>> getMenuByName(
            @Parameter(description = "菜单名", required = true)
            @RequestParam String name) {
        Map<String, Object> result = new HashMap<>();
        try {
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Menu::getName, name);
            List<Menu> menuList = menuService.list(queryWrapper);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", menuList);
            result.put("count", menuList.size());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据父菜单ID查询子菜单
     */
    @GetMapping("/getByParentId/{parentId}")
    @Operation(summary = "根据父菜单ID查询子菜单", description = "根据父菜单ID查询子菜单信息")
    public ResponseEntity<Map<String, Object>> getMenuByParentId(
            @Parameter(description = "父菜单ID", required = true)
            @PathVariable Integer parentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Menu::getParentId, parentId);
            List<Menu> menuList = menuService.list(queryWrapper);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", menuList);
            result.put("count", menuList.size());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 分页查询菜单列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询菜单列表", description = "分页查询菜单信息列表")
    public ResponseEntity<Map<String, Object>> getMenuList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "菜单名（模糊查询）")
            @RequestParam(required = false) String name,
            @Parameter(description = "父菜单ID")
            @RequestParam(required = false) Integer parentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<Menu> page = new Page<>(current, size);
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            
            // 添加查询条件
            if (name != null && !name.trim().isEmpty()) {
                queryWrapper.like(Menu::getName, name);
            }
            if (parentId != null) {
                queryWrapper.eq(Menu::getParentId, parentId);
            }
            
            // 按菜单ID倒序排列
            queryWrapper.orderByDesc(Menu::getMenuId);
            
            Page<Menu> menuPage = menuService.page(page, queryWrapper);
            
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", menuPage.getRecords());
            result.put("total", menuPage.getTotal());
            result.put("current", menuPage.getCurrent());
            result.put("size", menuPage.getSize());
            result.put("pages", menuPage.getPages());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 更新菜单信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新菜单信息", description = "更新菜单详细信息")
    public ResponseEntity<Map<String, Object>> updateMenu(
            @Parameter(description = "菜单信息", required = true)
            @RequestBody Menu menu) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (menu.getMenuId() == null) {
                result.put("success", false);
                result.put("message", "菜单ID不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            boolean success = menuService.updateById(menu);
            
            if (success) {
                result.put("success", true);
                result.put("message", "菜单信息更新成功");
                result.put("data", menu);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "菜单信息更新失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据ID删除菜单
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除菜单", description = "根据菜单ID删除菜单信息")
    public ResponseEntity<Map<String, Object>> deleteMenu(
            @Parameter(description = "菜单ID", required = true)
            @PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = menuService.removeById(id);
            if (success) {
                result.put("success", true);
                result.put("message", "菜单删除成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "菜单删除失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 批量删除菜单
     */
    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除菜单", description = "根据菜单ID列表批量删除菜单信息")
    public ResponseEntity<Map<String, Object>> batchDeleteMenus(
            @Parameter(description = "菜单ID列表", required = true)
            @RequestBody List<Integer> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (ids == null || ids.isEmpty()) {
                result.put("success", false);
                result.put("message", "菜单ID列表不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            boolean success = menuService.removeByIds(ids);
            if (success) {
                result.put("success", true);
                result.put("message", "批量删除成功，共删除 " + ids.size() + " 条记录");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "批量删除失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 获取菜单统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取菜单统计信息", description = "获取菜单总数等统计信息")
    public ResponseEntity<Map<String, Object>> getMenuStatistics() {
        Map<String, Object> result = new HashMap<>();
        try {
            long totalCount = menuService.count();
            
            Map<String, Object> statistics = new HashMap<>();
            statistics.put("totalMenus", totalCount);
            
            result.put("success", true);
            result.put("message", "统计信息获取成功");
            result.put("data", statistics);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "统计信息获取异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
}























































































































