实现对局录像页面
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
// 用于实现对局记录的分页功能
|
||||
package com.kob.backend.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MybatisConfig {
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.kob.backend.controller.record;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.kob.backend.service.record.GetRecordListService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class GetRecordListController {
|
||||
@Autowired
|
||||
private GetRecordListService getRecordListService;
|
||||
|
||||
// 这里只需要获取,因此只需要用 get 方法(需要新建和修改数据时,使用 post 方法)
|
||||
@GetMapping("/record/getlist/")
|
||||
public JSONObject getList(@RequestParam Map<String, String> data) {
|
||||
// 解析出 page 信息
|
||||
Integer page = Integer.parseInt(data.get("page_index"));
|
||||
return getRecordListService.getList(page);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
// 实现对局信息分页发送
|
||||
package com.kob.backend.service.impl.record;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.kob.backend.mapper.RecordMapper;
|
||||
import com.kob.backend.mapper.UserMapper;
|
||||
import com.kob.backend.pojo.Record;
|
||||
import com.kob.backend.pojo.User;
|
||||
import com.kob.backend.service.record.GetRecordListService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GetRecordListServiceImpl implements GetRecordListService {
|
||||
@Autowired
|
||||
private RecordMapper recordMapper;
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper; // 注入 UserMapper 用于查询用户名和用户头像
|
||||
|
||||
@Override
|
||||
public JSONObject getList(Integer page) {
|
||||
// 使用 Mybatis API : IPage 实现
|
||||
IPage<Record> recordIPage = new Page<>(page, 10); // 参数列表:Page<>(传第几页, 每一页有多少项目)
|
||||
QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.orderByDesc("id"); // 通过 record 的 id 降序排序
|
||||
List<Record> records = recordMapper.selectPage(recordIPage, queryWrapper).getRecords();
|
||||
|
||||
|
||||
JSONObject resp = new JSONObject();
|
||||
List<JSONObject> items = new LinkedList<>();
|
||||
for (Record record : records) { // 枚举对局记录
|
||||
// 获取用户 A 和 B
|
||||
User userA = userMapper.selectById(record.getAId());
|
||||
User userB = userMapper.selectById(record.getBId());
|
||||
|
||||
// 取出 A 和 B 的用户名和头像信息
|
||||
String aName = userA.getUsername(), bName = userB.getUsername();
|
||||
String aPhoto = userA.getPhoto(), bPhoto = userB.getPhoto();
|
||||
|
||||
String winner = "平局!"; // 定义赢家
|
||||
|
||||
// 将 A 和 B 的用户名和头像信息存到 jsonObject 对象 item 中
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("a_username", aName);
|
||||
item.put("a_photo", aPhoto);
|
||||
item.put("b_username", bName);
|
||||
item.put("b_photo", bPhoto);
|
||||
if ("A".equals(record.getLoser())) winner = userB.getUsername() + " 胜";
|
||||
else if ("B".equals(record.getLoser())) winner = userA.getUsername() + " 胜";
|
||||
item.put("winner", winner); // 存入赢家信息
|
||||
item.put("record", record); // 存入对战信息
|
||||
// 将 jsonObject 对象 item 中,添加到 items 列表中
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
resp.put("records", items); // 将 items 信息存入 records 中
|
||||
resp.put("records_count", recordMapper.selectCount(null)); // 存入当前记录页面的总数
|
||||
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.kob.backend.service.record;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
public interface GetRecordListService {
|
||||
// 参数:分页面列表编号
|
||||
JSONObject getList(Integer page);
|
||||
}
|
||||
Reference in New Issue
Block a user