Commit 582ff44c authored by licc's avatar licc

月度任务接口优化

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