-- 销售订单主表增加优先级列
-- 优先级：1-4级，数字越小优先级越高
-- 默认值为4（最低优先级）

ALTER TABLE `sal_order` ADD COLUMN `priority` INT(11) DEFAULT 4 COMMENT '优先级(1-4级,越小优先级越高)' AFTER `status`;

-- 更新现有数据的优先级为默认值4
UPDATE `sal_order` SET `priority` = 4 WHERE `priority` IS NULL;

-- 在销售订单列表中添加优先级字段显示（在订单编号后面）
-- 修改为字典类型，以便在列表中显示标签
INSERT INTO `sys_field_extend` 
(`source_bill`, `sort`, `field`, `field_name`, `type`, `quote_data`, `quote_field`, `formula`, `is_required`, `is_must`, 
 `status`, `remark`, `create_by`, `create_time`, `update_by`, `update_time`, `width`, `default_value`, `is_system`)
VALUES ('saleOrderEntry', 3.5, 'salOrder.priority', '优先级', 'dict:salorder_priority', NULL, NULL, NULL, NULL, NULL, '0', '1-4级,越小优先级越高', 'admin',
        NOW(), NULL, NULL, 80, '4', 'Y')
ON DUPLICATE KEY UPDATE 
    `field_name` = '优先级',
    `sort` = 3.5,
    `type` = 'dict:salorder_priority',
    `width` = 80,
    `remark` = '1-4级,越小优先级越高',
    `default_value` = '4',
    `update_time` = NOW();

-- 添加优先级字典类型
INSERT INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, update_by, update_time, remark)
VALUES ('订单优先级', 'salorder_priority', '0', 'admin', NOW(), '', NULL, '订单优先级列表')
ON DUPLICATE KEY UPDATE 
    dict_name = '订单优先级',
    status = '0',
    update_time = NOW();

-- 添加优先级字典数据
INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, update_by, update_time, remark)
VALUES 
(1, '一级', '1', 'salorder_priority', '', 'danger', 'N', '0', 'admin', NOW(), '', NULL, '最高优先级'),
(2, '二级', '2', 'salorder_priority', '', 'warning', 'N', '0', 'admin', NOW(), '', NULL, '高优先级'),
(3, '三级', '3', 'salorder_priority', '', 'primary', 'N', '0', 'admin', NOW(), '', NULL, '普通优先级'),
(4, '四级', '4', 'salorder_priority', '', 'info', 'N', '0', 'admin', NOW(), '', NULL, '低优先级')
ON DUPLICATE KEY UPDATE 
    dict_label = VALUES(dict_label),
    css_class = VALUES(css_class),
    list_class = VALUES(list_class),
    remark = VALUES(remark),
    update_time = NOW();

-- =====================================================
-- 生产计划排产权限配置
-- 作者: 周启威
-- 日期: 2026-02-09
-- 说明: 为生产计划添加排产功能权限
-- =====================================================

-- 查询生产计划菜单ID
SET @plan_menu_id = (SELECT menu_id FROM sys_menu WHERE menu_name = '生产计划' AND perms = 'production:plan:list' LIMIT 1);

-- 如果找到了生产计划菜单，则添加排产权限按钮
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '生产计划排产', @plan_menu_id, 5, '#', '', 1, 0, 'F', '0', '0', 'production:plan:schedule', '#', 'admin', NOW(), '', NULL, '生产计划排产权限'
WHERE @plan_menu_id IS NOT NULL
AND NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE perms = 'production:plan:schedule'
);