public boolean buy(String username, String componname) { List<User> users = userMapper.selectByUsername(username); List<Compon> compons = componMapper.selectAllByComponname(componname); Integer userid = users.get(0).getUserid(); Integer componid = compons.get(0).getComponid(); QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("userid",userid); queryWrapper.eq("componid",componid); Order order = orderMapper.selectOne(queryWrapper); if (order == null){ orderMapper.insert(new Order(userid, componid)); return true; }else { int res = orderMapper.updateNumberByUseridAndComponid(userid, componid); if (res > 0) return true; else return false; } }
时间: 2024-04-01 08:35:51 浏览: 24
这段代码实现了一个购买组件的功能。首先根据用户名和组件名查询用户和组件信息,然后获取用户 ID 和组件 ID。接着使用 MyBatis-Plus 提供的 QueryWrapper 构造查询条件,查询订单表中是否已经存在该用户对应该组件的订单记录。如果订单不存在,则向订单表插入一条新的记录,并返回 true 表示购买成功;否则更新订单数量并返回 true,若更新失败则返回 false。
需要注意的是,这段代码中没有对用户账户余额进行判断,如果用户账户余额不足以支付该组件的价格,购买会失败。同时,如果同一用户购买同一组件的操作过于频繁,可能会导致数据库中出现重复的订单记录,需要在应用层面上加以限制。
相关问题
List<ProtocolActualExecutionInfo> protocolActualExecutionInfos = protocolActualExecutionInfoMapper.selectByExample(protocolActualExecutionInfoExample); List<ProtocolDetailInfo> protocolDetailInfos = protocolDetailInfoMapper.selectByExample(protocolDetailInfoExample);根据ProtocolActualExecutionInfo中的dlcgid+dlcgxyid与ProtocolDetailInfo中的dlcgid+dlcgxyid匹配合并成一个list
可以使用Java8的Stream API来实现这个需求,具体代码如下:
```
List<ProtocolActualExecutionInfo> protocolActualExecutionInfos = protocolActualExecutionInfoMapper.selectByExample(protocolActualExecutionInfoExample);
List<ProtocolDetailInfo> protocolDetailInfos = protocolDetailInfoMapper.selectByExample(protocolDetailInfoExample);
List<ProtocolInfo> protocolInfos = Stream.concat(
protocolActualExecutionInfos.stream().map(info -> new ProtocolInfo(info.getDlcgid(), info.getDlcgxyid())),
protocolDetailInfos.stream().map(info -> new ProtocolInfo(info.getDlcgid(), info.getDlcgxyid()))
)
.distinct()
.collect(Collectors.toList());
```
其中,`ProtocolInfo`是一个简单的POJO类,表示`dlcgid`和`dlcgxyid`的组合:
```
public class ProtocolInfo {
private String dlcgid;
private String dlcgxyid;
public ProtocolInfo(String dlcgid, String dlcgxyid) {
this.dlcgid = dlcgid;
this.dlcgxyid = dlcgxyid;
}
// 省略getter和setter方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProtocolInfo that = (ProtocolInfo) o;
return Objects.equals(dlcgid, that.dlcgid) &&
Objects.equals(dlcgxyid, that.dlcgxyid);
}
@Override
public int hashCode() {
return Objects.hash(dlcgid, dlcgxyid);
}
}
```
这里使用了`Stream.concat()`方法将两个Stream合并起来,然后使用`distinct()`方法去重,最后使用`Collectors.toList()`方法将结果收集到一个List中。
优化代码Set<String> rdsSet = new HashSet<>(); Set<String> empSet = new HashSet<>(); int rdsPageNum = 1; int empPageNum = 1; boolean rdsFinished = false; boolean empFinished = false; while (!rdsFinished || !empFinished) { if (!rdsFinished) { PageHelper.startPage(rdsPageNum, 1000); List<String> accountCode = idmEmployeeMapper.getaccountCode(); if (accountCode.isEmpty()) { rdsFinished = true; } else { rdsSet.addAll(accountCode); rdsPageNum++; } } if (!empFinished) { PageHelper.startPage(empPageNum, 1000); List<String> userId = idmEmployeeMapper.getEmployeeUserId(); if (userId.isEmpty()) { empFinished = true; } else { empSet.addAll(userId); empPageNum++; } } } List<String> result = new ArrayList<>(empSet); result.removeAll(rdsSet);
可以考虑将两个while循环合并成一个,减少重复代码。同时,可以使用Set的addAll方法来添加元素,避免每次添加都需要遍历一遍。最终代码可优化为:
```
Set<String> rdsSet = new HashSet<>();
Set<String> empSet = new HashSet<>();
int rdsPageNum = 1;
int empPageNum = 1;
boolean rdsFinished = false;
boolean empFinished = false;
while (!rdsFinished || !empFinished) {
List<String> accountCode = new ArrayList<>();
List<String> userId = new ArrayList<>();
if (!rdsFinished) {
PageHelper.startPage(rdsPageNum, 1000);
accountCode = idmEmployeeMapper.getaccountCode();
if (accountCode.isEmpty()) {
rdsFinished = true;
} else {
rdsSet.addAll(accountCode);
rdsPageNum++;
}
}
if (!empFinished) {
PageHelper.startPage(empPageNum, 1000);
userId = idmEmployeeMapper.getEmployeeUserId();
if (userId.isEmpty()) {
empFinished = true;
} else {
empSet.addAll(userId);
empPageNum++;
}
}
// 如果两个列表都为空,则跳出循环
if (accountCode.isEmpty() && userId.isEmpty()) {
break;
}
}
List<String> result = new ArrayList<>(empSet);
result.removeAll(rdsSet);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)