我想在用户成功预订订单后创建自动奖励系统。在我们的奖励系统中,有15天的订单取消政策。如果取消期间未取消订单,则用户将获得的奖励积分为每order_amount(1000/100)=>每花费100 = 1积分1点
为实现这一点,我在取消期结束且付款状态成功时使用触发器,那么根据上述计算得出的奖励积分将自动插入到user_rewards表中
为此,我为触发器创建了如下代码
之后创建CREATE DEFINER =root
@ localhost
触发tbl_orders_after_insert
INSERT ON `tbl_orders` FOR EACH ROW BEGIN
INSERT INTO tbl_rewards
SET user_id = NEW.user_id,
order_number = NEW.order_number,
amount = amount + NEW.payable_amount,
reward_points = CASE NEW.payment_status WHEN 0 THEN reward_points + (NEW.payable_amount)/100 END;
END
wheere it appends the user_id, order_number but amount and reward_points will always 0 value... Please help me i am stuck on this point
CREATE DEFINER=`root`@`localhost` TRIGGER `tbl_orders_after_insert` AFTER INSERT ON `tbl_orders` FOR EACH ROW BEGIN
INSERT INTO tbl_rewards
SET user_id = NEW.user_id,
order_number = NEW.order_number,
amount = amount + NEW.payable_amount,
reward_points = CASE NEW.payment_status WHEN 0 THEN reward_points + (NEW.payable_amount)/100 END;
END
On Updating
CREATE DEFINER=`root`@`localhost` TRIGGER `after_orders_update` AFTER UPDATE ON `tbl_orders` FOR EACH ROW BEGIN
UPDATE tbl_rewards
SET
reward_points = (OLD.payable_amount)/100
WHERE NEW.order_number = OLD.order_number;
END
My Order Table is
CREATE TABLE `tbl_orders` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL,
`order_number` VARCHAR(25) NOT NULL,
`first_name` VARCHAR(255) NOT NULL,
`last_name` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`phone` VARCHAR(20) NOT NULL,
`fax` VARCHAR(25) NOT NULL,
`address_id` INT(11) NOT NULL,
`total_qty` INT(11) NOT NULL,
`sub_total` DOUBLE NOT NULL,
`eco_tax` DOUBLE NOT NULL,
`vat` DOUBLE NOT NULL,
`total_price` DOUBLE NOT NULL,
`payable_amount` DOUBLE NOT NULL,
`coupon` VARCHAR(50) NOT NULL,
`coupon_amount` DOUBLE NOT NULL,
`coupon_type` INT(11) NOT NULL COMMENT '0=>percentage, 1=>fixed_cart_amount',
`coupon_discount_amount` DOUBLE NOT NULL,
`gift_voucher` VARCHAR(50) NOT NULL,
`gift_voucher_amount` DOUBLE NOT NULL,
`gift_voucher_percentage` DOUBLE NOT NULL,
`credit_apply` INT(11) NOT NULL,
`credit_amount` DOUBLE NOT NULL,
`credit_percentage` DOUBLE NOT NULL,
`credit_installment_length` INT(11) NOT NULL,
`installment_plan_id` INT(11) NOT NULL,
`installment_months` INT(11) NOT NULL,
`installment_insterest` INT(11) NOT NULL,
`order_date` DATE NOT NULL,
`order_status` INT(11) NOT NULL COMMENT '0 => Pending, 1=> Approved, 2 => Shipped, 3 => Delivered, 10=>Cancelled',
`cancel_or_return_reason` VARCHAR(555) NOT NULL,
`order_admin_status` INT(11) NOT NULL DEFAULT '0' COMMENT '0 => New, 1 => PO, 2 => Packing, 3 => Dispatched',
`shipping_type` INT(11) NOT NULL,
`shipping_price` DOUBLE NOT NULL,
`payment_method` INT(11) NOT NULL COMMENT '0 => COD, 1 => Paypal',
`payment_status` INT(11) NOT NULL COMMENT '0=>unpaid, 1=>paid, 2=>returned',
`comment` TEXT NOT NULL,
`po_status` INT(11) NOT NULL DEFAULT '0' COMMENT '0 => Not Send, 1 => Partially Send ,2 => Send',
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`feedback` INT(11) NOT NULL COMMENT '0=>NO feedback, 1=>Feedback',
`agree_ip` VARCHAR(200) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=209
;
AND The rewards table is:
CREATE TABLE `tbl_rewards` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(10) NOT NULL,
`order_number` VARCHAR(10) NOT NULL,
`reward_points` DOUBLE NOT NULL,
`amount` DOUBLE NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=11
;
我要插入用户ID,订单号,金额和奖励积分