PartnerRuleServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package com.ydtech.modules.partner.service.Impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import cn.hutool.extra.spring.SpringUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.ydtech.exception.SystemException;
  9. import com.ydtech.modules.partner.dao.PartnerAttributeMapper;
  10. import com.ydtech.modules.partner.dao.PartnerRuleMapper;
  11. import com.ydtech.modules.partner.dao.PartnerUserMapper;
  12. import com.ydtech.modules.partner.model.PartnerAttribute;
  13. import com.ydtech.modules.partner.model.PartnerRule;
  14. import com.ydtech.modules.partner.model.param.CommissionValidityPeriod;
  15. import com.ydtech.modules.partner.model.param.PartnerRuleEntity;
  16. import com.ydtech.modules.partner.service.PartnerRuleService;
  17. import com.ydtech.modules.partner.utils.ExpiredCommissionUtils;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.math.BigDecimal;
  23. import java.text.SimpleDateFormat;
  24. import java.time.LocalDate;
  25. import java.time.LocalDateTime;
  26. import java.time.ZoneId;
  27. import java.time.temporal.ChronoUnit;
  28. import java.util.*;
  29. @Service
  30. public class PartnerRuleServiceImpl extends ServiceImpl<PartnerRuleMapper, PartnerRule> implements PartnerRuleService {
  31. @Autowired
  32. private PartnerAttributeMapper attributeMapper;
  33. @Autowired
  34. private PartnerUserMapper partnerUserMapper;
  35. @Override
  36. @Transactional(rollbackFor = Exception.class)
  37. public int save(List<PartnerRuleEntity> ruleEntityList) {
  38. if (CollectionUtil.isEmpty(ruleEntityList)){
  39. throw new RuntimeException("参数不能为空");
  40. }
  41. int num = 0;
  42. try {
  43. for (PartnerRuleEntity ruleEntity : ruleEntityList) {
  44. PartnerRule partnerRule = new PartnerRule();
  45. BeanUtil.copyProperties(ruleEntity,partnerRule);
  46. if (StringUtils.isNotBlank(partnerRule.getId())){
  47. baseMapper.updateById(partnerRule);
  48. }else {
  49. baseMapper.insert(partnerRule);
  50. }
  51. if (StringUtils.equals("1",ruleEntity.getUpgrade())){
  52. PartnerAttribute cycle = ruleEntity.getCycle();
  53. if (ObjectUtil.isNotNull(cycle)){
  54. if (StringUtils.isNotBlank(cycle.getId())){
  55. attributeMapper.updateById(cycle);
  56. }else {
  57. cycle.setRuleId(partnerRule.getId());
  58. attributeMapper.insert(cycle);
  59. }
  60. }
  61. List<PartnerAttribute> requirement = ruleEntity.getRequirement();
  62. if (CollectionUtil.isNotEmpty(requirement)){
  63. for (PartnerAttribute attribute : requirement) {
  64. if (StringUtils.isNotBlank(attribute.getId())){
  65. attributeMapper.updateById(attribute);
  66. }else {
  67. attribute.setRuleId(partnerRule.getId());
  68. attributeMapper.insert(attribute);
  69. }
  70. }
  71. }
  72. }
  73. // 保级策略
  74. List<PartnerAttribute> strategy = ruleEntity.getStrategyArray();
  75. if (CollectionUtil.isNotEmpty(strategy)){
  76. for (PartnerAttribute attribute : strategy) {
  77. if (StringUtils.isNotBlank(attribute.getId())){
  78. attributeMapper.updateById(attribute);
  79. }else {
  80. attribute.setRuleId(partnerRule.getId());
  81. attributeMapper.insert(attribute);
  82. }
  83. }
  84. }
  85. num ++;
  86. }
  87. }catch (Exception e){
  88. e.printStackTrace();
  89. throw new RuntimeException("保存失败");
  90. }
  91. return num;
  92. }
  93. @Override
  94. public List<PartnerRuleEntity> queryAll() {
  95. List<PartnerRuleEntity> partnerRuleEntities = attributeMapper.queryAll();
  96. return partnerRuleEntities;
  97. }
  98. @Override
  99. public List<Map<String,String>> queryGradeName() {
  100. return attributeMapper.queryGradeName();
  101. }
  102. private PartnerRuleEntity validateGradeAndFetchRuleEntity(String grade) {
  103. if (StringUtils.isBlank(grade)) {
  104. throw new SystemException("等级不能为空");
  105. }
  106. PartnerRuleEntity partnerRuleEntity = attributeMapper.selectByGrade(grade);
  107. if (StringUtils.equals("2", partnerRuleEntity.getUpgrade())) {
  108. throw new SystemException("当前等级不支持升级");
  109. }
  110. return partnerRuleEntity;
  111. }
  112. @Override
  113. public boolean verifyUpgradeConditions(String userId, String grade) {
  114. if (StringUtils.equals("1", grade) || StringUtils.equals("0", grade)) {
  115. return false;
  116. }
  117. VerifyData data = extractCommonLogic(userId, grade);
  118. return verifyConditionsInternal(data, data.partnerRuleEntity.getRequirement(),
  119. data.partnerRuleEntity.getAppraise());
  120. }
  121. @Override
  122. public boolean verifyDowngradeUpgradeConditions(String userId, String grade) {
  123. VerifyData data = downgradeUpgradeConditions(userId, grade);
  124. return verifyConditionsInternal(data, data.partnerRuleEntity.getStrategyArray(),
  125. data.partnerRuleEntity.getAppraise());
  126. }
  127. /**
  128. * 内部通用的条件验证方法
  129. * @param data 验证数据
  130. * @param attributes 属性列表(要求或策略数组)
  131. * @param appraise 评估标志
  132. * @return 是否满足条件
  133. */
  134. private boolean verifyConditionsInternal(VerifyData data, List<PartnerAttribute> attributes, String appraise) {
  135. if (attributes == null || attributes.size() < 3) {
  136. return false;
  137. }
  138. boolean c1 = attributes.get(0).isChecked();
  139. boolean c2 = attributes.get(1).isChecked();
  140. boolean c3 = attributes.get(2).isChecked();
  141. BigDecimal p = data.p;
  142. BigDecimal bf = data.bf;
  143. BigDecimal ownBf = data.ownBf;
  144. BigDecimal min1 = new BigDecimal(attributes.get(0).getMin());
  145. BigDecimal min2 = new BigDecimal(attributes.get(1).getMin());
  146. BigDecimal min3 = new BigDecimal(attributes.get(2).getMin());
  147. boolean isAndLogic = StringUtils.equals("1", appraise);
  148. if (c1 && c2 && c3) {
  149. return isAndLogic
  150. ? p.compareTo(min1) >= 0 && bf.compareTo(min2) >= 0 && ownBf.compareTo(min3) >= 0
  151. : p.compareTo(min1) >= 0 || bf.compareTo(min2) >= 0 || ownBf.compareTo(min3) >= 0;
  152. }
  153. if (c1 && c2) {
  154. return isAndLogic
  155. ? p.compareTo(min1) >= 0 && bf.compareTo(min2) >= 0
  156. : p.compareTo(min1) >= 0 || bf.compareTo(min2) >= 0;
  157. }
  158. if (c1 && c3) {
  159. return isAndLogic
  160. ? p.compareTo(min1) >= 0 && ownBf.compareTo(min3) >= 0
  161. : p.compareTo(min1) >= 0 || ownBf.compareTo(min3) >= 0;
  162. }
  163. if (c2 && c3) {
  164. return isAndLogic
  165. ? bf.compareTo(min2) >= 0 && ownBf.compareTo(min3) >= 0
  166. : bf.compareTo(min2) >= 0 || ownBf.compareTo(min3) >= 0;
  167. }
  168. if (c1) return p.compareTo(min1) >= 0;
  169. if (c2) return bf.compareTo(min2) >= 0;
  170. if (c3) return ownBf.compareTo(min3) >= 0;
  171. return false;
  172. }
  173. @Override
  174. public String verifyUpgradeConditionsMessage(String userId, String grade) {
  175. VerifyData data = extractCommonLogic(userId, grade);
  176. PartnerRuleEntity partnerRuleEntity = data.partnerRuleEntity;
  177. BigDecimal p = data.p;
  178. BigDecimal bf = data.bf;
  179. List<PartnerAttribute> requirement = partnerRuleEntity.getRequirement();
  180. BigDecimal min = new BigDecimal(requirement.get(0).getMin());
  181. BigDecimal min1 = new BigDecimal(requirement.get(1).getMin());
  182. BigDecimal min2 = new BigDecimal(requirement.get(2).getMin());
  183. StringBuilder message = new StringBuilder();
  184. if (StringUtils.equals("1", partnerRuleEntity.getAppraise())) {
  185. if (p.compareTo(min) >= 0 && bf.compareTo(min1) >= 0) {
  186. message.append("邀请").append(min).append("个合伙人, 累计保费达").append(min1).append("元");
  187. }
  188. } else {
  189. if (p.compareTo(min) >= 0) {
  190. message.append("邀请").append(min).append("个合伙人");
  191. }
  192. if (bf != null && bf.compareTo(min1) >= 0) {
  193. if (message.length() > 0) {
  194. message.append(", ");
  195. }
  196. message.append("累计保费达").append(min1).append("元");
  197. }
  198. }
  199. return message.toString();
  200. }
  201. private VerifyData extractCommonLogic(String userId, String grade) {
  202. PartnerRuleEntity partnerRuleEntity = validateGradeAndFetchRuleEntity(grade);
  203. if (partnerRuleEntity == null) {
  204. throw new SystemException("合作伙伴规则实体不能为空");
  205. }
  206. PartnerAttribute cycle1 = partnerRuleEntity.getCycle();
  207. List<PartnerAttribute> requirement = partnerRuleEntity.getRequirement();
  208. if (CollectionUtil.isEmpty(requirement)) {
  209. throw new SystemException("考核条件不能为空");
  210. }
  211. if (StringUtils.equals("5", grade)){
  212. grade = "4";
  213. }
  214. CommissionValidityPeriod period = getCommissionValidityPeriod(cycle1, userId, grade);
  215. if (period == null) {
  216. throw new SystemException("有效周期不能为空");
  217. }
  218. BigDecimal p = getInviteData(userId, grade, period);
  219. BigDecimal bf = partnerUserMapper.subordinateMembersInsurancePremiums(userId, grade, period);
  220. // 查询当前用户出单的保费
  221. BigDecimal ownBf = partnerUserMapper.selectOwnSumPremium(userId, period);
  222. if (bf == null){
  223. bf = BigDecimal.ZERO;
  224. }
  225. if (ownBf == null){
  226. ownBf = BigDecimal.ZERO;
  227. }
  228. return new VerifyData(partnerRuleEntity, p, bf,ownBf);
  229. }
  230. private VerifyData downgradeUpgradeConditions(String userId, String grade) {
  231. PartnerRuleEntity partnerRuleEntity = validateGradeAndFetchRuleEntity(grade);
  232. if (partnerRuleEntity == null) {
  233. throw new SystemException("合作伙伴规则实体不能为空");
  234. }
  235. partnerRuleEntity.getStrategyArray();
  236. CommissionValidityPeriod period = partnerUserMapper.selectUpgradeCycleStartTime(userId, Integer.valueOf(grade));
  237. period.setEndDate(SpringUtil.getBean(ExpiredCommissionUtils.class).generateDowngradeTime(grade,period.getStartDate()));
  238. BigDecimal p = getInviteData(userId, grade, period);
  239. BigDecimal bf = partnerUserMapper.subordinateMembersInsurancePremiums(userId, grade, period);
  240. // 查询当前用户出单的保费
  241. BigDecimal ownBf = partnerUserMapper.selectOwnSumPremium(userId, period);
  242. if (bf == null){
  243. bf = BigDecimal.ZERO;
  244. }
  245. if (ownBf == null){
  246. ownBf = BigDecimal.ZERO;
  247. }
  248. return new VerifyData(partnerRuleEntity, p, bf,ownBf);
  249. }
  250. private static class VerifyData {
  251. private final PartnerRuleEntity partnerRuleEntity;
  252. private final BigDecimal p;
  253. private final BigDecimal bf;
  254. private final BigDecimal ownBf;
  255. public VerifyData(PartnerRuleEntity partnerRuleEntity, BigDecimal p, BigDecimal bf,BigDecimal ownBf) {
  256. this.partnerRuleEntity = partnerRuleEntity;
  257. this.p = p;
  258. this.bf = bf;
  259. this.ownBf = ownBf;
  260. }
  261. }
  262. @Override
  263. public BigDecimal getInviteData(String userId, String grade,CommissionValidityPeriod period){
  264. int number = 0;
  265. if (StringUtils.equals("2", grade)){
  266. number = partnerUserMapper.assessment(userId,"3",period);
  267. }else if (StringUtils.equals("3", grade)){
  268. number = partnerUserMapper.assessment(userId,"4",period);
  269. }else if (StringUtils.equals("4", grade)){
  270. number = partnerUserMapper.assessment(userId,"5",period);
  271. }
  272. return new BigDecimal(number);
  273. }
  274. @Override
  275. public CommissionValidityPeriod getCommissionValidityPeriod(PartnerAttribute cycle1, String userId, String grade) {
  276. CommissionValidityPeriod period = new CommissionValidityPeriod();
  277. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  278. if (StringUtils.equals("1", cycle1.getCycle())) {
  279. return period;
  280. } else if (StringUtils.equals("2", cycle1.getCycle())) {
  281. Date upgradeTime = partnerUserMapper.selectUpgradeTime(userId, grade);
  282. if (upgradeTime == null) {
  283. partnerUserMapper.updateUpgradeTime(userId,grade);
  284. upgradeTime = partnerUserMapper.selectUpgradeTime(userId, grade);
  285. if (Objects.isNull(upgradeTime)){
  286. return period;
  287. }
  288. }
  289. String fixedDays = cycle1.getFixedDays();
  290. if (!isWithinDays(upgradeTime,Integer.valueOf(fixedDays) )) {
  291. updateUpgradeTimeByDate(upgradeTime,Integer.valueOf(fixedDays),userId,grade );
  292. upgradeTime = partnerUserMapper.selectUpgradeTime(userId, grade);
  293. }
  294. period.setStartDate(dateFormat.format(upgradeTime));
  295. period.setEndDate(dateFormat.format(addDays(upgradeTime,Integer.valueOf(fixedDays))));
  296. } else if (StringUtils.equals("3", cycle1.getCycle())) {
  297. // Date fixedCycle = partnerUserMapper.selectFixedCycleTime(userId, grade);
  298. // if (fixedCycle == null) {
  299. // partnerUserMapper.updateFixedCycleTime(userId);
  300. // fixedCycle = partnerUserMapper.selectFixedCycleTime(userId, grade);
  301. // if (fixedCycle == null){
  302. // return period;
  303. // }
  304. // }
  305. // period.setStartDate(rules.getStartDate());
  306. // period.setEndDate(rules.getEndDate());
  307. }
  308. return period;
  309. }
  310. public Date addDays(Date date, int days) {
  311. LocalDateTime localDate = date.toInstant()
  312. .atZone(ZoneId.systemDefault())
  313. .toLocalDateTime();
  314. LocalDateTime newLocalDate = localDate.plusDays(days);
  315. LocalDateTime endOfDay = newLocalDate.withHour(23)
  316. .withMinute(59)
  317. .withSecond(59)
  318. .withNano(999_999_999);
  319. return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
  320. }
  321. private void updateUpgradeTimeByDate(Date assessmentTime, int days, String userId,String grade) {
  322. Date newDate = addDaysToday(assessmentTime, days);
  323. while (!isWithinDays(newDate, days)) {
  324. newDate = addDaysToday(newDate, days);
  325. }
  326. partnerUserMapper.updateUpgradeTimeByDate(userId, newDate,Integer.valueOf(grade));
  327. }
  328. public Date addDaysToday(Date date, int days) {
  329. LocalDateTime localDate = date.toInstant()
  330. .atZone(ZoneId.systemDefault())
  331. .toLocalDateTime();
  332. LocalDateTime newLocalDate = localDate.plusDays(days);
  333. // 设置时分秒为0
  334. LocalDateTime newDateTime = newLocalDate.withHour(0)
  335. .withMinute(0)
  336. .withSecond(0)
  337. .withNano(0);
  338. return Date.from(newDateTime.atZone(ZoneId.systemDefault()).toInstant());
  339. }
  340. public boolean isWithinDays(Date date, int days) {
  341. LocalDate targetDate = date.toInstant()
  342. .atZone(ZoneId.systemDefault())
  343. .toLocalDate();
  344. return Math.abs(ChronoUnit.DAYS.between(LocalDate.now(), targetDate)) <= days;
  345. }
  346. @Override
  347. public PartnerRuleEntity selectByGrade(String grade) {
  348. return attributeMapper.selectByGrade(grade);
  349. }
  350. @Override
  351. public List<PartnerRule> selectAll() {
  352. return baseMapper.selectList(new LambdaQueryWrapper<>(PartnerRule.class).notIn(PartnerRule::getGrade,5).orderByAsc(PartnerRule::getGrade));
  353. }
  354. public boolean compare(String str1 ,String str2){
  355. try {
  356. int num1 = Integer.parseInt(str1);
  357. int num2 = Integer.parseInt(str2);
  358. if (num1 > num2) {
  359. return true;
  360. } else if (num1 < num2) {
  361. return false;
  362. } else {
  363. return true;
  364. }
  365. } catch (NumberFormatException e) {
  366. System.err.println("字符串包含非数字字符!");
  367. return false;
  368. }
  369. }
  370. }