use strict;

return sub
{
	my $dbh = shift;
	

	my $sth = $dbh->prepare("select distinct code from jin_funnel_visit where tstamp_start is null");
	$sth->execute;

	my $sth1 = $dbh->prepare("select min(tstamp) as tstamp from jin_funnel_visit where code=?");
	my $sth2 = $dbh->prepare("update jin_funnel_visit set tstamp_start=? where code=?");
	
	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1->execute($row->{code});

		while (my $row1 = $sth1->fetchrow_hashref)
		{
			$sth2->execute($row1->{tstamp}, $row->{code});
		}
	}
	

	my $sth = $dbh->prepare("select distinct step_id from jin_funnel_splitpage");
	$sth->execute;

	my $sth1 = $dbh->prepare("select count(*) as num from jin_funnel_splitpage where step_id=? and is_default=1");
	my $sth2 = $dbh->prepare("select id from jin_funnel_splitpage where step_id=? order by id limit 1");
	my $sth3 = $dbh->prepare("update jin_funnel_splitpage set is_default=1 where id=?");
	
	while (my $row = $sth->fetchrow_hashref)
	{
		$sth1->execute($row->{step_id});

		my $has;

		while (my $row1 = $sth1->fetchrow_hashref)
		{
			$has = $row1->{num};
		}
		
		next
			if $has;

		$sth2->execute($row->{step_id});

		while (my $row2 = $sth2->fetchrow_hashref)
		{
			$sth3->execute($row2->{id});
		}
	}

	
	return;
};
