package market.Qms.qcm.service.impl;

import java.util.TimerTask;

import javax.servlet.ServletContext;

import market.Qms.qcm.service.QcmJhyPhyresultService;
import market.Qms.qcm.service.QcmJudgePhysicalService;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hnshituo.basic.service.util.ServiceUtil;

/**
 * 自动判定所需
 */
public class AutoJudgeTask extends TimerTask {
	private ApplicationContext context;

	public AutoJudgeTask(ServletContext servletContext) {
		context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	}

	private boolean fristStart = true;// 首次启动
	private static ServiceUtil util = new ServiceUtil();
//	private QcmJhyElementsService chemService;// 成分
	private QcmJhyPhyresultService phyService;// 材质
	private QcmJudgePhysicalService utmService; // 综合

	@Override
	public void run() {
		if (fristStart) {
			util.setApplicationContext(context);
			fristStart = false;
			try {
				// 此处如果有问题,直接找配置这个项目的相关人员(已经咨询过'易*'),经实验发现浩鑫项目及自己创建个新的maven项目是可以使用的
				// 因此怀疑是某些配置方面的问题
				// （建议不使用后台开线程循环的这种方式自动判定）
//				chemService = util.getService(QcmJhyElementsServiceImpl.class);
				phyService = util.getService(QcmJhyPhyresultService.class);
				utmService = util.getService(QcmJudgePhysicalService.class);

			} catch (Exception ex) {
				System.out.print("创建自动判定相关类失败：" + ex.getMessage());
			}
		}
//		try {
//			chemService.AutoJudge();
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
		try {
			phyService.AutoJudge();
			utmService.autoJudge();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
