<%_ if (position === 'import') { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import <%= packageName %>.job.service.dto.XxlJobLogDTO;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    public List<Long> findFailJobLogIds(int i) {
        LambdaQueryWrapper<XxlJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
            .select(XxlJobLog::getId)
            .eq(XxlJobLog::getAlarmStatus, 0)
            .orderBy(true, false, XxlJobLog::getId)
            .last(" limit " + i);
        return xxlJobLogRepository.selectObjs(queryWrapper).stream().map(record -> Long.valueOf(record.toString())).collect(Collectors.toList());
    }

    public int updateAlarmStatus(long logId,
                                 int oldAlarmStatus,
                                 int newAlarmStatus) {
        LambdaUpdateWrapper<XxlJobLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(XxlJobLog::getAlarmStatus, newAlarmStatus)
            .eq(XxlJobLog::getId, logId)
            .eq(XxlJobLog::getAlarmStatus, oldAlarmStatus);

        return xxlJobLogRepository.update(null, updateWrapper);
    }

    public void updateTriggerInfo(XxlJobLogDTO log) {
        LambdaUpdateWrapper<XxlJobLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .set(XxlJobLog::getTriggerTime, log.getTriggerTime())
            .set(XxlJobLog::getTriggerCode, log.getTriggerCode())
            .set(XxlJobLog::getTriggerMsg, log.getTriggerMsg())
            .set(XxlJobLog::getExecutorAddress, log.getExecutorAddress())
            .set(XxlJobLog::getExecutorHandler, log.getExecutorHandler())
            .set(XxlJobLog::getExecutorParam, log.getExecutorParam())
            .set(XxlJobLog::getExecutorShardingParam, log.getExecutorShardingParam())
            .set(XxlJobLog::getExecutorFailRetryCount, log.getExecutorFailRetryCount())
            .eq(XxlJobLog::getId, log.getId());
        xxlJobLogRepository.update(null, updateWrapper);
    }

    public int updateHandleInfo(XxlJobLogDTO xxlJobLog) {
        LambdaUpdateWrapper<XxlJobLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .set(XxlJobLog::getHandleTime, xxlJobLog.getHandleTime())
            .set(XxlJobLog::getHandleCode, xxlJobLog.getHandleCode())
            .set(XxlJobLog::getHandleMsg, xxlJobLog.getHandleMsg())
            .eq(XxlJobLog::getId, xxlJobLog.getId());
        return xxlJobLogRepository.update(null, updateWrapper);
    }

    public List<Long> findClearLogIds(long jobGroup, long jobId, Date clearBeforeTime, int clearBeforeNum, int pagesize) {
        LambdaQueryWrapper<XxlJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
            .select(XxlJobLog::getId)
            .eq(jobGroup > 0, XxlJobLog::getJobGroup, jobGroup)
            .eq(jobId > 0, XxlJobLog::getJobId, jobId)
            .orderBy(true, false, XxlJobLog::getTriggerTime)
            .last(" limit 0, " + clearBeforeNum);
        List<Object> ids = xxlJobLogRepository.selectObjs(queryWrapper);
        queryWrapper.clear();
        queryWrapper
            .eq(jobGroup > 0, XxlJobLog::getJobGroup, jobGroup)
            .eq(jobId > 0, XxlJobLog::getJobId, jobId)
            .le(Objects.nonNull(clearBeforeTime), XxlJobLog::getTriggerTime, clearBeforeTime)
            .notIn(clearBeforeNum > 0, XxlJobLog::getId, ids);

        return xxlJobLogRepository.selectObjs(queryWrapper).stream().map(record -> Long.valueOf(record.toString())).collect(Collectors.toList());
    }

    public Map<String, Object> findLogReport(Date todayFrom, Date todayTo) {
        return xxlJobLogRepository.findLogReport(todayFrom, todayTo);
    }

    public List<Long> findLostJobIds(Date losedTime) {
        LambdaQueryWrapper<XxlJobLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
            .select(XxlJobLog::getId)
            .eq(XxlJobLog::getTriggerCode, 200)
            .eq(XxlJobLog::getHandleCode, 0)
            .le(XxlJobLog::getTriggerTime, losedTime);
        return xxlJobLogRepository.selectObjs(queryWrapper).stream().map(record -> Long.valueOf(record.toString())).collect(Collectors.toList());
    }

<%_ } _%>
