kira 6 years ago
parent 762a926d5a
commit 5a218b2090

@ -0,0 +1,241 @@
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;
import java.math.RoundingMode;
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 CityPartnerSeniorCommissionAnalysis {
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 BigDecimal orgRate;
private String channel;
private int commissionType;
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 CityPartnerSeniorCommissionAnalysis(int orgId, int year, int month, BigDecimal rate, String channel, BigDecimal alipayChargeRate,
BigDecimal wechatChargeRate, BigDecimal jdChargeRate, BigDecimal alipayonlineChargeRate, BigDecimal orgRate, int commissionType) {
this.orgId = orgId;
this.year = year;
this.month = month;
this.rate = rate;
this.channel = channel;
this.commissionType = commissionType;
this.alipayChargeRate = alipayChargeRate;
this.wechatChargeRate = wechatChargeRate;
this.jdChargeRate = jdChargeRate;
this.alipayonlineChargeRate = alipayonlineChargeRate;
this.orgRate = orgRate;
}
public CityPartnerSeniorCommissionAnalysis 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 CityPartnerSeniorCommissionAnalysis 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, RoundingMode.HALF_UP);
BigDecimal royalpayCharge = total.multiply(rate).divide(CommonConsts.HUNDRED, 2, RoundingMode.HALF_UP);
BigDecimal thirdPartyPaymentCharge = getThirdPartyCharge(channel, total);
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);
item.put("commission_type",commissionType);
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);
result.put("commission_type",commissionType);
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;
}
public CityPartnerSeniorCommissionAnalysis attachNetCommissionAnalysis(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, RoundingMode.HALF_UP);
BigDecimal royalpayCharge = total.multiply(rate).divide(CommonConsts.HUNDRED, 2, RoundingMode.HALF_UP);
BigDecimal thirdPartyPaymentCharge = getThirdPartyCharge(channel, total);
BigDecimal netCharge = royalpayCharge.subtract(thirdPartyPaymentCharge);
BigDecimal orgCharge = netCharge.multiply(orgRate).divide(CommonConsts.HUNDRED, 2, RoundingMode.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, RoundingMode.HALF_UP);
break;
case "wechat":
thirdPartyPaymentCharge = total.multiply(wechatChargeRate).divide(CommonConsts.HUNDRED, 2, RoundingMode.HALF_UP);
break;
case "jd":
thirdPartyPaymentCharge = total.multiply(jdChargeRate).divide(CommonConsts.HUNDRED, 2, RoundingMode.HALF_UP);
break;
case "alipayonline":
thirdPartyPaymentCharge = total.multiply(alipayonlineChargeRate).divide(CommonConsts.HUNDRED, 2, RoundingMode.HALF_UP);
break;
default:
break;
}
return thirdPartyPaymentCharge;
}
}

@ -39,5 +39,12 @@ public interface CityPartnerPrizeService {
List<JSONObject> getAgentPrizeInfoList(String monthStr);
void generateSenior(String monthStr);
List<JSONObject> querySenior(JSONObject param);
JSONObject getSenior(String monthStr, String orgId);
}

@ -1,27 +1,5 @@
package au.com.royalpay.payment.manage.citypartner.core.impls;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import au.com.royalpay.payment.manage.citypartner.beans.AgentCommissionAnalysis;
import au.com.royalpay.payment.manage.citypartner.beans.CityPartnerCommissionAnalysis;
import au.com.royalpay.payment.manage.citypartner.beans.ReferrerCommissionAnalysis;
@ -32,12 +10,35 @@ import au.com.royalpay.payment.manage.mappers.financial.FinancialPartnerCommissi
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.FinancialReferrerCommissionMapper;
import au.com.royalpay.payment.manage.mappers.financial.FinancialSeniorPartnerCommissionMapper;
import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper;
import au.com.royalpay.payment.manage.mappers.system.OrgMapper;
import au.com.royalpay.payment.tools.env.SysConfigManager;
import au.com.royalpay.payment.tools.exceptions.BadRequestException;
import au.com.royalpay.payment.tools.exceptions.ServerErrorException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
/**
* Created by yixian on 2017-03-08.
*/
@ -61,6 +62,8 @@ public class CityPartnerPrizeServiceImpl implements CityPartnerPrizeService {
private FinancialReferrerCommissionMapper financialReferrerCommissionMapper;
@Resource
private SysConfigManager sysConfigManager;
@Resource
private FinancialSeniorPartnerCommissionMapper financialSeniorPartnerCommissionMapper;
@Override
public List<JSONObject> listAvailableMonths(String year) {
@ -700,4 +703,61 @@ public class CityPartnerPrizeServiceImpl implements CityPartnerPrizeService {
return financialAgentCommissionMapper.list(year, month);
}
@Override
public void generateSenior(String monthStr) {
List<String> seniorOrgs = orgMapper.listSeniorOrgId(0);
if(CollectionUtils.isEmpty(seniorOrgs)){
return;
}
Date mon = checkMonth(monthStr);
Calendar monthCal = Calendar.getInstance();
monthCal.setTime(mon);
int year = monthCal.get(Calendar.YEAR);
int month = monthCal.get(Calendar.MONTH) + 1;
JSONObject sysConfig = sysConfigManager.getSysConfig();
BigDecimal rate = sysConfig.getBigDecimal("senior_charge_rate");
if(rate==null){
rate = new BigDecimal("0.0006");
}
Date now = new Date();
List<JSONObject> seniorOrgAnalysis = financialPartnerCommissionMapper.analysisSenior(year,month,seniorOrgs);
BigDecimal finalRate = rate;
seniorOrgAnalysis.parallelStream().forEach(p->{
JSONObject record = new JSONObject();
record.put("org_id",p.getString("org_id"));
record.put("year",year);
record.put("month",month);
record.put("create_time",now);
record.put("org_rate", finalRate);
record.put("gross_amount",p.getBigDecimal("gross_amount"));
financialSeniorPartnerCommissionMapper.save(record);
});
}
@Override
public List<JSONObject> querySenior(JSONObject param) {
Date mon = checkMonth(param.getString("monthStr"));
Calendar monthCal = Calendar.getInstance();
monthCal.setTime(mon);
int year = monthCal.get(Calendar.YEAR);
int month = monthCal.get(Calendar.MONTH) + 1;
List<String> orgs = orgMapper.listSeniorOrgId(param.getIntValue("org_id"));
List<JSONObject> result = financialPartnerCommissionMapper.analysisSenior(year,month,orgs);
result.parallelStream().forEach(p->{
p.put("gross_amount",p.getBigDecimal("gross_amount").multiply(new BigDecimal("0.0005")).setScale(2, BigDecimal.ROUND_HALF_DOWN));
});
return result;
}
@Override
public JSONObject getSenior(String monthStr, String orgId) {
Date monthDate = parseMonth(monthStr);
Calendar monthCal = Calendar.getInstance();
monthCal.setTime(monthDate);
int year = monthCal.get(Calendar.YEAR);
int month = monthCal.get(Calendar.MONTH) + 1;
return financialSeniorPartnerCommissionMapper.find(year,month,orgId);
}
}

@ -271,4 +271,15 @@ public class CityPartnerPrizeController {
result.put("partner_info_list", partnerInfos);
return result;
}
@RequestMapping(value = "/senior/generate", method = RequestMethod.POST)
public void generateSenior(@RequestBody JSONObject param) {
String month = param.getString("month");
cityPartnerPrizeService.generate(month);
}
@RequestMapping(value = "/senior/{orgId}", method = RequestMethod.GET)
public void findOne(@RequestParam String monthStr, @PathVariable String orgId) {
cityPartnerPrizeService.getSenior(monthStr,orgId);
}
}

@ -28,4 +28,6 @@ public interface FinancialPartnerCommissionMapper {
JSONObject getTotalPartnerCharge(@Param("year") int year, @Param("month") int month);
List<JSONObject> find(@Param("year") int year, @Param("month") int month, @Param("org_id") String orgId);
List<JSONObject> analysisSenior(@Param("year") int year, @Param("month")int month,@Param("orgIds") List<String> orgIds);
}

@ -0,0 +1,33 @@
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_senior_partner_commission", pkName = "id")
public interface FinancialSeniorPartnerCommissionMapper {
@AutoSql(type = SqlType.DELETE)
void clearData(@Param("year") int year, @Param("month") int month, @Param("commission_type") int commission_type);
@AutoSql(type = SqlType.INSERT)
void save(JSONObject commissionAnalysis);
List<Integer> listAvailableMonths(@Param("year") int year);
List<JSONObject> listWithOrgInfo(@Param("year") int year, @Param("month") int month);
List<JSONObject> list(@Param("year") int year, @Param("month") int month);
JSONObject getTotalPartnerCharge(@Param("year") int year, @Param("month") int month);
JSONObject find(@Param("year") int year, @Param("month") int month, @Param("org_id") String orgId);
}

@ -47,4 +47,6 @@ public interface OrgMapper {
PageList<JSONObject> listOrgWithPages(JSONObject paraams, PageBounds order);
List<JSONObject> listOrgAndChild(@Param("org_id") int org_id);
List<String> listSeniorOrgId(@Param("senior_parent_org_id") int senior_parent_org_id);
}

@ -38,4 +38,14 @@
INNER JOIN sys_org o ON o.org_id = c.org_id and o.type=0
WHERE c.year = #{year} AND c.month = #{month} and c.org_id=#{org_id}
</select>
<select id="analysisSenior" resultType="com.alibaba.fastjson.JSONObject">
select sum(gross_amount) gross_amount,org_id from financial_partner_commission
where `year` =#{year} and `month` = #{month}
and org_id in
(<foreach collection="orgIds" item="item" separator=",">
#{item}
</foreach>)
group by org_id
</select>
</mapper>

@ -70,4 +70,15 @@
</where>
</select>
<select id="listSeniorOrgId" resultType="java.lang.String">
select org_id from sys_org
where is_valid = 1
<if test="senior_parent_org_id!=0">
and senior_parent_org_id = #{senior_parent_org_id}
</if>
<if test="senior_parent_org_id==0">
and senior_parent_org_id !=0
</if>
</select>
</mapper>
Loading…
Cancel
Save