USE cattletrade;

-- 修复中转仓管理菜单数据
-- 1. 删除重复的菜单数据（保留较早创建的，删除较晚创建的）
-- 2. 修复被截断的字段值

-- ============================================
-- 步骤1：删除重复的菜单数据
-- 保留 id=82 的父菜单及其子菜单，删除 id=98 的父菜单及其子菜单
-- ============================================

-- 删除 id=98 的子菜单的按钮权限（如果存在）
-- 先删除所有以 id=99, 104, 109 为父菜单的按钮权限
DELETE FROM sys_menu 
WHERE parent_id IN (99, 104, 109) 
  AND type = 2
  AND is_delete = 0;

-- 删除 id=98 的子菜单（二级菜单）
DELETE FROM sys_menu 
WHERE id IN (99, 104, 109) 
  AND parent_id = 98
  AND type = 1
  AND is_delete = 0;

-- 删除 id=98 的父菜单（如果存在）
DELETE FROM sys_menu 
WHERE id = 98 
  AND name = '中转仓管理'
  AND parent_id = 0
  AND (type = 0 OR type IS NULL)
  AND is_delete = 0;

-- ============================================
-- 步骤2：修复被截断的字段值
-- ============================================

-- 修复进仓管理菜单的 page_url（修复所有被截断的情况）
-- 包括：warehouse/warehouselr, warehouse/warehouselr* 等
UPDATE sys_menu 
SET 
    page_url = 'warehouse/warehouseIn',
    update_time = NOW()
WHERE name = '进仓管理' 
  AND type = 1
  AND parent_id = 82
  AND (page_url LIKE 'warehouse/warehouselr%' 
       OR page_url = 'warehouse/warehouselr'
       OR page_url LIKE 'warehouse/warehouseIn%' AND page_url != 'warehouse/warehouseIn'
       OR page_url IS NULL
       OR LENGTH(page_url) < 20)  -- 如果长度小于20，可能是被截断了
  AND is_delete = 0;

-- 修复出仓管理菜单的 page_url 和 authority（修复所有被截断的情况）
-- 包括：warehouse/warehouseO, warehouse/warehouseO* 等
-- 以及：warehouseout:li, warehouseout:li* 等
UPDATE sys_menu 
SET 
    page_url = 'warehouse/warehouseOut',
    authority = 'warehouseout:list',
    update_time = NOW()
WHERE name = '出仓管理' 
  AND type = 1
  AND parent_id = 82
  AND (page_url LIKE 'warehouse/warehouseO%' AND page_url != 'warehouse/warehouseOut'
       OR page_url = 'warehouse/warehouseO'
       OR page_url LIKE 'warehouse/warehouseOut%' AND page_url != 'warehouse/warehouseOut'
       OR page_url IS NULL
       OR LENGTH(page_url) < 20  -- 如果长度小于20，可能是被截断了
       OR authority LIKE 'warehouseout:li%' AND authority != 'warehouseout:list'
       OR authority = 'warehouseout:li'
       OR authority IS NULL
       OR LENGTH(authority) < 15)  -- 如果长度小于15，可能是被截断了
  AND is_delete = 0;

-- ============================================
-- 步骤3：确保父菜单的 type 字段正确（应该是 0）
-- ============================================
UPDATE sys_menu 
SET 
    type = 0,
    update_time = NOW()
WHERE id = 82 
  AND (type IS NULL OR type != 0)
  AND is_delete = 0;

-- ============================================
-- 步骤4：验证修复结果
-- ============================================
-- 执行以下查询验证数据是否正确：
-- SELECT id, parent_id, type, name, route_url, page_url, authority 
-- FROM sys_menu 
-- WHERE (id = 82 OR parent_id = 82) 
--   AND is_delete = 0 
-- ORDER BY id, sort;

