%%-----------------------------------------------------------------------------
%% @Module  :       lib_limit_goal_api.erl
%% @Author  :       WJQ
%% @Created :       2019-03-29
%% @Description:    限时目标
%%-----------------------------------------------------------------------------
-module (lib_limit_goal_api).

-include("server.hrl").
-include("common.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("scene.hrl").
-include("limit_goal.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("vip.hrl").
-include("def_fun.hrl").

-export ([
	handle_event/2					%% 事件监听
	,sync_task_progress/1			%% 同步所有未完成任务进度
	%,kill_mon/3						%% 击杀怪物处理
	,trigger/2						%% 触发器
	,active_limit_goal/2			%% 完成激活目标
	,gm_active_limit_goal/2			%% 秘籍激活目标
	,gm_reset_limit_goal/1			%% 秘籍重置限时目标
	,gm_finish_limit_goal_task/3	%% 秘籍完成目标，只能完成已开放的目标
	,gm_finish_limit_goal_task/2	%% 秘籍完成目标所有任务
	,gm_sync_limit_goal/0			%% 修复限时目标目标为1任务为4的数据进度
	,do_gm_sync_limit_goal_online_helper/2
	,gm_repair_limit_goal_status/0	%% 修复已达到完成限时目标1任务4进度但没完成的玩家数据
	,gm_repair_limit_goal_status_online/2
]).

%% 达到等级检测是否开放目标
handle_event(PS, #event_callback{type_id = ?EVENT_LV_UP}) when is_record(PS, player_status) ->
	#player_status{id = RoleId, figure = Figure, limit_goal = LimitGoal} = PS,
	#figure{lv = Lv} = Figure,
	NotOpen = case data_limit_goal:get_all_goal_ids() of
		[FirstGoalId|_] ->
			case maps:get(FirstGoalId, LimitGoal, false) of
				false -> %% 不存在记录
					true;
				#main_status{open_time = 0} -> %% 存在第一章任务数据，但是没写入数据库
					true;
				_ -> %% 已存在记录且已写入数据库
					false
			end;
		_ ->
			false
	end,
	case Lv >= ?LIMIT_GOAL_LV andalso NotOpen of
		true -> %% 检测是否开放目标
			NewLimitGoal = lib_limit_goal:check_open_goal(RoleId, Lv, LimitGoal),
			NewPS = PS#player_status{limit_goal = NewLimitGoal};
		false -> %% 已开放目标
			NewPS = PS
	end,
	{ok, NewPS};
%% 通关副本
handle_event(PS, #event_callback{type_id = ?EVENT_DUNGEON_SUCCESS, data = Data}) when is_record(PS, player_status) ->
	#callback_dungeon_succ{dun_type = DungeonType} = Data,
	NewLevel = lib_dungeon:get_dungeon_level(PS, DungeonType),
	{ok, NewPS} = trigger(PS, {dungeon_level, DungeonType, NewLevel}),
	{ok, NewPS};
%% 转职成功
handle_event(PS, #event_callback{type_id = ?EVENT_TURN_UP}) when is_record(PS, player_status) ->
	#player_status{figure = Figure} = PS,
	#figure{turn = Turn} = Figure,
	{ok, NewPS} = trigger(PS, {turn, Turn}),
	{ok, NewPS};
%% 成功登录
handle_event(PS, #event_callback{type_id = ?EVENT_LOGIN_CAST}) when is_record(PS, player_status) ->
	{ok, NewPS} = trigger(PS, {login_days}),
	{ok, NewPS2} = sync_task_progress(NewPS),
	{ok, NewPS2};
handle_event(PS, _Data) ->
	{ok, PS}.

%%kill_mon(Minfo, IsClsType, BLWhos) ->
%%	#scene_object{mon=Mon} = Minfo,
%%	#scene_mon{boss = Boss, mid = Mid} = Mon,
%%	case Boss of
%%		?MON_REWARD_BOSS -> %% 本服boss
%%			case data_mon:get(Mid) of
%%				#mon{lv = Lv} ->
%%					ok;
%%				_ ->
%%					Lv = 0
%%			end,
%%			case data_boss:get_boss_cfg(Mid) of
%%				#boss_cfg{type = MType} ->
%%					ok;
%%				_ ->
%%					MType = 0
%%			end,
%%			F = fun(T) ->
%%		        case T of
%%		            #mon_atter{id = RoleId} when IsClsType == 0 ->
%%		                trigger(RoleId, {local_boss, MType, Lv});
%%		            _ -> skip
%%		        end
%%		    end,
%%		    lists:foreach(F, BLWhos);
%%		_ ->
%%			skip
%%	end.

%% 同步所有未完成任务进度
sync_task_progress(Player) ->
    #player_status{limit_goal = LimitGoal} = Player,
    FinishList = get_finish_list(LimitGoal),
    Conditions = data_limit_goal:get_all_condition(),
    LessConditions = filter_finish_condition(FinishList, Conditions, []),
    NewPlayer = sync_task_progress_helper(LessConditions, Player),
    {ok, NewPlayer}.

sync_task_progress_helper([], Player) -> Player;
sync_task_progress_helper([[{dungeon_times, _NeedDungeonType, _NeedTimes}]|T], Player) ->
	sync_task_progress_helper(T, Player);
sync_task_progress_helper([[{dungeon_level, NeedDungeonType, _NeedLevel}]|T], Player) ->
	NewLevel = lib_dungeon:get_dungeon_level(Player, NeedDungeonType),
	{ok, NewPlayer} = trigger(Player, {dungeon_level, NeedDungeonType, NewLevel}),
	sync_task_progress_helper(T, NewPlayer);
sync_task_progress_helper([[{turn, _NeedLevel}]|T], Player) ->
	#player_status{figure = Figure} = Player,
	#figure{turn = Turn} = Figure,
	{ok, NewPlayer} = trigger(Player, {turn, Turn}),
	sync_task_progress_helper(T, NewPlayer);
sync_task_progress_helper([[{login_days, _NeedDays}]|T], Player) ->
	sync_task_progress_helper(T, Player);
sync_task_progress_helper([[{local_boss, _NeedBossType, _NeedBossLv, _NeedKillTimes}]|T], Player) ->
	sync_task_progress_helper(T, Player);
sync_task_progress_helper([_H|T], Player) ->
	%?ERR("unknow condition _H:~p~n", [_H]),
    sync_task_progress_helper(T, Player).

%% 过滤已完成或已领取目标列表
get_finish_list(LimitGoal) ->
    F = fun(MainStatus, TempList) ->
        case MainStatus of
            #main_status{task_list = TaskList} ->
                get_finish_list_helper(TaskList, TempList);
            _ ->
                TempList
        end
    end,
    lists:foldl(F, [], maps:values(LimitGoal)).

get_finish_list_helper([], AccList) -> AccList;
get_finish_list_helper([#task_status{goal_id = GoalId, task_id = TaskId, status = Status}|T], AccList) when Status =/= ?UNFINISH ->
	NewAccList = [{GoalId, TaskId}|AccList],
	get_finish_list_helper(T, NewAccList);
get_finish_list_helper([_H|T], AccList) ->
	get_finish_list_helper(T, AccList);
get_finish_list_helper(_TaskStatus, AccList) ->
	?ERR("bad args _TaskStatus:~p~n", [_TaskStatus]),
	AccList.

%% 过滤已完成或已领取章节任务条件
filter_finish_condition(_FinishList, [], FilterConditions) -> FilterConditions;
filter_finish_condition(FinishList, [{GoalId, TaskId, Condition}|T], FilterConditions) ->
    case lists:member({GoalId, TaskId}, FinishList) of
        false ->
            NewFilterConditions = [Condition|FilterConditions];
        true ->
            NewFilterConditions = FilterConditions
    end,
    filter_finish_condition(FinishList, T, NewFilterConditions);
filter_finish_condition(FinishList, [_H|T], FilterConditions) ->
    ?ERR("bad args _H:~p~n", [_H]),
    filter_finish_condition(FinishList, T, FilterConditions);
filter_finish_condition(_FinishList, _Conditions, FilterConditions) ->
    ?ERR("bad args _Conditions:~p~n", [_Conditions]),
    FilterConditions.

%% ------------------------------------------------------------------------
%% @describe 	触发器
%% @param 		Player 	:: integer() | #player_status{}
%% @param 		Args 	:: tuple()
%% @return 		ok | error | {ok, #player_status}
%% ------------------------------------------------------------------------
trigger(Player, Args) when is_integer(Player) ->
	lib_player:apply_cast(Player, ?APPLY_CAST_SAVE, lib_limit_goal_api, trigger, [Args]),
	ok;
trigger(Player, Args) when is_record(Player, player_status) ->
	case catch do_trigger(Player, Args) of
		{ok, NewPlayer} when is_record(NewPlayer, player_status) ->
			{ok, NewPlayer};
		Err ->
			?ERR("limit goal trigger err:~p~n", [Err]),
			{ok, Player}
	end;
trigger(_Player, _Args) ->
	?ERR("bad args Player:~p~n", [_Player]),
	error.

do_trigger(PS, {dungeon_times, _DungeonType} = Args) ->
	#player_status{id = RoleId, figure = Figure, vip_status = Vip, limit_goal = LimitGoal} = PS,
    #figure{lv = RoleLv} = Figure,
    NewLimitGoal = lib_limit_goal:trigger(RoleId, RoleLv, Vip#vip_status.lv, LimitGoal, [Args]),
    NewPS = PS#player_status{limit_goal = NewLimitGoal},
    {ok, NewPS};
do_trigger(PS, {dungeon_level, _DungeonType, _NewLevel} = Args) ->
    #player_status{id = RoleId, figure = Figure, vip_status = Vip, limit_goal = LimitGoal} = PS,
    #figure{lv = RoleLv} = Figure,
    NewLimitGoal = lib_limit_goal:trigger(RoleId, RoleLv, Vip#vip_status.lv, LimitGoal, [Args]),
    NewPS = PS#player_status{limit_goal = NewLimitGoal},
    {ok, NewPS};
do_trigger(PS, {turn, _Level} = Args) ->
    #player_status{id = RoleId, figure = Figure, vip_status = Vip, limit_goal = LimitGoal} = PS,
    #figure{lv = RoleLv} = Figure,
    NewLimitGoal = lib_limit_goal:trigger(RoleId, RoleLv, Vip#vip_status.lv, LimitGoal, [Args]),
    NewPS = PS#player_status{limit_goal = NewLimitGoal},
    {ok, NewPS};
do_trigger(PS, {login_days} = Args) ->
    #player_status{id = RoleId, figure = Figure, vip_status = Vip, limit_goal = LimitGoal} = PS,
    #figure{lv = RoleLv} = Figure,
    NewLimitGoal = lib_limit_goal:trigger(RoleId, RoleLv, Vip#vip_status.lv, LimitGoal, [Args]),
    NewPS = PS#player_status{limit_goal = NewLimitGoal},
    {ok, NewPS};
do_trigger(PS, {god_upgrade, _GodId, _NewGrade} = Args) ->
    #player_status{id = RoleId, figure = Figure, vip_status = Vip, limit_goal = LimitGoal} = PS,
    #figure{lv = RoleLv} = Figure,
    NewLimitGoal = lib_limit_goal:trigger(RoleId, RoleLv, Vip#vip_status.lv, LimitGoal, [Args]),
    NewPS = PS#player_status{limit_goal = NewLimitGoal},
    {ok, NewPS};
%% 只处理本服boss
do_trigger(PS, {local_boss, _BossType, _BossLv} = Args) ->
	#player_status{id = RoleId, figure = Figure, vip_status = Vip, limit_goal = LimitGoal} = PS,
    #figure{lv = RoleLv} = Figure,
    NewLimitGoal = lib_limit_goal:trigger(RoleId, RoleLv, Vip#vip_status.lv, LimitGoal, [Args]),
    NewPS = PS#player_status{limit_goal = NewLimitGoal},
    {ok, NewPS};
do_trigger(PS, _Args) ->
	{ok, PS}.

%% ------------------------------------------------------------------------
%% @describe 	完成激活目标
%% @param 		Player 	:: integer() | #player_status{}
%% @param 		GoalId	:: integer()
%% @return 		ok | error | {ok, #player_status}
%% ------------------------------------------------------------------------
active_limit_goal(Player, GoalId) when is_integer(Player) ->
	lib_player:apply_cast(Player, ?APPLY_CAST_SAVE, lib_limit_goal_api, active_limit_goal, [GoalId]),
	ok;
active_limit_goal(Player, GoalId) when is_record(Player, player_status), is_integer(GoalId) ->
	case catch lib_limit_goal:active_limit_goal(Player, GoalId) of
		{ok, NewPlayer} when is_record(NewPlayer, player_status) ->
			{ok, NewPlayer};
		_ ->
			{ok, Player}
	end;
active_limit_goal(Player, {god, GodId}) when is_record(Player, player_status) ->
	#player_status{limit_goal = LimitGoal} = Player,
	F = fun(TGoalId, MainStatus, TempGoalId) ->
		case MainStatus#main_status.status == ?HAS_RECEIVE of
			true -> %% 已激活
				TempGoalId;
			false ->
				case data_limit_goal:get_limit_goal_main(TGoalId) of
					#limit_goal_main{activate = [{god, GodId}]} ->
						TGoalId;
					_ ->
						TempGoalId
				end
		end
	end,
	case maps:fold(F, 0, LimitGoal) of
		0 ->
			{ok, Player};
		GoalId ->
			case catch lib_limit_goal:active_limit_goal(Player, GoalId) of
				{ok, NewPlayer} when is_record(NewPlayer, player_status) ->
					{ok, NewPlayer};
				_ ->
					{ok, Player}
			end
	end;
active_limit_goal(_Player, _Args) ->
	?ERR("active limit goal error Player:~p Args:~p~n", [_Player, _Args]),
	error.

%% 秘籍激活目标，不检测目标任务完成状态
gm_active_limit_goal(PS, GoalId) ->
	#player_status{limit_goal = LimitGoal} = PS,
	MainStatus = maps:get(GoalId, LimitGoal, false),
	case is_record(MainStatus, main_status) of
		true -> %% 已开放
			{ok, NewPS} = active_limit_goal(PS, GoalId);
		false -> %% 未开放
			NewPS = PS
	end,
	{ok, NewPS}.

%% 秘籍完成目标，只能完成已开放的目标
gm_finish_limit_goal_task(PS, GoalId, TaskId) ->
	#player_status{id = RoleId, limit_goal = LimitGoal} = PS,
	case maps:get(GoalId, LimitGoal, false) of
		#main_status{status = ?UNFINISH, task_list = TaskList, open_time = OpenTime} = MainStatus when OpenTime > 0 ->
			case lists:keyfind(TaskId, #task_status.task_id, TaskList) of
				#task_status{status = ?UNFINISH} = TaskStatus ->
					ReSql = io_lib:format(?UPDATE_LIMIT_GOAL_TASK_STATUS, [?FINISH, RoleId, GoalId, TaskId]),
					db:execute(ReSql),
					NewTaskStatus = TaskStatus#task_status{status = ?FINISH};
				false ->
					ReSql = io_lib:format(?INSERT_LIMIT_GOAL_TASK, [RoleId, GoalId, TaskId, 0, ?FINISH, util:term_to_bitstring([])]),
					db:execute(ReSql),
					NewTaskStatus = #task_status{goal_id = GoalId, task_id = TaskId, status = ?FINISH};
				NewTaskStatus ->
					skip
			end,
			NewTaskList = lists:keystore(TaskId, #task_status.task_id, TaskList, NewTaskStatus),
			NewMainStatus = MainStatus#main_status{task_list = NewTaskList},
			NewLimitGoal = maps:put(GoalId, NewMainStatus, LimitGoal),
			lib_limit_goal:send_info(RoleId, NewLimitGoal),
			NewPS = PS#player_status{limit_goal = NewLimitGoal};
		_ ->
			NewPS = PS
	end,
	NewPS.

%% 秘籍完成目标所有任务
gm_finish_limit_goal_task(PS, GoalId) ->
	TaskIds = data_limit_goal:get_all_task_ids(GoalId),
	F = fun(TaskId, TempPS) ->
		gm_finish_limit_goal_task(TempPS, GoalId, TaskId)
	end,
	lists:foldl(F, PS, TaskIds).

%% 秘籍重置限时目标
gm_reset_limit_goal(PS) ->
	#player_status{id = RoleId, figure = Figure, limit_goal = LimitGoal} = PS,
	#figure{lv = Lv} = Figure,
	case LimitGoal =:= #{} of
		true ->
			skip;
		false ->
			ReSql1 = io_lib:format(<<"DELETE FROM `limit_goal_main` WHERE `role_id` = ~p">>, [RoleId]),
			ReSql2 = io_lib:format(<<"DELETE FROM `limit_goal_task` WHERE `role_id` = ~p">>, [RoleId]),
			F = fun() ->
				db:execute(ReSql1),
				db:execute(ReSql2),
				NewLimitGoal = lib_limit_goal:check_open_goal(RoleId, Lv, #{}),
				{ok, NewLimitGoal}
			end,
			case db:transaction(F) of
				{ok, NewLimitGoal} ->
					lib_limit_goal:send_info(RoleId, NewLimitGoal),
					NewPS = PS#player_status{limit_goal = NewLimitGoal};
				_ ->
					NewPS = PS
			end,
			NewPS
	end.

%% 修复限时目标目标为1任务为4的数据进度
gm_sync_limit_goal() ->
	ReSql = io_lib:format(<<"select `bl_id` from `log_boss` where `boss_id` in (1202004,1202005,1202006,1203001,1203002,1203003,1203004,1203005,1203006,1204001)">>, []),
	case db:get_all(ReSql) of
		[] -> skip;
		LogList ->
			F1 = fun(Element, TempList) ->
				case Element of
					{RoleId, _, _} ->
						case lists:keyfind(RoleId, 1, TempList) of
							{RoleId, Num1} ->
								lists:keyreplace(RoleId, 1, TempList, {RoleId, Num1+1});
							_ ->
								lists:keystore(RoleId, 1, TempList, {RoleId, 1})
						end;
					_ ->
						TempList
				end
			end,
			F2 = fun([SBLWhos], TempList) ->
				BLWhos = util:bitstring_to_term(SBLWhos),
				lists:foldl(F1, TempList, BLWhos)
			end,
			CountList = lists:foldl(F2, [], LogList),
			F3 = fun({RoleId, Num2}, Id) ->
				case Id rem 40 of
					0 ->
						timer:sleep(100);
					_ ->
						skip
				end,
				case lib_player:is_online_global(RoleId) of
					true ->
						do_gm_sync_limit_goal_online(RoleId, Num2);
					false ->
						do_gm_sync_limit_goal_offline(RoleId, Num2)
				end,
				Id + 1
			end,
			lists:foldl(F3, 1, CountList)
	end.

do_gm_sync_limit_goal_online(RoleId, NowNum) ->
	lib_player:apply_cast(RoleId, ?APPLY_CAST_SAVE, ?MODULE, do_gm_sync_limit_goal_online_helper, [NowNum]).

do_gm_sync_limit_goal_online_helper(PS, NowNum) ->
	#player_status{id = RoleId, limit_goal = LimitGoal} = PS,
	MainStatus = maps:get(1, LimitGoal, #main_status{goal_id = 1}),
	#main_status{task_list = TaskList} = MainStatus,
	case lists:keyfind(4, #task_status.task_id, TaskList) of
		#task_status{status = Status} when Status =:= ?FINISH orelse Status =:= ?HAS_RECEIVE ->
			NewTaskList = TaskList;
		#task_status{progress = Progress} when Progress >= NowNum ->
			NewTaskList = TaskList;
		#task_status{progress = _Progress} = TaskStatus ->
			case NowNum >= 4 of
				true -> %% 完成
					NewProgress = 4,
					NewStatus = ?FINISH;
				false ->
					NewProgress = max(0, NowNum),
					NewStatus = ?UNFINISH
			end,
			NewTaskStatus = TaskStatus#task_status{status = NewStatus, progress = NewProgress},
			lib_limit_goal:db_update_limit_goal_task(NewProgress, NewStatus, [], RoleId, 1, 4),
			NewTaskList = lists:keyreplace(4, #task_status.task_id, TaskList, NewTaskStatus);
		_ ->
			case NowNum >= 4 of
				true ->
					NewProgress = 4,
					NewStatus = ?FINISH;
				false ->
					NewProgress = max(0, NowNum),
					NewStatus = ?UNFINISH
			end,
			TaskStatus = #task_status{
				goal_id = 1, task_id = 4, progress = NewProgress, status = NewStatus,
				extra = [], write_time = 0, write_status = 0
			},
			lib_limit_goal:db_insert_limit_goal_task(RoleId, 1, 4, NewProgress, NewStatus, []),
			NewTaskList = lists:keystore(4, #task_status.task_id, TaskList, TaskStatus)
	end,
	NewMainStatus = MainStatus#main_status{task_list = NewTaskList},
	NewLimitGoal = maps:put(1, NewMainStatus, LimitGoal),
	NewPS = PS#player_status{limit_goal = NewLimitGoal},
	{ok, NewPS}.

do_gm_sync_limit_goal_offline(RoleId, NowNum) ->
	ReSql = io_lib:format(?SELECT_LIMIT_GOAL_TASK, [RoleId]),
	case db:get_all(ReSql) of
		[] ->
			case NowNum >= 4 of
				true ->
					NewProgress = 4,
					NewStatus = ?FINISH;
				false ->
					NewProgress = max(0, NowNum),
					NewStatus = ?UNFINISH
			end,
			lib_limit_goal:db_insert_limit_goal_task(RoleId, 1, 4, NewProgress, NewStatus, []);
		ResList ->
			UseList = [{GoalId, TaskId, Progress, Status} || [GoalId, TaskId, Progress, Status, _SExtra] <- ResList, GoalId == 1, TaskId == 4],
			case UseList of
				[] ->
					case NowNum >= 4 of
						true ->
							NewProgress = 4,
							NewStatus = ?FINISH;
						false ->
							NewProgress = max(0, NowNum),
							NewStatus = ?UNFINISH
					end,
					lib_limit_goal:db_insert_limit_goal_task(RoleId, 1, 4, NewProgress, NewStatus, []);
				[{_GoalId, _TaskId, _Progress, Status}|_T] when Status =:= ?FINISH orelse Status =:= ?HAS_RECEIVE ->
					skip;
				[{_GoalId, _TaskId, Progress, _Status}|_T] when Progress >= NowNum ->
					skip;
				[{_GoalId, _TaskId, _Progress, _Status}|_T] ->
					case NowNum >= 4 of
						true -> %% 完成
							NewProgress = 4,
							NewStatus = ?FINISH;
						false ->
							NewProgress = max(0, NowNum),
							NewStatus = ?UNFINISH
					end,
					lib_limit_goal:db_update_limit_goal_task(NewProgress, NewStatus, [], RoleId, 1, 4)
			end
	end.

%% 修复已达到完成限时目标1任务4进度但没完成的玩家数据
gm_repair_limit_goal_status() ->
	ReSql = io_lib:format(<<"select * from `limit_goal_task` where `goal_id` = 1 and `task_id` = 4 and `progress` >= 4 and `status` = 0">>, []),
	TaskList = db:get_all(ReSql),
	F = fun([RoleId, _GoalId, _TaskId, _Progress, _Status, _SExtra] = TaskSqlList, N) ->
		case N rem 40 of
			0 ->
				timer:sleep(100);
			_ ->
				skip
		end,
		case lib_player:is_online_global(RoleId) of
			true ->
				lib_player:apply_cast(RoleId, ?APPLY_CAST_SAVE, ?MODULE, gm_repair_limit_goal_status_online, [TaskSqlList]);
			false ->
				gm_repair_limit_goal_status_offline(TaskSqlList)
		end,
		N + 1
	end,
	lists:foldl(F, 0, TaskList).

gm_repair_limit_goal_status_online(PS, [RoleId, GoalId, TaskId, Progress, _Status, _SExtra]) ->
	#player_status{limit_goal = LimitGoal} = PS,
	MainStatus = maps:get(GoalId, LimitGoal, #main_status{goal_id = GoalId}),
	#main_status{task_list = TaskList} = MainStatus,
	NewTaskStatus = #task_status{
		goal_id = GoalId, task_id = TaskId, progress = Progress,
		status = ?FINISH, extra = []
	},
	lib_limit_goal:db_update_limit_goal_task(Progress, ?FINISH, [], RoleId, GoalId, TaskId),
	NewTaskList = lists:keyreplace(TaskId, #task_status.task_id, TaskList, NewTaskStatus),
	NewMainStatus = MainStatus#main_status{task_list = NewTaskList},
	NewLimitGoal = maps:put(GoalId, NewMainStatus, LimitGoal),
	lib_limit_goal:send_info(RoleId, NewLimitGoal),
	NewPS = PS#player_status{limit_goal = NewLimitGoal},
	{ok, NewPS}.

gm_repair_limit_goal_status_offline([RoleId, GoalId, TaskId, Progress, _Status, _SExtra]) ->
	lib_limit_goal:db_update_limit_goal_task(Progress, ?FINISH, [], RoleId, GoalId, TaskId).
