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

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springblade.etl.dto.InstanceStatusCount;
import org.springblade.etl.dto.dsy.RunningInstanceServerDto;
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.DsyJobInstance;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;

/**
 *	数据同步任务实例 数据处理
 *
 * @author aokunsang
 * @date   2023-09-21
 */
public interface DsyJobInstanceMapper extends BaseMapper<DsyJobInstance>{
	
	@Select({
		"<script>",
		"select instance_status status, count(1) as cnt from dsy_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);
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select * from dsy_job_instance where instance_status = 1")
	List<DsyJobInstance> selectAllRunningInstanceList();
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select * from dsy_job_instance where instance_status = 0")
	List<DsyJobInstance> selectAllWaitingInstanceList();
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select server_ip as serverIp, count(1) as cnt from dsy_job_instance where instance_status = 1 group by server_ip")
	List<RunningInstanceServerDto> countServerByRunningInstance();
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select job_id, min(start_time) as start_time, max(end_time) as end_time, max(id) as id, sum(timestampdiff(SECOND, start_time, end_time)) as total_secs from dsy_job_instance ${ew.customSqlSegment}")
	public IPage<DsyJobInstance> customPage(IPage<DsyJobInstance> page, @Param(Constants.WRAPPER) LambdaQueryWrapper<DsyJobInstance> queryWrapper);

	@InterceptorIgnore(tenantLine = "true")
	@Select("select count(distinct job_id) from dsy_job_instance ${ew.customSqlSegment}")
	public Long countPage(@Param(Constants.WRAPPER) LambdaQueryWrapper<DsyJobInstance> queryWrapper);
	
	@InterceptorIgnore(tenantLine = "true")
	@Select("select \n" + 
			"count(IF(instance_status = 0, 1, null)) as waitingCnt, \n" + 
			"count(IF(instance_status = 1, 1, null)) as runningCnt,\n" + 
			"count(IF(instance_status = 2, 1, null)) as successCnt,\n" + 
			"count(IF(instance_status = 3, 1, null)) as failureCnt,\n" + 
			"count(IF(instance_status = 4, 1, null)) as canceldCnt,\n" + 
			"count(IF(instance_status = 5, 1, null)) as unknownCnt\n" + 
			"from dsy_job_instance where job_id = #{value}\n" + 
			"group by job_id")
	public InstanceStatusCount statInstanceStatusByJobId(Integer jobId);

	@Select("select count(1)                                             as total,\n" +
			"       SUM(CASE WHEN instance_status = 1 THEN 1 ELSE 0 END) as runningCount,\n" +
			"       SUM(CASE WHEN instance_status = 2 THEN 1 ELSE 0 END)    successCount,\n" +
			"       SUM(CASE WHEN instance_status = 3 THEN 1 ELSE 0 END)    failCount,\n" +
			"       SUM(CASE WHEN instance_status = 4 THEN 1 ELSE 0 END)    cancelCount,\n" +
			"       SUM(CASE WHEN instance_status = 5 THEN 1 ELSE 0 END)    waitCount\n" +
			"from dsy_job_instance")
    InstanceCountVO getCounts();

	@Select("select count(1)                             as              total,\n" +
			"       DATE_FORMAT(create_time, '%Y-%m-%d') as              date,\n" +
			"       SUM(CASE WHEN instance_status = 2 THEN 1 ELSE 0 END) successCount,\n" +
			"       SUM(CASE WHEN instance_status = 3 THEN 1 ELSE 0 END) failCount,\n" +
			"       SUM(CASE WHEN instance_status = 5 THEN 1 ELSE 0 END) waitCount\n" +
			"FROM dsy_job_instance\n" +
			"where DATE_SUB(CURDATE(), INTERVAL #{day} DAY) <= date(create_time)\n" +
			"group by DATE_FORMAT(create_time, '%Y-%m-%d')\n" +
			"order by DATE_FORMAT(create_time, '%Y-%m-%d')")
	List<InstanceCountVO> getCountByDays(int day);

	@Select("select count(1)                             as              total,\n" +
			"       DATE_FORMAT(create_time, '%Y-%m-%d') as              `date`,\n" +
			"       SUM(CASE WHEN instance_status = 1 THEN 1 ELSE 0 END) runningCnt,\n" +
			"       SUM(CASE WHEN instance_status = 2 THEN 1 ELSE 0 END) successCount,\n" +
			"       SUM(CASE WHEN instance_status = 3 THEN 1 ELSE 0 END) failCount,\n" +
			"       SUM(CASE WHEN instance_status = 4 THEN 1 ELSE 0 END) cancelCount,\n" +
			"       SUM(CASE WHEN instance_status = 5 THEN 1 ELSE 0 END) waitCount\n" +
			"FROM dsy_job_instance\n" +
			"where DATE_FORMAT(create_time, 'y%Y-%m-%d') <= DATE_FORMAT(now(), '%Y-%m-%d')\n" +
			"group by `date`")
	InstanceCountVO getCountToday();

	@Select("select count(1)                                             as total,\n" +
			"       DATE_FORMAT(create_time, '%Y-%m-%d %H')              as date,\n" +
			"       SUM(CASE WHEN instance_status = 1 THEN 1 ELSE 0 END) as runningCount,\n" +
			"       SUM(CASE WHEN instance_status = 2 THEN 1 ELSE 0 END)    successCount,\n" +
			"       SUM(CASE WHEN instance_status = 3 THEN 1 ELSE 0 END)    failCount,\n" +
			"       SUM(CASE WHEN instance_status = 4 THEN 1 ELSE 0 END)    cancelCount\n" +
			"FROM dsy_job_instance\n" +
			"where DATE(create_time) = CURDATE()\n" +
			"group by DATE_FORMAT(create_time, '%Y-%m-%d %H')")
	List<InstanceCountVO> instanceLineChart();

	@Select("select dj.job_name jobName, dji.*\n" +
			"from dsy_job_instance dji\n" +
			"         inner join dsy_job dj on dji.job_id = dj.id\n" +
			"order by dji.start_time desc")
    IPage<DsyJobInstance> pageDo(IPage<DsyJobInstance> page);
}