package com.unis.detectionnum.server.schedule;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.unis.detectionnum.server.dao.TaskInfoDao;
import com.unis.detectionnum.server.entity.TaskInfoDO;
import com.unis.detectionnum.server.schedule.pojo.TaskPojo;

import lombok.extern.slf4j.Slf4j;

/** 
 * @ClassName: TaskServer 
 * @Description: TODO 
 * @author LiuFeng 
 * @date 2019年11月6日 下午5:27:21  
 */
@Service
@Slf4j
public class TaskServer {

	@Autowired
	private TaskInfoDao taskInfoDao;

	/** 
	 * @Title: selectList 
	 * @Description: TODO
	 * @param @return
	 * @return List<TaskPojo>
	 * @throws 
	 */
	public List<TaskPojo> selectList() {

		List<TaskPojo> tasks = new ArrayList<TaskPojo>();
		List<TaskInfoDO> taskInfos = taskInfoDao.selectList(null);// 根据日期获取task

		for (TaskInfoDO tcTaskInfo : taskInfos) {// 处理task数据
			TaskPojo taskPojo = taskInfoToTaskPojo(tcTaskInfo);
			if (null == taskPojo) {
				continue;
			}
			tasks.add(taskPojo);
		}
		return tasks;
	}
	
	// task信息转换
		private TaskPojo taskInfoToTaskPojo(TaskInfoDO tcTaskInfo) {
			/*Integer executeType = tcTaskInfo.getExecuteType();
			final String executeTime = tcTaskInfo.getExecuteTime();
			if (StringUtils.isBlank(executeTime)) {
				return null;
			}
			final String cronExpression = generateCronExpression(executeType, executeTime);
			if (null == tcTaskInfo.getCronExpr() || !tcTaskInfo.getCronExpr().equals(cronExpression)) {
				tcTaskInfo.setCronExpr(cronExpression);
				taskRepository.update(tcTaskInfo.getTaskNo(),cronExpression);
			}*/
			TaskPojo taskPojo = new TaskPojo();
			taskPojo.setId(tcTaskInfo.getTaskNo());
			taskPojo.setName(tcTaskInfo.getTaskName());
			taskPojo.setCronExpression(tcTaskInfo.getCronExpr());
			Class<?> clazz = null;
			try {
				clazz = Class.forName(tcTaskInfo.getExecuteClass());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("", e);
			}
			if (null == clazz) {
				return null;
			}
			taskPojo.setExecuteJobClass(clazz);
			taskPojo.setSbCode(tcTaskInfo.getSbCode());
			return taskPojo;
		}
}
