package com.ssj.aijia.task;

import java.util.Date;
import java.util.List;


import com.ssj.bean.common.files.domain.FileInfo;
import com.ssj.bean.kmt.kmt.domain.KmtAiRecord;
import com.ssj.bean.kmt.kmt.domain.KmtQuestionTempExam;
import com.ssj.framework.core.util.PropertiesUtil;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.service.ai.utils.ExcelUtils;
import com.ssj.service.kmt.service.KmtAiRecordService;
import com.ssj.service.kmt.service.KmtQuestionTempExamService;
import com.ssj.taskthread.Task;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;



/**
* 2018年9月20日15:34:29
* @author wuwen
* AI上传的试卷数据处理
*/
public class SyncAiExamDataTask extends Task {
	
	 private List<FileInfo> fileInfos;
	 
	 private String fileName;

	 public SyncAiExamDataTask(List<FileInfo> fileInfos,String fileName) {
	        super();
	        this.fileInfos=fileInfos;
	        this.fileName=fileName;
	 } 
	 
	 public List<FileInfo> getFileInfos() {
		return fileInfos;
	}

	public void setFileInfos(List<FileInfo> fileInfos) {
		this.fileInfos = fileInfos;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	 
	 @Override
	 public void run() {
		logger.error("AI试卷上传的数据处理任务开始---------------");
		KmtQuestionTempExamService kmtQuestionTempService= SystemResourceLocator.getBean(KmtQuestionTempExamService.class);
		KmtAiRecordService kmtAiRecordService=SystemResourceLocator.getBean(KmtAiRecordService.class);
		String savePath = PropertiesUtil.getValue("file_save_path");

		
		for (FileInfo fileInfo : fileInfos) {
			
			//记录AI上传的数据
			KmtAiRecord aiRecord=new KmtAiRecord();
			aiRecord.setCountNum(0);
			aiRecord.setCorrectNum(0);
			aiRecord.setErrorNum(0);
			aiRecord.setFileCode(fileInfo.getFileCode());
			aiRecord.setFileName(fileName);
			aiRecord.setFilePath(fileInfo.getFilePath());
			aiRecord.setCreateTime(new Date());
			aiRecord.setRemarks("");
			aiRecord.setState(0);//未导入
			aiRecord.setType(2);//试卷识别的题目
			kmtAiRecordService.save(aiRecord);
			
			
			if(StringUtils.isNotEmpty(aiRecord.getId())){
				Integer countNum=0;//总量
				Integer correctNum=0;//正确量
				Integer errorNum=0;//错误量
				StringBuffer bb=new StringBuffer("");
				
				String filePath=savePath+"/"+fileInfo.getFilePath();
				List<KmtQuestionTempExam> kmtQuestionModels= ExcelUtils.runExamExcelUtils(filePath.trim());
				countNum=kmtQuestionModels!=null? kmtQuestionModels.size() : 0;
				for (KmtQuestionTempExam kmtQuestionTemp : kmtQuestionModels) {
					try {
						
						kmtQuestionTemp.setState(1);
						kmtQuestionTempService.save(kmtQuestionTemp);

						correctNum=correctNum+1;
					} catch (Exception e) {
						e.printStackTrace();
						logger.error("AI试卷上传的数据处理任务中，异常题目："+JSONObject.toJSONString(kmtQuestionTemp));
						bb.append(JSONObject.toJSONString(kmtQuestionTemp));
						errorNum=errorNum+1;
					}
				}
				aiRecord.setState(1);//导入完成
				aiRecord.setCountNum(countNum);
				aiRecord.setCorrectNum(correctNum);
				aiRecord.setErrorNum(errorNum);
				aiRecord.setRemarks(bb.toString());//保存所有异常的数据
				kmtAiRecordService.save(aiRecord);
			}
		}
		logger.error("AI试卷上传的数据处理任务结束---------------");
	 }
	 
		
}
