/**
 * 
 */
package org.springblade.etl.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.springblade.etl.dto.etl.JobStatReq;
import org.springblade.etl.dto.etl.JobStatRes;
import org.springblade.etl.dto.vo.InstanceCountVO;
import org.springblade.etl.entity.JobInstance;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 *	任务实例 数据处理
 *
 * @author aokunsang
 * @date   2023-07-28
 */
public interface JobInstanceMapper extends BaseMapper<JobInstance>{
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select * from etl_job_instance where instance_status = 1")
	List<JobInstance> selectAllRunningInstanceList();
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select count(1) from etl_job_instance where instance_status = 1 and job_id = #{value}")
	Long countRunningInstanceByJobId(Integer jobId);
	
	@Select({
		"<script>",
		"select instance_status status, count(1) as cnt from etl_job_instance",
		"<where>",
		"<if test=\"jobId != null\">job_id = #{jobId}</if>",
		"<if test=\"startTime != null and startTime != ''\">and create_time &gt; #{startTime}</if>",
		"<if test=\"endTime != null and endTime != ''\">and create_time &lt; #{endTime}</if>",
		"</where>",
		"group by instance_status",
		"</script>"
	})
	List<JobStatRes> stat(JobStatReq statReq);


	@Select({
			"select count(1) as total, ",
			"SUM(CASE WHEN instance_status=1 THEN 1 ELSE 0 END) as runningCount,",
			"SUM(CASE WHEN instance_status=2 THEN 1 ELSE 0 END) successCount,",
			"SUM(CASE WHEN instance_status=3 THEN 1 ELSE 0 END) failCount,",
			"SUM(CASE WHEN instance_status=4 THEN 1 ELSE 0 END) cancelCount",
			"from etl_job_instance"
	})
	InstanceCountVO getCounts();


	@Select({
			"select count(1) as total, ",
			"DATE_FORMAT(create_time, '%Y-%m-%d') as date,",
			"SUM(CASE WHEN instance_status=2 THEN 1 ELSE 0 END) successCount,",
			"SUM(CASE WHEN instance_status=3 THEN 1 ELSE 0 END) failCount",
			"FROM etl_job_instance where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) group by DATE_FORMAT(create_time, '%Y-%m-%d')",
			"order by DATE_FORMAT(create_time, '%Y-%m-%d')"
	})
	List<InstanceCountVO> getCountByDate();

	@Select({
			"<script>",
			"select count(1) as total, ",
			"SUM(CASE WHEN instance_status=1 THEN 1 ELSE 0 END) as runningCount,",
			"SUM(CASE WHEN instance_status=2 THEN 1 ELSE 0 END) successCount,",
			"SUM(CASE WHEN instance_status=3 THEN 1 ELSE 0 END) failCount,",
			"SUM(CASE WHEN instance_status=4 THEN 1 ELSE 0 END) cancelCount",
			"FROM etl_job_instance ",
			"<where>",
			"DATE(create_time) = CURDATE()",
			"</where>",
			"</script>"
	})
//	"<if test=\"instance_type != null \">and instance_type =#{instanceType}</if>",
	InstanceCountVO getCountByType(Integer instanceType);


	@Select({
			"select count(1) as total, ",
			"DATE_FORMAT(create_time, '%Y-%m-%d %H') as date,",
			"SUM(CASE WHEN instance_status=1 THEN 1 ELSE 0 END) as runningCount,",
			"SUM(CASE WHEN instance_status=2 THEN 1 ELSE 0 END) successCount,",
			"SUM(CASE WHEN instance_status=3 THEN 1 ELSE 0 END) failCount,",
			"SUM(CASE WHEN instance_status=4 THEN 1 ELSE 0 END) cancelCount",
			"FROM etl_job_instance where DATE(create_time) = CURDATE() and instance_type =#{instanceType}",
			"group by DATE_FORMAT(create_time, '%Y-%m-%d %H')"

	})
	List<InstanceCountVO> instanceLineChart(Integer instanceType);


}