package com.xxl.job.admin.controller.custom;

import java.util.List;

import javax.annotation.Resource;

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

import com.xxl.job.admin.core.model.OldXxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.dao.OldXxlJobInfoDao;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2021年9月24日 下午3:28:23
 */
@Component
public class CustomInfoHandler {

	@Resource
	private XxlJobService xxlJobService;
	
	@Autowired
	private OldXxlJobInfoDao oldXxlJobInfoDao;
	
	/**
	 * 原数据迁移；group 的迁移走sql
	 * <p>
	 *  delete from xxl_job_group;
	 *	insert into xxl_job_group (id, app_name, title, address_type, address_list, update_time) 
	 *	SELECT id, app_name, title, 1, CONCAT("http://", address_list) AS address_list, now() FROM `xxl_job_qrtz_trigger_info` ;
	 * </p>
	 * 
	 * @param jobInfo
	 */
	public ReturnT<String> transfer() {
		// 获取原数据
		List<OldXxlJobInfo> infoList = this.oldXxlJobInfoDao.infoList();
		if (CollectionUtil.isEmpty(infoList)) {
			return ReturnT.SUCCESS;
		}
		
		for (OldXxlJobInfo oldXxlJobInfo : infoList) {
			// 添加并启动
			ReturnT<String> returnT = xxlJobService.add(this.transferDataInfo(oldXxlJobInfo));
			if (ReturnT.SUCCESS_CODE == returnT.getCode()) {
				xxlJobService.start(Integer.valueOf(returnT.getContent()));
			} else {
				return ReturnT.FAIL;
			}
		}
		
		return ReturnT.SUCCESS;
	}
	
	/**
	 * 数据信息转换
	 * 
	 * @param oldXxlJobInfo
	 * @return
	 */
	private XxlJobInfo transferDataInfo(OldXxlJobInfo oldXxlJobInfo) {
		XxlJobInfo jobInfo = new XxlJobInfo();
		jobInfo.setAuthor(oldXxlJobInfo.getAuthor());
		jobInfo.setAddTime(oldXxlJobInfo.getAddTime());
		jobInfo.setAlarmEmail(oldXxlJobInfo.getAlarmEmail());
		jobInfo.setChildJobId(oldXxlJobInfo.getChildJobId());
		jobInfo.setExecutorBlockStrategy(oldXxlJobInfo.getExecutorBlockStrategy());
		jobInfo.setExecutorFailRetryCount(oldXxlJobInfo.getExecutorFailRetryCount());
		jobInfo.setExecutorHandler(oldXxlJobInfo.getExecutorHandler());
		jobInfo.setExecutorParam(oldXxlJobInfo.getExecutorParam());
		jobInfo.setExecutorRouteStrategy(oldXxlJobInfo.getExecutorRouteStrategy());
		jobInfo.setExecutorTimeout(oldXxlJobInfo.getExecutorTimeout());
		jobInfo.setGlueRemark(oldXxlJobInfo.getGlueRemark());
		jobInfo.setGlueSource(oldXxlJobInfo.getGlueSource());
		jobInfo.setGlueType(oldXxlJobInfo.getGlueType());
		jobInfo.setGlueUpdatetime(oldXxlJobInfo.getGlueUpdatetime());
		jobInfo.setId(oldXxlJobInfo.getId());
		jobInfo.setJobDesc(oldXxlJobInfo.getJobDesc());
		jobInfo.setJobGroup(oldXxlJobInfo.getJobGroup());
		jobInfo.setMisfireStrategy("DO_NOTHING");
		jobInfo.setScheduleConf(oldXxlJobInfo.getJobCron());
		jobInfo.setScheduleType("CRON");
		jobInfo.setTriggerLastTime(0);
		jobInfo.setTriggerNextTime(0);
		jobInfo.setTriggerStatus(0);
		return jobInfo;
	}
	
}
