将多行数据基于列的数值映射为一个对象。

huangapple go评论138阅读模式
英文:

Map multiple rows to one object based on column value

问题

在我的系统中,我有一个user_transactions表,其中包含id、transaction_type、transaction_date、linked_transaction列。在每个班次开始时,系统会在数据库中创建一个名为START_SHIFT的交易记录,而在结束时会创建一个名为END_SHIFT的新交易记录,其中在linked_transaction列中会包含对应START_SHIFT交易记录的id。现在,我需要提取所有的START_SHIFT和END_SHIFT交易记录,并将它们映射到一个会话(session)对象,该对象具有以下字段:id、startDate、endDate。其中,id应为START_SHIFT交易记录的id,startDate为START_SHIFT交易记录的日期,而endDate为END_SHIFT交易记录的日期。

目前,我从数据库中获取这两种类型的交易记录作为交易列表,我为每个START_SHIFT交易记录创建一个对象,并将它们添加到列表中,但我不知道如何在这些对象中设置endDate。

List<ats.wallet.model.Transaction> transactions = walletDao.getTransactions(query, parameters, 0, 100, true);
List<TerminalSession> userSessions = new ArrayList<>();

transactions.stream().filter(t -> t.getType().equals(TransactionType.START_SHIFT)).forEach(t -> {
    UserSession userSession = new UserSession();
    userSession.setId(Long.getLong(t.getId()));
    userSession.setStartDate(t.getTransactionDate());
    userSessions.add(userSession);
});
英文:

In my system I have user_transactions table, it contains id, transaction_type, transaction_date, linked_transaction columns. At the beginning of a shift, the system creates transaction START_SHIFT in db, and at the end a new transaction called END_SHIFT with START_SHIFT transaction id in the linked_transaciton column. Right now I need to take out all start_shift and end_shift transactions and map them to session object with fields: id, startDate, endDate id should be start_shift transaction id, startDate is START_SHIFT transaction date and endDate is END_SHIFT transaction date.
For now, I take these two types of transactions from db as a list of transactions, I create an object for every START_SHIFT transaction and I add them to the list but I don't know how to set endDate in these objects

        List&lt;TerminalSession&gt; userSessions = new ArrayList&lt;&gt;();

        transactions.stream().filter(t -&gt; t.getType().equals(TransactionType.START_SHIFT)).forEach(t -&gt; {
            UserSession userSession = new UserSession();
            userSession.setId(Long.getLong(t.getId()));
            userSession.setStartDate(t.getTransactionDate());
            userSessions.add(userSession);
        }); ```

</details>


# 答案1
**得分**: 1

因为是END_SHIFT交易与其对应的START_SHIFT交易相连接,所以您可以从它们开始创建会话对象。

要从END_SHIFT链接的交易ID中检索START_SHIFT交易,您可以准备一个以ID为键的映射:

```java
Map<Long, Transaction> map = transactions.stream()
        .collect(Collectors.toMap(Transaction::getId, Function.identity()));

transactions.stream()
    // 使用END_SHIFT而不是START_SHIFT
    .filter(t -> t.getType().equals(TransactionType.END_SHIFT))  
    .forEach(t -> {
        UserSession userSession = new UserSession();

        // 从映射中获取链接的START_SHIFT的ID和开始日期
        Transaction startShift = map.get(t.getLinkedTransaction());
        userSession.setId(startShift.getId());
        userSession.setStartDate(startShift.getTransactionDate());

        userSession.setEndDate(t.getTransactionDate());
        userSessions.add(userSession);
    });
英文:

Since it's the END_SHIFT transaction that has the link to its corresponding START_SHIFT transaction, you probably could start from them to create the session objects.

To retrieve the START_SHIFT transaction from the END_SHIFT linked transaction id you could prepare a map with the id as the key:

Map&lt;Long, Transaction&gt; map = transactions.stream()
            .collect(Collectors.toMap(Transaction::getId, Function.identity()));

transactions.stream()
    // END_SHIFT instead of START_SHIFT
    .filter(t -&gt; t.getType().equals(TransactionType.END_SHIFT))  
    .forEach(t -&gt; {
        UserSession userSession = new UserSession();

        // get id and start date from the linked START_SHIFT in the map
        Transaction startShift = map.get(t.getLinkedTransaction());
        userSession.setId(startShift.getId());
        userSession.setStartDate(startShift.getTransactionDate());

        userSession.setEndDate(t.getTransactionDate());
        userSessions.add(userSession);
    });

huangapple
  • 本文由 发表于 2020年9月18日 05:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63946503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定