package cn.com.qmth.stmms.ms.admin.assembler;

import cn.com.qmth.stmms.ms.admin.dto.ScoreCheckDTO;
import cn.com.qmth.stmms.ms.admin.dto.ScoreItem;
import cn.com.qmth.stmms.ms.commons.config.SystemConfig;
import cn.com.qmth.stmms.ms.commons.utils.MD5Util;
import cn.com.qmth.stmms.ms.core.cache.ParamCache;
import cn.com.qmth.stmms.ms.core.domain.Paper;
import cn.com.qmth.stmms.ms.core.domain.Student;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by zhengmin on 2017/1/6.
 */
@Component
public class ScoreAssembler {

    @Autowired
    private SystemConfig systemConfig;

    public ScoreCheckDTO toDTO(Student student, List<Paper> papers) {
        ScoreCheckDTO scoreDTO = null;
        if (student != null) {
            scoreDTO = new ScoreCheckDTO();
            scoreDTO.setName(student.getName());
            scoreDTO.setId(student.getId());
            scoreDTO.setAreaName(student.getAreaName());
            scoreDTO.setExamRoom(student.getExamRoom());
            scoreDTO.setExamNumber(student.getExamNumber());
            for (Paper p : papers) {
                ScoreItem scoreItem = new ScoreItem();
                scoreItem.setPaperId(p.getId());
                scoreItem.setSubjectName(p.getSubject().getName());
                scoreItem.setScore(p.getScore());
                scoreItem.setSubject(p.getSubject());

                String fileName = p.getExamNumber();
                //0：随机码 1：考号
                if(ParamCache.paramMap.get(p.getWorkId()).getNameRule() == 1){
//                    fileName = DigestUtils.md5Hex(p.getSubject().name() + fileName);
                    fileName = MD5Util.getImageRuleMd5(p.getWorkId(), p.getSubject().ordinal(), student.getAreaCode(), student.getExamNumber(), student.getId());
                }
                String imgSrc = systemConfig.getImageUrl(p.getWorkId(), p.getSubject().toString(), p.getAreaCode(), fileName);
                String thumbSrc = systemConfig.getThumbUrl(p.getWorkId(), p.getSubject().toString(), p.getAreaCode(), fileName);

                scoreItem.setImgSrc(imgSrc);
                scoreItem.setThumbSrc(thumbSrc);

                scoreDTO.getScoreItems().add(scoreItem);
            }
        }
        return scoreDTO;
    }
}
