|
|
@@ -10,8 +10,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.ydtech.exception.SystemException;
|
|
|
import com.ydtech.modules.partner.dao.PartnerAttributeMapper;
|
|
|
import com.ydtech.modules.partner.dao.PartnerRuleMapper;
|
|
|
+import com.ydtech.modules.partner.dao.PartnerUserMapper;
|
|
|
import com.ydtech.modules.partner.model.PartnerAttribute;
|
|
|
+import com.ydtech.modules.partner.model.PartnerCommissionRules;
|
|
|
import com.ydtech.modules.partner.model.PartnerRule;
|
|
|
+import com.ydtech.modules.partner.model.param.CommissionValidityPeriod;
|
|
|
import com.ydtech.modules.partner.model.param.PartnerRuleEntity;
|
|
|
import com.ydtech.modules.partner.service.PartnerRuleService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -20,9 +23,12 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@@ -33,6 +39,9 @@ public class PartnerRuleServiceImpl extends ServiceImpl<PartnerRuleMapper, Partn
|
|
|
@Autowired
|
|
|
private PartnerAttributeMapper attributeMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PartnerUserMapper partnerUserMapper;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@@ -104,64 +113,209 @@ public class PartnerRuleServiceImpl extends ServiceImpl<PartnerRuleMapper, Partn
|
|
|
return attributeMapper.queryGradeName();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public boolean verifyUpgradeConditions(String userId, String grade) {
|
|
|
- if (StringUtils.isBlank(grade)){
|
|
|
+ private PartnerRuleEntity validateGradeAndFetchRuleEntity(String grade) {
|
|
|
+ if (StringUtils.isBlank(grade)) {
|
|
|
throw new SystemException("等级不能为空");
|
|
|
}
|
|
|
PartnerRuleEntity partnerRuleEntity = attributeMapper.selectByGrade(grade);
|
|
|
- if (StringUtils.equals("2",partnerRuleEntity.getUpgrade())){
|
|
|
+ if (StringUtils.equals("2", partnerRuleEntity.getUpgrade())) {
|
|
|
throw new SystemException("当前等级不支持升级");
|
|
|
}
|
|
|
- //传入用户的id 成为当前等级 的天数
|
|
|
- String days = "10";
|
|
|
- //考核条件
|
|
|
- List<PartnerAttribute> requirement = partnerRuleEntity.getRequirement();
|
|
|
- if (CollectionUtil.isNotEmpty(requirement)){
|
|
|
- Map<Integer, List<PartnerAttribute>> peopleByType = requirement.stream()
|
|
|
- .collect(Collectors.groupingBy(PartnerAttribute::getType));
|
|
|
- //考核周期
|
|
|
- List<PartnerAttribute> partnerAttributes = peopleByType.get(1);
|
|
|
- if (CollectionUtil.isNotEmpty(partnerAttributes)){
|
|
|
- for (PartnerAttribute partnerAttribute : partnerAttributes) {
|
|
|
- String cycle = partnerAttribute.getCycle();
|
|
|
- //固定日期
|
|
|
- if (StringUtils.equals("3",cycle)){
|
|
|
-
|
|
|
- }else if (StringUtils.equals("2",cycle)){
|
|
|
- //固定天数
|
|
|
- String fixedDays = partnerAttribute.getFixedDays();
|
|
|
- if (StringUtils.isNotBlank(fixedDays) && StringUtils.isNotBlank(days)){
|
|
|
- if (!compare(days, fixedDays)){
|
|
|
- throw new SystemException("考核周期不满足");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ return partnerRuleEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean verifyUpgradeConditions(String userId, String grade) {
|
|
|
+ VerifyData data = extractCommonLogic(userId, grade);
|
|
|
+ PartnerRuleEntity partnerRuleEntity = data.partnerRuleEntity;
|
|
|
+ BigDecimal p = data.p;
|
|
|
+ BigDecimal bf = data.bf;
|
|
|
+ BigDecimal people = data.people;
|
|
|
+ BigDecimal allBf = data.allBf;
|
|
|
+ if (StringUtils.equals("1", partnerRuleEntity.getAppraise())) {
|
|
|
+ return p.compareTo(people) >= 0 && bf.compareTo(allBf) >= 0;
|
|
|
+ } else {
|
|
|
+ return p.compareTo(people) >= 0 || bf.compareTo(allBf) >= 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String verifyUpgradeConditionsMessage(String userId, String grade) {
|
|
|
+ VerifyData data = extractCommonLogic(userId, grade);
|
|
|
+ PartnerRuleEntity partnerRuleEntity = data.partnerRuleEntity;
|
|
|
+ BigDecimal p = data.p;
|
|
|
+ BigDecimal bf = data.bf;
|
|
|
+ BigDecimal people = data.people;
|
|
|
+ BigDecimal allBf = data.allBf;
|
|
|
+ StringBuilder message = new StringBuilder();
|
|
|
+ if (StringUtils.equals("1", partnerRuleEntity.getAppraise())) {
|
|
|
+ if (p.compareTo(people) >= 0 && bf.compareTo(allBf) >= 0) {
|
|
|
+ message.append("邀请").append(people).append("个合伙人, 累计保费达").append(allBf).append("元");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (p.compareTo(people) >= 0) {
|
|
|
+ message.append("邀请").append(people).append("个合伙人");
|
|
|
+ }
|
|
|
+ if (bf != null && bf.compareTo(allBf) >= 0) {
|
|
|
+ if (message.length() > 0) {
|
|
|
+ message.append(", ");
|
|
|
}
|
|
|
- // 当前用户的邀请人数
|
|
|
- String inviterNumber = "10";
|
|
|
-
|
|
|
- // 当前用户的出单保费
|
|
|
- BigDecimal issuancePremium = new BigDecimal("522222");
|
|
|
- //考核条件
|
|
|
- List<PartnerAttribute> rulesList = peopleByType.get(2);
|
|
|
- // 考核是否满足 1 满足所有条件 2 满足任意一条条件
|
|
|
- String appraise = partnerRuleEntity.getAppraise();
|
|
|
- for (PartnerAttribute attribute : rulesList) {
|
|
|
- String attributeRequirement = attribute.getRequirement();
|
|
|
- if (StringUtils.equals("1",attributeRequirement)){
|
|
|
- //邀请合伙人
|
|
|
-
|
|
|
- }else if (StringUtils.equals("2",attributeRequirement)){
|
|
|
- // 出单保费
|
|
|
- }
|
|
|
+ message.append("累计保费达").append(allBf).append("元");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return message.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private VerifyData extractCommonLogic(String userId, String grade) {
|
|
|
+ PartnerRuleEntity partnerRuleEntity = validateGradeAndFetchRuleEntity(grade);
|
|
|
+ if (partnerRuleEntity == null) {
|
|
|
+ throw new SystemException("合作伙伴规则实体不能为空");
|
|
|
+ }
|
|
|
+ PartnerAttribute cycle1 = partnerRuleEntity.getCycle();
|
|
|
+ List<PartnerAttribute> requirement = partnerRuleEntity.getRequirement();
|
|
|
+ if (CollectionUtil.isEmpty(requirement)) {
|
|
|
+ throw new SystemException("考核条件不能为空");
|
|
|
+ }
|
|
|
+ CommissionValidityPeriod period = getCommissionValidityPeriod(cycle1, userId, grade);
|
|
|
+ if (period == null) {
|
|
|
+ throw new SystemException("有效周期不能为空");
|
|
|
+ }
|
|
|
+ BigDecimal p = getInviteData(userId, grade, period);
|
|
|
+ BigDecimal bf = partnerUserMapper.selectTotalSumPremium(userId, grade, period);
|
|
|
+ BigDecimal people;
|
|
|
+ BigDecimal allBf;
|
|
|
+ if (requirement.size() >= 2) {
|
|
|
+ people = getRequirementValue(requirement, 0);
|
|
|
+ allBf = getRequirementValue(requirement, 1);
|
|
|
+ } else {
|
|
|
+ throw new SystemException("考核条件不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ return new VerifyData(partnerRuleEntity, p, bf, people, allBf);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal getRequirementValue(List<PartnerAttribute> requirement, int index) {
|
|
|
+ if (requirement.size() > index) {
|
|
|
+ return new BigDecimal(requirement.get(index).getMin());
|
|
|
+ }
|
|
|
+ throw new SystemException("考核条件配置错误");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static class VerifyData {
|
|
|
+ private final PartnerRuleEntity partnerRuleEntity;
|
|
|
+ private final BigDecimal p;
|
|
|
+ private final BigDecimal bf;
|
|
|
+ private final BigDecimal people;
|
|
|
+ private final BigDecimal allBf;
|
|
|
+
|
|
|
+ public VerifyData(PartnerRuleEntity partnerRuleEntity, BigDecimal p, BigDecimal bf, BigDecimal people, BigDecimal allBf) {
|
|
|
+ this.partnerRuleEntity = partnerRuleEntity;
|
|
|
+ this.p = p;
|
|
|
+ this.bf = bf;
|
|
|
+ this.people = people;
|
|
|
+ this.allBf = allBf;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public BigDecimal getInviteData(String userId, String grade,CommissionValidityPeriod period){
|
|
|
+ int number = 0;
|
|
|
+ if (StringUtils.equals("2", grade)){
|
|
|
+ number = partnerUserMapper.assessment(userId,"3",period);
|
|
|
+ }else if (StringUtils.equals("3", grade)){
|
|
|
+ number = partnerUserMapper.assessment(userId,"4",period);
|
|
|
+ }else if (StringUtils.equals("4", grade)){
|
|
|
+ number = partnerUserMapper.assessment(userId,"5",period);
|
|
|
+ }
|
|
|
+ return new BigDecimal(number);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CommissionValidityPeriod getCommissionValidityPeriod(PartnerAttribute cycle1, String userId, String grade) {
|
|
|
+ CommissionValidityPeriod period = new CommissionValidityPeriod();
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if (StringUtils.equals("1", cycle1.getCycle())) {
|
|
|
+ return period;
|
|
|
+ } else if (StringUtils.equals("2", cycle1.getCycle())) {
|
|
|
+ Date upgradeTime = partnerUserMapper.selectUpgradeTime(userId, grade);
|
|
|
+ if (upgradeTime == null) {
|
|
|
+ partnerUserMapper.updateUpgradeTime(userId,grade);
|
|
|
+ upgradeTime = partnerUserMapper.selectUpgradeTime(userId, grade);
|
|
|
+ if (Objects.isNull(upgradeTime)){
|
|
|
+ return period;
|
|
|
}
|
|
|
}
|
|
|
+ String fixedDays = cycle1.getFixedDays();
|
|
|
+
|
|
|
+ if (!isWithinDays(upgradeTime,Integer.valueOf(fixedDays) )) {
|
|
|
+ updateAssessmentTimeRecursively(upgradeTime,Integer.valueOf(fixedDays),userId,grade );
|
|
|
+ upgradeTime = partnerUserMapper.selectUpgradeTime(userId, grade);
|
|
|
+ }
|
|
|
+ period.setStartDate(dateFormat.format(upgradeTime));
|
|
|
+ period.setEndDate(dateFormat.format(addDays(upgradeTime,Integer.valueOf(fixedDays))));
|
|
|
+ } else if (StringUtils.equals("3", cycle1.getCycle())) {
|
|
|
+// Date fixedCycle = partnerUserMapper.selectFixedCycleTime(userId, grade);
|
|
|
+// if (fixedCycle == null) {
|
|
|
+// partnerUserMapper.updateFixedCycleTime(userId);
|
|
|
+// fixedCycle = partnerUserMapper.selectFixedCycleTime(userId, grade);
|
|
|
+// if (fixedCycle == null){
|
|
|
+// return period;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// period.setStartDate(rules.getStartDate());
|
|
|
+// period.setEndDate(rules.getEndDate());
|
|
|
+ }
|
|
|
+ return period;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Date addDays(Date date, int days) {
|
|
|
+ LocalDateTime localDate = date.toInstant()
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
+ .toLocalDateTime();
|
|
|
+ LocalDateTime newLocalDate = localDate.plusDays(days);
|
|
|
+ LocalDateTime endOfDay = newLocalDate.withHour(23)
|
|
|
+ .withMinute(59)
|
|
|
+ .withSecond(59)
|
|
|
+ .withNano(999_999_999);
|
|
|
+ return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateAssessmentTimeRecursively(Date assessmentTime, int days, String userId,String grade) {
|
|
|
+ Date newDate = addDaysToday(assessmentTime, days);
|
|
|
+ while (!isWithinDays(newDate, days)) {
|
|
|
+ newDate = addDaysToday(newDate, days);
|
|
|
}
|
|
|
+ partnerUserMapper.updateAssessmentTimeByDate(userId, newDate);
|
|
|
+ }
|
|
|
|
|
|
- return false;
|
|
|
+
|
|
|
+ public Date addDaysToday(Date date, int days) {
|
|
|
+ LocalDateTime localDate = date.toInstant()
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
+ .toLocalDateTime();
|
|
|
+ LocalDateTime newLocalDate = localDate.plusDays(days);
|
|
|
+ // 设置时分秒为0
|
|
|
+ LocalDateTime newDateTime = newLocalDate.withHour(0)
|
|
|
+ .withMinute(0)
|
|
|
+ .withSecond(0)
|
|
|
+ .withNano(0);
|
|
|
+ return Date.from(newDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public boolean isWithinDays(Date date, int days) {
|
|
|
+ LocalDate targetDate = date.toInstant()
|
|
|
+ .atZone(ZoneId.systemDefault())
|
|
|
+ .toLocalDate();
|
|
|
+ return Math.abs(ChronoUnit.DAYS.between(LocalDate.now(), targetDate)) <= days;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public PartnerRuleEntity selectByGrade(String grade) {
|
|
|
return attributeMapper.selectByGrade(grade);
|