package cn.hmsoft.skill.service.std;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hmsoft.frame.constants.FrameThreadCallbackType;
import cn.hmsoft.frame.constants.FrameThreadStatus;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameThread;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.thread.FrameThreadHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.skill.constants.StdLogType;
import cn.hmsoft.skill.data.dao.std.StdAdmissionDao;
import cn.hmsoft.skill.data.model.std.StdAdmission;
import cn.hmsoft.skill.service.SkillService;

@Service
public class StdAdmissionService extends SkillService {

	@Autowired
	private StdAdmissionDao stdAdmissionDao;
	

	/**
	 * 导入批量录取数据.
	 * @param thread
	 * @param values
	 * @param frameOptr
	 */
	public void batchAdmission(FrameThread thread, List<List<String>> values, FrameOptr frameOptr) {
		int total = 0;
		int row = 2;
		for (List<String> list : values) {
			
			String no = list.get(0).trim() == null ? null : list.get(0).trim(); //序号
			String notification_number = list.get(1).trim() == null ? null : list.get(1).trim(); //通知书编号
			String region = list.get(2).trim() == null ? null : list.get(2).trim(); //地区
			String examinee_number = list.get(3).trim() == null ? null : list.get(3).trim(); //考生号
			String stu_name = list.get(4).trim() == null ? null : list.get(4).trim(); //姓名
			String nation = list.get(5).trim() == null ? null : list.get(5).trim();   //名族
			String sex = list.get(6).trim() == null ? null : list.get(6).trim();    //性别
			String card_no = list.get(7).trim() == null ? null : list.get(7).trim();    //身份证号
			String department = list.get(8).trim() == null ? null : list.get(8).trim();    //系(院)
			String major = list.get(9).trim() == null ? null : list.get(9).trim();    //专业
			String filing_unit = list.get(10).trim() == null ? null : list.get(10).trim();    //投档单位名称
			String political = list.get(11).trim() == null ? null : list.get(11).trim();    //政治面貌
			String address = list.get(12).trim() == null ? null : list.get(12).trim();    //考生家庭地址
			String consignee = list.get(13).trim() == null ? null : list.get(13).trim();    //收件人
			String stu_Telephone = list.get(14).trim() == null ? null : list.get(14).trim();    //考生联系电话
			String postal_code = list.get(15).trim() == null ? null : list.get(15).trim();    //邮政编码
			String stu_category  = list.get(16).trim() == null ? null : list.get(16).trim();    //考生类别
			String graduate_middle_school = list.get(17).trim() == null ? null : list.get(17).trim();    //毕业中学
			String graduation_type = list.get(18).trim() == null ? null : list.get(18).trim();    //毕业类别
			String ticket_number = list.get(19).trim() == null ? null : list.get(19).trim();    //准考证号
			String stu_area_residece  = list.get(20).trim() == null ? null : list.get(20).trim();    //考生户口所在地区
			String date_of_birth  = list.get(21).trim() == null ? null : list.get(21).trim();    //出生年月
			String foreign_language = list.get(22).trim() == null ? null : list.get(22).trim();    //外语语种
			String stu_features = list.get(23).trim() == null ? null : list.get(23).trim();    //考生特征
			String stu_speciality = list.get(24).trim() == null ? null : list.get(24).trim();    //考生特长
			String stu_reward_punishment  = list.get(25).trim() == null ? null : list.get(25).trim();    //考生奖励或处分
			String foreign_language_oral_test  = list.get(26).trim() == null ? null : list.get(26).trim();    //外语口试
			String political_opinions  = list.get(27).trim() == null ? null : list.get(27).trim();    //政审意见
			String examination_type = list.get(28).trim() == null ? null : list.get(28).trim();    //考试类型
			String batch  = list.get(29).trim() == null ? null : list.get(29).trim();    // 批次
			String families  = list.get(30).trim() == null ? null : list.get(30).trim();    //科类
			String planning_nature  = list.get(31).trim() == null ? null : list.get(31).trim();    //计划性质
		    String stu_score  = list.get(32).trim() == null ? null : list.get(32).trim();    // 考生成绩
			String characteristic_score = list.get(33).trim() == null ? null : list.get(33).trim();    //特征成绩
			String filing_score  = list.get(34).trim() == null ? null : list.get(34).trim();    //投档成绩
			String care_score  = list.get(35).trim() == null ? null : list.get(35).trim();    //照顾分
			String college_introduction_score = list.get(36).trim() == null ? null : list.get(36).trim();  //院校导入成绩
			String benchmark_score  = list.get(37).trim() == null ? null : list.get(37).trim();//专业投档基准成绩
			String additional_score = list.get(38).trim() == null ? null : list.get(38).trim();//专业投档附加成绩
			String volunteering  = list.get(39).trim() == null ? null : list.get(39).trim();//投档志愿
			String educational_system  = list.get(40).trim() == null ? null : list.get(40).trim();//学制
			String qualification  = list.get(41).trim() == null ? null : list.get(41).trim();// 专业合格
			String physical_examination = list.get(42).trim() == null ? null : list.get(42).trim(); //体检合格
			String isReported = list.get(43).trim() == null ? null : list.get(43).trim();  //是否报道： 1.是；2.否，3.保留学籍
			String campus = list.get(44).trim() == null ? null : list.get(44).trim();   //校区，主校区、西校区
			String families_type =  list.get(45).trim() == null ? null : list.get(45).trim();   //科类性质 文史类    【文史类、理工类、综合类、3+2转段、技能高考、单独招生】
			
			if (StringHelper.isEmpty(notification_number)) {
				uploadError(thread, "第【" + row + "】行，通知书编号不能为空");
			}
			if (StringHelper.isEmpty(region))
				uploadError(thread, "第【" + row + "】行，地区不能为空");
			if (StringHelper.isEmpty(examinee_number))
				uploadError(thread, "第【" + row + "】行，考生号不能为空");
			if (StringHelper.isEmpty(stu_name))
				uploadError(thread, "第【" + row + "】行，姓名不能为空");
			if (StringHelper.isEmpty(nation))
				uploadError(thread, "第【" + row + "】行，名族不能为空");

			// 判断对应年份数据是否导入数据库
			StdAdmission admission = this.stdAdmissionDao.findByYear(notification_number);
			if (admission != null) {
				uploadError(thread, "【" + notification_number.substring(0,4) + "】年的录取数据已经导入数据库，不能重复导入录取数据！");
			} else {
				admission = new StdAdmission();				
				admission.setNo(Integer.valueOf(no));
				admission.setNotification_number(notification_number);
				admission.setRegion(region);
				admission.setExaminee_number(examinee_number);
				admission.setStu_name(stu_name);
				admission.setNation(nation);
				admission.setSex(sex);
				admission.setCard_no(card_no);
				admission.setDepartment(department);
				admission.setMajor(major);
				admission.setFiling_unit(filing_unit);
				admission.setPolitical(political);
				admission.setAddress(address);
				admission.setConsignee(consignee);
				admission.setStu_Telephone(stu_Telephone);
				admission.setPostal_code(postal_code);
				admission.setStu_category(stu_category);
				admission.setGraduate_middle_school(graduate_middle_school);
				admission.setGraduation_type(graduation_type);
				admission.setTicket_number(ticket_number);
				admission.setStu_area_residece(stu_area_residece);
				admission.setDate_of_birth(date_of_birth);
				admission.setForeign_language(foreign_language);
				admission.setStu_features(stu_features);
				admission.setStu_speciality(stu_speciality);
				admission.setStu_reward_punishment(stu_reward_punishment);
				admission.setForeign_language_oral_test(foreign_language_oral_test);
				admission.setPolitical_opinions(political_opinions);
				admission.setExamination_type(examination_type);
				admission.setBatch(batch);
				admission.setFamilies(families);
				admission.setPlanning_nature(planning_nature);
				admission.setStu_score(Double.valueOf(stu_score));
				admission.setCharacteristic_score(Double.valueOf(characteristic_score));
				admission.setFiling_score(Double.valueOf(filing_score));
				admission.setCare_score(Double.valueOf(care_score));
				admission.setCollege_introduction_score(Double.valueOf(college_introduction_score));
				admission.setBenchmark_score(Double.valueOf(benchmark_score));
				admission.setAdditional_score(Double.valueOf(additional_score));
				admission.setVolunteering(volunteering);
				admission.setEducational_system(educational_system);
				admission.setQualification(qualification);
				admission.setPhysical_examination(physical_examination);
				admission.setSchool_id(frameOptr.getOrg_id());
				admission.setIsReported(isReported);
				admission.setCampus(campus);
				admission.setFamilies_type(families_type);
				try {
					this.stdAdmissionDao.insert(admission);
				} catch (Exception e) {
					e.printStackTrace();
					uploadError(thread, "第【" + row + "】行，文字信息过长，请缩减后重新上传！");
				}
			}
			
			total++;
			row++;
			if (total % 2 == 0) {
				// 每100条记录下
				FrameThreadHelper.updateThread(thread, total * 2 / values.size(),
						"已处理【" + total + "】条录取数据，一共需要处理【" + values.size() + "】条记录");
			}

		}
		FrameThreadHelper.completeThread(thread, "导入成功：已处理【" + total + "】条录取数据");
		this.log(frameOptr, StdLogType.BatchReg.toString(), values.size());
	}
	
	private void uploadError(FrameThread thread, String value) {
		FrameThreadHelper.completeThread(thread, "出现业务错误，导入失败！", FrameThreadCallbackType.Swal, value,
				FrameThreadStatus.Error);
		throw new BusinessException(value);
	}
}
