<%_ if (position === 'import') { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import <%= packageName %>.job.service.dto.XxlJobRegistryDTO;
import org.springframework.transaction.annotation.Transactional;
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    public int registryUpdate(String registryGroup, String registryKey, String registryValue, Date updateTime) {
        LambdaUpdateWrapper<XxlJobRegistry> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .set(XxlJobRegistry::getUpdateTime, updateTime)
            .eq(XxlJobRegistry::getRegistryGroup, registryGroup)
            .eq(XxlJobRegistry::getRegistryKey, registryKey)
            .eq(XxlJobRegistry::getRegistryValue, registryValue);
        return xxlJobRegistryRepository.update(null, updateWrapper);
    }

    @Transactional
    public void registrySave(String registryGroup, String registryKey, String registryValue, Date date) {
        XxlJobRegistryDTO dto = new XxlJobRegistryDTO();
        dto.registryGroup(registryGroup)
            .registryKey(registryKey)
            .registryValue(registryValue)
            .updateTime(ZonedDateTime.now());
        this.save(dto);
    }

    public int registryDelete(String registryGroup, String registryKey, String registryValue) {
        LambdaQueryWrapper<XxlJobRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XxlJobRegistry::getRegistryGroup, registryGroup)
            .eq(XxlJobRegistry::getRegistryKey, registryKey)
            .eq(XxlJobRegistry::getRegistryValue, registryValue);
        return xxlJobRegistryRepository.delete(queryWrapper);
    }

    public List<Integer> findDead(int deadTimeout, Date date) {
        LambdaQueryWrapper<XxlJobRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(XxlJobRegistry::getId)
            .lt(XxlJobRegistry::getUpdateTime, ZonedDateTime.now(Clock.systemDefaultZone()).minusSeconds(deadTimeout));
        return xxlJobRegistryRepository.selectObjs(queryWrapper).stream().map(record -> Integer.valueOf(record.toString())).collect(Collectors.toList());
    }

    public List<XxlJobRegistryDTO> findAll(int deadTimeout, Date date) {
        LambdaQueryWrapper<XxlJobRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(XxlJobRegistry::getUpdateTime, ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).minusSeconds(deadTimeout));
        return xxlJobRegistryRepository.selectList(queryWrapper).stream().map(xxlJobRegistryMapper::toDto).collect(Collectors.toList());
    }

    public void removeDead(List<Integer> ids) {
        LambdaQueryWrapper<XxlJobRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(XxlJobRegistry::getId, ids);
        xxlJobRegistryRepository.delete(queryWrapper);
    }

<%_ } _%>
