package com.iflytek.edu.zx.answersheet.service.impl;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.swing.plaf.basic.BasicComboBoxUI.KeyHandler;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import com.iflytek.edu.elp.common.dto.page.PageParam;
import com.iflytek.edu.elp.common.util.JSONUtils;
import com.iflytek.edu.elp.common.util.KeyHolder;
import com.iflytek.edu.zx.answersheet.dao.AnswerSheetDao;
import com.iflytek.edu.zx.answersheet.model.AnswersheetConfig;
import com.iflytek.edu.zx.answersheet.model.AnswersheetSource;
import com.iflytek.edu.zx.answersheet.model.Desc;
import com.iflytek.edu.zx.answersheet.service.AnswersheetService;
/**
* 答题卡服务实现。
*
* @author mt
*
*/
@Service("answersheetService")
public class AnswersheetServiceImpl implements AnswersheetService{
/**
* answerSheetDao
*/
@Resource
private AnswerSheetDao answerSheetDao;
@Override
public AnswersheetConfig getAnswersheet(String uuid) {
Assert.hasLength(uuid,"uuid must has length.");
return answerSheetDao.find(uuid);
}
@Override
public AnswersheetConfig getBaseInfo(String uuid) {
Assert.hasLength(uuid,"uuid must has length.");
return answerSheetDao.findBaseInfo(uuid);
}
@Override
public String createAnswersheet(AnswersheetConfig answersheetConfig) {
check4Create(answersheetConfig);
answersheetConfig.setCreateTime(new Date());
answersheetConfig.setId(KeyHolder.getKey());
answersheetConfig.setUpdateTime(new Date());
answersheetConfig.setIsDelete(true);//没被删除的
answersheetConfig.setIsUsed(false);
answerSheetDao.insert(answersheetConfig);
return answersheetConfig.getId();
}
private void check4Create(AnswersheetConfig answersheetConfig){
Assert.notNull(answersheetConfig, "answersheetConfig must not be null.");
Assert.notNull(answersheetConfig.getDesc(), "answersheetConfig.getDesc must not be null.");
Assert.hasLength(answersheetConfig.getLocationJson(), "answersheetConfig.locationJson must has length.");
Assert.hasLength(answersheetConfig.getCreator().getId(), "answersheetConfig.creator.code must has length.");
Assert.hasLength(answersheetConfig.getHtml(), "answersheetConfig.html must has length.");
}
@Override
public void modifyAnswersheet(String uuid, String updatorId,
String locationJson, Desc desc, String html) {
check4Modify(uuid, updatorId, locationJson, desc, html);
answerSheetDao.update(uuid, updatorId, locationJson, desc, html);
}
/**
* 对修改答题卡进行参数检查
*/
private void check4Modify(String uuid, String updatorId,String locationJson, Desc desc, String html){
Assert.hasLength(uuid,"uuid must has length.");
Assert.hasLength(updatorId,"updatorId must has length.");
Assert.hasLength(locationJson,"locationJson must has length.");
Assert.notNull(desc,"desc must not be null.");
Assert.hasLength(html,"html must has length.");
}
@Override
public void delete(String uuid) {
Assert.hasLength(uuid,"uuid must has length.");
answerSheetDao.delete(uuid);
}
@Override
public int getCount(String creatorId) {
Assert.hasLength(creatorId,"creatorId must has length.");
return answerSheetDao.findCount(creatorId);
}
@Override
public void setUsed(List<String> uuidList, boolean isUsed) {
Assert.notEmpty(uuidList, "uuidList must not be empty.");
answerSheetDao.updateIsUsed(uuidList,isUsed);
}
@Override
public Boolean isUsed(String uuid) {
Assert.hasLength(uuid,"uuid must has length. ");
return answerSheetDao.findIsUsed(uuid);
}
@Override
public AnswersheetConfig getAnswersheetByPaperId(String paperId) {
Assert.hasLength(paperId,"paperId must has length.");
return answerSheetDao.findAnswersheetByPaperId(paperId);
}
@Override
public void deleteByPaperId(String paperId) {
Assert.hasLength(paperId,"paperId must has length.");
answerSheetDao.deleteByPaperId(paperId);
}
@Override
public Desc getDesc(String uuid) {
Assert.hasLength(uuid,"uuid must has length.");
Desc desc = JSONUtils.parseObject(answerSheetDao.findFrontJson(uuid), Desc.class);
return desc;
}
@Override
public List<AnswersheetConfig> getAnswersheets(String creatorId,PageParam pageParam) {
Assert.hasLength(creatorId,"creatorId has length.");
Assert.notNull(pageParam, "pageParam must not be null.");
return answerSheetDao.findAnswersheets(creatorId, pageParam);
}
@Override
public Desc getDesc(String paperId, AnswersheetSource source) {
Assert.hasLength(paperId,"paperId must has length.");
Assert.notNull(source,"source must not be null.");
Desc desc = JSONUtils.parseObject(answerSheetDao.findFrontJson(paperId,source), Desc.class);
return desc;
}
@Override
public String getLocationJson(String uuid) {
Assert.hasLength(uuid,"uuid must has length.");
return answerSheetDao.findLocationJson(uuid);
}
}