USE cattletrade;

-- 最终修复中转仓管理菜单数据
-- 根据表结构：page_url VARCHAR(100), authority VARCHAR(50)
-- 需要存储的值：
--   warehouse/warehouseIn = 20 字符（在100以内，足够）
--   warehouse/warehouseOut = 21 字符（在100以内，足够）
--   warehouseout:list = 16 字符（在50以内，足够）
-- 
-- 字段长度足够，问题可能是插入时被截断，直接UPDATE修复即可

-- ============================================
-- 步骤1：删除重复的菜单数据（如果存在）
-- ============================================
-- 删除 id=98 的子菜单的按钮权限（如果存在）
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：修复被截断的字段值（直接通过ID修复，最可靠）
-- ============================================

-- 修复进仓管理菜单（id=88）的 page_url
-- 修复：warehouse/warehouselr -> warehouse/warehouseIn
UPDATE sys_menu 
SET 
    page_url = 'warehouse/warehouseIn',
    update_time = NOW()
WHERE id = 88
  AND is_delete = 0;

-- 修复出仓管理菜单（id=93）的 page_url 和 authority
-- 修复：warehouse/warehouseO -> warehouse/warehouseOut
-- 修复：warehouseout:li -> warehouseout:list
UPDATE sys_menu 
SET 
    page_url = 'warehouse/warehouseOut',
    authority = 'warehouseout:list',
    update_time = NOW()
WHERE id = 93
  AND is_delete = 0;

-- ============================================
-- 步骤3：通过名称匹配修复（作为后备方案，确保所有记录都被修复）
-- ============================================

-- 修复进仓管理菜单的 page_url（通过名称匹配）
UPDATE sys_menu 
SET 
    page_url = 'warehouse/warehouseIn',
    update_time = NOW()
WHERE name = '进仓管理' 
  AND type = 1
  AND parent_id = 82
  AND page_url != 'warehouse/warehouseIn'
  AND is_delete = 0;

-- 修复出仓管理菜单的 page_url 和 authority（通过名称匹配）
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 != 'warehouse/warehouseOut' OR authority != 'warehouseout:list')
  AND is_delete = 0;

-- ============================================
-- 步骤4：确保父菜单的 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;

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

-- 预期结果：
-- id=82: type=0, page_url=NULL, authority=NULL
-- id=83: route_url='list', page_url='warehouse/warehouse', authority='warehouse:list'
-- id=88: route_url='in', page_url='warehouse/warehouseIn' (长度=20), authority='warehousein:list'
-- id=93: route_url='out', page_url='warehouse/warehouseOut' (长度=21), authority='warehouseout:list' (长度=16)

