当前位置:网站首页>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 







边栏推荐
- Bitmap加载内存分析
- List中subList的add造成的死循环
- 在OpenCloudOS使用snap安装.NET 6
- 国内外最好的12款项目管理系统优劣势分析
- Which securities dealers recommend? Is online account opening safe?
- Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
- PHP的curl功能扩展基本用法
- 【HackTheBox】 meow
- 谈谈数字化转型晓知识
- Data interpretation! Ideal L9 sprints to "sell more than 10000 yuan a month" to grab share from BBA
猜你喜欢

STM32 ------ external interrupt

Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)

评估和选择最佳学习模型的一些指标总结

How to index websites in Google
云原生流水线工具汇总

Face and lining of fresh food pre storage

Short video enters the hinterland of online music

企业网站的制作流程是什么?设计和制作一个网站需要多长时间?

The Sandbox 归属周来啦!

【设计】1359- Umi3 如何实现插件化架构
随机推荐
短视频挺进在线音乐腹地
Telecommuting: how to become a master of time management| Community essay solicitation
Ambire Guide: the arbitrum odyssey begins! Week 1 - Cross Chain Bridge
巨头下场“摆摊”,大排档陷入“苦战”
图像分割-数据标注
MySQL transaction isolation
2022云顾问技术系列之存储&CDN专场分享会
Is Everbright futures safe? What do I need to open an account?
Flutter中的GetX状态管理用起来真的那么香吗?
Kotlin 协程 异步 异步流
Fabric.js 手动加粗文本iText
Bitmap load memory analysis
Several cases of index invalidation caused by MySQL
[observation] Dell technology + Intel aoteng Technology: leading storage innovation with "nanosecond speed"
7、STM32——LCD
The fortress machine installs pytorch, mmcv, and mmclassification, and trains its own data sets
iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
[js] 去除小数点后面多余的零
AndroidKotlin全面详细类使用语法学习指南
List中subList的add造成的死循环