wangning 7 years ago
parent 0cbefe6558
commit f55329661c

@ -0,0 +1,155 @@
package au.com.royalpay.payment.manage.citypartner.beans;
import au.com.royalpay.payment.tools.CommonConsts;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by yixian on 2017-03-08.
*/
public class AgentCommissionAnalysis {
private int orgId;
private int year;
private int month;
private BigDecimal alipayChargeRate;
private BigDecimal wechatChargeRate;
private BigDecimal jdChargeRate;
private BigDecimal alipayonlineChargeRate;
private BigDecimal rate;
private String channel;
private Map<Integer, List<JSONObject>> clientAnalysis = new HashMap<>();
private BigDecimal totalGrossAmount = BigDecimal.ZERO;
private BigDecimal totalChargeSum = BigDecimal.ZERO;
private BigDecimal totalRoyalPayCharge = BigDecimal.ZERO;
private BigDecimal totalOrgChargeSum = BigDecimal.ZERO;
private BigDecimal totalnetCharge = BigDecimal.ZERO;
public AgentCommissionAnalysis(int orgId, int year, int month, BigDecimal rate, String channel, BigDecimal alipayChargeRate, BigDecimal wechatChargeRate, BigDecimal jdChargeRate, BigDecimal alipayonlineChargeRate) {
this.orgId = orgId;
this.year = year;
this.month = month;
this.rate = rate;
this.channel = channel;
this.alipayChargeRate = alipayChargeRate;
this.wechatChargeRate = wechatChargeRate;
this.jdChargeRate = jdChargeRate;
this.alipayonlineChargeRate = alipayonlineChargeRate;
}
public AgentCommissionAnalysis attachAnalysis(JSONObject dayAnalysis, String channel) {
int clientId = dayAnalysis.getIntValue("client_id");
List<JSONObject> 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 = 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 orgCharge = dayCharge.subtract(royalpayCharge);
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;
}
public JSONObject totalCommission() {
JSONObject result = new JSONObject();
result.put("org_id", orgId);
result.put("year", year);
result.put("month", month);
result.put("create_time", new Date());
result.put("org_rate", rate);
result.put("gross_amount", totalGrossAmount);
result.put("total_charge", totalChargeSum);
result.put("royalpay_charge", totalRoyalPayCharge);
result.put("org_charge", totalOrgChargeSum);
result.put("net_charge",totalnetCharge);
result.put("channel",channel);
return result;
}
public List<JSONObject> getCommissionDetails(String recordId) {
List<JSONObject> list = new ArrayList<>();
for (Map.Entry<Integer, List<JSONObject>> entry : clientAnalysis.entrySet()) {
int clientId = entry.getKey();
for (JSONObject item : entry.getValue()) {
item.put("client_id", clientId);
item.put("record_id", recordId);
list.add(item);
}
}
return list;
}
}

@ -21,6 +21,8 @@ public interface CityPartnerPrizeService {
void generateReferrer(String month); void generateReferrer(String month);
void generateAgent(String month);
List<JSONObject> listReferrerAvailableMonths(String year); List<JSONObject> listReferrerAvailableMonths(String year);
JSONObject getReferrerPrizeInfo(String monthStr); JSONObject getReferrerPrizeInfo(String monthStr);

@ -3,6 +3,8 @@ package au.com.royalpay.payment.manage.citypartner.core.impls;
import au.com.royalpay.payment.manage.citypartner.beans.CityPartnerCommissionAnalysis; import au.com.royalpay.payment.manage.citypartner.beans.CityPartnerCommissionAnalysis;
import au.com.royalpay.payment.manage.citypartner.beans.ReferrerCommissionAnalysis; import au.com.royalpay.payment.manage.citypartner.beans.ReferrerCommissionAnalysis;
import au.com.royalpay.payment.manage.citypartner.core.CityPartnerPrizeService; import au.com.royalpay.payment.manage.citypartner.core.CityPartnerPrizeService;
import au.com.royalpay.payment.manage.mappers.financial.FinancialAgentCommissionDetailMapper;
import au.com.royalpay.payment.manage.mappers.financial.FinancialAgentCommissionMapper;
import au.com.royalpay.payment.manage.mappers.financial.FinancialPartnerCommissionDetailMapper; import au.com.royalpay.payment.manage.mappers.financial.FinancialPartnerCommissionDetailMapper;
import au.com.royalpay.payment.manage.mappers.financial.FinancialPartnerCommissionMapper; import au.com.royalpay.payment.manage.mappers.financial.FinancialPartnerCommissionMapper;
import au.com.royalpay.payment.manage.mappers.financial.FinancialReferrerCommissionDetailMapper; import au.com.royalpay.payment.manage.mappers.financial.FinancialReferrerCommissionDetailMapper;
@ -41,6 +43,10 @@ public class CityPartnerPrizeServiceImpl implements CityPartnerPrizeService {
@Resource @Resource
private FinancialReferrerCommissionDetailMapper financialReferrerCommissionDetailMapper; private FinancialReferrerCommissionDetailMapper financialReferrerCommissionDetailMapper;
@Resource @Resource
private FinancialAgentCommissionDetailMapper financialAgentCommissionDetailMapper;
@Resource
private FinancialAgentCommissionMapper financialAgentCommissionMapper;
@Resource
private FinancialReferrerCommissionMapper financialReferrerCommissionMapper; private FinancialReferrerCommissionMapper financialReferrerCommissionMapper;
@Resource @Resource
private SysConfigManager sysConfigManager; private SysConfigManager sysConfigManager;
@ -323,6 +329,39 @@ public class CityPartnerPrizeServiceImpl implements CityPartnerPrizeService {
} }
} }
@Override
public void generateAgent(String monthStr) {
Date mon = checkMonth(monthStr);
Calendar monthCal = Calendar.getInstance();
monthCal.setTime(mon);
int year = monthCal.get(Calendar.YEAR);
int month = monthCal.get(Calendar.MONTH) + 1;
financialAgentCommissionDetailMapper.clearData(year, month);
financialAgentCommissionMapper.clearData(year, month);
List<JSONObject> transactionAnalysis = transactionMapper.listTransactionsForAgentCommission(year, month);
Map<Integer, ReferrerCommissionAnalysis> results = new HashMap<>();
for (JSONObject analysisDay : transactionAnalysis) {
Integer orgId = analysisDay.getInteger("org_id");
ReferrerCommissionAnalysis referrerCommissionAnalysis = results.get(orgId);
if (referrerCommissionAnalysis == null) {
referrerCommissionAnalysis = new ReferrerCommissionAnalysis(orgId, year, month, analysisDay.getBigDecimal("rate_value"));
results.put(orgId, referrerCommissionAnalysis);
}
referrerCommissionAnalysis.attachAnalysis(analysisDay);
}
for (ReferrerCommissionAnalysis commission : results.values()) {
JSONObject commissionAnalysis = commission.totalCommission();
financialReferrerCommissionMapper.save(commissionAnalysis);
String recordId = commissionAnalysis.getString("record_id");
List<JSONObject> details = commission.getCommissionDetails(recordId);
for (JSONObject detail : details) {
financialReferrerCommissionDetailMapper.save(detail);
}
}
}
@Override @Override
public List<JSONObject> listReferrerAvailableMonths(String year) { public List<JSONObject> listReferrerAvailableMonths(String year) {
int yearNum = checkValidYear(year); int yearNum = checkValidYear(year);

@ -0,0 +1,27 @@
package au.com.royalpay.payment.manage.mappers.financial;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.alibaba.fastjson.JSONObject;
import cn.yixblog.support.mybatis.autosql.annotations.AutoMapper;
import cn.yixblog.support.mybatis.autosql.annotations.AutoSql;
import cn.yixblog.support.mybatis.autosql.annotations.SqlType;
/**
* Created by yixian on 2017-03-08.
*/
@AutoMapper(tablename = "financial_agent_commission_detail", pkName = "detail_id")
public interface FinancialAgentCommissionDetailMapper {
@AutoSql(type = SqlType.DELETE)
void clearData(@Param("year") int year, @Param("month") int month);
@AutoSql(type = SqlType.INSERT)
void save(JSONObject detail);
List<JSONObject> listDetails(@Param("record_id") String recordId);
List<JSONObject> listDetailsByRecordIds(List<String> recordId);
}

@ -0,0 +1,31 @@
package au.com.royalpay.payment.manage.mappers.financial;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.alibaba.fastjson.JSONObject;
import cn.yixblog.support.mybatis.autosql.annotations.AutoMapper;
import cn.yixblog.support.mybatis.autosql.annotations.AutoSql;
import cn.yixblog.support.mybatis.autosql.annotations.SqlType;
/**
* Created by yixian on 2017-03-08.
*/
@AutoMapper(tablename = "financial_agent_commission", pkName = "record_id")
public interface FinancialAgentCommissionMapper {
@AutoSql(type = SqlType.INSERT)
void save(JSONObject commissionAnalysis);
@AutoSql(type = SqlType.DELETE)
void clearData(@Param("year") int year, @Param("month") int month);
List<Integer> listAvailableMonths(@Param("year") int year);
List<JSONObject> find(@Param("year") int year, @Param("month") int month, @Param("org_id") String orgId);
List<JSONObject> list(@Param("year") int year, @Param("month") int month);
}

@ -96,6 +96,8 @@ public interface TransactionMapper {
List<JSONObject> listTransactionsForReferrerCommission(@Param("year") int year, @Param("month") int month); List<JSONObject> listTransactionsForReferrerCommission(@Param("year") int year, @Param("month") int month);
List<JSONObject> listTransactionsForAgentCommission(@Param("year") int year, @Param("month") int month);
BigDecimal checkBalance(@Param("end") Date endDate); BigDecimal checkBalance(@Param("end") Date endDate);
@AutoSql(type = SqlType.COUNT) @AutoSql(type = SqlType.COUNT)

@ -509,6 +509,24 @@
ORDER BY c.org_id ASC, t.client_id ASC, trade_date ASC ORDER BY c.org_id ASC, t.client_id ASC, trade_date ASC
</select> </select>
<select id="listTransactionsForAgentCommission" resultType="com.alibaba.fastjson.JSONObject">
SELECT
sum(if(t.transaction_type = 'Credit', t.clearing_amount, -t.clearing_amount))
total,
t.client_id client_id,
so.org_id org_id,
c.source client_source,
date(t.transaction_time) trade_date,
ifnull(so.rate_value, 1) rate_value
FROM pmt_transactions t
INNER JOIN sys_clients c ON c.client_id = t.client_id
INNER JOIN sys_org so
ON c.referrer_id = so.org_id AND so.is_valid = 1 AND so.type = 1 AND so.commission = 1
WHERE year(t.transaction_time) = #{year} AND month(t.transaction_time) = #{month} AND t.channel != 'Settlement'
GROUP BY so.org_id, trade_date,t.client_id
ORDER BY c.org_id ASC, t.client_id ASC, trade_date ASC
</select>
<select id="checkBalance" resultType="java.math.BigDecimal"> <select id="checkBalance" resultType="java.math.BigDecimal">
<![CDATA[ <![CDATA[

Loading…
Cancel
Save