|
|
@@ -3,16 +3,9 @@ package com.ydtech.modules.partner;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.ydtech.modules.partner.dao.PartnerPremiumDetailsMapper;
|
|
|
import com.ydtech.modules.partner.dao.PartnerUserMapper;
|
|
|
import com.ydtech.modules.partner.model.*;
|
|
|
-import com.ydtech.modules.partner.model.param.CommissionValidityPeriod;
|
|
|
-import com.ydtech.modules.partner.model.vo.CommissionRulesVo;
|
|
|
-import com.ydtech.modules.partner.model.vo.PartnerRulesRatio;
|
|
|
-import com.ydtech.modules.partner.service.PartnerCommissionService;
|
|
|
import com.ydtech.modules.partner.service.PartnerKickbackChangeService;
|
|
|
-import com.ydtech.modules.partner.service.PartnerUserService;
|
|
|
import com.ydtech.utils.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -21,7 +14,6 @@ import org.springframework.stereotype.Component;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
public class UpdateRatioUtils {
|
|
|
@@ -29,21 +21,12 @@ public class UpdateRatioUtils {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(UpdateRatioUtils.class);
|
|
|
|
|
|
- private static PartnerCommissionService commissionService;
|
|
|
private static PartnerUserMapper partnerUserMapper;
|
|
|
- private static PartnerPremiumDetailsMapper partnerPremiumDetailsMapper;
|
|
|
- private static PartnerUserService partnerUserService;
|
|
|
private static PartnerKickbackChangeService kickbackChangeService;
|
|
|
@Autowired
|
|
|
- public void setCommissionService(PartnerCommissionService commissionService,
|
|
|
- PartnerUserMapper partnerUserMapper,
|
|
|
- PartnerKickbackChangeService kickbackChangeService,
|
|
|
- PartnerPremiumDetailsMapper partnerPremiumDetailsMapper,
|
|
|
- PartnerUserService partnerUserService) {
|
|
|
- UpdateRatioUtils.commissionService = commissionService;
|
|
|
+ public void setCommissionService(PartnerUserMapper partnerUserMapper,
|
|
|
+ PartnerKickbackChangeService kickbackChangeService) {
|
|
|
UpdateRatioUtils.partnerUserMapper = partnerUserMapper;
|
|
|
- UpdateRatioUtils.partnerPremiumDetailsMapper = partnerPremiumDetailsMapper;
|
|
|
- UpdateRatioUtils.partnerUserService = partnerUserService;
|
|
|
UpdateRatioUtils.kickbackChangeService = kickbackChangeService;
|
|
|
}
|
|
|
|
|
|
@@ -54,11 +37,9 @@ public class UpdateRatioUtils {
|
|
|
public void loginBefore(String userId){
|
|
|
Map<Integer, PartnerUser> allByUserId = getAllByUserId(userId);
|
|
|
allByUserId.forEach((key, value) -> {
|
|
|
-
|
|
|
String grade = String.valueOf(key);
|
|
|
String string = getString(allByUserId, grade);
|
|
|
BigDecimal latest = CalculateCommissionUtils.calculateCommission(grade, value.getUserId(),string);
|
|
|
- //BigDecimal latest = calculateCommission(String.valueOf(key),value.getUserId(),allByUserId);
|
|
|
initializeCommissionRatio(value, latest);
|
|
|
});
|
|
|
}
|
|
|
@@ -123,7 +104,6 @@ public class UpdateRatioUtils {
|
|
|
PartnerUser value = entry.getValue();
|
|
|
String string = getString(map, key);
|
|
|
BigDecimal latest = CalculateCommissionUtils.calculateCommission(String.valueOf(key), value.getUserId(), string);
|
|
|
- //BigDecimal latest = calculateCommission(key,value.getUserId(),map);
|
|
|
BigDecimal mostRecent = SpringUtil.getBean(PartnerKickbackChangeService.class).mostRecentRecord(value.getUserId(),key);
|
|
|
getPartnerKickbackChange(latest, mostRecent, value, kickbackChange,map);
|
|
|
}
|
|
|
@@ -160,10 +140,7 @@ public class UpdateRatioUtils {
|
|
|
PartnerUser value = entry.getValue();
|
|
|
BigDecimal latest = CalculateCommissionUtils.calculateCommission(key,value.getUserId(),null);
|
|
|
initializeCommissionRatio(value, latest);
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public void addChangeRecord(String userId){
|
|
|
@@ -199,270 +176,4 @@ public class UpdateRatioUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static BigDecimal calculateCommission(String grade, String userId,Map<Integer, PartnerUser> map) {
|
|
|
- CommissionRulesVo rulesVo = commissionService.queryAll();
|
|
|
- if (rulesVo == null) return BigDecimal.ZERO;
|
|
|
- BigDecimal gl = BigDecimal.ZERO;
|
|
|
- if (StringUtils.equals("1", grade)){
|
|
|
- gl = BigDecimal.ZERO;
|
|
|
- }
|
|
|
- if (StringUtils.equals("2", grade) || StringUtils.equals("3", grade)){
|
|
|
- gl = ladderCommission("gl" , grade,
|
|
|
- rulesVo.getAdministrator().getRulesRatioList(), userId, rulesVo.getAdministrator(),map);
|
|
|
- }
|
|
|
- BigDecimal hh = BigDecimal.ZERO;
|
|
|
- if (StringUtils.equals("3", grade) || StringUtils.equals("4", grade)){
|
|
|
- hh = ladderCommission(StringUtils.equals("3",grade) ? "levelGzs" :"gzs",grade, rulesVo.getPartner().getRulesRatioList(), userId, rulesVo.getPartner(),map);
|
|
|
- }
|
|
|
- if (StringUtils.equals("1", grade)) {
|
|
|
- return new BigDecimal(rulesVo.getOriginator());
|
|
|
- }
|
|
|
- if (StringUtils.equals("2", grade)) {
|
|
|
- PartnerCommissionRules administrator = rulesVo.getAdministrator();
|
|
|
- if (isZero(gl)){
|
|
|
- return new BigDecimal(administrator.getDefaultRatio());
|
|
|
- }else {
|
|
|
- return new BigDecimal(administrator.getSharedRatio()).subtract(gl);
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.equals("3", grade)) {
|
|
|
- PartnerCommissionRules partner = rulesVo.getPartner();
|
|
|
- if (isZero(gl) && isZero(hh)) {
|
|
|
- return new BigDecimal(partner.getDefaultRatio())
|
|
|
- .add(subtractRatios(rulesVo.getAdministrator().getSharedRatio(), rulesVo.getAdministrator().getDefaultRatio()));
|
|
|
- } else if (isZero(hh) && !isZero(gl)) {
|
|
|
- return gl.add(new BigDecimal(partner.getDefaultRatio()));
|
|
|
- } else if (isZero(gl) && !isZero(hh)) {
|
|
|
- return new BigDecimal(partner.getSharedRatio()).subtract(hh)
|
|
|
- .add(subtractRatios(rulesVo.getAdministrator().getSharedRatio(), rulesVo.getAdministrator().getDefaultRatio()));
|
|
|
- }else{
|
|
|
- return gl.add(new BigDecimal(rulesVo.getPartner().getSharedRatio()).subtract(hh));
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.equals("4", grade)) {
|
|
|
- return isZero(hh) ? subtractRatios(rulesVo.getPartner().getSharedRatio(), rulesVo.getPartner().getDefaultRatio()) : hh;
|
|
|
- }
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static String getReason(String grade, String userId,Map<Integer, PartnerUser> map) {
|
|
|
- CommissionRulesVo rulesVo = commissionService.queryAll();
|
|
|
- if (rulesVo == null) return null;
|
|
|
- BigDecimal gl = BigDecimal.ZERO;
|
|
|
- if (StringUtils.equals("1", grade)){
|
|
|
- return "";
|
|
|
- }
|
|
|
- if (StringUtils.equals("2", grade) || StringUtils.equals("3", grade)){
|
|
|
- return obtainReason("gl" , grade,
|
|
|
- rulesVo.getAdministrator().getRulesRatioList(), userId, rulesVo.getAdministrator(),map);
|
|
|
- }
|
|
|
- BigDecimal hh = BigDecimal.ZERO;
|
|
|
- if (StringUtils.equals("3", grade) || StringUtils.equals("4", grade)){
|
|
|
- return obtainReason(StringUtils.equals("3",grade) ? "levelGzs" :"gzs",grade, rulesVo.getPartner().getRulesRatioList(), userId, rulesVo.getPartner(),map);
|
|
|
- }
|
|
|
- return null;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static String obtainReason(String type, String grade, List<PartnerRulesRatio> rulesRatioList, String userId, PartnerCommissionRules rules,Map<Integer, PartnerUser> map) {
|
|
|
- CommissionValidityPeriod period = CalculateCommissionUtils.getCommissionValidityPeriod(rules, userId, grade);
|
|
|
- List<PartnerRulesRatio> sortedList = rulesRatioList.stream()
|
|
|
- .sorted(Comparator.comparing(PartnerRulesRatio::getKickback).reversed())
|
|
|
- .collect(Collectors.toList());
|
|
|
- BigDecimal sumPremium = BigDecimal.ZERO;
|
|
|
- Set<String> userIdList = new HashSet<>(0);
|
|
|
- if(StringUtils.equals("2",grade)){
|
|
|
- PartnerUser s = map.get(3);
|
|
|
- if (!Objects.isNull(s)){
|
|
|
- userIdList = CalculateCommissionUtils.getUserIdList(s.getUserId());
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.equals("3",grade) ){
|
|
|
- PartnerUser s = map.get(4);
|
|
|
- if (!Objects.isNull(s)){
|
|
|
- userIdList = CalculateCommissionUtils.getUserIdList(s.getUserId());
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.equals("4",grade)){
|
|
|
- PartnerUser s = map.get(4);
|
|
|
- if (!Objects.isNull(s)){
|
|
|
- userIdList = CalculateCommissionUtils.getUserIdList(s.getUserId());
|
|
|
- }
|
|
|
- }
|
|
|
- if (CollectionUtil.isNotEmpty(userIdList)){
|
|
|
- QueryWrapper<PartnerPremiumDetails> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.in("user_id", userIdList);
|
|
|
- if (StringUtils.isNotEmpty(period.getStartDate()) && StringUtils.isNotEmpty(period.getEndDate())) {
|
|
|
- queryWrapper.between("create_time", period.getStartDate(), period.getEndDate());
|
|
|
- }
|
|
|
- BigDecimal totalSumPremium = partnerPremiumDetailsMapper.selectTotalSumPremium(queryWrapper);
|
|
|
- sumPremium = Optional.ofNullable(totalSumPremium).orElse(BigDecimal.ZERO);
|
|
|
- }
|
|
|
- Integer number = 0;
|
|
|
- switch (type) {
|
|
|
- case "gl":
|
|
|
- PartnerUser s = map.get(3);
|
|
|
- if (!Objects.isNull( s)){
|
|
|
- number = partnerUserMapper.selectNumber(s.getUserId(), "4", period);
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- case "levelGzs":
|
|
|
- PartnerUser s1 = map.get(4);
|
|
|
- if (!Objects.isNull( s1)){
|
|
|
- number = partnerUserMapper.selectNumber4(s1.getUserId(), "5", period);
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- case "gzs":
|
|
|
- number = partnerUserMapper.selectByUserIdNumber(userId, "5", period);
|
|
|
- break;
|
|
|
- default:
|
|
|
- return null;
|
|
|
- }
|
|
|
- BigDecimal numberBD = BigDecimal.valueOf(number);
|
|
|
- for (PartnerRulesRatio ratio : sortedList) {
|
|
|
- int allSatisfied = ratio.getAllSatisfied();
|
|
|
- List<PartnerRatio> ratioList = ratio.getPartnerRatioList();
|
|
|
- if (CollectionUtil.isEmpty(ratioList)) continue;
|
|
|
- BigDecimal min = ratioList.get(0).getMin();
|
|
|
- BigDecimal min1 = ratioList.get(1).getMin();
|
|
|
- boolean ry = ratioList.get(0).isRy();
|
|
|
- boolean bf = ratioList.get(1).isBf();
|
|
|
- if (ry && bf) {
|
|
|
- if (allSatisfied == 1) {
|
|
|
- if (numberBD.compareTo(min) >= 0 && sumPremium.compareTo(min1) >= 0) {
|
|
|
- return "邀请人达" + min + "人,累计保费达" + min1 + "元";
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (numberBD.compareTo(min) >= 0) {
|
|
|
- return "邀请人达" + min + "人";
|
|
|
- }else if(sumPremium.compareTo(min1) >= 0){
|
|
|
- return "累计保费达" + min1 + "元";
|
|
|
- }
|
|
|
- }
|
|
|
- }else if (ry && !bf){
|
|
|
- if (allSatisfied != 1) {
|
|
|
- if (numberBD.compareTo(min) >= 0) {
|
|
|
- return "邀请人达" + min + "人";
|
|
|
- }
|
|
|
- }
|
|
|
- }else if (!ry && bf){
|
|
|
- if (allSatisfied != 1) {
|
|
|
- if (sumPremium.compareTo(min1) >= 0) {
|
|
|
- return "累计保费达" + min1 + "元";
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static BigDecimal ladderCommission(String type, String grade, List<PartnerRulesRatio> rulesRatioList, String userId, PartnerCommissionRules rules,Map<Integer, PartnerUser> map) {
|
|
|
- CommissionValidityPeriod period = CalculateCommissionUtils.getCommissionValidityPeriod(rules, userId, grade);
|
|
|
- List<PartnerRulesRatio> sortedList = rulesRatioList.stream()
|
|
|
- .sorted(Comparator.comparing(PartnerRulesRatio::getKickback).reversed())
|
|
|
- .collect(Collectors.toList());
|
|
|
- BigDecimal sumPremium = BigDecimal.ZERO;
|
|
|
- Set<String> userIdList = new HashSet<>(0);
|
|
|
- if (StringUtils.equals("2",grade)){
|
|
|
- PartnerUser s = map.get(3);
|
|
|
- if (!Objects.isNull(s)){
|
|
|
- userIdList = CalculateCommissionUtils.getUserIdList(s.getUserId());
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- if (StringUtils.equals("3",grade)){
|
|
|
- PartnerUser s = map.get(4);
|
|
|
- if (!Objects.isNull(s)){
|
|
|
- userIdList = CalculateCommissionUtils.getUserIdList(s.getUserId());
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.equals("4",grade)){
|
|
|
- PartnerUser s = map.get(4);
|
|
|
- if (!Objects.isNull(s)){
|
|
|
- userIdList = CalculateCommissionUtils.getUserIdList(s.getUserId());
|
|
|
- }
|
|
|
- }
|
|
|
- if (CollectionUtil.isNotEmpty(userIdList)){
|
|
|
- QueryWrapper<PartnerPremiumDetails> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.in("user_id", userIdList);
|
|
|
- if (StringUtils.isNotEmpty(period.getStartDate()) && StringUtils.isNotEmpty(period.getEndDate())) {
|
|
|
- queryWrapper.between("create_time", period.getStartDate(), period.getEndDate());
|
|
|
- }
|
|
|
- BigDecimal totalSumPremium = partnerPremiumDetailsMapper.selectTotalSumPremium(queryWrapper);
|
|
|
- sumPremium = Optional.ofNullable(totalSumPremium).orElse(BigDecimal.ZERO);
|
|
|
- }
|
|
|
- Integer number = 0;
|
|
|
- switch (type) {
|
|
|
- case "gl":
|
|
|
- PartnerUser s = map.get(3);
|
|
|
- if (!Objects.isNull( s)){
|
|
|
- number = partnerUserMapper.selectNumber(s.getUserId(), "4", period);
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- case "levelGzs":
|
|
|
- PartnerUser s1 = map.get(4);
|
|
|
- if (!Objects.isNull( s1)){
|
|
|
- number = partnerUserMapper.selectNumber4(s1.getUserId(), "5", period);
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- case "gzs":
|
|
|
- number = partnerUserMapper.selectByUserIdNumber(userId, "5", period);
|
|
|
- break;
|
|
|
- default:
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
- BigDecimal numberBD = BigDecimal.valueOf(number);
|
|
|
- for (PartnerRulesRatio ratio : sortedList) {
|
|
|
- int allSatisfied = ratio.getAllSatisfied();
|
|
|
- List<PartnerRatio> ratioList = ratio.getPartnerRatioList();
|
|
|
- if (CollectionUtil.isEmpty(ratioList)) continue;
|
|
|
- BigDecimal min = ratioList.get(0).getMin();
|
|
|
- BigDecimal min1 = ratioList.get(1).getMin();
|
|
|
- boolean ry = ratioList.get(0).isRy();
|
|
|
- boolean bf = ratioList.get(1).isBf();
|
|
|
- if (ry && bf) {
|
|
|
- if (allSatisfied == 1) {
|
|
|
- if (numberBD.compareTo(min) >= 0 && sumPremium.compareTo(min1) >= 0) {
|
|
|
- return ratio.getKickback();
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (numberBD.compareTo(min) >= 0 || sumPremium.compareTo(min1) >= 0) {
|
|
|
- return ratio.getKickback();
|
|
|
- }
|
|
|
- }
|
|
|
- }else if (ry && !bf){
|
|
|
- if (allSatisfied != 1) {
|
|
|
- if (numberBD.compareTo(min) >= 0) {
|
|
|
- return ratio.getKickback();
|
|
|
- }
|
|
|
- }
|
|
|
- }else if (!ry && bf){
|
|
|
- if (allSatisfied != 1) {
|
|
|
- if (sumPremium.compareTo(min1) >= 0) {
|
|
|
- return ratio.getKickback();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean isZero(BigDecimal value) {
|
|
|
- return value.compareTo(BigDecimal.ZERO) == 0;
|
|
|
- }
|
|
|
-
|
|
|
- private static BigDecimal subtractRatios(String a, String b) {
|
|
|
- return new BigDecimal(a).subtract(new BigDecimal(b));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|