package com.pangu.system.controller;

import com.pangu.common.core.controller.BaseController;
import com.pangu.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 菜单管理Controller
 * @author 湖北新华业务中台研发团队
 */
@RestController
@RequestMapping("/api/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询菜单列表
     */
    @GetMapping("/list")
    public AjaxResult list(
            @RequestParam(required = false) String menuName,
            @RequestParam(required = false) String status) {
        
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT menu_id, menu_name, parent_id, order_num, path, component, ");
        sql.append("query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_time ");
        sql.append("FROM sys_menu WHERE 1=1 ");
        
        List<Object> params = new ArrayList<>();
        if (menuName != null && !menuName.isEmpty()) {
            sql.append("AND menu_name LIKE ? ");
            params.add("%" + menuName + "%");
        }
        if (status != null && !status.isEmpty()) {
            sql.append("AND status = ? ");
            params.add(status);
        }
        sql.append("ORDER BY parent_id, order_num");
        
        List<Map<String, Object>> menus = jdbcTemplate.queryForList(sql.toString(), params.toArray());
        return AjaxResult.success(menus);
    }

    /**
     * 获取菜单详情
     */
    @GetMapping("/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        String sql = "SELECT * FROM sys_menu WHERE menu_id = ?";
        List<Map<String, Object>> menus = jdbcTemplate.queryForList(sql, menuId);
        if (menus.isEmpty()) {
            return AjaxResult.error("菜单不存在");
        }
        return AjaxResult.success(menus.get(0));
    }

    /**
     * 获取菜单树（下拉选项）
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect() {
        String sql = "SELECT menu_id as id, parent_id as parentId, menu_name as label " +
                    "FROM sys_menu ORDER BY parent_id, order_num";
        List<Map<String, Object>> menus = jdbcTemplate.queryForList(sql);
        
        // 添加根节点
        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, Object> root = new HashMap<>();
        root.put("id", 0L);
        root.put("parentId", -1L);
        root.put("label", "主目录");
        root.put("children", buildTree(menus));
        result.add(root);
        
        return AjaxResult.success(result);
    }

    /**
     * 新增菜单
     */
    @PostMapping
    public AjaxResult add(@RequestBody Map<String, Object> menu) {
        String sql = "INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, " +
                    "query, is_frame, is_cache, menu_type, visible, status, perms, icon, " +
                    "create_by, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'admin', NOW())";
        
        jdbcTemplate.update(sql,
            menu.get("menuName"),
            menu.getOrDefault("parentId", 0),
            menu.getOrDefault("orderNum", 0),
            menu.get("path"),
            menu.get("component"),
            menu.get("query"),
            menu.getOrDefault("isFrame", 1),
            menu.getOrDefault("isCache", 0),
            menu.getOrDefault("menuType", "M"),
            menu.getOrDefault("visible", "0"),
            menu.getOrDefault("status", "0"),
            menu.get("perms"),
            menu.getOrDefault("icon", "#")
        );
        
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Map<String, Object> menu) {
        Long menuId = Long.valueOf(menu.get("menuId").toString());
        Long parentId = menu.get("parentId") != null ? Long.valueOf(menu.get("parentId").toString()) : 0L;
        
        // 不能将父菜单设为自己
        if (menuId.equals(parentId)) {
            return AjaxResult.error("修改菜单失败，上级菜单不能选择自己");
        }
        
        String sql = "UPDATE sys_menu SET menu_name = ?, parent_id = ?, order_num = ?, path = ?, " +
                    "component = ?, query = ?, is_frame = ?, is_cache = ?, menu_type = ?, " +
                    "visible = ?, status = ?, perms = ?, icon = ?, update_by = 'admin', update_time = NOW() " +
                    "WHERE menu_id = ?";
        
        jdbcTemplate.update(sql,
            menu.get("menuName"),
            parentId,
            menu.getOrDefault("orderNum", 0),
            menu.get("path"),
            menu.get("component"),
            menu.get("query"),
            menu.getOrDefault("isFrame", 1),
            menu.getOrDefault("isCache", 0),
            menu.getOrDefault("menuType", "M"),
            menu.getOrDefault("visible", "0"),
            menu.getOrDefault("status", "0"),
            menu.get("perms"),
            menu.getOrDefault("icon", "#"),
            menuId
        );
        
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable Long menuId) {
        // 检查是否有子菜单
        Integer count = jdbcTemplate.queryForObject(
            "SELECT COUNT(*) FROM sys_menu WHERE parent_id = ?",
            Integer.class, menuId);
        if (count != null && count > 0) {
            return AjaxResult.error("存在子菜单，不允许删除");
        }
        
        // 检查是否已分配给角色
        count = jdbcTemplate.queryForObject(
            "SELECT COUNT(*) FROM sys_role_menu WHERE menu_id = ?",
            Integer.class, menuId);
        if (count != null && count > 0) {
            return AjaxResult.error("菜单已分配给角色，不允许删除");
        }
        
        jdbcTemplate.update("DELETE FROM sys_menu WHERE menu_id = ?", menuId);
        return AjaxResult.success("删除成功");
    }

    private List<Map<String, Object>> buildTree(List<Map<String, Object>> list) {
        List<Map<String, Object>> result = new ArrayList<>();
        Map<Long, Map<String, Object>> nodeMap = new HashMap<>();
        
        for (Map<String, Object> node : list) {
            Long id = ((Number) node.get("id")).longValue();
            nodeMap.put(id, node);
            node.put("children", new ArrayList<Map<String, Object>>());
        }
        
        for (Map<String, Object> node : list) {
            Long parentId = ((Number) node.get("parentId")).longValue();
            if (parentId == 0) {
                result.add(node);
            } else {
                Map<String, Object> parent = nodeMap.get(parentId);
                if (parent != null) {
                    @SuppressWarnings("unchecked")
                    List<Map<String, Object>> children = (List<Map<String, Object>>) parent.get("children");
                    children.add(node);
                }
            }
        }
        return result;
    }
}
