-- ============================
-- 发送默认配置（模板）表 + 初始化数据
-- 说明：
-- 1) 该模板表用于前端加载默认 device_name / OPC address / 英文label / param_code
-- 2) 不污染 biz_send_job* 历史表
-- 3) 公共字段风格对齐 cgldb.sql：create_by/create_time/update_by/update_time/remark
-- ============================

-- ----------------------------
-- 1. 模板主表
-- ----------------------------
DROP TABLE IF EXISTS `biz_send_template`;
CREATE TABLE `biz_send_template`  (
  `template_id` int NOT NULL AUTO_INCREMENT COMMENT '模板ID',
  `template_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '模板编码(唯一)：FURNACE_DEFAULT、DRIVE_DEFAULT',
  `template_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '模板名称（英文界面标题用）',
  `device_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '默认目标设备/产线名称（写入 SendJobCreateDTO.deviceName）',
  `group_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '组类型：DRIVE / FURNACE',
  `enabled` tinyint NOT NULL DEFAULT 1 COMMENT '是否启用：1启用 0停用',

  `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',

  PRIMARY KEY (`template_id`) USING BTREE,
  UNIQUE INDEX `uk_send_template_code`(`template_code`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '发送默认模板主表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- 2. 模板明细表
-- ----------------------------
DROP TABLE IF EXISTS `biz_send_template_item`;
CREATE TABLE `biz_send_template_item`  (
  `template_item_id` int NOT NULL AUTO_INCREMENT COMMENT '模板明细ID',
  `template_id` int NOT NULL COMMENT '模板ID',
  `item_no` int NOT NULL DEFAULT 1 COMMENT '明细序号',

  `param_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '参数编码（英文，建议与前端字段一致）',
  `label_en` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '英文显示名',
  `group_name_en` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '英文分组名（可选，前端可按此分组渲染）',

  `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'OPC地址 ns=2;s=...',
  `default_value_raw` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '默认值（字符串）',

  `enabled` tinyint NOT NULL DEFAULT 1 COMMENT '是否启用：1启用 0停用',

  `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',

  PRIMARY KEY (`template_item_id`) USING BTREE,
  INDEX `idx_tpl_item_tplid`(`template_id`) USING BTREE,
  INDEX `idx_tpl_item_code`(`template_id`, `param_code`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '发送默认模板明细表' ROW_FORMAT = Dynamic;


-- ============================
-- 初始化数据：炉火默认模板（FURNACE_DEFAULT）
-- ============================
INSERT INTO `biz_send_template`
(`template_code`,`template_name`,`device_name`,`group_type`,`enabled`,`create_by`,`create_time`,`update_by`,`update_time`,`remark`)
VALUES
('FURNACE_DEFAULT','Furnace Parameter Sending','CGL_LINE_1','FURNACE',1,'system',NOW(),'system',NOW(),'Default furnace template');

SET @tpl_furnace_id := (SELECT template_id FROM biz_send_template WHERE template_code='FURNACE_DEFAULT' LIMIT 1);

-- 炉火点位（英文界面 + 中文注释：这里的 address 来自 OpcMessageIdsManager.furLineMeasureIds 中 SettingValue 相关节点）
INSERT INTO `biz_send_template_item`
(`template_id`,`item_no`,`param_code`,`label_en`,`group_name_en`,`address`,`default_value_raw`,`enabled`,`create_by`,`create_time`,`update_by`,`update_time`,`remark`)
VALUES
-- NOF1
(@tpl_furnace_id, 1, 'NOF1_TEMP_SET', 'NOF1 Furnace Temperature Setting Value', 'NOF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF1FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 2, 'NOF1_GAS_FLOW_SET', 'NOF1 Gas Flow Rate Setting Value', 'NOF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF1GasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 3, 'NOF1_AIR_FLOW_SET', 'NOF1 Air Flow Rate Setting Value', 'NOF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF1AirsFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 4, 'NOF1_RATIO_SET', 'NOF1 Air-Fuel Ratio Setting Value', 'NOF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF1UtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- NOF2
(@tpl_furnace_id, 5, 'NOF2_TEMP_SET', 'NOF2 Furnace Temperature Setting Value', 'NOF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF2FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 6, 'NOF2_GAS_FLOW_SET', 'NOF2 Gas Flow Rate Setting Value', 'NOF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF2GasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 7, 'NOF2_AIR_FLOW_SET', 'NOF2 Air Flow Rate Setting Value', 'NOF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF2AirsFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 8, 'NOF2_RATIO_SET', 'NOF2 Air-Fuel Ratio Setting Value', 'NOF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF2UtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- NOF3
(@tpl_furnace_id, 9, 'NOF3_TEMP_SET', 'NOF3 Furnace Temperature Setting Value', 'NOF3', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF3FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 10, 'NOF3_GAS_FLOW_SET', 'NOF3 Gas Flow Rate Setting Value', 'NOF3', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF3GasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 11, 'NOF3_AIR_FLOW_SET', 'NOF3 Air Flow Rate Setting Value', 'NOF3', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF3AirsFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 12, 'NOF3_RATIO_SET', 'NOF3 Air-Fuel Ratio Setting Value', 'NOF3', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF3UtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- NOF4
(@tpl_furnace_id, 13, 'NOF4_TEMP_SET', 'NOF4 Furnace Temperature Setting Value', 'NOF4', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF4FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 14, 'NOF4_GAS_FLOW_SET', 'NOF4 Gas Flow Rate Setting Value', 'NOF4', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF4GasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 15, 'NOF4_AIR_FLOW_SET', 'NOF4 Air Flow Rate Setting Value', 'NOF4', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF4AirsFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 16, 'NOF4_RATIO_SET', 'NOF4 Air-Fuel Ratio Setting Value', 'NOF4', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOF4UtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- RTF1
(@tpl_furnace_id, 17, 'RTF1_TEMP_SET', 'RTF1 Furnace Temperature Setting Value', 'RTF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF1FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 18, 'RTF1_GAS_FLOW_SET', 'RTF1 Gas Flow Rate Setting Value', 'RTF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF1GasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 19, 'RTF1_AIR_FLOW_SET', 'RTF1 Air Flow Rate Setting Value', 'RTF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF1AirFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 20, 'RTF1_RATIO_SET', 'RTF1 Air-Fuel Ratio Setting Value', 'RTF1', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF1UtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- RTF2
(@tpl_furnace_id, 21, 'RTF2_TEMP_SET', 'RTF2 Furnace Temperature Setting Value', 'RTF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF2FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 22, 'RTF2_GAS_FLOW_SET', 'RTF2 Gas Flow Rate Setting Value', 'RTF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF2GasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 23, 'RTF2_AIR_FLOW_SET', 'RTF2 Air Flow Rate Setting Value', 'RTF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF2AirFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 24, 'RTF2_RATIO_SET', 'RTF2 Air-Fuel Ratio Setting Value', 'RTF2', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTF2UtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- SF
(@tpl_furnace_id, 25, 'SF_TEMP_SET', 'SF Furnace Temperature Setting Value', 'SF', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.SFFurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 26, 'SF_GAS_FLOW_SET', 'SF Gas Flow Rate Setting Value', 'SF', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.SFGasFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 27, 'SF_AIR_FLOW_SET', 'SF Air Flow Rate Setting Value', 'SF', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.SFAirFlowRateSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 28, 'SF_RATIO_SET', 'SF Air-Fuel Ratio Setting Value', 'SF', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.SFUtheisaKongCombustionRatioSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- PH
(@tpl_furnace_id, 29, 'PH_PRESSURE_SET', 'PH Furnace Pressure Setting Value', 'PH', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.PHFurnacePressureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- JCF
(@tpl_furnace_id, 30, 'JCF_TEMP_SET', 'JCF Furnace Temperature Setting Value', 'JCF', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.JCF1FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- LTH
(@tpl_furnace_id, 31, 'LTH1_TEMP_SET', 'LTH1 Furnace Temperature Setting Value', 'LTH', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.LTH1FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 32, 'LTH2_TEMP_SET', 'LTH2 Furnace Temperature Setting Value', 'LTH', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.LTH2FurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- TDS
(@tpl_furnace_id, 33, 'TDS_TEMP_SET', 'TDS Furnace Temperature Setting Value', 'TDS', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.TDSFurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- LBZ
(@tpl_furnace_id, 34, 'LBZ_TEMP_SET', 'LBZ Furnace Temperature Setting Value', 'LBZ', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.LBZFurnaceTemperatureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- JCF Fans
(@tpl_furnace_id, 35, 'JCF_FAN1_SPEED_SET', 'JCF Fan 1 Setting Speed', 'JCF Fan', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.JCFFan1SettingSpeed', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 36, 'JCF_FAN2_SPEED_SET', 'JCF Fan 2 Setting Speed', 'JCF Fan', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.JCFFan2SettinglSpeed', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 37, 'JCF_FAN3_SPEED_SET', 'JCF Fan 3 Setting Speed', 'JCF Fan', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.JCFFan3SettingSpeed', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 38, 'JCF_FAN4_SPEED_SET', 'JCF Fan 4 Setting Speed', 'JCF Fan', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.JCFFan4SettingSpeed', '', 1, 'system', NOW(), 'system', NOW(), NULL),
-- Pressure set
(@tpl_furnace_id, 39, 'NOF_AIR_PRESSURE_SET', 'NOF Air Pressure Setting Value', 'Pressure', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOFAirPressureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 40, 'NOF_GAS_PRESSURE_SET', 'NOF Flue Gas Pressure Setting Value', 'Pressure', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.NOFGasPressureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 41, 'RTF_AIR_PRESSURE_SET', 'RTF Air Pressure Setting Value', 'Pressure', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTFAirPressureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL),
(@tpl_furnace_id, 42, 'RTF_GAS_PRESSURE_SET', 'RTF Flue Gas Pressure Setting Value', 'Pressure', 'ns=2;s=ProcessCGL.PLCFur.FurLineMeasure.RTFGasPressureSettingValue', '', 1, 'system', NOW(), 'system', NOW(), NULL);

-- ============================
-- 你如果需要 DRIVE_DEFAULT，也可以按同样结构插入。
-- DRIVE 默认地址建议来自 OpcMessageIdsManager.pdiSetupIds。
-- ============================

