在VaultCustomQuery中获取帐户名称

时间:2020-08-07 15:54:48

标签: corda

我正在尝试从Corda获取一些汇总数据,并且正在使用“帐户”模块。

我的状态:

    @BelongsToContract(InvoiceContractTk::class)
class SMInvoiceStateTk constructor(val amount: Amount<Currency>,
                                   val issuedDate : LocalDateTime,
                                   val amountPaid: Amount<IssuedTokenType>,
                                   val paidFrom : Party?,
                                   val seller: AnonymousParty,
                                   val buyer: AnonymousParty,
                                   val typeOfGoods : String,
                                   val quantity : Int,                                   
                                   val paidDate : LocalDateTime?,
                                   val nettAmountReceived: Amount<Currency>,
                                   override val linearId: UniqueIdentifier) : LinearState, QueryableState {
    /**
     * @constructor
     */
    constructor(amount: Amount<Currency>,
                issuedDate: LocalDateTime,
                amountPaid: Amount<IssuedTokenType>,
                paidFrom : Party? = amountPaid.token.issuer,
                seller: AnonymousParty,
                buyer: AnonymousParty,
                typeOfGoods: String,
                quantity: Int,
                paidDate: LocalDateTime?,
                linearId: UniqueIdentifier) :
            this(amount,
                    issuedDate,
                    amountPaid,
                    paidFrom,
                    seller,
                    buyer,
                    typeOfGoods,
                    quantity,
                    Amount(0, amount.token),
                    paidDate,
                    Amount(0, amount.token),
                    linearId
) {    }
    /**
     * Participants , seller, buyer
     */
    override val participants: List<AbstractParty> get() = listOf(seller, buyer).map { it }
    
        /**
     * Generates a Mapped Object of State
     *
     * @param schema
     */
    override fun generateMappedObject(schema: MappedSchema): PersistentState {
        return when (schema) {
            is SMInvoiceTkSchemaV1 -> SMInvoiceTkSchemaV1.PersistantSMTkInvoice(
                    this.linearId.toString(),
                    this.amount.toDecimal(),
                    this.issuedDate,
                    this.amountPaid.toDecimal(),
                    this.paidFrom.toString(),
                    this.seller,
                    this.buyer,
                    this.typeOfGoods,
                    this.quantity,
                    this.paidDate,
                    this.nettAmountReceived.toDecimal()
            )
            else -> throw IllegalArgumentException("Unrecognised schema $schema")
        }
    }

    /**
     * Gets the Supported schemas for this object
     */
    override fun supportedSchemas(): Iterable<MappedSchema> = listOf(SMInvoiceTkSchemaV1)
    

架构:

/**
 * The Database Schema to hold a record of an [SMInvoiceTkSchema]
 */
object SMInvoiceTkSchema

/**
 * Defines the fields for an [SMInvoiceTkSchemaV1]
 */
object SMInvoiceTkSchemaV1 : MappedSchema (
        schemaFamily = SMInvoiceTkSchema.javaClass,
        version = 1,
        mappedTypes = listOf(SMInvoiceTkSchemaV1.PersistantSMTkInvoice::class.java)) {
    @Entity
    @Table(name ="sm_tkinvoice_states")
    /**
     * Table SM_INVOICE_STATES
     *
     * @property invoiceId the id of the Invoice
     * @property amount the amount of the invoice
     * @property issuedDate the date the invoice was issued
     * @property amountPaid the amount has been paid
     * @property paidFrom the CBDC account from which theinvoice was paid
     * @property seller the seller of the goods on the invoice
     * @property buyer the buyer of the goods
     * @property typeOfGoods the type of goods purchased
     * @property quantity the quantity of goods purchased
     * @property paidDate   the date the invoice was paid
     * @property nettAmountReceived the amount received by the seller after paying the VAT
     */
    class PersistantSMTkInvoice(

            @Column(name="invoice_Id")
            var invoiceId: String,
            @Column(name="amount")
            var amount: BigDecimal,
            @Column(name="issuedDate")
            var issuedDate: LocalDateTime,
            @Column(name="amountPaid")
            var amountPaid: BigDecimal,
            @Column(name="paidFrom")
            var paidFrom: String,
            @Column(name="seller")
            var seller: AbstractParty?,
            @Column(name="buyer")
            var buyer: AbstractParty?,
            @Column(name="typeOfGoods")
            var typeOfGoods: String,
            @Column(name="quantity")
            var quantity: Int,
            @Column(name="paidDate")
            var paidDate: LocalDateTime?,
            @Column(name="nettAmountreceived")
            var nettAmountReceived: BigDecimal
    )
        : PersistentState() {
        /**
         * Default constructor required by hibernate.
         */
        constructor(): this(
                "",
                BigDecimal.ZERO,
                LocalDateTime.now(),
                BigDecimal.ZERO,
               "",
                null,
                null,
                "",
                0,
                null,
                BigDecimal.ZERO
        )
    }
}

对于开具发票和运行一些查询,这一切都很好。我现在想运行另外2个总计为:

的查询
SELECT buyer,typeOfGoods,Round(SUM(amountPaid),2)
From SM_TKINVOICE_STATES
Group by buyer,typeOfGoods

select typeOfGoods,seller,round(SUM(nettAmountReceived),2)
from SM_TKINVOICE_STATES
group by typeOfGoods,seller

强文本 买方将是帐户名称,例如爱丽丝(Alice),吉姆(Jim),弗雷德(Fred)和卖方MegaCorp,MiniCorp等。

我的问题是,我似乎只能按照持久化架构来聚合查询,对于买卖双方,它拥有CordaX500名称,即节点而不是帐户。无论我在uyer和Seller字段中输入什么内容,所有存储的都是节点(我相信这是设计使然)

我如何获得像以下这样的结果集:

 Alice   Adult Clothing         52.60
 Alice   Books                  34.10
 Alice   Children's Clothing    84.68
 Bob     Adult Clothing         31.16
 Bob     Alcohol                41.40
 George  Energy                 101.44
 Jim     Groceries              52.82
 Jim     Electrical             46.79
 Jim     Energy                 112.12
 Kathy   Adult Clothing         40.72
 Alice   Alcohol                38.45
 Alice   Children's car seat    40.15
 Alice   Electrical             41.93
 Alice   Groceries              56.30
 Alice   Energy                 62.68
 Anne    Adult Clothing         17.95
 Anne    Alcohol                10.79
 Anne    Books                  5.68
 Anne    Children's Clothing    57.98
 Anne    Children's car seat    31.42
 Anne    Electrical             60.98
 Anne    Groceries              64.68
 Anne    Energy                 37.31
 Bob     Books                  27.95
 Bob     Children's Clothing    41.92
 Bob     Children's car seat    45.07
 Bob     Electrical 59.23
...

 Adult Clothing             MegaCompany     234.81
 Books                      MediumCompany   230.45
 Children's Clothing        MediumCompany   291.78
 Alcohol                    MegaCompany     194.02
 Energy                     ElectricAndGas  573.24
 Groceries                  SmallBusiness   258.37
 Electrical                 SmallBusiness   242.74
 Children's car seat        MiniCompamy     248.37
 Electrical                 MiniCompamy     208.11
 Groceries                  SoleTrader      317.32
 Children's Clothing        SoleTrader      186.74

我的流程:

   @Suspendable
    override fun call(): List<Any> {

        val buyerNode = serviceHub.networkMapCache.getNodeByLegalName(CordaX500Name(organisation = "BuyerCWP",locality = "London",country = "GB"))!!.legalIdentities[0]
        val sellerNode = serviceHub.networkMapCache.getNodeByLegalName(CordaX500Name(organisation = "SellerCWP",locality = "Glasgow",country = "GB"))!!.legalIdentities[0]

        val buyerAccounts = subFlow(AccountsForHost(buyerNode))
        val sellerAccountsForHost =  subFlow(AccountsForHost(sellerNode))

        val buyerIds  = buyerAccounts.map {it.state.data.linearId.id}

        val criteria = QueryCriteria.VaultQueryCriteria(
                status = Vault.StateStatus.UNCONSUMED
        )

        val sum = builder {SMInvoiceTkSchemaV1.PersistantSMTkInvoice::amountPaid.sum(groupByColumns = listOf(SMInvoiceTkSchemaV1.PersistantSMTkInvoice::buyer  ,SMInvoiceTkSchemaV1.PersistantSMTkInvoice::typeOfGoods))}
        val sumCriteria = QueryCriteria.VaultCustomQueryCriteria(sum)

        val allStates = serviceHub.vaultService.queryBy(
                contractStateType = SMInvoiceStateTk::class.java,
                criteria = criteria
                        .and( sumCriteria)
                //**************  SOMETHING HERE I THINK TO MAP TO ACCOUNT NAMES ************************
        )

        val otherRes = allStates.otherResults
       return otherRes
    }

这给了我:

O=BuyerCWP, L=London, C=GB, Adult Clothing, 221.79,
O=BuyerCWP, L=London, C=GB, Alcohol, 213.40,
O=BuyerCWP, L=London, C=GB, Books, 526.20, 
O=BuyerCWP, L=London, C=GB, Children's Clothing, 261.40, 
O=BuyerCWP, L=London, C=GB, Children's car seat, 547.47, 
O=BuyerCWP, L=London, C=GB, Electrical, 541.72, 
O=BuyerCWP, L=London, C=GB, Energy, 561.94
...

Obvoiusly O = BuyerCWP,L =伦敦,C = GB应该是Alice,Jim,Bob等。

我需要怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用类似方法来获取某人的帐户列表

val myAccounts = accountService.ourAccounts().map { it.state.data.name }

然后在查询条件中使用它

QueryCriteria.VaultQueryCriteria(externalIds = listOf(myAccount.identifier.id))

尝试看看the samples-kotlin/Accounts/worldcupticketbooking上的工作流测试,我想您会找到您需要的很好的例子。