wjli
2023-05-09 be396e19af86f49cc2c966c73b5f59cd36c7402e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cn.exrick.xboot.quartz.serviceimpl;
 
import cn.exrick.xboot.quartz.dao.QuartzJobDao;
import cn.exrick.xboot.quartz.entity.QuartzJob;
import cn.exrick.xboot.quartz.service.QuartzJobService;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 定时任务接口实现
 * @author Exrick
 */
@Slf4j
@Service
@Transactional
public class QuartzJobServiceImpl implements QuartzJobService {
 
    @Autowired
    private QuartzJobDao quartzJobDao;
 
    @Override
    public QuartzJobDao getRepository() {
        return quartzJobDao;
    }
 
    @Override
    public List<QuartzJob> findByJobClassName(String jobClassName) {
 
        return quartzJobDao.findByJobClassName(jobClassName);
    }
 
    @Override
    public Page<QuartzJob> findByCondition(String key, Pageable pageable) {
 
        return quartzJobDao.findAll(new Specification<QuartzJob>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<QuartzJob> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
 
                Path<String> jobClassNameField = root.get("jobClassName");
                Path<String> parameterField = root.get("parameter");
                Path<String> descriptionField = root.get("description");
 
                List<Predicate> list = new ArrayList<>();
 
                // 模糊搜素
                if (StrUtil.isNotBlank(key)) {
                    Predicate p1 = cb.like(jobClassNameField, '%' + key + '%');
                    Predicate p2 = cb.like(parameterField, '%' + key + '%');
                    Predicate p3 = cb.like(descriptionField, '%' + key + '%');
                    list.add(cb.or(p1, p2, p3));
                }
 
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }
}