-- ----------------------------
-- 审批负责人配置（或签）
-- ----------------------------
DROP TABLE IF EXISTS biz_approval_config_user;
DROP TABLE IF EXISTS biz_approval_config;

CREATE TABLE biz_approval_config (
  id            BIGINT       NOT NULL AUTO_INCREMENT,
  biz_type      VARCHAR(64)  NOT NULL COMMENT '业务类型: PURCHASE_ORDER/CLIENT_QUOTE/QUOTATION/DELIVERY_ORDER/ORDER_OBJECTION',
  biz_name      VARCHAR(64)  NOT NULL COMMENT '业务名称',
  sign_type     CHAR(1)      DEFAULT '1' COMMENT '审批方式: 1或签 2会签(预留)',
  enabled       CHAR(1)      DEFAULT '1' COMMENT '是否启用 0停用 1启用',
  remark        VARCHAR(255) DEFAULT NULL,
  create_by     VARCHAR(64)  DEFAULT '',
  create_time   DATETIME     DEFAULT NULL,
  update_by     VARCHAR(64)  DEFAULT '',
  update_time   DATETIME     DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_biz_type (biz_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审批负责人配置';

CREATE TABLE biz_approval_config_user (
  id            BIGINT       NOT NULL AUTO_INCREMENT,
  config_id     BIGINT       NOT NULL COMMENT 'biz_approval_config.id',
  user_id       BIGINT       NOT NULL COMMENT 'sys_user.user_id',
  sort_no       INT          DEFAULT 0,
  PRIMARY KEY (id),
  KEY idx_config (config_id),
  UNIQUE KEY uk_config_user (config_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审批人(或签)';

-- 种子数据
INSERT INTO biz_approval_config(biz_type,biz_name,sign_type,enabled,create_by,create_time) VALUES
 ('PURCHASE_ORDER',  '采购订单',   '1','1','admin',NOW()),
 ('CLIENT_QUOTE',    '客户报价',   '1','1','admin',NOW()),
 ('QUOTATION',       '供应商报价', '1','1','admin',NOW()),
 ('DELIVERY_ORDER',  '发货单',     '1','1','admin',NOW()),
 ('ORDER_OBJECTION', '订单异议',   '1','1','admin',NOW());

-- 字典：审批状态（业务表 status 复用，10=审批中）
DELETE FROM sys_dict_type WHERE dict_type='biz_approval_status';
INSERT INTO sys_dict_type(dict_name,dict_type,status,create_by,create_time,remark)
 VALUES('审批状态','biz_approval_status','0','admin',NOW(),'业务单据审批中状态');

DELETE FROM sys_dict_data WHERE dict_type='biz_approval_status';
INSERT INTO sys_dict_data(dict_sort,dict_label,dict_value,dict_type,css_class,list_class,is_default,status,create_by,create_time) VALUES
 (1,'审批中','10','biz_approval_status','','warning','N','0','admin',NOW()),
 (2,'已通过','confirmed','biz_approval_status','','success','N','0','admin',NOW()),
 (3,'已驳回','rejected','biz_approval_status','','danger','N','0','admin',NOW());

-- 菜单：审批配置
DELETE FROM sys_menu WHERE menu_name='审批配置' AND parent_id=2000;
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)
 VALUES('审批配置',(SELECT menu_id FROM (SELECT menu_id FROM sys_menu WHERE menu_name='系统管理' AND parent_id=0 LIMIT 1) t),100,
        'approval','bid/approval/index',1,0,'C','0','0','bid:approval:list','tree','admin',NOW());
