package com.xmomen.module.job.service.impl;

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

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.module.job.entity.TbJob;
import com.xmomen.module.job.entity.TbJobOperationLog;
import com.xmomen.module.job.mapper.JobOperationLogMapper;
import com.xmomen.module.job.mapper.PackageTaskMapper;
import com.xmomen.module.job.model.JobOperationLogModel;
import com.xmomen.module.job.service.JobOperationLogService;

@Service
public class JobOperationLogServiceImpl implements JobOperationLogService {

    @Autowired
    MybatisDao mybatisDao;

    @Override
	public void delete(Integer id) {
    	//根据id查询操作日志
    	TbJobOperationLog jobOperationLog = mybatisDao.selectByPrimaryKey(TbJobOperationLog.class, id);
    	//根据操作日志中的任务id查询任务
    	TbJob job = mybatisDao.selectByPrimaryKey(TbJob.class, jobOperationLog.getJobId());
    	//删除操作日志
    	mybatisDao.delete(jobOperationLog);
    	//任务完成值减1
    	job.setFinishValue(job.getFinishValue() - 1);
    	//保存任务
    	mybatisDao.save(job);
    	//改变任务状态
    	if(job.getFinishValue() == 0){
    		//更新任务开始时间
    		mybatisDao.getSqlSessionTemplate().update(PackageTaskMapper.PackageTaskMapperNameSpace + "updateBeginTime",jobOperationLog.getJobId());
    	}
    	if(job.getJobStatus() == 2){
    		//更新任务完成时间
    		mybatisDao.getSqlSessionTemplate().update(PackageTaskMapper.PackageTaskMapperNameSpace + "updateFinishTime",jobOperationLog.getJobId());
    	}
    }

}
