@iamfox
2016-10-09T15:40:22.000000Z
字数 5045
阅读 1464
学习
这是知识库模块,修改试卷的实现。
//选择和修改试卷试题
public void ModifyKBQuestionnaire(ModifyKBQuestionCommand command) throws KBQuestionException, KBResourceException{
KBQuestionnaire kbQuestionnaire = (KBQuestionnaire) get(command.getQuestionnaireId());
if(kbQuestionnaire == null){
throw new KBResourceException(KBResourceException.ENTITY_NOT_FOUND, "试卷不存在");
}
List<String> addList = command.getAddQuestionIds();
List<String> delList = command.getDelQuestionIds();
System.out.println(addList.size());
System.out.println(delList.size());
String a=addList.get(0);
String[] b = a.split(",");
System.out.println(b.length);
if (StringUtils.isNotBlank(b[0])) {
for (int j = 0; j < b.length; j++) {
KBQuestion kbQuestion = kbQuestionService.get(b[j]);
if(kbQuestion == null){
throw new KBQuestionException(KBQuestionException.ENTITY_NOT_FOUND, "问题不存在");
}
KBQuestionnaireAndQuestionLink link = new KBQuestionnaireAndQuestionLink();
link.create(kbQuestion, kbQuestionnaire);
kbQuestionnaire.addQuestionNum(1);
update(kbQuestionnaire);
save(link);
}
}
String c=delList.get(0);
String[] d = c.split(",");
System.out.println(d.length);
if (StringUtils.isNotBlank(d[0]) ){
for (int j = 0; j < d.length; j++) {
System.out.println(d[j]);
KBQuestionQO kbQuestionQO=new KBQuestionQO();
kbQuestionQO.setId(d[j]);
KBQuestionnaireAndQuestionLinkQO kbQuestionnaireAndQuestionLinkQO=new KBQuestionnaireAndQuestionLinkQO();
kbQuestionnaireAndQuestionLinkQO.setKbQuestionQO(kbQuestionQO);
KBQuestionnaireAndQuestionLink link=kbQuestionnaireAndQuestionLinkService.queryUnique(kbQuestionnaireAndQuestionLinkQO);
if(link != null){
kbQuestionnaireAndQuestionLinkService.deleteById(link.getId());
kbQuestionnaire.delQuestionNum(1);
update(kbQuestionnaire);
}
}}
}
/**
* 修改试卷题目
*/
public class ModifyKBQuestionCommand extends BaseCommand {
private List<String> addQuestionIds;//问题id
private List<String> delQuestionIds;//问题id
private String questionnaireId;//试卷id
public List<String> getAddQuestionIds() {
return addQuestionIds;
}
public void setAddQuestionIds(List<String> addQuestionIds) {
this.addQuestionIds = addQuestionIds;
}
public List<String> getDelQuestionIds() {
return delQuestionIds;
}
public void setDelQuestionIds(List<String> delQuestionIds) {
this.delQuestionIds = delQuestionIds;
}
public String getQuestionnaireId() {
return questionnaireId;
}
public void setQuestionnaireId(String questionnaireId) {
this.questionnaireId = questionnaireId;
}
update(kbQuestionnaire);
没有意义,如果不是hibernate自动优化这里update sql语句已经泛滥了。再次强调:
- 代码写给人看比跑起来更重要,不要为了省几秒钟时间随意乱写!节省的时间最后会在调试和扩展时十倍几十倍耗掉。
- 代码混乱一般不是因为逻辑复杂,也和技术水平无关,先规划,不要想到哪写到哪。
要修改的command,实体id是最重要的参数,放最上面。变量名和注释都要表达准确含义。少用缩略英文写法,每个人缩略习惯不一样,大多数缩略只会降低整体阅读效率。
/**
* 修改试卷题目
*/
@SuppressWarnings("serial")
public class ModifyKBQuestionnaireCommand extends BaseCommand {
private String questionnaireId;// 试卷id
private List<String> addQuestionIds;// 要添加的问题id集合
private List<String> deleteQuestionIds;// 要删除的问题id集合
public List<String> getAddQuestionIds() {
return addQuestionIds;
}
public void setAddQuestionIds(List<String> addQuestionIds) {
this.addQuestionIds = addQuestionIds;
}
public List<String> getDeleteQuestionIds() {
return deleteQuestionIds;
}
public void setDeleteQuestionIds(List<String> deleteQuestionIds) {
this.deleteQuestionIds = deleteQuestionIds;
}
public String getQuestionnaireId() {
return questionnaireId;
}
public void setQuestionnaireId(String questionnaireId) {
this.questionnaireId = questionnaireId;
}
}
public void modifyKBQuestionnaire(ModifyKBQuestionnaireCommand command) throws KBResourceException {
// 查出要修改的试卷
// 查出要添加的全部问题
// 遍历问题逐个创建和试卷的关联
// 查出要删除的全部试卷问题关联
// 循环删除关联
// 保存更新试卷问题数量统计
}
整个方法其实就几大步骤,用注释规划好步骤。
public void modifyKBQuestionnaire(ModifyKBQuestionnaireCommand command) throws KBResourceException {
// 查出要修改的试卷
KBQuestionnaire questionnaire = get(command.getQuestionnaireId());
if (questionnaire == null) {
throw new KBResourceException(KBResourceException.ENTITY_NOT_FOUND, "试卷不存在");
}
// 查出要添加的全部问题
KBQuestionQO questionQO = new KBQuestionQO();
questionQO.setIds(new ArrayList<String>());
for (String addQuestionId : command.getAddQuestionIds()) {
questionQO.getIds().add(addQuestionId);
}
List<KBQuestion> questions = questionService.queryList(questionQO);
// 遍历问题逐个创建和试卷的关联
for (KBQuestion question : questions) {
KBQuestionnaireAndQuestionLink link = new KBQuestionnaireAndQuestionLink();
link.create(question, questionnaire);
save(link);
questionnaire.addQuestionNum(1);
}
// 查出要删除的试卷和问题关联
KBQuestionnaireAndQuestionLinkQO linkQO = new KBQuestionnaireAndQuestionLinkQO();
linkQO.setKbQuestionQO(new KBQuestionQO());
linkQO.getKbQuestionQO().setIds(new ArrayList<String>());
for (String deleteQuestionId : command.getDeleteQuestionIds()) {
linkQO.getKbQuestionQO().getIds().add(deleteQuestionId);
}
List<KBQuestionnaireAndQuestionLink> links = kbQuestionnaireAndQuestionLinkService.queryList(linkQO);
// 循环删除关联
for (KBQuestionnaireAndQuestionLink link : links) {
kbQuestionnaireAndQuestionLinkService.delete(link);
questionnaire.addQuestionNum(-1);
}
// 更新试卷问题计数
update(questionnaire);
}