diff --git a/src/db/modify.sql b/src/db/modify.sql index 79ba69618..7958b57ca 100644 --- a/src/db/modify.sql +++ b/src/db/modify.sql @@ -181,3 +181,7 @@ CREATE TABLE `financial_agent_commission_detail` ( `org_net_charge` decimal(12,2) DEFAULT NULL COMMENT '净值', PRIMARY KEY (`detail_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC; + + +alter table financial_partner_commission add column commission_type smallint(1) DEFAULT 1 COMMENT '提成类型 1:渠道计算法 2:总交易额比例 3:收益比例'; +alter table financial_partner_commission_detail add column commission_type smallint(1) DEFAULT 1 COMMENT '提成类型 1:渠道计算法 2:总交易额比例 3:收益比例'; \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/CityPartnerCommissionAnalysis.java b/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/CityPartnerCommissionAnalysis.java index 932b55ec3..28853a182 100644 --- a/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/CityPartnerCommissionAnalysis.java +++ b/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/CityPartnerCommissionAnalysis.java @@ -1,6 +1,8 @@ package au.com.royalpay.payment.manage.citypartner.beans; import au.com.royalpay.payment.tools.CommonConsts; +import au.com.royalpay.payment.tools.exceptions.ServerErrorException; + import com.alibaba.fastjson.JSONObject; import java.math.BigDecimal; @@ -18,6 +20,7 @@ public class CityPartnerCommissionAnalysis { private BigDecimal jdChargeRate; private BigDecimal alipayonlineChargeRate; private BigDecimal rate; + private BigDecimal orgRate; private String channel; private Map> clientAnalysis = new HashMap<>(); private BigDecimal totalGrossAmount = BigDecimal.ZERO; @@ -26,7 +29,8 @@ public class CityPartnerCommissionAnalysis { private BigDecimal totalOrgChargeSum = BigDecimal.ZERO; private BigDecimal totalnetCharge = BigDecimal.ZERO; - public CityPartnerCommissionAnalysis(int orgId, int year, int month, BigDecimal rate,String channel,BigDecimal alipayChargeRate,BigDecimal wechatChargeRate,BigDecimal jdChargeRate,BigDecimal alipayonlineChargeRate) { + public CityPartnerCommissionAnalysis(int orgId, int year, int month, BigDecimal rate, String channel, BigDecimal alipayChargeRate, + BigDecimal wechatChargeRate, BigDecimal jdChargeRate, BigDecimal alipayonlineChargeRate, BigDecimal orgRate) { this.orgId = orgId; this.year = year; this.month = month; @@ -36,9 +40,22 @@ public class CityPartnerCommissionAnalysis { this.wechatChargeRate = wechatChargeRate; this.jdChargeRate = jdChargeRate; this.alipayonlineChargeRate = alipayonlineChargeRate; + this.orgRate = orgRate; } - public CityPartnerCommissionAnalysis attachAnalysis(JSONObject dayAnalysis,String channel) { + public CityPartnerCommissionAnalysis calculator(JSONObject dayAnalysis, String channel, int type) { + switch (type) { + case 1: + return attachAnalysis(dayAnalysis, channel); + case 3: + return attachNetCommissionAnalysis(dayAnalysis, channel); + default: + throw new ServerErrorException("unkonw calculate type"); + } + + } + + public CityPartnerCommissionAnalysis attachAnalysis(JSONObject dayAnalysis, String channel) { int clientId = dayAnalysis.getIntValue("client_id"); List clientTrades = clientAnalysis.get(clientId); if (clientTrades == null) { @@ -47,31 +64,13 @@ public class CityPartnerCommissionAnalysis { } Date tradeDate = dayAnalysis.getDate("trade_date"); BigDecimal total = dayAnalysis.getBigDecimal("total"); - BigDecimal dayRate = dayAnalysis.getBigDecimal(channel+"_rate_value"); + BigDecimal dayRate = dayAnalysis.getBigDecimal(channel + "_rate_value"); BigDecimal dayCharge = total.multiply(dayRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); BigDecimal royalpayCharge = total.multiply(rate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); - BigDecimal thirdPartyPaymentCharge = BigDecimal.ZERO; - switch (channel){ - case "alipay": - thirdPartyPaymentCharge = total.multiply(alipayChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); - break; - case "wechat": - thirdPartyPaymentCharge = total.multiply(wechatChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); - break; - case "jd": - thirdPartyPaymentCharge = total.multiply(jdChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); - break; - case "alipayonline": - thirdPartyPaymentCharge = total.multiply(alipayonlineChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); - break; - default: - break; - } - - BigDecimal netCharge = royalpayCharge.subtract(thirdPartyPaymentCharge); - + BigDecimal thirdPartyPaymentCharge = getThirdPartyCharge(channel, total); + BigDecimal netCharge = royalpayCharge.subtract(thirdPartyPaymentCharge); BigDecimal orgCharge = dayCharge.subtract(royalpayCharge); @@ -113,8 +112,8 @@ public class CityPartnerCommissionAnalysis { item.put("total_charge", dayCharge); item.put("royalpay_charge", royalpayCharge); item.put("org_charge", orgCharge); - item.put("channel",channel); - item.put("net_charge",netCharge); + item.put("channel", channel); + item.put("net_charge", netCharge); clientTrades.add(item); return this; } @@ -130,8 +129,8 @@ public class CityPartnerCommissionAnalysis { result.put("total_charge", totalChargeSum); result.put("royalpay_charge", totalRoyalPayCharge); result.put("org_charge", totalOrgChargeSum); - result.put("net_charge",totalnetCharge); - result.put("channel",channel); + result.put("net_charge", totalnetCharge); + result.put("channel", channel); return result; } @@ -147,4 +146,87 @@ public class CityPartnerCommissionAnalysis { } return list; } + + public CityPartnerCommissionAnalysis attachNetCommissionAnalysis(JSONObject dayAnalysis, String channel) { + int clientId = dayAnalysis.getIntValue("client_id"); + List clientTrades = clientAnalysis.get(clientId); + if (clientTrades == null) { + clientTrades = new ArrayList<>(); + clientAnalysis.put(clientId, clientTrades); + } + Date tradeDate = dayAnalysis.getDate("trade_date"); + BigDecimal total = dayAnalysis.getBigDecimal("total"); + BigDecimal dayRate = dayAnalysis.getBigDecimal(channel + "_rate_value"); + BigDecimal dayCharge = total.multiply(dayRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + + BigDecimal royalpayCharge = total.multiply(rate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + + BigDecimal thirdPartyPaymentCharge = getThirdPartyCharge(channel, total); + BigDecimal netCharge = royalpayCharge.subtract(thirdPartyPaymentCharge); + BigDecimal orgCharge = netCharge.multiply(orgRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + totalGrossAmount = totalGrossAmount.add(total); + totalChargeSum = totalChargeSum.add(dayCharge); + totalRoyalPayCharge = totalRoyalPayCharge.add(royalpayCharge); + totalOrgChargeSum = totalOrgChargeSum.add(orgCharge); + totalnetCharge = totalnetCharge.add(netCharge); + if (!clientTrades.isEmpty()) { + JSONObject item = clientTrades.get(clientTrades.size() - 1); + BigDecimal rate = item.getBigDecimal("client_rate"); + if (rate.compareTo(dayRate) == 0) { + BigDecimal gross = item.getBigDecimal("gross_amount").add(total); + item.put("gross_amount", gross); + BigDecimal totalCharge = item.getBigDecimal("total_charge").add(dayCharge); + item.put("total_charge", totalCharge); + BigDecimal totalRoyalCharge = item.getBigDecimal("royalpay_charge").add(royalpayCharge); + item.put("royalpay_charge", totalRoyalCharge); + BigDecimal totalOrgCharge = item.getBigDecimal("org_charge").add(orgCharge); + item.put("org_charge", totalOrgCharge); + BigDecimal totalRoyalpayClearingCharge = item.getBigDecimal("net_charge").add(netCharge); + item.put("net_charge", totalRoyalpayClearingCharge); + Date from = item.getDate("date_from"); + Date to = item.getDate("date_to"); + from = from.before(tradeDate) ? from : tradeDate; + to = to.after(tradeDate) ? to : tradeDate; + item.put("date_from", from); + item.put("date_to", to); + return this; + } + } + JSONObject item = new JSONObject(); + item.put("year", year); + item.put("month", month); + item.put("date_from", tradeDate); + item.put("date_to", tradeDate); + item.put("client_rate", dayRate); + item.put("gross_amount", total); + item.put("total_charge", dayCharge); + item.put("royalpay_charge", royalpayCharge); + item.put("org_charge", orgCharge); + item.put("channel", channel); + item.put("net_charge", netCharge); + clientTrades.add(item); + return this; + } + + private BigDecimal getThirdPartyCharge(String channel, BigDecimal total) { + BigDecimal thirdPartyPaymentCharge = BigDecimal.ZERO; + switch (channel) { + case "alipay": + thirdPartyPaymentCharge = total.multiply(alipayChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + break; + case "wechat": + thirdPartyPaymentCharge = total.multiply(wechatChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + break; + case "jd": + thirdPartyPaymentCharge = total.multiply(jdChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + break; + case "alipayonline": + thirdPartyPaymentCharge = total.multiply(alipayonlineChargeRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); + break; + default: + break; + } + return thirdPartyPaymentCharge; + } + } diff --git a/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/ReferrerCommissionAnalysis.java b/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/ReferrerCommissionAnalysis.java index 8aa123851..f3c71e8b6 100644 --- a/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/ReferrerCommissionAnalysis.java +++ b/src/main/java/au/com/royalpay/payment/manage/citypartner/beans/ReferrerCommissionAnalysis.java @@ -36,7 +36,6 @@ public class ReferrerCommissionAnalysis { BigDecimal total = dayAnalysis.getBigDecimal("total"); BigDecimal orgCharge = total.multiply(referrerRate).divide(CommonConsts.HUNDRED, 2, BigDecimal.ROUND_HALF_UP); Date tradeDate = dayAnalysis.getDate("trade_date"); - BigDecimal dayRate = dayAnalysis.getBigDecimal("rate_value"); totalGrossAmount = totalGrossAmount.add(total); totalOrgChargeSum = totalOrgChargeSum.add(orgCharge); diff --git a/src/main/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImpl.java index 494ed175d..2c821a00e 100644 --- a/src/main/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImpl.java @@ -242,8 +242,7 @@ public class CityPartnerPrizeServiceImpl implements CityPartnerPrizeService { int year = monthCal.get(Calendar.YEAR); int month = monthCal.get(Calendar.MONTH) + 1; - financialPartnerCommissionDetailMapper.clearData(year, month); - financialPartnerCommissionMapper.clearData(year, month); + JSONObject sysConfig = sysConfigManager.getSysConfig(); BigDecimal alipayChargeRate = new BigDecimal("0.6"); BigDecimal wechatChargeRate = new BigDecimal("0.6"); @@ -270,26 +269,31 @@ public class CityPartnerPrizeServiceImpl implements CityPartnerPrizeService { List transactionAnalysis = transactionMapper.listTransactionsForCityPartnerCommission(year, month); Map results = new HashMap<>(); for (JSONObject analysisDay : transactionAnalysis) { - String key = analysisDay.getString("channel"); - String channel = analysisDay.getString("channel"); - if (!channelMap.containsKey(key)) { - continue; - } int orgId = analysisDay.getIntValue("org_id"); - JSONObject org = orgMapper.findOne(orgId); if (org == null) { // shall never happen throw new ServerErrorException("Organization Id not exists:" + orgId); } + financialPartnerCommissionDetailMapper.clearData(year, month,org.getIntValue("commission_type")); + financialPartnerCommissionMapper.clearData(year, month,org.getIntValue("commission_type")); + + + String key = analysisDay.getString("channel"); + String channel = analysisDay.getString("channel"); + if (!channelMap.containsKey(key)) { + continue; + } + + channel = channel.toLowerCase(); CityPartnerCommissionAnalysis orgAnalysis = results.get(orgId + channel); if (orgAnalysis == null) { orgAnalysis = new CityPartnerCommissionAnalysis(orgId, year, month, org.getBigDecimal(channel + "_rate_value"), key, alipayChargeRate, - wechatChargeRate, jdChargeRate, alipayonlineChargeRate); + wechatChargeRate, jdChargeRate, alipayonlineChargeRate,org.getBigDecimal("rate_value")); results.put(orgId + channel, orgAnalysis); } - orgAnalysis.attachAnalysis(analysisDay, channel); + orgAnalysis.calculator(analysisDay, channel,org.getIntValue("commission_type")); } for (CityPartnerCommissionAnalysis commission : results.values()) { diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionDetailMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionDetailMapper.java index 7dece311e..3efb8de35 100644 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionDetailMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionDetailMapper.java @@ -14,7 +14,7 @@ import java.util.List; @AutoMapper(tablename = "financial_partner_commission_detail", pkName = "detail_id") public interface FinancialPartnerCommissionDetailMapper { @AutoSql(type = SqlType.DELETE) - void clearData(@Param("year") int year, @Param("month") int month); + void clearData(@Param("year") int year, @Param("month") int month,@Param("commission_type") int commission_type); @AutoSql(type = SqlType.INSERT) void save(JSONObject detail); diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionMapper.java index 720b8b371..cd44243f5 100644 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/financial/FinancialPartnerCommissionMapper.java @@ -14,7 +14,7 @@ import java.util.List; @AutoMapper(tablename = "financial_partner_commission", pkName = "record_id") public interface FinancialPartnerCommissionMapper { @AutoSql(type = SqlType.DELETE) - void clearData(@Param("year") int year, @Param("month") int month); + void clearData(@Param("year") int year, @Param("month") int month,@Param("commission_type") int commission_type); @AutoSql(type = SqlType.INSERT) void save(JSONObject commissionAnalysis); diff --git a/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImplTest.java b/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImplTest.java index 67dce5ea6..7851d03dc 100644 --- a/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImplTest.java +++ b/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/CityPartnerPrizeServiceImplTest.java @@ -17,6 +17,7 @@ import javax.annotation.Resource; @ActiveProfiles({"local","alipay","wechat","jd","bestpay"}) @RunWith(SpringRunner.class) public class CityPartnerPrizeServiceImplTest { + @Resource private CityPartnerPrizeService cityPartnerPrizeService; @Test @@ -24,4 +25,9 @@ public class CityPartnerPrizeServiceImplTest { cityPartnerPrizeService.generateAgent("2017-06",9); } + @Test + public void generate() throws Exception { + cityPartnerPrizeService.generate("2017-06"); + } + } \ No newline at end of file