Commit 7c421d96 authored by licc's avatar licc

代码优化4

parent 1ee88893
...@@ -7,15 +7,18 @@ import cn.wisenergy.model.enums.RebateStatusEnum; ...@@ -7,15 +7,18 @@ import cn.wisenergy.model.enums.RebateStatusEnum;
import cn.wisenergy.model.enums.TradeRecordEnum; import cn.wisenergy.model.enums.TradeRecordEnum;
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.text.SimpleDateFormat; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author 86187 * @author 86187
...@@ -91,11 +94,17 @@ public class AccountManager { ...@@ -91,11 +94,17 @@ public class AccountManager {
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfos, List<ProgressPrize> prizes) { public void 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);
//判断 prizes 是否为空集合,是新增 //构造一个map key:userId value progressPrize
Map<String, ProgressPrize> map = new HashedMap(32);
for (ProgressPrize progressPrize : prizes) {
map.put(progressPrize.getUserId(), progressPrize);
}
//1、判断 prizes 是否为空集合,是新增
//新增 //新增
if (CollectionUtils.isEmpty(prizes)) { if (CollectionUtils.isEmpty(prizes)) {
for (TeamPerformanceSortVo sortVo : listVo) { for (TeamPerformanceSortVo sortVo : listVo) {
...@@ -108,7 +117,51 @@ public class AccountManager { ...@@ -108,7 +117,51 @@ public class AccountManager {
progressPrizeMapper.add(progressPrize); progressPrizeMapper.add(progressPrize);
} }
} else { } 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());
//遍历 筛选出要修改、新增、删除 对象
for (TeamPerformanceSortVo sortVo : listVo) {
if (userIds.contains(sortVo.getUserId())) {
//修改
ProgressPrize prize = map.get(sortVo.getUserId());
prize.setGrowthRate(sortVo.getGrowthRate());
prize.setAwardMoney(sortVo.getMonthPerformance());
updateList.add(prize);
//移除要修改的
prizes.remove(prize);
} else {
//新增
String userId = sortVo.getTeamPerformance().getUserId();
ProgressPrize progress = new ProgressPrize();
progress.setUserId(userId);
progress.setGrowthRate(sortVo.getGrowthRate());
progress.setAwardMoney(sortVo.getMonthPerformance());
progress.setYearMonth(yearMonth);
addList.add(progress);
}
}
deleteList.addAll(prizes);
for (ProgressPrize updatePrize : updateList) {
progressPrizeMapper.edit(updatePrize);
}
for (ProgressPrize addPrize : addList) {
progressPrizeMapper.add(addPrize);
}
for (ProgressPrize deletePrize : deleteList) {
progressPrizeMapper.delById(deletePrize.getId());
}
}
//3、添加账户获得的收益
for (AccountInfo accountInfo : accountInfoList) {
accountMapper.edit(accountInfo);
} }
} }
} }
...@@ -423,6 +423,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo> ...@@ -423,6 +423,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountInfo>
String yearMonth = DateUtil.convertDateToStr(date, PATTERN); String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
List<ProgressPrize> prizes = progressPrizeMapper.getByYearMonth(yearMonth); List<ProgressPrize> prizes = progressPrizeMapper.getByYearMonth(yearMonth);
//修改或保存最大进步奖信息
accountManager.updateOrSavePrize(listVo,updateAccountList,prizes); accountManager.updateOrSavePrize(listVo,updateAccountList,prizes);
} }
} }
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