%%%-------------------------------------------------------------------
%%% @author  tyl
%%% @doc
%%%      全民动员数据管理
%%% @end
%%% Created : 2021-04-28
%%%-------------------------------------------------------------------
-module(mobilize_data).

-include("custom_act.hrl").
-include("custom_act_mobilize.hrl").

%% API
-export([
	db_save_mobilize_task/3
	,db_save_mobilize_days_reward/3

	,db_save_mobilize_task/7
	,db_select_mobilize_days_reward/2
	,db_save_mobilize_days_reward/6
	,db_select_mobilize_task/2

	,save_custom_data/2
	,custom_act_data/3

]).


save_custom_data(_RoleId, CustomData) ->
	#mobilize_act_data{
		type = Type, subtype = SubType
	} = CustomData,
	ActDataKey = ?MOBILIZE_ACT_DATA(Type, SubType),
	erlang:put(ActDataKey, CustomData),
	ok.

custom_act_data(RoleId, Type, SubType) ->
	ActDataKey = ?MOBILIZE_ACT_DATA(Type, SubType),
	case erlang:get(ActDataKey) of
		#mobilize_act_data{} = ActData -> ActData;
		_ ->
			CustomData = load_act_data(RoleId, SubType),
			erlang:put(ActDataKey, CustomData),
			CustomData
	end.

load_act_data(RoleId, SubType) ->
	case db_select_mobilize_task(RoleId, SubType) of
		[_|_] = DbInfos ->
			TaskStatMap = make_task_stat(DbInfos, #{});
		_ ->
			TaskStatMap = #{}
	end,
	case db_select_mobilize_days_reward(RoleId, SubType) of
		[_|_] = DbInfos1 ->
			DayList = make_day_list(DbInfos1, []);
		_ ->
			DayList = []
	end,
	ActData = #mobilize_role{
		task_stat = TaskStatMap
		,day_list = DayList
	},
	MobilizeActData = #mobilize_act_data{
		type = ?CUSTOM_ACT_TYPE_MOBILIZE
		,subtype  = SubType
		,act_data = ActData
	},
	MobilizeActData.

make_task_stat([], TaskMap) -> TaskMap;
make_task_stat([DbInfo|T], TaskMap) ->
	case DbInfo of
		[StartDay, EndDay, TaskType, Progress, AwardLogStr, Time] ->
			AwardLog = util:bitstring_to_term(AwardLogStr),
			Task = #mobilize_task{
				day_interval = {StartDay, EndDay}
				,task_type   = TaskType
				,task_pro    = Progress
				,task_award_log = AwardLog
				,time = Time
			},
			TaskList = maps:get({StartDay, EndDay}, TaskMap, []),
			NewTaskList = [Task|TaskList],
			NewTaskMap  = maps:put({StartDay, EndDay}, NewTaskList, TaskMap),
			make_task_stat(T, NewTaskMap);
		_ ->
			make_task_stat(T, TaskMap)
	end.

make_day_list([], DayList) -> DayList;
make_day_list([DbInfo|T], DayList) ->
	case DbInfo of
		[StartDay, EndDay, Total, State] ->
			DayInfo = #mobilize_day{
				day_interval = {StartDay, EndDay},
				total = Total,
				state = State
			},
			NewDayList = [DayInfo|DayList],
			make_day_list(T, NewDayList);
		_ ->
			make_day_list(T, DayList)
	end.

db_save_mobilize_task(RoleId, SubType, TaskInfo) ->
	{StartDay, EndDay, TaskType, Pro, AwardLog} = TaskInfo,
	db_save_mobilize_task(RoleId, SubType, StartDay, EndDay, TaskType, Pro, AwardLog),
	ok.

db_save_mobilize_task(RoleId, SubType, StartDay, EndDay, TaskType, Pro, AwardLog) ->
	AwardLogStr = util:term_to_string(AwardLog),
	Time = utime:unixtime(),
	Sql = io_lib:format(?SAVE_MOBILIZE_TASK, [RoleId, SubType, StartDay, EndDay, TaskType, Pro, AwardLogStr, Time]),
	db:execute(Sql),
	ok.

db_select_mobilize_task(RoleId, SubType) ->
	Sql = io_lib:format(?SELECT_MOBILIZE_TASK, [RoleId, SubType]),
	db:get_all(Sql).


db_save_mobilize_days_reward(RoleId, SubType, DaysInfo) ->
	{StartDay, EndDay, Total, State} = DaysInfo,
	db_save_mobilize_days_reward(RoleId, SubType, StartDay, EndDay, Total, State),
	ok.

db_save_mobilize_days_reward(RoleId, SubType, StartDay, EndDay, Total, State) ->
	Sql = io_lib:format(?SAVE_MOBILIZE_DAYS_REWARD, [RoleId, SubType, StartDay, EndDay,Total, State]),
	db:execute(Sql),
	ok.

db_select_mobilize_days_reward(RoleId, SubType) ->
	Sql = io_lib:format(?SELECT_MOBILIZE_DAYS_REWARD, [RoleId, SubType]),
	db:get_all(Sql).

