生成訂單儲存過程(mysql)
阿新 • • 發佈:2018-11-12
/*
*生成商超訂單儲存過程
*
*/
DELIMITER $$
DROP PROCEDURE IF EXISTS generate_storeOrder_cart$$
CREATE DEFINER=`lixin`@`%` PROCEDURE generate_storeOrder_cart(IN _orderNo VARCHAR(50),IN _customerId BIGINT,IN _sendDescription VARCHAR(200),IN _remark VARCHAR(200),IN _amount DECIMAL(13,2),IN _source INT,IN _customerAddId BIGINT,
IN _couponId BIGINT,IN _cartIds VARCHAR(4000),IN _goodsIds VARCHAR(4000),IN _numIds VARCHAR(2000),OUT _code VARCHAR(6),OUT _msg VARCHAR(200),OUT _orderId BIGINT)
label_pro:BEGIN
/**
過程說明:新增商超訂單
建立人:luolong
建立時間:2016412
入參:
_orderNo:訂單編號
_customerId:使用者id,
_sendDescription:送達時間描述
_remark:訂單備註
_amount:訂單金額
_source:訂單來源
_customerAdd:收貨地址id
_couponId:優惠劵id
_cartIds:購物車ids
_goodsIds:商品ids
_numIds:商品數量ids
出參:
_orderId:訂單id
_code:返回編碼
_msg:返回資訊
*/
DECLARE cnt INT DEFAULT 0; #_goodsIds被分割資料個數
DECLARE cnt_i INT DEFAULT 0;
DECLARE size INT;#使用者未支付的訂單size
DECLARE orderNo VARCHAR(50);#訂單編碼
DECLARE orderId INT;#訂單id
DECLARE cartId INT;#購物車id
DECLARE goodsId INT;#商品id
DECLARE goodsNumber INT;#商品數量
DECLARE shopId,goodsProperty,goodsInventory INT;#店鋪id,商品屬性,庫存
DECLARE shopEnabledFlag VARCHAR(2);#店鋪是否可用 'Y'代表可用 'N'代表不可用
DECLARE goodsName VARCHAR(100);#商品名稱
DECLARE goodsInPrice,goodsOutPrice,commissionRate DECIMAL(13,2) DEFAULT 0.00;#商品進價,商品售價,佣金比例
DECLARE couponState,activityType INT;#優惠劵狀態,優惠劵活動型別
DECLARE distributionFlag INT;#店鋪是否開啟免配送 0 關閉 1 開啟
DECLARE distributionAmount,freeAmount DECIMAL(13,2);#配送金額 免配送金額
DECLARE totalAmount,commissionAmountTotal,costAmountTotal,couponAmount DECIMAL(13,2) DEFAULT 0.00;#訂單總金額,佣金總金額,進價總金額,優惠劵金額
#異常宣告
DECLARE _err INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING , NOT FOUND
BEGIN
SET _err=1;
ROLLBACK;
END;
START TRANSACTION;
SET _code='000000';
SET _msg='處理成功';
#查詢使用者是否有未支付的訂單
SELECT COUNT(id) INTO size FROM kb_store_order WHERE 1=1 AND state=1 AND created_by=_customerId;
IF size > 0 THEN
SET _code='000006';
SET _msg='有未支付的訂單,不能下單';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#獲取訂單編號,將訂單編號後面拼上隨機3位數
SET orderNo=CONCAT(_orderNo,CEILING(RAND()*500+500));
#生成商超訂單
INSERT INTO kb_store_order (order_no,state,pay_type,city_manage_id,rate_discount,amount,shop_id,
source,contacts,contact_phone,address,address_detail,remark,rank_flag,display_flag,integral,integral_money,
coupon_id,coupon_money,customerAdd_id,commission_amount,cost_amount,send_time,complete_time,send_description,created_by,creation_date,enabled_flag)
VALUES(orderNo,1,null,null,null,0,null,_source,null,null,null,null,_remark,'N',1,null,null,_couponId,null,_customerAddId,0,0,null,null,_sendDescription,_customerId,NOW(),1);
IF _err=1 THEN
SET _code='999999';
SET _msg='建立訂單失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#獲取訂單id
SET orderId = LAST_INSERT_ID();
#獲取_goodsIds被分割資料個數
SET cnt = 1+(LENGTH(_goodsIds) - LENGTH(REPLACE(_goodsIds,',','')));
#遍歷_goodsIds(商品ids)
WHILE cnt_i<cnt DO
SET cnt_i = cnt_i + 1;
SET cartId=REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(_cartIds,',',cnt_i)),',',1)) + 0;
SET goodsId=REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(_goodsIds,',',cnt_i)),',',1)) + 0;
SET goodsNumber=REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(_numIds,',',cnt_i)),',',1)) + 0;
#查詢商品資訊
SELECT
t1.shop_id,t4.enabled_flag,t2.name,t2.goods_property,t3.commission,t1.goods_in_price,t1.goods_out_price,IFNULL(t1.goods_inventory ,0),t4.distribution_flag,t4.distribution_amount,t4.free_amount
INTO shopId,shopEnabledFlag,goodsName,goodsProperty,commissionRate,goodsInPrice,goodsOutPrice,goodsInventory,distributionFlag,distributionAmount,freeAmount
FROM
(
SELECT id,shop_id,goods_id,goods_state,goods_in_price,goods_out_price,goods_inventory FROM kb_store_goods_relation WHERE id=goodsId
UNION ALL
SELECT id,shop_id,goods_id,goods_state,goods_in_price,special_price goods_out_price,goods_inventory FROM kb_store_sale_goods_relation WHERE id=goodsId
) t1
LEFT JOIN kb_goods t2 ON t1.goods_id = t2.id
LEFT JOIN kb_goods_category t3 ON t2.second_id = t3.id
LEFT JOIN kb_shop t4 ON t1.shop_id = t4.id
WHERE 1=1 AND t1.goods_state =4;
IF _err=1 THEN
SET _code='000009';
SET _msg='該商品不存在或已下架';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
IF goodsInventory < goodsNumber THEN
SET _code='000007';
SET _msg=CONCAT('[',goodsName,']庫存不足');
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
IF shopEnabledFlag !='Y' THEN
SET _code='000010';
SET _msg='商鋪不存在或已停用';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#新增訂單詳細資訊
INSERT INTO kb_store_order_detail(order_id,goods_id,parent_id,rate_discount,cost_price,discount_price,number,cost_total,commission_rate,commission_amount,cost_amount,discount_total)
VALUES
(orderId,goodsId,null,null,goodsOutPrice,null,goodsNumber,goodsOutPrice*goodsNumber,commissionRate,ROUND(0.01*goodsOutPrice*goodsNumber*commissionRate,2),goodsInPrice*goodsNumber,null);
IF _err=1 THEN
SET _code='999999';
SET _msg='新增訂單詳細資訊失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#更新商品庫存
IF goodsProperty=3 THEN
UPDATE kb_store_sale_goods_relation set goods_inventory = IFNULL(goods_inventory,0)-goodsNumber,
goods_sales_count=IFNULL(goods_sales_count,0)+goodsNumber
WHERE id=goodsId;
ELSE
UPDATE kb_store_goods_relation set goods_inventory = IFNULL(goods_inventory,0)-goodsNumber,
goods_sales_count=IFNULL(goods_sales_count,0)+goodsNumber
WHERE id=goodsId;
END IF;
IF _err=1 THEN
SET _code='999999';
SET _msg='更新商品庫存資訊失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#刪除購物車資訊
DELETE FROM kb_shopping_cart WHERE id=cartId;
IF _err=1 THEN
SET _code='999999';
SET _msg='刪除購物車資訊失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
END WHILE;
#查詢訂單總金額,佣金金額,進價總金額
SELECT SUM(cost_total),SUM(commission_amount),SUM(cost_amount) INTO totalAmount,commissionAmountTotal,costAmountTotal FROM kb_store_order_detail WHERE order_id = orderId;
#店鋪是否開啟免配送(0 關閉 1 開啟)
IF distributionFlag = 0 THEN
SET totalAmount = totalAmount + distributionAmount;
SET freeAmount = 0;
END IF;
IF distributionFlag = 1 THEN
IF totalAmount >= freeAmount THEN
SET freeAmount = distributionAmount;
ELSE
SET totalAmount = totalAmount + distributionAmount;
SET freeAmount =0;
END IF;
END IF;
#有沒有優惠劵id
IF _couponId IS NOT NULL THEN
SELECT t2.amount,t1.use_state,t1.activity_type INTO couponAmount,couponState,activityType FROM kb_customer_coupon_relation t1 LEFT JOIN kb_coupon t2 ON t1.coupon_id = t2.id WHERE t1.id = _couponId;
SET totalAmount = totalAmount -couponAmount;
IF couponState = 2 THEN
SET _code='000011';
SET _msg='該優惠卷已使用過了';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#更新優惠劵的狀態為已使用
UPDATE kb_customer_coupon_relation set use_state=2
WHERE id=_couponId;
IF _err=1 THEN
SET _code='999999';
SET _msg='更新優惠劵狀態失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#更新使用者與紅包與優惠劵關係表
IF activityType = 2 THEN
UPDATE kb_customer_red_coupon_relation SET state = 2 WHERE customer_coupon_id=_couponId;
IF _err=1 THEN
SET _code='999999';
SET _msg='使用者與紅包與優惠劵關係表狀態失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
END IF;
#插入使用者優惠劵使用記錄表
INSERT INTO kb_customer_coupon_log ( customer_id, customer_coupon_id, creation_date ) VALUES ( _customerId, _couponId, NOW() );
IF _err=1 THEN
SET _code='999999';
SET _msg='插入使用者優惠劵使用記錄表失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
END IF;
#判斷傳過來金額與算出來的總金額是否相等
IF _amount!= totalAmount THEN
SET _code='000008';
SET _msg='傳過來總金額有問題';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#更新訂單金額
UPDATE kb_store_order SET amount=totalAmount,commission_amount=commissionAmountTotal,cost_amount=costAmountTotal,
coupon_money=couponAmount,shop_id=shopId,distribution_amount=distributionAmount,free_amount=freeAmount
WHERE id = orderId;
IF _err=1 THEN
SET _code='999999';
SET _msg='更新訂單金額失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#新增訂單日誌
INSERT INTO kb_store_order_log
(order_id,state,description,created_by,creation_date) VALUES(orderId,1,'生成商超訂單',_customerId,NOW());
IF _err=1 THEN
SET _code='999999';
SET _msg='新增訂單日誌失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#修改最後使用地址的lastUseTime
UPDATE kb_customer_address SET last_use_time=NOW() WHERE id= _customerAddId;
IF _err=1 THEN
SET _code='999999';
SET _msg='修改最後使用地址的lastUseTime失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
SET _orderId = orderId;
SELECT _code,_msg,_orderId;
COMMIT;
END$$
DELIMITER ;
*生成商超訂單儲存過程
*
*/
DELIMITER $$
DROP PROCEDURE IF EXISTS generate_storeOrder_cart$$
CREATE DEFINER=`lixin`@`%` PROCEDURE generate_storeOrder_cart(IN _orderNo VARCHAR(50),IN _customerId BIGINT,IN _sendDescription VARCHAR(200),IN _remark VARCHAR(200),IN _amount DECIMAL(13,2),IN _source INT,IN _customerAddId BIGINT,
IN _couponId BIGINT,IN _cartIds VARCHAR(4000),IN _goodsIds VARCHAR(4000),IN _numIds VARCHAR(2000),OUT _code VARCHAR(6),OUT _msg VARCHAR(200),OUT _orderId BIGINT)
label_pro:BEGIN
/**
過程說明:新增商超訂單
建立人:luolong
建立時間:2016412
入參:
_orderNo:訂單編號
_customerId:使用者id,
_sendDescription:送達時間描述
_remark:訂單備註
_amount:訂單金額
_source:訂單來源
_customerAdd:收貨地址id
_couponId:優惠劵id
_cartIds:購物車ids
_goodsIds:商品ids
_numIds:商品數量ids
出參:
_orderId:訂單id
_code:返回編碼
_msg:返回資訊
*/
DECLARE cnt INT DEFAULT 0; #_goodsIds被分割資料個數
DECLARE cnt_i INT DEFAULT 0;
DECLARE size INT;#使用者未支付的訂單size
DECLARE orderNo VARCHAR(50);#訂單編碼
DECLARE orderId INT;#訂單id
DECLARE cartId INT;#購物車id
DECLARE goodsId INT;#商品id
DECLARE goodsNumber INT;#商品數量
DECLARE shopId,goodsProperty,goodsInventory INT;#店鋪id,商品屬性,庫存
DECLARE shopEnabledFlag VARCHAR(2);#店鋪是否可用 'Y'代表可用 'N'代表不可用
DECLARE goodsName VARCHAR(100);#商品名稱
DECLARE goodsInPrice,goodsOutPrice,commissionRate DECIMAL(13,2) DEFAULT 0.00;#商品進價,商品售價,佣金比例
DECLARE couponState,activityType INT;#優惠劵狀態,優惠劵活動型別
DECLARE distributionFlag INT;#店鋪是否開啟免配送 0 關閉 1 開啟
DECLARE distributionAmount,freeAmount DECIMAL(13,2);#配送金額 免配送金額
DECLARE totalAmount,commissionAmountTotal,costAmountTotal,couponAmount DECIMAL(13,2) DEFAULT 0.00;#訂單總金額,佣金總金額,進價總金額,優惠劵金額
#異常宣告
DECLARE _err INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING , NOT FOUND
BEGIN
SET _err=1;
ROLLBACK;
END;
START TRANSACTION;
SET _code='000000';
SET _msg='處理成功';
#查詢使用者是否有未支付的訂單
SELECT COUNT(id) INTO size FROM kb_store_order WHERE 1=1 AND state=1 AND created_by=_customerId;
IF size > 0 THEN
SET _code='000006';
SET _msg='有未支付的訂單,不能下單';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#獲取訂單編號,將訂單編號後面拼上隨機3位數
SET orderNo=CONCAT(_orderNo,CEILING(RAND()*500+500));
#生成商超訂單
INSERT INTO kb_store_order (order_no,state,pay_type,city_manage_id,rate_discount,amount,shop_id,
source,contacts,contact_phone,address,address_detail,remark,rank_flag,display_flag,integral,integral_money,
coupon_id,coupon_money,customerAdd_id,commission_amount,cost_amount,send_time,complete_time,send_description,created_by,creation_date,enabled_flag)
VALUES(orderNo,1,null,null,null,0,null,_source,null,null,null,null,_remark,'N',1,null,null,_couponId,null,_customerAddId,0,0,null,null,_sendDescription,_customerId,NOW(),1);
IF _err=1 THEN
SET _code='999999';
SET _msg='建立訂單失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#獲取訂單id
SET orderId = LAST_INSERT_ID();
#獲取_goodsIds被分割資料個數
SET cnt = 1+(LENGTH(_goodsIds) - LENGTH(REPLACE(_goodsIds,',','')));
#遍歷_goodsIds(商品ids)
WHILE cnt_i<cnt DO
SET cnt_i = cnt_i + 1;
SET cartId=REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(_cartIds,',',cnt_i)),',',1)) + 0;
SET goodsId=REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(_goodsIds,',',cnt_i)),',',1)) + 0;
SET goodsNumber=REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(_numIds,',',cnt_i)),',',1)) + 0;
#查詢商品資訊
SELECT
t1.shop_id,t4.enabled_flag,t2.name,t2.goods_property,t3.commission,t1.goods_in_price,t1.goods_out_price,IFNULL(t1.goods_inventory ,0),t4.distribution_flag,t4.distribution_amount,t4.free_amount
INTO shopId,shopEnabledFlag,goodsName,goodsProperty,commissionRate,goodsInPrice,goodsOutPrice,goodsInventory,distributionFlag,distributionAmount,freeAmount
FROM
(
SELECT id,shop_id,goods_id,goods_state,goods_in_price,goods_out_price,goods_inventory FROM kb_store_goods_relation WHERE id=goodsId
UNION ALL
SELECT id,shop_id,goods_id,goods_state,goods_in_price,special_price goods_out_price,goods_inventory FROM kb_store_sale_goods_relation WHERE id=goodsId
) t1
LEFT JOIN kb_goods t2 ON t1.goods_id = t2.id
LEFT JOIN kb_goods_category t3 ON t2.second_id = t3.id
LEFT JOIN kb_shop t4 ON t1.shop_id = t4.id
WHERE 1=1 AND t1.goods_state =4;
IF _err=1 THEN
SET _code='000009';
SET _msg='該商品不存在或已下架';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
IF goodsInventory < goodsNumber THEN
SET _code='000007';
SET _msg=CONCAT('[',goodsName,']庫存不足');
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
IF shopEnabledFlag !='Y' THEN
SET _code='000010';
SET _msg='商鋪不存在或已停用';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#新增訂單詳細資訊
INSERT INTO kb_store_order_detail(order_id,goods_id,parent_id,rate_discount,cost_price,discount_price,number,cost_total,commission_rate,commission_amount,cost_amount,discount_total)
VALUES
(orderId,goodsId,null,null,goodsOutPrice,null,goodsNumber,goodsOutPrice*goodsNumber,commissionRate,ROUND(0.01*goodsOutPrice*goodsNumber*commissionRate,2),goodsInPrice*goodsNumber,null);
IF _err=1 THEN
SET _code='999999';
SET _msg='新增訂單詳細資訊失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#更新商品庫存
IF goodsProperty=3 THEN
UPDATE kb_store_sale_goods_relation set goods_inventory = IFNULL(goods_inventory,0)-goodsNumber,
goods_sales_count=IFNULL(goods_sales_count,0)+goodsNumber
WHERE id=goodsId;
ELSE
UPDATE kb_store_goods_relation set goods_inventory = IFNULL(goods_inventory,0)-goodsNumber,
goods_sales_count=IFNULL(goods_sales_count,0)+goodsNumber
WHERE id=goodsId;
END IF;
IF _err=1 THEN
SET _code='999999';
SET _msg='更新商品庫存資訊失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#刪除購物車資訊
DELETE FROM kb_shopping_cart WHERE id=cartId;
IF _err=1 THEN
SET _code='999999';
SET _msg='刪除購物車資訊失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
END WHILE;
#查詢訂單總金額,佣金金額,進價總金額
SELECT SUM(cost_total),SUM(commission_amount),SUM(cost_amount) INTO totalAmount,commissionAmountTotal,costAmountTotal FROM kb_store_order_detail WHERE order_id = orderId;
#店鋪是否開啟免配送(0 關閉 1 開啟)
IF distributionFlag = 0 THEN
SET totalAmount = totalAmount + distributionAmount;
SET freeAmount = 0;
END IF;
IF distributionFlag = 1 THEN
IF totalAmount >= freeAmount THEN
SET freeAmount = distributionAmount;
ELSE
SET totalAmount = totalAmount + distributionAmount;
SET freeAmount =0;
END IF;
END IF;
#有沒有優惠劵id
IF _couponId IS NOT NULL THEN
SELECT t2.amount,t1.use_state,t1.activity_type INTO couponAmount,couponState,activityType FROM kb_customer_coupon_relation t1 LEFT JOIN kb_coupon t2 ON t1.coupon_id = t2.id WHERE t1.id = _couponId;
SET totalAmount = totalAmount -couponAmount;
IF couponState = 2 THEN
SET _code='000011';
SET _msg='該優惠卷已使用過了';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#更新優惠劵的狀態為已使用
UPDATE kb_customer_coupon_relation set use_state=2
WHERE id=_couponId;
IF _err=1 THEN
SET _code='999999';
SET _msg='更新優惠劵狀態失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#更新使用者與紅包與優惠劵關係表
IF activityType = 2 THEN
UPDATE kb_customer_red_coupon_relation SET state = 2 WHERE customer_coupon_id=_couponId;
IF _err=1 THEN
SET _code='999999';
SET _msg='使用者與紅包與優惠劵關係表狀態失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
END IF;
#插入使用者優惠劵使用記錄表
INSERT INTO kb_customer_coupon_log ( customer_id, customer_coupon_id, creation_date ) VALUES ( _customerId, _couponId, NOW() );
IF _err=1 THEN
SET _code='999999';
SET _msg='插入使用者優惠劵使用記錄表失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
END IF;
#判斷傳過來金額與算出來的總金額是否相等
IF _amount!= totalAmount THEN
SET _code='000008';
SET _msg='傳過來總金額有問題';
SELECT _code,_msg;
ROLLBACK;
LEAVE label_pro;
END IF;
#更新訂單金額
UPDATE kb_store_order SET amount=totalAmount,commission_amount=commissionAmountTotal,cost_amount=costAmountTotal,
coupon_money=couponAmount,shop_id=shopId,distribution_amount=distributionAmount,free_amount=freeAmount
WHERE id = orderId;
IF _err=1 THEN
SET _code='999999';
SET _msg='更新訂單金額失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#新增訂單日誌
INSERT INTO kb_store_order_log
(order_id,state,description,created_by,creation_date) VALUES(orderId,1,'生成商超訂單',_customerId,NOW());
IF _err=1 THEN
SET _code='999999';
SET _msg='新增訂單日誌失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
#修改最後使用地址的lastUseTime
UPDATE kb_customer_address SET last_use_time=NOW() WHERE id= _customerAddId;
IF _err=1 THEN
SET _code='999999';
SET _msg='修改最後使用地址的lastUseTime失敗';
SELECT _code,_msg;
LEAVE label_pro;
END IF;
SET _orderId = orderId;
SELECT _code,_msg,_orderId;
COMMIT;
END$$
DELIMITER ;