如何更新交易处理器功能内的关系值?

时间:2019-06-01 10:19:11

标签: hyperledger-fabric hyperledger hyperledger-composer

我有一个模型文件(org.acme.interm.container.cto),看起来像这样:

        namespace org.acme.interm.container

        asset Container identified by containerId {

          o   String            containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
          o   String            containerNumber
          --> Truck        truck  optional

        }

        asset Truck identified by truckId {
          o String      truckId

          o Ownership   ownershipType default="LEASED"

          o Integer     normalWeight      range = [,100]
          o Integer     fragileWeight   range = [,50]

        }

        enum Ownership {
          o   LEASED
          o   OWNED
        }

    transaction AssignTruck {
      o   String    containerId
      o   String    truckId
    }

event TruckAssigned {
  o   String    containerId
  o   String    truckId
}

transaction LoadContainer {
o String containerId
--> Truck    truck
o Integer fragileWeight
o Integer normalWeight

}

在以下交易中,我将 Truck 关系分配给集装箱资产:

function    AssignTruck(containerTruckData){  
    console.log("ASSIGN TRUCK CALLED!");
    var containerRegistry={}
    return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
        containerRegistry = registry
        return containerRegistry.get(containerTruckData.containerId);
    }).then(function(container){
      console.log("Got Container",container);
        if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
        var   factory = getFactory();
        var   relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
        container.truck = relationship;

        return containerRegistry.update(container);
    }).then(function(){
        // Successful update
        var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
        event.containerId = containerTruckData.containerId;
        event.truckId = containerTruckData.truckId;
        emit(event);
    }).catch(function(error){
        throw new Error(error);
    });

}

现在,我该如何编写loadContainer事务,在该事务中,应将容器(对于给定containerId)的normalWeight和fragileWeight属性的现有值添加到新传递的fragileWeight和normalWeight中 值?

1 个答案:

答案 0 :(得分:1)

首先,除非每个容器都有一个重量变量,否则不可能跟踪单个重量。即使您不断更新卡车的重量,您也将知道集装箱的总重量还不够。特别是如果您要卸载容器。我建议这种方法

asset Container identified by containerId {

  o String containerId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
  o String containerNumber
  o Integer normalWeight range = [,100]
  o Integer fragileWeight range = [,50]    
  --> Truck truck  optional
}

asset Truck identified by truckId {
  o String truckId          
  o Ownership ownershipType default="LEASED"
  o Integer totalNormalWeight range default=0
  o Integer totalFragileWeight range default=0
  --> Container containersLoaded[] optional
}

因此,这将使您可以跟踪每个集装箱的重量,然后将它们相加以获得卡车的总重量。 containersLoaded变量可让您跟踪加载的容器。这样做的好处是,如果卸载或拆分容器(我认为这将是用户流程的一部分),则可以准确地减轻重量

function AssignTruck(containerTruckData) {
   var myContainer; // store data from the promise
   var myTruck;
   console.log("ASSIGN TRUCK CALLED!");
   var containerRegistry={}
   var truckRegistry;
   return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
            containerRegistry = registry
            return containerRegistry.get(containerTruckData.containerId);
        }).then(function(container){
            console.log("Got Container",container);
            myContainer = container;
            if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
            var factory = getFactory();
            var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
            container.truck = relationship;
            return containerRegistry.update(container);
        }).then(function() {
          return getAssetRegistry('org.acme.interm.truck.Truck');
        }).then(function (truckRegistry) {
          truckRegistry = truckRegistry;
          return truckRegistry.get(containerTruckData.truckId);
        }).then(function (truck) {
          truck.totalNormalWeight +=  myContainer.normalWeight;
          truck.totalFragileWeight += myContainer.fragileWeight;
          if (!truck.containersLoaded) {
            truck.containersLoaded = [];
          } else {
            truck.containersLoaded.push(factory.newRelationship('org.acme.interm.container', 'Container', myContainer.containerId))
          };
          return truckRegistry.update(truck)
        }).then(function(){
            // Successful update
            var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
            event.containerId = containerTruckData.containerId;
            event.truckId = containerTruckData.truckId;
            emit(event);
        }).catch(function(error){
            throw new Error(error);
        });
}

这应该为您工作。现在,您可以通过检查totalNormalWeighttotalFragileWeight变量来查询卡车并获得所有集装箱的总重量。您还可以检查containersLoaded数组以获取容器列表。要获取容器的单个重量,可以查询容器并获取其重量,也可以直接从containersLoaded变量获取容器的重量。 当需要从卡车上卸下/卸下集装箱时,您可以获取它们的重量并从总量中减去,然后从containersLoaded中弹出关系。