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

@Service
public class SchoolProvinceBatchService extends SkillService {

	@Autowired
	private ProvinceBatchDao provinceBatchDao;
	
	
	/**
	 * 导入省份批次计划数
	 * @param thread
	 * @param values
	 * @param frameOptr
	 */
	public void batchRegion(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 province = list.get(1).trim() == null ? null : list.get(1).trim(); //省份
			String batch = 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(province))
				uploadError(thread, "第【" + row + "】行，省份不能为空");
			if (StringHelper.isEmpty(batch))
				uploadError(thread, "第【" + row + "】行，批次不能为空");
			if (StringHelper.isEmpty(plan_num))
				uploadError(thread, "第【" + row + "】行，计划数不能为空");

			// 判断对应年份数据是否导入数据库
			ProvinceBatch provinceBatch = this.provinceBatchDao.findByYear(year);
			if (provinceBatch != null) {
				uploadError(thread, "【" + year + "】年的省份批次计划数据已经导入数据库，不能重复导入省份批次计划数据！");
			} else {
				provinceBatch = new ProvinceBatch();				
				provinceBatch.setYear(year);
				provinceBatch.setProvince(province);
				provinceBatch.setBatch(batch);
				provinceBatch.setPlan_num(Integer.valueOf(plan_num));
				try {
					this.provinceBatchDao.insert(provinceBatch);
				} 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);
	}
}
