Commit 582ff44c authored by licc's avatar licc

月度任务接口优化

parent e9904ab8
...@@ -8,17 +8,13 @@ import cn.wisenergy.model.enums.TradeRecordEnum; ...@@ -8,17 +8,13 @@ import cn.wisenergy.model.enums.TradeRecordEnum;
import cn.wisenergy.model.enums.TradeStatusEnum; import cn.wisenergy.model.enums.TradeStatusEnum;
import cn.wisenergy.model.vo.TeamPerformanceSortVo; import cn.wisenergy.model.vo.TeamPerformanceSortVo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -46,6 +42,9 @@ public class AccountManager { ...@@ -46,6 +42,9 @@ public class AccountManager {
@Autowired @Autowired
private ProgressPrizeMapper progressPrizeMapper; private ProgressPrizeMapper progressPrizeMapper;
@Autowired
private TradeRecordMapper tradeRecordMapper;
private static final String PATTERN = "yyyy-MM"; private static final String PATTERN = "yyyy-MM";
...@@ -102,12 +101,12 @@ public class AccountManager { ...@@ -102,12 +101,12 @@ public class AccountManager {
* @param prizes 上一次统计的进步奖 对象 * @param prizes 上一次统计的进步奖 对象
*/ */
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfoList, List<ProgressPrize> prizes) { public Boolean updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfoList, List<ProgressPrize> prizes) {
Date date = new Date(); Date date = new Date();
String yearMonth = DateUtil.convertDateToStr(date, PATTERN); String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
//构造一个map key:userId value progressPrize //构造一个map key:userId value progressPrize
Map<String, ProgressPrize> map = new HashedMap(32); Map<String, ProgressPrize> map = new HashMap<>(32);
for (ProgressPrize progressPrize : prizes) { for (ProgressPrize progressPrize : prizes) {
map.put(progressPrize.getUserId(), progressPrize); map.put(progressPrize.getUserId(), progressPrize);
} }
...@@ -122,13 +121,15 @@ public class AccountManager { ...@@ -122,13 +121,15 @@ public class AccountManager {
progressPrize.setGrowthRate(sortVo.getGrowthRate()); progressPrize.setGrowthRate(sortVo.getGrowthRate());
progressPrize.setAwardMoney(sortVo.getMonthPerformance()); progressPrize.setAwardMoney(sortVo.getMonthPerformance());
progressPrize.setYearMonth(yearMonth); progressPrize.setYearMonth(yearMonth);
progressPrizeMapper.add(progressPrize); int count = progressPrizeMapper.add(progressPrize);
if (count == 0) {
return false;
}
} }
} else { } else {
//2、不为空。存在的修改,不存在的新增,多余的,删除 //2、不为空。存在的修改,不存在的新增,多余的,删除
List<ProgressPrize> updateList = new ArrayList<>(); List<ProgressPrize> updateList = new ArrayList<>();
List<ProgressPrize> addList = new ArrayList<>(); List<ProgressPrize> addList = new ArrayList<>();
List<ProgressPrize> deleteList = new ArrayList<>();
List<String> userIds = prizes.stream().map(ProgressPrize::getUserId).collect(Collectors.toList()); List<String> userIds = prizes.stream().map(ProgressPrize::getUserId).collect(Collectors.toList());
//遍历 筛选出要修改、新增、删除 对象 //遍历 筛选出要修改、新增、删除 对象
...@@ -152,18 +153,27 @@ public class AccountManager { ...@@ -152,18 +153,27 @@ public class AccountManager {
addList.add(progress); addList.add(progress);
} }
} }
deleteList.addAll(prizes); List<ProgressPrize> deleteList = new ArrayList<>(prizes);
for (ProgressPrize updatePrize : updateList) { for (ProgressPrize updatePrize : updateList) {
progressPrizeMapper.edit(updatePrize); int count = progressPrizeMapper.edit(updatePrize);
if (count == 0) {
return false;
}
} }
for (ProgressPrize addPrize : addList) { for (ProgressPrize addPrize : addList) {
progressPrizeMapper.add(addPrize); int count = progressPrizeMapper.add(addPrize);
if (count == 0) {
return false;
}
} }
for (ProgressPrize deletePrize : deleteList) { for (ProgressPrize deletePrize : deleteList) {
progressPrizeMapper.delById(deletePrize.getId()); int count = progressPrizeMapper.delById(deletePrize.getId());
if (count == 0) {
return false;
}
} }
} }
...@@ -177,7 +187,42 @@ public class AccountManager { ...@@ -177,7 +187,42 @@ public class AccountManager {
tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode()); tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
tradeRecord.setTradeNo(null); tradeRecord.setTradeNo(null);
tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode()); tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
recordMapper.add(tradeRecord); int count = recordMapper.add(tradeRecord);
if (count == 0) {
return false;
}
}
return true;
}
/**
* 更新账户信息和保存交易流水记录
*
* @param accountInfoList 账户列表
* @param recordList 交易流水信息
*/
@Transactional(rollbackFor = Exception.class)
public Boolean updateAccountAddRecord(List<AccountInfo> accountInfoList, List<TradeRecord> recordList) {
//更新月收益
if (!CollectionUtils.isEmpty(accountInfoList)) {
for (AccountInfo accountInfo : accountInfoList) {
int count = accountMapper.updateById(accountInfo);
if (count == 0) {
return false;
}
}
}
//新增交易流水记录
if (!CollectionUtils.isEmpty(recordList)) {
for (TradeRecord tradeRecord : recordList) {
int count = tradeRecordMapper.add(tradeRecord);
if (count == 0) {
return false;
}
}
} }
return true;
} }
} }
...@@ -57,6 +57,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -57,6 +57,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
private static final String PATTERN = "yyyy-MM"; private static final String PATTERN = "yyyy-MM";
private static final Integer TWENTY = 20;
@Override @Override
public R<Boolean> orderRebate(List<OrderInfo> list) { public R<Boolean> orderRebate(List<OrderInfo> list) {
log.info("shop-mall[]AccountServiceImpl[]orderRebate[]input.param.list:{}", list.size()); log.info("shop-mall[]AccountServiceImpl[]orderRebate[]input.param.list:{}", list.size());
...@@ -171,8 +173,10 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -171,8 +173,10 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
if (CollectionUtils.isEmpty(userList)) { if (CollectionUtils.isEmpty(userList)) {
return R.ok(0, true); return R.ok(0, true);
} }
for (User user : userList) {
monthlyIncome(totalMoney, user); boolean bool = monthlyIncome(totalMoney, userList);
if (!bool) {
return R.ok(1, false);
} }
return R.ok(0, true); return R.ok(0, true);
...@@ -216,6 +220,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -216,6 +220,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
//获取20名进步最大的月业绩和 //获取20名进步最大的月业绩和
Double twentyTotal = teamPerformanceMapper.countTwenty(yearMonth); Double twentyTotal = teamPerformanceMapper.countTwenty(yearMonth);
List<AccountInfo> accountInfoList = new ArrayList<>();
List<TradeRecord> tradeRecordList = new ArrayList<>();
for (TeamPerformance user : list) { for (TeamPerformance user : list) {
//获取最大进步奖 百分比 //获取最大进步奖 百分比
MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(user.getUserLevel(), 3); MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(user.getUserLevel(), 3);
...@@ -229,9 +235,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -229,9 +235,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
//获取账户信息 //获取账户信息
AccountInfo accountInfo = accountMapper.getByUserId(user.getUserId()); AccountInfo accountInfo = accountMapper.getByUserId(user.getUserId());
accountInfo.setEarningsMonth(new BigDecimal(income)); accountInfo.setEarningsMonth(new BigDecimal(income));
accountInfoList.add(accountInfo);
//更新用户账户表
accountMapper.updateById(accountInfo);
//添加交易流水记录 //添加交易流水记录
TradeRecord tradeRecord = new TradeRecord(); TradeRecord tradeRecord = new TradeRecord();
...@@ -239,7 +243,13 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -239,7 +243,13 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode()); tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
tradeRecord.setTradeNo(null); tradeRecord.setTradeNo(null);
tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode()); tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
tradeRecordMapper.add(tradeRecord); tradeRecordList.add(tradeRecord);
}
//更新账户信息,添加交易流水记录
boolean bool = accountManager.updateAccountAddRecord(accountInfoList, tradeRecordList);
if (!bool) {
return R.ok(1, false);
} }
} }
return R.ok(0, true); return R.ok(0, true);
...@@ -256,9 +266,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -256,9 +266,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
List<TeamPerformanceSortVo> listVo = progressPrizeStatistics(userList); List<TeamPerformanceSortVo> listVo = progressPrizeStatistics(userList);
//5、如果集合大于20 ,取前二十名,小于,取全部 //5、如果集合大于20 ,取前二十名,小于,取全部
if (listVo.size() >= 20) { if (listVo.size() >= TWENTY) {
//取排名前20的 //取排名前20的
listVo.subList(0, 20); listVo.subList(0, TWENTY);
//计算前20的总业绩 //计算前20的总业绩
double total = listVo.stream().mapToDouble(TeamPerformanceSortVo::getMonthPerformance).sum(); double total = listVo.stream().mapToDouble(TeamPerformanceSortVo::getMonthPerformance).sum();
...@@ -284,64 +294,59 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -284,64 +294,59 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
/** /**
* 如果会员等级是黄金以上,计算月度收益 * 如果会员等级是黄金以上,计算月度收益
* *
* @param totalMoney * @param totalMoney 月金额总额
* @param user * @param userList 用户列表
* @return * @return
*/ */
private boolean monthlyIncome(BigDecimal totalMoney, User user) { private boolean monthlyIncome(BigDecimal totalMoney, List<User> userList) {
double total = totalMoney.doubleValue(); double total = totalMoney.doubleValue();
BigDecimal money;
Date date = new Date(); Date date = new Date();
String yearMonth = DateUtil.convertDateToStr(date, PATTERN); String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
//当月所有人订单成交总金额/3980/12 向下整 //当月所有人订单成交总金额/3980/12 向下整
double moneyMonth = Math.floor(total / 3980 / 12); double moneyMonth = Math.floor(total / 3980 / 12);
List<AccountInfo> accountInfoList = new ArrayList<>();
if (moneyMonth != 0) { List<TradeRecord> tradeRecordList = new ArrayList<>();
//获取等级优惠百分比 for (User user : userList) {
MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(user.getUserLevel(), 2); if (moneyMonth != 0) {
if (null != memberPercent) { //获取等级优惠百分比
//获取本月团队总金额 MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(user.getUserLevel(), 2);
TeamPerformance teamPerformance = teamPerformanceMapper.getByUserIdAndTime(user.getUserId(), yearMonth); if (null != memberPercent) {
//获取本月团队总金额
//获取该等级团队总金额 TeamPerformance teamPerformance = teamPerformanceMapper.getByUserIdAndTime(user.getUserId(), yearMonth);
Double teamTotal = teamPerformanceMapper.countTeamMoney(user.getUserLevel(), yearMonth);
if (null != teamPerformance && null != teamTotal) { //获取该等级团队总金额
double month = teamPerformance.getMonthTeamPerformance().doubleValue(); Double teamTotal = teamPerformanceMapper.countTeamMoney(user.getUserLevel(), yearMonth);
double percent = memberPercent.getPercent().doubleValue(); if (null != teamPerformance && null != teamTotal) {
//计算收益 double month = teamPerformance.getMonthTeamPerformance().doubleValue();
double income = moneyMonth * 3980 * percent * month / teamTotal; double percent = memberPercent.getPercent().doubleValue();
//计算收益
//获取账户信息 double income = moneyMonth * 3980 * percent * month / teamTotal;
AccountInfo accountInfo = accountMapper.getByUserId(user.getUserId());
BigDecimal bigDecimal = new BigDecimal(income); //获取账户信息
BigDecimal performanceMonth = accountInfo.getEarningsMonth().add(bigDecimal); AccountInfo accountInfo = accountMapper.getByUserId(user.getUserId());
accountInfo.setEarningsMonth(performanceMonth); BigDecimal bigDecimal = new BigDecimal(income);
BigDecimal performanceMonth = accountInfo.getEarningsMonth().add(bigDecimal);
BigDecimal performanceTotal = accountInfo.getEarningsMonth().add(bigDecimal); accountInfo.setEarningsMonth(performanceMonth);
accountInfo.setEarningsMonth(performanceTotal);
BigDecimal performanceTotal = accountInfo.getEarningsMonth().add(bigDecimal);
//更新月收益 accountInfo.setEarningsMonth(performanceTotal);
int count = accountMapper.updateById(accountInfo); accountInfoList.add(accountInfo);
if (count != 0) {
return true;
} //5、添加交易流水记录
TradeRecord tradeRecord = new TradeRecord();
//5、添加交易流水记录 tradeRecord.setUserId(user.getUserId());
TradeRecord tradeRecord = new TradeRecord(); tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode());
tradeRecord.setUserId(user.getUserId()); tradeRecord.setTradeNo(null);
tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode()); tradeRecord.setStatus(TradeStatusEnum.NO_SETTLE_ACCOUNTS.getCode());
tradeRecord.setTradeNo(null); tradeRecordList.add(tradeRecord);
tradeRecord.setStatus(TradeStatusEnum.NO_SETTLE_ACCOUNTS.getCode());
int number = tradeRecordMapper.add(tradeRecord);
if (number == 0) {
return false;
} }
} }
} }
} }
return false; //更新账户信息,新增交易流水记录
return accountManager.updateAccountAddRecord(accountInfoList, tradeRecordList);
} }
/** /**
...@@ -382,6 +387,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -382,6 +387,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
} }
teamVo.setGrowthRate(growthRate); teamVo.setGrowthRate(growthRate);
double monthPerformance; double monthPerformance;
assert teamPerformance != null;
if (null == teamPerformance.getMonthTeamPerformance()) { if (null == teamPerformance.getMonthTeamPerformance()) {
monthPerformance = 0.00; monthPerformance = 0.00;
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment