%%%------------------------------------------------------------------- %%% @author aresei %%% @copyright (C) 2023, %%% @doc %%% %%% @end %%% Created : 04. 7月 2023 12:31 %%%------------------------------------------------------------------- -module(service_config_model). -author("aresei"). -include("iot_tables.hrl"). -include_lib("stdlib/include/qlc.hrl"). -define(TAB, service_config). %% API -export([create_table/0]). -export([insert/4, update/4, get_config/1, delete/1]). -export([as_map/1]). create_table() -> %% id生成器 mnesia:create_table(service_config, [ {attributes, record_info(fields, service_config)}, {record_name, service_config}, {disc_copies, [node()]}, {type, ordered_set} ]). -spec insert(ServiceId :: binary(), HostUUID :: binary(), ConfigJson :: binary(), LastEditUser :: integer()) -> ok | {error, Reason :: term()}. insert(ServiceId, HostUUID, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(HostUUID), is_binary(ConfigJson), is_integer(LastEditUser) -> ServiceConfig = #service_config{ service_id = ServiceId, host_uuid = HostUUID, config_json = ConfigJson, last_config_json = <<>>, last_edit_user = LastEditUser, create_ts = iot_util:current_time(), update_ts = iot_util:current_time() }, case mnesia:transaction(fun() -> mnesia:write(?TAB, ServiceConfig, write) end) of {'atomic', ok} -> ok; {'aborted', Reason} -> {error, Reason} end. -spec update(ServiceId :: binary(), HostUUID :: binary(), ConfigJson :: binary(), LastEditUser :: integer()) -> ok | {error, Reason :: term()}. update(ServiceId, HostUUID, ConfigJson, LastEditUser) when is_binary(ServiceId), is_binary(HostUUID), is_binary(ConfigJson), is_integer(LastEditUser) -> Fun = fun() -> case mnesia:read(?TAB, ServiceId, write) of [] -> ServiceConfig = #service_config{ service_id = ServiceId, host_uuid = HostUUID, config_json = ConfigJson, last_config_json = <<>>, last_edit_user = LastEditUser, create_ts = iot_util:current_time(), update_ts = iot_util:current_time() }, mnesia:write(?TAB, ServiceConfig, write); [ServiceConfig0 = #service_config{config_json = OldConfigJson}] -> NServiceConfig = ServiceConfig0#service_config{ config_json = ConfigJson, last_config_json = OldConfigJson, last_edit_user = LastEditUser, update_ts = iot_util:current_time() }, mnesia:write(?TAB, NServiceConfig, write) end end, case mnesia:transaction(Fun) of {'atomic', ok} -> ok; {'aborted', Reason} -> {error, Reason} end. -spec get_config(ServiceId :: any()) -> error | {ok, Config :: #service_config{}}. get_config(ServiceId) when is_binary(ServiceId) -> case mnesia:dirty_read(?TAB, ServiceId) of [] -> error; [Config] -> {ok, Config} end. -spec delete(ServiceId :: binary()) -> ok | {error, Reason :: any()}. delete(ServiceId) when is_binary(ServiceId) -> Fun = fun() -> case mnesia:read(?TAB, ServiceId, write) of [] -> ok; [ServiceConfig0 = #service_config{config_json = OldConfigJson}] -> NServiceConfig = ServiceConfig0#service_config{ config_json = <<"">>, last_config_json = OldConfigJson, update_ts = iot_util:current_time() }, mnesia:write(?TAB, NServiceConfig, write) end end, case mnesia:transaction(Fun) of {'atomic', ok} -> ok; {'aborted', Reason} -> {error, Reason} end. -spec as_map(ServiceConfig :: #service_config{}) -> map(). as_map(#service_config{service_id = ServiceId, config_json = ConfigJson, last_config_json = LastConfigJson, last_edit_user = LastEditUser, update_ts = UpdateTs, create_ts = CreateTs}) -> #{ <<"service_id">> => ServiceId, <<"config_json">> => ConfigJson, <<"last_config_json">> => LastConfigJson, <<"last_edit_user">> => LastEditUser, <<"update_ts">> => UpdateTs, <<"create_ts">> => CreateTs }.