HOJ二次开发—增加班级同步题单功能
原文链接:HOJ二次开发—增加班级同步题单功能 – 每天进步一点点
</vxe-table-column>
</vxe-table>
<Pagination
:total="total"
:page-size="limit"
@on-change="currentChange"
:current.sync="currentPage"
@on-page-size-change="onPageSizeChange"
:layout="'prev, pager, next, sizes'"
></Pagination>
</template>
<TrainingList
ref="trainingList"
v-if="adminPage && !createPage && !problemPage"
@handleEditPage="handleEditPage"
@currentChange="currentChange"
@handleProblemPage="handleProblemPage"
></TrainingList>
<TrainingProblemList
v-if="problemPage"
:trainingId="trainingId"
@currentChangeProblem="currentChangeProblem"
@handleEditProblemPage="handleEditProblemPage"
ref="trainingProblemList"
>
</TrainingProblemList>
<Training
v-if="createPage && !editPage && !problemPage"
mode="add"
:title="$t('m.Create_Training')"
apiMethod="addGroupTraining"
@handleCreatePage="handleCreatePage"
@currentChange="currentChange"
></Training>
<!-- 团队添加公共题目 -->
<el-dialog
:title="$t('m.Add_Training_Problem')"
width="90%"
:visible.sync="publicPage"
:close-on-click-modal="false"
>
<AddPublicProblem
v-if="publicPage"
:trainingId="trainingId"
apiMethod="getGroupTrainingProblemList"
@currentChangeProblem="currentChangeProblem"
ref="addPublicProblem"
></AddPublicProblem>
</el-dialog>
<!-- 团队添加团队题目 -->
<el-dialog
:title="$t('m.Add_Training_Problem')"
width="350px"
:visible.sync="groupPage"
:close-on-click-modal="false"
>
<AddGroupProblem
:trainingId="trainingId"
@currentChangeProblem="currentChangeProblem"
@handleGroupPage="handleGroupPage"
></AddGroupProblem>
</el-dialog>
<!-- 打开公共题单弹窗 -->
<el-dialog
:title="$t('m.Training_To_Group')"
width="850px"
:visible.sync="groupListPage"
:close-on-click-modal="true"
>
<SyncGroupList
:groupID="groupID"
@syncGroupListClose="syncGroupListClose"
ref="SyncGroupListChild"
> </SyncGroupList>
</el-dialog>
</el-card>
</template>
<script>
import { mapGetters } from "vuex";
import { TRAINING_TYPE } from "@/common/constants";
import Pagination from "@/components/oj/common/Pagination";
import TrainingList from "@/components/oj/group/TrainingList";
import Training from "@/components/oj/group/Training";
import TrainingProblemList from "@/components/oj/group/TrainingProblemList";
import AddPublicProblem from "@/components/oj/group/AddPublicProblem.vue";
import AddGroupProblem from "@/components/oj/group/AddGroupProblem.vue";
import SyncGroupList from "@/components/oj/group/SyncGroupList.vue";
import api from "@/common/api";
export default {
name: "GroupTrainingList",
components: {
Pagination,
TrainingList,
Training,
TrainingProblemList,
AddPublicProblem,
AddGroupProblem,
SyncGroupList,
},
data() {
return {
total: 0,
currentPage: 1,
limit: 10,
trainingList: [],
TRAINING_TYPE: {},
loading: false,
adminPage: false,
createPage: false,
editPage: false,
problemPage: false,
publicPage: false,
groupPage: false,
groupListPage: false, //打开同步题单弹窗
editProblemPage: false,
trainingId: null,
groupID:"", //团队/班级id
};
},
mounted() {
this.TRAINING_TYPE = Object.assign({}, TRAINING_TYPE);
this.init();
},
methods: {
init() {
this.groupID=this.$route.params.groupID
this.getGroupTrainingList();
},
onPageSizeChange(pageSize) {
this.limit = pageSize;
this.init();
},
currentChange(page) {
this.currentPage = page;
this.init();
},
currentChangeProblem() {
this.$refs.trainingProblemList.currentChange(1);
},
getGroupTrainingList() {
this.trainingList = [];
this.loading = true;
api
.getGroupTrainingList(
this.currentPage,
this.limit,
this.$route.params.groupID
)
.then(
(res) => {
this.trainingList = res.data.data.records;
console.log("团队this.trainingList ", this.trainingList);
this.total = res.data.data.total;
this.loading = false;
},
(err) => {
this.loading = false;
}
);
},
goGroupTraining(event) {
this.$router.push({
name: "GroupTrainingDetails",
params: {
trainingID: event.row.id,
groupID: this.$route.params.groupID,
},
});
},
handleCreatePage() {
this.createPage = !this.createPage;
},
// 公共训练同步至班级
handleTrainingToGroup() {
console.log("公共训练同步至班级");
this.groupListPage = true;
setTimeout(() => {
this.$refs.SyncGroupListChild.getTrainingList();
});
},
//关闭弹窗
syncGroupListClose(){
this.groupListPage=false
this.getGroupTrainingList()
},
handleEditPage() {
this.editPage = !this.editPage;
this.$refs.trainingList.editPage = this.editPage;
},
handleAdminPage() {
this.adminPage = !this.adminPage;
this.createPage = false;
this.editPage = false;
},
handleProblemPage(trainingId) {
this.trainingId = trainingId;
this.problemPage = !this.problemPage;
},
handleGroupPage() {
this.groupPage = !this.groupPage;
},
handleEditProblemPage() {
this.editProblemPage = !this.editProblemPage;
this.$refs.trainingProblemList.editPage = this.editProblemPage;
},
getPassingRate(ac, total) {
if (!total) {
return 0;
}
return ((ac / total) * 100).toFixed(2);
},
},
computed: {
...mapGetters(["isAuthenticated", "isSuperAdmin", "isGroupAdmin"]),
},
};
</script>
<style scoped>
.title {
font-size: 20px;
vertical-align: middle;
float: left;
}
.filter-row {
margin-bottom: 5px;
text-align: center;
}
@media screen and (max-width: 768px) {
.filter-row span {
margin-left: 5px;
margin-right: 5px;
}
}
@media screen and (min-width: 768px) {
.filter-row span {
margin-left: 10px;
margin-right: 10px;
}
}
</style>
后台的实现也比较简单,原作者留下了很多现成的接口,直接使用就行。
String groupID=jsonObject.getStr("groupID"); //要同步到哪个班级
JSONArray trainingList= jsonObject.getJSONArray("selectedTrain"); //题单列表(id)
//循环遍历每一个题单数据
for(Object o:trainingList){
Long tid = Long.valueOf(o.toString());
//获取当前训练
Training training = trainingEntityService.getById(tid);
if (training == null) {
throw new StatusNotFoundException("该训练不存在!");
}
training.setGid(Long.valueOf(groupID));
//获取训练的类别
QueryWrapper<MappingTrainingCategory> mappingTrainingCategoryQueryWrapper = new QueryWrapper<>();
mappingTrainingCategoryQueryWrapper.eq("tid", tid);
MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryEntityService.getOne(mappingTrainingCategoryQueryWrapper);
TrainingCategory trainingCategory = null;
if (mappingTrainingCategory != null) {
trainingCategory = trainingCategoryEntityService.getById(mappingTrainingCategory.getCid());
}
TrainingDTO trainingDto = new TrainingDTO();
trainingDto.setTraining(training);
trainingDto.setTrainingCategory(trainingCategory);
Long newTrainId = groupTrainingManager.addTraining(trainingDto); //新的训练id
//往新题单里插入题目
trainingProblemMapper.getOldProblemAndInsertNew(tid,newTrainId);
}
大部分都是现成的,只有getOldProblemAndInsertNew这个是自己写的,也很简单:
<select id="getOldProblemAndInsertNew">
insert into training_problem(tid,pid,display_id)
SELECT #{newTrainId},pid,display_id from training_problem
WHERE tid=#{oldTrainId}
</select>
这里的oldTrainId对应上面的tid。
3.缺点
这个功能只能将公共训练的题单同步到班级内,每次都是新的,无法将原来一模一样的题单进行更新,好在做题记录都是通用的。
后面的文章再写一篇同步题库的。