@deletemapping("deletequestion")
@apioperation(value = "删除问题")
public serverresponse deletequestion(integer id){
sysquestionmapper.deletebyprimarykey(id);
sysquestionanswermapper.deletebyquestionid(id);
return serverresponse.createbysuccess("删除成功");
}
@getmapping("getquestionlist")
@apioperation(value = "获得问题列表")
public serverresponse getquestionlist(){
list<sysquestion> list = sysquestionmapper.selectallquestion();
return serverresponse.createbysuccess(list);
}
@getmapping("getquestionanswerlist")
@apioperation(value = "获得问题选项列表")
public serverresponse getquestionanswerlist(integer question_id){
list<sysquestionanswer> list = sysquestionanswermapper.selectbyquestionid(question_id);
return serverresponse.createbysuccess(list);
}
@postmapping("addquestion")
@apioperation(value = "添加问题")
public serverresponse addquestion(string question,string[] answerlist,integer[] answer){
integer type = 1;
if (answer.length != 1) {
type = 2;
}
string stringanswer = "";
list<integer> list = arrays.aslist(answer);
sysquestion sysquestion = new sysquestion();
sysquestion.setquestionname(question);
sysquestion.setcreatetime(new date());
sysquestion.settype(type);
sysquestionmapper.insert(sysquestion);
integer question_id = sysquestionmapper.selectlastquestionid();
for (int i=0;i<answerlist.length;i++){
sysquestionanswer sysquestionanswer = new sysquestionanswer();
sysquestionanswer.setanswer(answerlist[i]);
sysquestionanswer.setquestionid(question_id);
sysquestionanswermapper.insert(sysquestionanswer);
integer answer_id = sysquestionanswermapper.selectlastanswerid();
if (list.contains(i)) {
stringanswer = stringanswer + "," + answer_id;
}
}
system.out.println(stringanswer);
stringanswer = stringanswer.substring(1,stringanswer.length());
system.out.println(stringanswer);
sysquestion sysquestion1 = sysquestionmapper.selectbyprimarykey(question_id);
sysquestion1.setanswerid(stringanswer);
sysquestionmapper.updatebyprimarykey(sysquestion1);
return serverresponse.createbysuccess("创建成功");
}
@postmapping("updatequestion")
@apioperation(value = "更新问题")
public serverresponse updatequestion(integer question_id,string question,string[] answerlist,integer[] answer){
integer type = 1;
if (answer.length != 1) {
type = 2;
}
string stringanswer = "";
list<integer> list = arrays.aslist(answer);
sysquestionanswermapper.deletebyquestionid(question_id);
for (int i=0;i<answerlist.length;i++){
sysquestionanswer sysquestionanswer = new sysquestionanswer();
sysquestionanswer.setanswer(answerlist[i]);
sysquestionanswer.setquestionid(question_id);
sysquestionanswermapper.insert(sysquestionanswer);
integer answer_id = sysquestionanswermapper.selectlastanswerid();
if (list.contains(i)) {
stringanswer = stringanswer + "," + answer_id;
}
}
stringanswer = stringanswer.substring(1,stringanswer.length());
sysquestion sysquestion1 = sysquestionmapper.selectbyprimarykey(question_id);
sysquestion1.setanswerid(stringanswer);
sysquestion1.setquestionname(question);
sysquestion1.settype(type);
sysquestion1.setupdatetime(new date());
sysquestionmapper.updatebyprimarykey(sysquestion1);
return serverresponse.createbysuccess("更新成功");
}
代码中涉及的sql语句
<select id="selectlastquestionid" resulttype="int">
select max(id) from sys_question
</select>
<select id="selectallquestion" resultmap="baseresultmap">
select * from sys_question order by create_time desc
</select>
<select id="selectbyquestionid" resultmap="baseresultmap">
select * from sys_question_answer
where question_id=#{question_id}
</select>
<select id="selectlastanswerid" resulttype="int">
select max(id) from sys_question_answer
</select>
<delete id="deletebyquestionid">
delete from sys_question_answer where question_id=#{question_id}
</delete>