package cn.hmsoft.frame.thread;

import java.io.File;
import java.time.LocalDateTime;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.frame.constants.FrameThreadCallbackType;
import cn.hmsoft.frame.constants.FrameThreadStatus;
import cn.hmsoft.frame.data.dao.FrameThreadDao;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameThread;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.service.FrameThreadService;
import cn.hmsoft.frame.util.FrameParamUtil;
import cn.hmsoft.helper.RandomHelper;
import cn.hmsoft.jdbc.entity.DataRecord;

public abstract class FrameThreadHelper {

	/********************
	 * 线程等待，单位秒
	 */
	public static void sleepSeconds(int seconds) {
		try {
			Thread.sleep(seconds * 1000);
		} catch (Exception e) {

		}
	}

	/**************************
	 * 事务外创建线程
	 */
	public static FrameThread createThread(String thread_name, String thread_notice,
			FrameThreadCallbackType callback_type, String callback_param, FrameOptr optr) {
		FrameThreadDao daoThread = SpringHelper.getBean(FrameThreadDao.class);
		FrameThread td = daoThread.findThread(thread_name, optr.getOptr_id());
		// 同一操作员同一种线程只能使用一次
		if (td != null)
			throw new BusinessException("您的同一操作正在后台运行中，请等待操作完成！");

		DataRecord dr = daoThread.totalThread(optr.getOptr_id());
		// 判断全局是否超过限制
		if (FrameParamUtil.FrameThreadGobalMax > 0) {
			if (dr.getI01().intValue() >= FrameParamUtil.FrameThreadGobalMax)
				throw new BusinessException("后台线程数量已达最大限制，请稍候再进行此操作！");
		}
		if (optr.getThread_max() == null) {
			// 来自于系统配置
			if (FrameParamUtil.FrameThreadOptrMax > 0) {
				if (dr.getI02().intValue() >= FrameParamUtil.FrameThreadOptrMax)
					throw new BusinessException("后台线程数量已达最大限制，请稍候再进行此操作！");
			}
		} else {
			// 来自于操作员角色配置
			if (optr.getThread_max().intValue() > 0) {
				if (dr.getI02().intValue() >= optr.getThread_max().intValue())
					throw new BusinessException("后台线程数量已达最大限制，请稍候再进行此操作！");

			}
		}

		FrameThread thread = new FrameThread();
		thread.setThread_name(thread_name);
		thread.setThread_notice(thread_notice == null ? "线程后台启动 ......" : thread_notice);
		thread.setCreate_optr_id(optr.getOptr_id());
		thread.setCreate_login_name(optr.getLogin_name());
		thread.setCreate_optr_name(optr.getOptr_name());
		thread.setThread_status_time(LocalDateTime.now());
		thread.setThread_percent(1);
		thread.setThread_status(FrameThreadStatus.Running.toString());
		thread.setThread_type("Normal");
		thread.setCallback_type(callback_type.toString());
		thread.setCallback_param(callback_param);
		thread.setThread_start_time(LocalDateTime.now());
		SpringHelper.getBean(FrameThreadService.class).addThread(thread);
		return thread;
	}

	public static void updateThread(FrameThread thread, Integer thread_percent, String thread_notice) {
		updateThread(thread, thread_percent, thread_notice, FrameThreadStatus.Running);
	}

	public static void updateThread(FrameThread thread, Integer thread_percent, String thread_notice,
			FrameThreadStatus status) {
		if (thread == null)
			return;

		new Thread(() -> {
			thread.setThread_percent(thread_percent);
			thread.setThread_notice(thread_notice);
			thread.setThread_status(status.toString());
			thread.setThread_status_time(LocalDateTime.now());
			if (FrameThreadStatus.Complete.equals(status) || FrameThreadStatus.Error.equals(status))
				thread.setThread_end_time(LocalDateTime.now());
			SpringHelper.AppGobalDao.update(thread);
		}).start();
	}

	public static void completeThread(FrameThread thread, String thread_notice) {

		try {
			Thread.sleep(3000);
		} catch (Exception e) {

		}
		updateThread(thread, 100, thread_notice, FrameThreadStatus.Complete);
	}

	public static void completeThread(FrameThread thread, String thread_notice, FrameThreadStatus status) {

		try {
			Thread.sleep(3000);
		} catch (Exception e) {

		}
		updateThread(thread, 100, thread_notice, status);
	}

	public static void completeThread(FrameThread thread, String thread_notice, FrameThreadCallbackType callback_type,
			String callback_param) {
		if (thread == null)
			return;

		try {
			Thread.sleep(3000);
		} catch (Exception e) {

		}
		completeThread(thread, thread_notice, callback_type, callback_param, FrameThreadStatus.Complete);
	}

	public static void completeThread(FrameThread thread, String thread_notice, FrameThreadCallbackType callback_type,
			String callback_param, FrameThreadStatus status) {
		if (thread == null)
			return;

		try {
			Thread.sleep(3000);
		} catch (Exception e) {

		}
		new Thread(() -> {
			thread.setThread_percent(100);
			thread.setThread_notice(thread_notice);
			thread.setThread_status(status.toString());
			thread.setThread_status_time(LocalDateTime.now());
			thread.setThread_end_time(LocalDateTime.now());
			thread.setCallback_type(callback_type.toString());
			thread.setCallback_param(callback_param);
			SpringHelper.AppGobalDao.update(thread);
		}).start();
	}

	/*****************
	 * 生成下载的参数
	 */
	public static String generateDownloadParam(File file, String real_name) {
		return "{\"file_name\":\"" + file.getName() + "\", \"real_name\":\"" + real_name + "\"}";
	}

	/*****************
	 * 生成下载的临时文件
	 */
	public static File generateTempFile() {
		return new File(SpringHelper.getRootPath() + "temporary" + File.separator + "thread_"
				+ RandomHelper.getRandomValue(12) + ".tmp");
	}
}