package cn.hmsoft.skill.school.service;

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.school.DepartmentMajorDao;
import cn.hmsoft.skill.data.model.school.DepartmentMajor;
import cn.hmsoft.skill.service.SkillService;

@Service
public class SchoolDepartmentMajorService extends SkillService {

	@Autowired
	private DepartmentMajorDao departmentMajorDao;
	
	/**
	 * 导入院系专业计划数据
	 * @param thread
	 * @param values
	 * @param frameOptr
	 */
	public void batchMajor(FrameThread thread, List<List<String>> values, FrameOptr frameOptr) {
		int total = 0;
		int row = 2;
		for (List<String> list : values) {
			
			String year = list.get(0).trim() == null ? null : list.get(0).trim(); //年份
			String department = list.get(1).trim() == null ? null : list.get(1).trim(); //院系
			String major = list.get(2).trim() == null ? null : list.get(2).trim(); //专业
			String plan_num = list.get(3).trim() == null ? null : list.get(3).trim(); //计划数
			if (StringHelper.isEmpty(year)) {
				uploadError(thread, "第【" + row + "】行，年份不能为空");
			}
			if (StringHelper.isEmpty(department))
				uploadError(thread, "第【" + row + "】行，院系不能为空");
			if (StringHelper.isEmpty(major))
				uploadError(thread, "第【" + row + "】行，专业不能为空");
			if (StringHelper.isEmpty(plan_num))
				uploadError(thread, "第【" + row + "】行，计划数不能为空");

			// 判断对应年份数据是否导入数据库
			DepartmentMajor departmentMajor = this.departmentMajorDao.findByYear(year);
			if (departmentMajor != null) {
				uploadError(thread, "【" + year + "】年的院系专业计划数据已经导入数据库，不能重复导入院系专业计划数据！");
			} else {
				departmentMajor = new DepartmentMajor();				
				departmentMajor.setYear(year);
				departmentMajor.setDepartment(department);
				departmentMajor.setMajor(major);
				departmentMajor.setPlan_num(Integer.valueOf(plan_num));
				try {
					this.departmentMajorDao.insert(departmentMajor);
				} 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);
	}
}
