Я написал планировщик событий в MySQL. После выполнения этого события это не работает вообще. Задача, которая написана, не происходит. Ниже мой планировщик событий, DELIMITER $$Планировщик событий в Mysql не работает
CREATE EVENT `Untravelled_Deduction` ON SCHEDULE EVERY 1 DAY STARTS '2016-04-01 06:42:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
DECLARE UserId INT;
DECLARE v_finished INT DEFAULT 0;
DECLARE GetDate DATE DEFAULT DATE(NOW());
/*get each user who's account is activated and not swiped for the given date*/
DECLARE UnTravelled CURSOR FOR
SELECT DISTINCT U.user_id
FROM `um.user` U
INNER JOIN `um.user_ps.pass` UP ON UP.user_id=U.user_id
INNER JOIN `ps.pass` P ON P.pass_id=UP.pass_id AND P.status_id=4
INNER JOIN `um.user_trs.tariff` UT ON UT.user_id = U.user_id
WHERE U.is_verified_email=1 AND U.is_active=1
AND UT.user_tariff_id = (
/*check user available_journeys journeys is available or not*/
SELECT MAX(UT2.user_tariff_id) FROM `um.user_trs.tariff` UT2 WHERE UT2.user_id = UT.user_id
AND UT2.available_journeys>0 AND UT2.current_balance>0 AND UT2.end_date>=GetDate
);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished=1;
/*Match th date with holiday list*/
SET @HolidayCount=(SELECT COUNT(*) FROM `holiday_list` WHERE DATE(holiday_date)=GetDate);
/*Match date for saturday and sunday*/
IF DAYOFWEEK(GetDate)!=1 AND DAYOFWEEK(GetDate)!=7 AND @HolidayCount=0 THEN
OPEN UnTravelled;
get_userid:LOOP
FETCH UnTravelled INTO UserId;
IF v_finished=1 THEN
LEAVE get_userid;
END IF;
/*Find user is registered for two way or one way, if @UserRouteCount=2 i.e. two way, @UserRouteCount=1 i.e. one way, */
SET @UserRouteCount = (SELECT COUNT(*) FROM `um.user_rm.route` WHERE user_id = UserId);
/*Get user swipe count to check how many times he swiped for the day. if only one and he is one way then deduct only once*/
SET @UserSwipeCount = (SELECT COUNT(*) FROM `ts.swipe_information` WHERE user_id = UserId AND DATE(swipe_in_time)=GetDate);
/*if user is two way and swiped only once for the day then deduct only once*/
IF (@UserRouteCount=2 AND @UserSwipeCount=1) THEN
SET @RouteCount=1;
ELSE
SET @[email protected];
END IF;
SET @i=1;
/*Get ticket details on this date for the user*/
SET @TicketCont= (SELECT COUNT(ticket_id) FROM `ts.ticket` WHERE DATE(`issued_on`)=GetDate AND user_id=UserId);
SET @IsInsert=0;
/*Check if any ticket is issued for the user on this date. if not he not travelled and go ahead to deduct*/
IF (@TicketCont=0) THEN
SET @IsInsert=1;
END IF;
/*check if ticket issued once, if he is two way user then decuct once*/
IF (@TicketCont=1 AND @UserRouteCount=2) THEN
SET @IsInsert=1;
END IF;
WHILE @i <= @RouteCount DO
IF (@IsInsert=1) THEN
/*Generate ticket if not exist for given date*/
/*get user current tariff plan*/
SET @UserTariffId = (SELECT user_tariff_id FROM `um.user_trs.tariff` WHERE user_id =UserId AND expired_on >= GetDate AND available_journeys > 0 ORDER BY user_tariff_id LIMIT 1);
IF(@UserTariffId IS NOT NULL)
THEN
SET @PerJourneyCost = (SELECT per_journey_cost FROM `um.user_trs.tariff` WHERE [email protected]);
SET @TariffCurrentBalance = (SELECT current_balance FROM `um.user_trs.tariff` WHERE [email protected]);
INSERT INTO `ts.ticket`(user_id,ticket_type_id,ticket_number,issued_on,
amount_charged,user_tariff_id,opening_balance,is_untravelled) VALUES
(UserId,1,'',UTC_TIMESTAMP(),@PerJourneyCost,@UserTariffId, @TariffCurrentBalance,1);
IF @PerJourneyCost IS NOT NULL THEN
/*Update user current tariff balance and number of journeys*/
UPDATE `um.user_trs.tariff` SET current_balance=([email protected]),
available_journeys =(available_journeys-1) WHERE user_tariff_id = @UserTariffId;
END IF;
END IF;
END IF;
SET @[email protected]+1;
END WHILE;
/*Update user balance details and update Updated date in User table*/
UPDATE `um.user` SET updated_on=UTC_TIMESTAMP() WHERE user_id = UserId;
END LOOP get_userid;
CLOSE UnTravelled;
END IF;
END$$
DELIMITER ;
Не удалось исправить эту проблему, необходимо предложить какие-либо улучшения.
С уважением Sangeetha
Было бы лучше, если бы вы создать хранимую процедуру для выполнения всей вашей условной задачи как вставки и обновление, а затем вы можете планировать эту хранимую процедуру в определенное время. –