当前位置:网站首页>Three simple steps to quickly complete order data processing through workflow (ASW)

Three simple steps to quickly complete order data processing through workflow (ASW)

2022-06-24 17:25:00 Tencent cloud workflow

How does this article describe workflow ASW Edit and arrange cloud function , Quickly complete the order data processing .

working principle

  1. Workflow calls function to get order data in a certain period of time , Preprocess the data .
  2. Give the preprocessed data to Map Iterative task processing : After data processing of each order , Write to different database tables , Or draw a chart to show .

Operation steps

Creating a workflow requires first creating a state machine , By choreographing the different components of the state machine , Change the state machine structure , So as to achieve user-defined function set .

It's a simple three-step process : Create cloud functions → Create workflow → Running state machine

Step 1: Create cloud functions

establish GetOrder function

  1. Sign in Cloud function console , Click... On the left navigation bar 【 Function service 】.
  2. Select Guangzhou in the function service area above the main interface , And click 【 newly build 】, Enter the function creation process .
  3. On the new function page , Fill in the following information in the basic information :
  4. The name of the function :GetOrder.
  5. Running environment :Nodejs10.15.
  6. How it was created : Select the blank function , single click 【 next step 】 Go to function configuration .
  7. In the function configuration page Cloud Studio In the pane , Delete the original code , Copy the code shown below :
'use strict';
   exports.main_handler = async (event, context) => {
       console.log("this is get order function");
       #  You can api Request for real order data , The data in the example is convenient to simulate workflow execution 
          var orderlist = [
           {
               "orderId":"202012200001",
               "goodsId":"1004",
               "goodsName":" a mandarin orange #1004",
               "unit":" Pieces of ",
               "specific":"5 One kilo a box ",
               "linePrice":100,
               "salePrice":90,
               "costPrice":80,
               "number":30,
            "isVoucher":1,
            "voucherPrice":2,
            "voucherId":"3dr55678hj",
            "isDiscount":1,
            "discountPrice":3,
            "carriage": 8,
            "receiver":"susu",
            "phone":"18633567898",
            "address":" Tencent building, Nanshan District, Shenzhen 20 floor ",
            "createTime":"2020-12-20 10:00:00",
            "payTime":"2020-12-20 11:00:00",
            "payMethod":1, 
            "payOrder":"202012201100003940",
            "orderStatus":3,
            "deliveryTime":"2020-12-21 11:00:00",
            "finishTime":"2020-12-25 11:00:00",
            "deliveryOrder":"ZT12345789d786",
            "isReturn":1,
            "returnId":"2020122600012",
               "returnNumber":2,    
           },
         {
               "orderId":"202012200001",
               "goodsId":"2001",
               "goodsName":" pears #2001",
               "unit":" Pieces of ",
               "specific":"6 One kilo a box ",
               "linePrice":150,
               "salePrice":120,
               "costPrice":90,
               "number":20,
            "isVoucher":1,
            "voucherPrice":3,
            "voucherId":"3dr55678hj",
            "isDiscount":1,
            "discountPrice":5,
            "carriage": 0,
            "receiver":"susu",
            "phone":"18633567898",
            "address":" Tencent building, Nanshan District, Shenzhen 20 floor ",
            "createTime":"2020-12-20 10:00:00",
            "payTime":"2020-12-20 11:00:00",
            "payMethod":1, 
            "payOrder":"202012201100003940",
            "orderStatus":3,
            "deliveryTime":"2020-12-21 11:00:00",
            "finishTime":"2020-12-25 11:00:00",
            "deliveryOrder":"ZT12345789d786",
            "isReturn":0,
            "returnId":"",
               "returnNumber":0,        
           },
        {
               "orderId":"202012200001",
               "goodsId":"3005",
               "goodsName":" Banana #3005",
               "unit":" Pieces of ",
               "specific":"10 One kilo a box ",
               "linePrice":180,
               "salePrice":150,
               "costPrice":98,
               "number":6,
            "isVoucher":1,
            "voucherPrice":8,
            "voucherId":"3dr55678hj",
            "isDiscount":1,
            "discountPrice":20,
            "carriage": 0,
            "receiver":"susu",
            "phone":"18633567898",
            "address":" Tencent building, Nanshan District, Shenzhen 20 floor ",
            "createTime":"2020-12-20 10:00:00",
            "payTime":"2020-12-20 11:00:00",
            "payMethod":1, 
            "payOrder":"202012201100003940",
            "orderStatus":3,
            "deliveryTime":"2020-12-21 11:00:00",
            "finishTime":"2020-12-25 11:00:00",
            "deliveryOrder":"ZT12345789d786",
            "isReturn":1,
            "returnId":"2020122600013",
               "returnNumber":3,        
           }
       ];

       return {"orderList":orderlist};
   };
  1. single click 【 preservation 】, The cloud function is created successfully

establish ProcessOrder function

Reference resources 【 establish GetOrder function 】 The way , establish ProcessOrder function , The code is as follows :

'use strict';
exports.main_handler = async (event, context) => {
    console.log("this is processOrder function");
    var order = event;
    # Data processing 
    var income = order["salePrice"]-order["costPrice"];
    var goodsInfo = {"goodsId":order["goodId"],"goodsName":order["goodsName"],"number":order["number"]};
    var incomeInfo = {"goodsId":order["goodId"],"goodsName":order["goodsName"],"number":order["number"],"income":income};
   return {
        "goodsInfo":goodsInfo,
        "incomeInfo":incomeInfo,
        "salesInfo":salesInfo
    };
}

establish GoodsSold function

Reference resources 【 establish GetOrder function 】 The way , establish GoodsSold function , The code is as follows :

'use strict';
exports.main_handler = async (event, context) => {
    console.log("this is goodsSold function");
    // Some write to database or graph display operations 
    console.log(event);
    return "GoodsSold success";
};

establish Income function

Reference resources 【 establish GetOrder function 】 The way , establish Income function , The code is as follows :

'use strict';
exports.main_handler = async (event, context) => {
    console.log("this is income function");
    // Some write to database or graph display operations 
    console.log(event);
    return "Income success";
};

establish SalesReturn function

Reference resources 【 establish GetOrder function 】 The way , establish SalesReturn function , The code is as follows :

'use strict';
exports.main_handler = async (event, context) => {
    console.log("this is salesReturn function");
    // Some write to database or graph display operations 
    console.log(event);
    return "SalesReturn success";
};

Step 2: Create workflow

  1. Sign in Application and choreography service flow console .
  2. On the state machine page , single click 【 newly build 】, Go to the create workflow page , Choose to use 【 Code creation 】:
  1. Paste the following directly in the code edit box TCSL Code :
{
     "Comment": " The order processing ",
     "StartAt": "GetOrder",
     "States": {
       "GetOrder": {
         "Type": "Task",
         "Comment": " Pull data ",
         "Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/GetOrder",
         "Next": "MapProcess"
       },
       "MapProcess": {
         "Type": "Map",
         "ItemsPath": "$.orderList",
         "MaxConcurrency": 6,
         "Iterator": {
           "StartAt": "ProcessOrder",
           "States": {
             "ProcessOrder": {
               "Type": "Task",
               "Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/ProcessOrder",
               "Next": "ParallelDataProcess"
             },
             "ParallelDataProcess": {
               "Type": "Parallel",
               "End": true,
               "Branches": [
                 {
                   "StartAt": "GoodsSold",
                   "States": {
                     "GoodsSold": {
                       "InputPath": "$.goodsInfo",
                       "Type": "Task",
                       "Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/GoodsSold",
                       "End": true
                     }
                   }
                 },
                 {
                   "StartAt": "Income",
                   "States": {
                     "Income": {
                       "InputPath": "$.incomeInfo",
                       "Type": "Task",
                       "Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/Income",
                       "End": true
                     }
                   }
                 },
                 {
                   "StartAt": "SalesReturn",
                   "States": {
                     "SalesReturn": {
                       "InputPath": "$.salesInfo",
                       "Type": "Task",
                       "Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/SalesReturn",
                       "End": true
                     }
                   }
                 }
               ]
             }
           }
         },
         "End": true
       }
     }
   }
  1. Click in the upper right corner 【 next step 】, Enter the save interface , Enter the name of the state machine , Run role selection 【 New character 】, Type selection 【 Standard state machine 】, Click in the upper right corner 【 complete 】, You can see the created state machine on the state machine list page .

To use an existing role, you need to create a role first , And empower the role with relevant policies , Please refer to Running roles .

Step 3: Running state machine

After the state machine is created , You can view the created state machine on the main page after login .

  1. Click... Of the state machine you want to run 【 name 】, Enter the state machine .
  1. You can see the basic information of the state machine in the interface . single click 【 Workflow execution 】 Under the 【 Start execution 】 .
  1. In the pop-up “ Input ” Window , With JSON Format the input required by the state machine . for example :
{"comment": "invoke workflow"}
  1. single click 【 determine 】, After completing the state execution , You can view the execution results on the details page :
  1. Slide to the bottom of the page , stay 【 Execution history 】 Under entry , You can view the operation of the child node .

The above steps introduce the basic workflow of an order data processing scenario .

In a real business scenario, every Task Nodes will be involved in configuring some relevant parameter information , For example, do parameter transfer 、 Exception retrial and error capture processing, etc , For more details, please refer to State machine language .

Apply for probation ASW

ASW At present, it is in the public beta stage , The public beta stage is free of charge . Welcome to suggest product improvements , After the feedback is adopted, you can get Tencent Mengxin short goose doll !

immediately Apply for the public test , We're going to be 3 Complete the approval within working days , And inform you through SMS and on-site letters , Thank you for your support .

原网站

版权声明
本文为[Tencent cloud workflow]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/03/20210319175837601R.html