实体类
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);
});