根据多个字段分组,并对多个字段分别求和


实体类

package com.jxz.entity.cost;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jxz.entity.BaseModel;
import com.jxz.entity.institute.Institute;
import com.jxz.entity.merchant.Merchant;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * <p>
 * 用户成本核算表
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2022-08-23
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class MerchantCost {

    /**
     * id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 通道id
     */
    private Integer instituteId;

    /**
     * 商户id
     */
    private Integer merchantId;

    /**
     * 商户类别 电商 小贷等
     */
    private Integer merchantIndustry;

    /**
     * 交易日期 以交易完成时间为主
     */
    private Date transactionDate;

    /**
     * 支付类型 1支付 2代付
     */
    private Integer payType;

    /**
     * 产品类型
     */
    private Integer productType;

    /**
     * 交易笔数
     */
    private Integer transactionNum;

    /**
     * 交易金额
     */
    private Long transactionAmount;

    /**
     * 商户支付服务费 不含pending
     */
    private Long fee;

    /**
     * 通道手续费
     */
    private Long instituteFee;

    /**
     * 通道手续费
     */
    private Long agentCommission;

    /**
     * 状态 1 成功 2 pending
     */
    @TableField(exist = false)
    private Integer status;

    /**
     * 通道
     */
    @TableField(exist = false)
    private Institute institute;

    @TableField(exist = false)
    private Merchant merchant;

    public static MerchantCost defaultSummary() {
        MerchantCost merchantCost = new MerchantCost();
        merchantCost.setTransactionNum(0);
        merchantCost.setTransactionAmount(0L);
        merchantCost.setFee(0L);
        merchantCost.setInstituteFee(0L);
        return merchantCost;
    }

    @Data
    public static class MerchantCostAgentSummary {
        /**
         * 通道id
         */
        private Integer instituteId;

        /**
         * 产品类型
         */
        private Integer productType;
    }

    @Data
    public static class SumData {
        /**
         * 交易笔数
         */
        private Integer totalTransactionNum;

        /**
         * 交易金额
         */
        private Long totalTransactionAmount;

        /**
         * 商户支付服务费 不含pending
         */
        private Long totalFee;

        /**
         * 通道手续费
         */
        private Long totalInstituteFee;

        /**
         * 通道手续费
         */
        private Long totalAgentCommission;
    }
}
根据 instituteId、productType 2个字段分组,
并对 transactionNum、transactionAmount、fee、instituteFee、agentCommission 5个字段分别求和
List<MerchantCost> costList = new new ArrayList<>();

// 根据 instituteId、productType 2个字段分组,并对 transactionNum、transactionAmount、fee、instituteFee、agentCommission 5个字段分别求和
            Map<MerchantCost.MerchantCostAgentSummary, MerchantCost.SumData> collect = merchantCosts.stream()
                    .collect(Collectors.groupingBy(this::getMerchantCostAgentSummary, Collectors.collectingAndThen(Collectors.toList(), m -> {
                        Integer totalTransactionNum = m.stream().mapToInt(MerchantCost::getTransactionNum).sum();
                        Long totalTransactionAmount = m.stream().mapToLong(MerchantCost::getTransactionAmount).sum();
                        Long totalFee = m.stream().mapToLong(MerchantCost::getFee).sum();
                        Long totalInstituteFee = m.stream().mapToLong(MerchantCost::getInstituteFee).sum();
                        Long totalAgentCommission = m.stream().mapToLong(MerchantCost::getAgentCommission).sum();

                        MerchantCost.SumData sumData = new MerchantCost.SumData();
                        sumData.setTotalTransactionNum(totalTransactionNum);
                        sumData.setTotalTransactionAmount(totalTransactionAmount);
                        sumData.setTotalFee(totalFee);
                        sumData.setTotalInstituteFee(totalInstituteFee);
                        sumData.setTotalAgentCommission(totalAgentCommission);
                        return sumData;
                    })));

            // 将代理的汇总数据存入成本核算表中
            collect.forEach((k, v) -> {
                MerchantCost merchantCost = new MerchantCost();

                merchantCost.setInstituteId(k.getInstituteId());
                merchantCost.setProductType(k.getProductType());
                merchantCost.setMerchantId(agent.getId());
                merchantCost.setTransactionDate(date);
                merchantCost.setPayType(1);
                merchantCost.setTransactionNum(v.getTotalTransactionNum());
                merchantCost.setTransactionAmount(v.getTotalTransactionAmount());
                merchantCost.setFee(v.getTotalFee());
                merchantCost.setInstituteFee(v.getTotalInstituteFee());
                merchantCost.setAgentCommission(v.getTotalAgentCommission());

                costList.add(merchantCost);
            });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值