package cn.hmsoft.skill.school.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.skill.constants.SkillLogType;
import cn.hmsoft.skill.data.dao.school.SchoolLogDao;
import cn.hmsoft.skill.data.dao.std.StdEnrolDao;
import cn.hmsoft.skill.data.dao.std.StdEnrolSubjectDao;
import cn.hmsoft.skill.data.dao.std.StdRegDao;
import cn.hmsoft.skill.data.model.std.StdEnrol;
import cn.hmsoft.skill.data.model.std.StdEnrolSubject;
import cn.hmsoft.skill.data.model.std.StdReg;
import cn.hmsoft.skill.helper.SkillParamHelper;

@Service
public class SchoolScoreService {

	@Autowired
	private StdRegDao daoStd;
	@Autowired
	private StdEnrolDao daoEnrol;
	@Autowired
	private SchoolLogDao daoLog;
	@Autowired
	private StdEnrolSubjectDao daoEnrolSubject;


	public void upload(List<List<String>> array, int school_id) {
		String sql = "update std_enrol e set e.std_score=null where school_id=?";
		this.daoEnrol.updateBySql(sql,school_id);
		sql = "update std_enrol_subject e set e.std_subject_score=null where school_id=?";
		int index = 1;
		this.daoEnrol.updateBySql(sql,school_id);
		
		String sbSql = "select * from std_enrol_subject where school_id=? and aspect_id=? and std_id=? and subject_name=?";
		for (List<String> list : array) {
			String ticket_num = list.get(1);
			String cert_id = list.get(2);
			String std_name = list.get(3);
			String aspect_name = list.get(4);
			String std_score = list.get(5);
			String std_level = list.get(6);
			String sb_name1 = list.get(7);
			if (StringHelper.isEmpty(ticket_num))
				continue;
			FrameAssertUtil.isNotEmpty(cert_id, ("第【" + index + "】行错误：证件号码为空"));
			FrameAssertUtil.isNotEmpty(std_name, ("第【" + index + "】行错误：考生姓名为空"));
			FrameAssertUtil.isNotEmpty(aspect_name, ("第【" + index + "】行错误：专业名称为空"));
			FrameAssertUtil.isNotEmpty(sb_name1, ("第【" + index + "】行错误：第1个考试科目名称为空"));

			StdReg std = this.daoStd.find("cert_id", cert_id);
			FrameAssertUtil.isNotNull(std, ("第【" + index + "】行错误：不存在证件号码为【" + cert_id + "】的考生"));
			// 考生姓名就不验证了
			Map<String, Object> enrol = this.daoEnrol.findEnrol(school_id, std.getStd_id(), aspect_name);
			FrameAssertUtil.isNotNull(enrol, ("第【" + index + "】行错误：考生未报考专业【" + aspect_name + "】"));

			int sb_count = 1;
			for (int i = 0; i < SkillParamHelper.MaxExamSubjectCount; i++) {
				String sb_name = list.get(7 + i * 2);
				String sb_score = list.get(8 + i * 2);
				if (StringHelper.isEmpty(sb_name))
					continue;
				FrameAssertUtil.isNotEmpty(sb_score, ("第【" + index + "】行错误：第" + sb_count + "个科目成绩为空，缺考请填缺考"));
				
				
				StdEnrolSubject sb = this.daoEnrol.findBySql(StdEnrolSubject.class,sbSql,school_id,(int) enrol.get("aspect_id"),std.getStd_id(),sb_name);
				FrameAssertUtil.isNotNull(sb, ("第【" + index + "】行错误：未找到该考生的第" + sb_count + "个科目"));
				sb.setStd_subject_score(sb_score);
				sb_count++;
				//this.daoEnrol.update(sb);
				this.daoEnrolSubject.update(sb);
			}
			// 更新准考证号
			StdEnrol e = this.daoEnrol.find((int) enrol.get("enrol_id"));
			e.setStd_score(std_score);
			e.setStd_level(std_level);
			this.daoEnrol.update(e);

		}
		daoLog.log(school_id, SkillLogType.UploadScore.toString());
	}
}
