-- 执行完成后 需要在字典配置项页面刷新缓存
#if($meta.enableOperatorLog)

-- 模块 key id
SELECT @MODULE_KEY_ID:= id FROM dict_key WHERE key_name = 'operatorLogModule' AND deleted = 0;
-- 模块 key 排序
SELECT @MODULE_KEY_MAX_SORT:= IFNULL(MAX(sort), 0) FROM dict_value where key_id = @MODULE_KEY_ID AND deleted = 0;
-- 类型 key id
SELECT @TYPE_KEY_ID:= id FROM dict_key WHERE key_name = 'operatorLogType' AND deleted = 0;
-- 插入类型
INSERT INTO dict_value
  (`key_id`, `key_name`, `value`, `label`, `extra`, `sort`, `create_time`, `update_time`, `creator`, `updater`, `deleted`)
VALUES
  (@MODULE_KEY_ID, 'operatorLogModule', '${package.ModuleName}:${typeHyphen}', '$!{table.comment}', '{}', @MODULE_KEY_MAX_SORT + 10, now(), now(), '1', '1', 0),
  (@TYPE_KEY_ID, 'operatorLogType', '${typeHyphen}:create', '创建$!{table.comment}', '{}', 10, now(), now(), '1', '1', 0),
  (@TYPE_KEY_ID, 'operatorLogType', '${typeHyphen}:update', '更新$!{table.comment}', '{}', 20, now(), now(), '1', '1', 0),
  #if($meta.enableExport)
  (@TYPE_KEY_ID, 'operatorLogType', '${typeHyphen}:delete', '删除$!{table.comment}', '{}', 30, now(), now(), '1', '1', 0),
  (@TYPE_KEY_ID, 'operatorLogType', '${typeHyphen}:export', '导出$!{table.comment}', '{}', 40, now(), now(), '1', '1', 0);
  #else
  (@TYPE_KEY_ID, 'operatorLogType', '${typeHyphen}:delete', '删除$!{table.comment}', '{}', 30, now(), now(), '1', '1', 0);
  #end
#end

#if($dictMap.entrySet().size() > 0)
#foreach($enumEntity in $dictMap.entrySet())
-- 插入字典配置项 FIXME 检查这里的类型
INSERT INTO dict_key
  (`key_name`, `value_type`, `extra_schema`, `description`, `create_time`, `update_time`, `creator`, `updater`, `deleted`)
VALUES
  ('$enumEntity.value.keyName', 'STRING', '$enumEntity.value.extraSchema', '$enumEntity.value.comment', now(), now(), '1', '1', 0);

-- 设置临时配置项id
SELECT @TMP_KEY_ID:=LAST_INSERT_ID();

-- 插入字典配置值 FIXME 检查这里的类型
INSERT INTO dict_value
  (`key_id`, `key_name`, `value`, `label`, `extra`, `sort`, `create_time`, `update_time`, `creator`, `updater`, `deleted`)
VALUES
#set($count = $enumEntity.value.fields.size() - 1)
#foreach($index in [0..$count])
#set($sort = $index * 10 + 10)
  (@TMP_KEY_ID, '$enumEntity.value.keyName', '$enumEntity.value.values.get($index)', '$enumEntity.value.labels.get($index)', #if($enumEntity.value.extraJson.size() > $index)'$enumEntity.value.extraJson.get($index)'#else'{}'#end, $sort, now(), now(), '1', '1', 0)#if($foreach.hasNext),#else;#end
#end

#end
#end