当前位置:网站首页>Task queue of laravel

Task queue of laravel

2022-06-23 23:37:00 Ximen eats snow

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
Insert picture description here
 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
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
// 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 ';

 Insert picture description here
 Insert picture description here
 Insert picture description here

  /** *  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 ');
        }
        


    }

 Insert picture description here

    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
 Insert picture description here

 Insert picture description here
 Insert picture description here
 Insert picture description here
Create email and SMS notifications
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Ximen eats snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206232014034878.html