use strict;

return sub
{
	my $dbh = shift;
	

	$dbh->do("UPDATE jin_order SET aff_camp_id=0 WHERE aff_user_id=0");
	$dbh->do("UPDATE jin_sale SET aff_camp_id=0 WHERE aff_user_id=0");
	$dbh->do("UPDATE jin_package_hit SET camp_id=0,tstamp=tstamp WHERE aff_user_id=0");
	
	my $sth = $dbh->prepare("SELECT * FROM jin_campaign ORDER BY owner_id");
	$sth->execute;
	while (my $camp = $sth->fetchrow_hashref)
	{
		$dbh->do("UPDATE jin_order SET aff_camp_id=0 WHERE aff_camp_id=$camp->{id} and aff_user_id<>$camp->{owner_id}");
		$dbh->do("UPDATE jin_sale SET aff_camp_id=0 WHERE aff_camp_id=$camp->{id} and aff_user_id<>$camp->{owner_id}");
		$dbh->do("UPDATE jin_package_hit SET camp_id=0,tstamp=tstamp WHERE camp_id=$camp->{id} and aff_user_id<>$camp->{owner_id}");
	}
	
	
	my $deleted_prods = join ',', map { $_->[0] } @{$dbh->selectall_arrayref("SELECT id FROM jin_product WHERE is_deleted=1")};
	$dbh->do("DELETE FROM jin_package_product WHERE find_in_set(prod_id,'$deleted_prods')");
	
	return;
};
