package cn.tsy.idsse.dsds.service.sar;

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

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

import cn.hme.data.model.frame.optr.FrameOptr;
import cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.jdbc.provider.builder.SqlQuery;
import cn.hme.framework.web.helper.FrameAssertHelper;
import cn.tsy.data.idsse.dsds.mapper.sar.SarFileMapper;
import cn.tsy.data.idsse.dsds.mapper.sar.SarLogMapper;
import cn.tsy.data.idsse.dsds.mapper.sar.SarRegMapper;
import cn.tsy.data.idsse.dsds.model.sar.SarFile;
import cn.tsy.data.idsse.dsds.model.sar.SarLog;
import cn.tsy.data.idsse.dsds.model.sar.SarReg;
import cn.tsy.idsse.dsds.constant.SarLogType;
import cn.tsy.idsse.dsds.constant.SarStatus;

@Service
public class SarRegService {
	@Autowired
	private SarRegMapper mReg;
	@Autowired
	private SarLogMapper mLog;
	@Autowired
	private SarFileMapper mFile;

	/***********************
	 * 增加或者修改
	 */
	public void editSar(SarReg sar, FrameOptr optr) {

		FrameAssertHelper.isNotEmpty(sar.getSar_type(), "任务类型不允许为空！");
		FrameAssertHelper.isNotEmpty(sar.getTarget_type(), "目标类型不允许为空！");
		// 计算总数
		Integer total = this.mReg.selectCount(SqlQuery.create());
		String seq = "001";
		if (total != null && total > 0)
			seq = StringHelper.leftPadding((seq + 1) + "", 3, '0');

		if (sar.getTsy_id() != null) {

			this.mReg.update(sar);
		} else {
			// 任务类型_目标类别_总序列号
			sar.setSar_name(sar.getSar_type() + "_" + sar.getTarget_type() + "_" + seq);
			sar.setCreate_time(LocalDateTime.now());
			sar.setSar_status_time(LocalDateTime.now());
			sar.setSar_status(SarStatus.Init);
			this.mReg.insert(sar);
			/***************
			 * 写日志
			 */
			logSar(this.mLog, sar.getTsy_id(), optr, SarLogType.Create, sar.getTsy_id() + "->" + sar.getSar_name());

		}
	}

	/****************
	 * 删除
	 * 
	 */
	public void deleteSar(int sar_id) {
		/****************
		 * @todo 判断是否能实际删除
		 */
		this.mLog.deleteByColumn("sar_id", sar_id);
		this.mLog.deleteOtherByColumn(SarFile.class, "sar_id", sar_id);
		this.mReg.deleteByKey(sar_id);
	}
	
	public void startSar(int sar_id,FrameOptr optr) {
		SarReg sar = this.mReg.find(sar_id);
		FrameAssertHelper.isNotNull(sar,"未找到指定的任务，请检查！");
		FrameAssertHelper.isEqual(sar.getSar_status(), SarStatus.Init, "该任务状态不为未启动，不可进行启动操作！");
		SarReg s = new SarReg();
		s.setTsy_id(sar_id);
		s.setSar_status(SarStatus.Working);
		s.setSar_status_time(LocalDateTime.now());
		this.mReg.update(s);
		logSar(this.mLog, sar.getTsy_id(), optr, SarLogType.ModifyStatus, sar.getSar_status() + "->" + s.getSar_status());

	}

	
	public void completeSar(int sar_id,FrameOptr optr) {
		SarReg sar = this.mReg.find(sar_id);
		FrameAssertHelper.isNotNull(sar,"未找到指定的任务，请检查！");
		FrameAssertHelper.isEqual(sar.getSar_status(), SarStatus.Working, "该任务状态不为执行中，不可进行完成操作！");
		SarReg s = new SarReg();
		s.setTsy_id(sar_id);
		s.setSar_status(SarStatus.Complete);
		s.setSar_status_time(LocalDateTime.now());
		this.mReg.update(s);
		logSar(this.mLog, sar.getTsy_id(), optr, SarLogType.ModifyStatus, sar.getSar_status() + "->" + s.getSar_status());

	}
	
	public void cancelSar(int sar_id,FrameOptr optr) {
		SarReg sar = this.mReg.find(sar_id);
		FrameAssertHelper.isNotNull(sar,"未找到指定的任务，请检查！");
		FrameAssertHelper.isNotEqual(sar.getSar_status(), SarStatus.Complete, "该任务状态已完成，不可进行取消操作！");
		SarReg s = new SarReg();
		s.setTsy_id(sar_id);
		s.setSar_status(SarStatus.Cancel);
		s.setSar_status_time(LocalDateTime.now());
		this.mReg.update(s);
		logSar(this.mLog, sar.getTsy_id(), optr, SarLogType.ModifyStatus, sar.getSar_status() + "->" + s.getSar_status());

	}

	public static SarLog logSar(SarLogMapper mLog, int sar_id, FrameOptr optr, String log_type, String log_value) {
		SarLog log = new SarLog();
		log.setLog_type(log_type);
		log.setLog_value(log_value);
		log.setLogin_name(optr.getLogin_name());
		log.setOptr_id(optr.getTsy_id());
		log.setOptr_name(optr.getOptr_name());
		log.setSar_id(sar_id);
		mLog.insert(log);
		return log;
	}
	
	/*************
	 * 上传任务附件
	 */
	public void updateFile(SarReg sar,String file_name,String remark,File f,FrameOptr optr) {
		SarFile file = new SarFile();
		file.setUpload_time(LocalDateTime.now());
		file.setFile_name(file_name);
		file.setFile_path(f.getPath());
		file.setFile_size(f.length()+"");
		file.setFile_type("File");
		if(f.getName().toLowerCase().endsWith(".png") || f.getName().toLowerCase().endsWith(".jpg")||f.getName().toLowerCase().endsWith(".jpeg"))
			file.setFile_type("Image");
		file.setFile_remark(remark);
		file.setSar_id(sar.getTsy_id());
		this.mFile.insert(file);
		logSar(this.mLog, sar.getTsy_id(), optr, SarLogType.UploadFile, file_name);
	}
}
