当前位置:网站首页>Task queue of laravel
Task queue of laravel
2022-06-23 23:37:00 【Ximen eats snow】












Insert picture description here 
–queue Only run the tasks in the specified parameter queue
–daemon This parameter is obsolete
–once Execute once and push
–stop–when-empty It stops when the queue is empty
–delay Set the interval between failed tasks
–force If the maintenance mode is turned on The queue bucket can work normally
–memory Go to sleep
–timeout Task timeout 



// Order state machine
The order sheet
CREATE TABLE `litemall_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT ' User of user table ID',
`order_sn` varchar(63) NOT NULL COMMENT ' The order no. ',
`order_status` smallint(6) NOT NULL COMMENT ' The order status ',
`aftersale_status` smallint(6) DEFAULT '0' COMMENT ' After sales status ,0 Yes, you can apply ,1 Yes, the user has applied ,2 It is approved by the administrator ,3 The administrator refunds successfully ,4 The administrator approves and rejects ,5 Yes, the user has canceled ',
`consignee` varchar(63) NOT NULL COMMENT ' Consignee name ',
`mobile` varchar(63) NOT NULL COMMENT ' Consignee's mobile number ',
`address` varchar(127) NOT NULL COMMENT ' The specific receiving address ',
`message` varchar(512) NOT NULL DEFAULT '' COMMENT ' User order message ',
`goods_price` decimal(10,2) NOT NULL COMMENT ' The total cost of the goods ',
`freight_price` decimal(10,2) NOT NULL COMMENT ' Delivery fee ',
`coupon_price` decimal(10,2) NOT NULL COMMENT ' Coupon remission ',
`integral_price` decimal(10,2) NOT NULL COMMENT ' User credit reduction ',
`groupon_price` decimal(10,2) NOT NULL COMMENT ' The group purchase preferential price is reduced ',
`order_price` decimal(10,2) NOT NULL COMMENT ' Order fee , = goods_price + freight_price - coupon_price',
`actual_price` decimal(10,2) NOT NULL COMMENT ' Out of pocket expenses , = order_price - integral_price',
`pay_id` varchar(63) DEFAULT NULL COMMENT ' Wechat payment No ',
`pay_time` datetime DEFAULT NULL COMMENT ' Wechat payment time ',
`ship_sn` varchar(63) DEFAULT NULL COMMENT ' Shipment No ',
`ship_channel` varchar(63) DEFAULT NULL COMMENT ' Delivery express company ',
`ship_time` datetime DEFAULT NULL COMMENT ' Shipment start time ',
`refund_amount` decimal(10,2) DEFAULT NULL COMMENT ' Actual refund amount ,( It is possible that the refund amount is less than the actual payment amount )',
`refund_type` varchar(63) DEFAULT NULL COMMENT ' Refund method ',
`refund_content` varchar(127) DEFAULT NULL COMMENT ' Refund remarks ',
`refund_time` datetime DEFAULT NULL COMMENT ' Refund time ',
`confirm_time` datetime DEFAULT NULL COMMENT ' The user confirms the receiving time ',
`comments` smallint(6) DEFAULT '0' COMMENT ' Quantity of order items to be evaluated ',
`end_time` datetime DEFAULT NULL COMMENT ' Order closing time ',
`add_time` datetime DEFAULT NULL COMMENT ' Creation time ',
`update_time` datetime DEFAULT NULL COMMENT ' Update time ',
`deleted` tinyint(1) DEFAULT '0' COMMENT ' Logical deletion ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=797 DEFAULT CHARSET=utf8mb4 COMMENT=' The order sheet ';
Order list
CREATE TABLE `litemall_order_goods` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL DEFAULT '0' COMMENT ' The order in the order form ID',
`goods_id` int(11) NOT NULL DEFAULT '0' COMMENT ' Items in the item list ID',
`goods_name` varchar(127) NOT NULL DEFAULT '' COMMENT ' Name of commodity ',
`goods_sn` varchar(63) NOT NULL DEFAULT '' COMMENT ' Product id ',
`product_id` int(11) NOT NULL DEFAULT '0' COMMENT ' Goods in the goods list ID',
`number` smallint(5) NOT NULL DEFAULT '0' COMMENT ' Purchase quantity of goods ',
`price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT ' The price of a commodity ',
`specifications` varchar(1023) NOT NULL COMMENT ' Specification list of goods ',
`pic_url` varchar(255) NOT NULL DEFAULT '' COMMENT ' Product picture or product picture ',
`comment` int(11) DEFAULT '0' COMMENT ' Order item reviews , If it is -1, You can't evaluate if it is overdue ; If it is 0, You can evaluate ; If other values , It is comment Comments in the table ID.',
`add_time` datetime DEFAULT NULL COMMENT ' Creation time ',
`update_time` datetime DEFAULT NULL COMMENT ' Update time ',
`deleted` tinyint(1) DEFAULT '0' COMMENT ' Logical deletion ',
PRIMARY KEY (`id`),
KEY `order_id` (`order_id`),
KEY `goods_id` (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1051 DEFAULT CHARSET=utf8mb4 COMMENT=' Order list ';



/** * Order cancellation * @param $userId * @param $orderId * @param string $role Support :user/admin/system * @return bool * @throws BusinessException * @throws Throwable */
private function cancel($userId, $orderId, $role = 'user')
{
// Through users id And orderid Get order id
$order = $this->getOrderByUserIdAndId($userId, $orderId);
// If the order is empty, there is a problem
if (is_null($order)) {
$this->throwBadArgumentValue();
}
// if ($order->order_status != OrderEnums::ORDER_INVALID_OPERATION, ' The order cannot be cancelled ');
// }
if (!$order->canCancelHandle()) {
$this->throwBusinessException(CodeResponse::ORDER_INVALID_OPERATION, ' The order cannot be cancelled ');
}
}

private function cancel($userId, $orderId, $role = 'user')
{
// Through users id And orderid Get order id
$order = $this->getOrderByUserIdAndId($userId, $orderId);
// If the order is empty, there is a problem
if (is_null($order)) {
$this->throwBadArgumentValue();
}
if (!$order->canCancelHandle()) {
$this->throwBusinessException(CodeResponse::ORDER_INVALID_OPERATION, ' The order cannot be cancelled ');
}
$order->order_status = OrderEnums::StATUS_CANCEL;
}
private function cancel($userId, $orderId, $role = 'user')
{
// Through users id And orderid Get order id
$order = $this->getOrderByUserIdAndId($userId, $orderId);
// If the order is empty, there is a problem
if (is_null($order)) {
$this->throwBadArgumentValue();
}
if (!$order->canCancelHandle()) {
$this->throwBusinessException(CodeResponse::ORDER_INVALID_OPERATION, ' The order cannot be cancelled ');
}
switch ($role) {
case 'system':
$order->order_status = OrderEnums::STATUS_AUTO_CANCEL;
break;
case 'admin':
$order->order_status = OrderEnums::STATUS_ADMIN_CANCEL;
break;
default:
$order->order_status = OrderEnums::STATUS_CANCEL;
}
Order::query()->where('update_time',$order->update_time)->where('id',$order->id)
// The original state must be the created state
->where('order_status', OrderEnums::STATUS_cancel]);
->update([order_status] => orderEnums::STATUS_CANCEL]);
// Next, judge whether it is normal to save the update
// if (!$order->save()){
//throwBusinessException(CodeResponse::UPDATED_FAIL);
//}
}
cas encapsulate
/** * Optimistic lock update compare and save * @return int * @throws Throwable */
public function cas()
{
// Get the modified order status
$dirty = $this->getDirty();
// Get the current update time of the object
$updateAt = $this->getUpdateAtColumn(); //update_time $this->update_time
$query = self::query()->where($this->getKeyname(),$this->getKey())
->where($updateAt,$this->($updateAt));
foreach ($where as $key => $value) {
//getOriginsl You can get the value before it is modified
$query -> where($key, $this->getOriginsl($key))
}
return $query->update($dirty);
}
Optimized order cancellation interface
/** * Order cancellation * @param $userId * @param $orderId * @param string $role Support :user/admin/system * @return bool * @throws BusinessException * @throws Throwable */
private function cancel($userId, $orderId, $role = 'user')
{
// Through users id And orderid Get order id
$order = $this->getOrderByUserIdAndId($userId, $orderId);
// If the order is empty, there is a problem
if (is_null($order)) {
$this->throwBadArgumentValue();
}
if (!$order->canCancelHandle()) {
$this->throwBusinessException(CodeResponse::ORDER_INVALID_OPERATION, ' The order cannot be cancelled ');
}
switch ($role) {
case 'system':
$order->order_status = OrderEnums::STATUS_AUTO_CANCEL;
break;
case 'admin':
$order->order_status = OrderEnums::STATUS_ADMIN_CANCEL;
break;
default:
$order->order_status = OrderEnums::STATUS_CANCEL;
}
// end_time Forgot to update in the video
$order->end_time = now()->toDateTimeString();
if ($order->cas() == 0) {
$this->throwBusinessException(CodeResponse::UPDATED_FAIL);
}
// Add inventory low configuration version
// Get order list
$orderGoods = $this->getOrderGoodsList($orderId);
foreach ($orderGoods as $goods) {
$row = GoodsServices::getInstance()->addStock($goods->product_id, $goods->number);
if ($row == 0) {
$this->throwBusinessException(CodeResponse::UPDATED_FAIL);
}
}
return true;
}
Add inventory not optimized version
public function reduceStock($productId, $num)
{
return GoodsProduct::query()->where('id', $productId)->where('product_id', $productId)
->increment('number', $num);
}
Add inventory optimization version
public function addStock($productId, $num)
{
$product = $this->getGoodsProductById($productId);
$product->number = $product->number + $num;
return $product->cas();
}
// Here is a reminder 



Create email and SMS notifications 







边栏推荐
- MySQL导致索引失效的几种情况
- 3D打印和激光切割流程的初步了解
- MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。
- 抖音支付十万级 TPS 流量发券实践
- What is an immunohistochemical experiment? Immunohistochemical experiment
- PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
- Generate post order traversal according to pre order traversal and mid order traversal
- iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
- 详解四元数
- "Shanda Diwei Cup" the 12th Shandong ICPC undergraduate program design competition
猜你喜欢

Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon

百万消息量IM系统技术要点分享

STM32-------定时器

STM32-------外部中断

Desai wisdom number - histogram (basic histogram): the way to celebrate father's day in 2022

ASM文件系统 数据如何写和读数据

浩哥的博客之路

CTF go topic recurrence

Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad

谈谈数字化转型晓知识
随机推荐
Preliminary understanding of 3D printing and laser cutting process
C# Winform 自定义进度条ProgressBar
Fabric.js 手动加粗文本iText
Summary of some indicators for evaluating and selecting the best learning model
腾讯会议号设计的几种猜测
根据先序遍历和中序遍历生成后序遍历
Kotlin coroutine asynchronous flow
Bitmap load memory analysis
[observation] Dell technology + Intel aoteng Technology: leading storage innovation with "nanosecond speed"
iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
AndroidKotlin全面详细类使用语法学习指南
Telecommuting: how to become a master of time management| Community essay solicitation
Giants end up "setting up stalls" and big stalls fall into "bitter battle"
6、STM32——串口数据收发基础
The national post office and other three departments: strengthen the security management of personal information related to postal express delivery, and promote the de identification technology of per
Come on, touch and write a hook
STM32-------外部中断
STM32-------定时器
Can postman be integrated into Ci and CD pipelines for automated interface testing?
微信视频号如何用 PC 电脑做直播?