xuefei
2020-12-10 eeeb7233935ea9b10e99043bdbf740ef86c9bf20
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package ${entity.serviceImplPackage};
 
import ${entity.daoPackage}.${entity.className}Dao;
import ${entity.entityPackage}.${entity.className};
import ${entity.servicePackage}.${entity.className}Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
<%
if(!entity.isTree){
%>
import SearchVo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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 java.util.List;
<%
if(!entity.isTree){
%>
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.lang.reflect.Field;
<%
}
%>
 
/**
 * ${entity.description}接口实现
 * @author ${entity.author}
 */
@Slf4j
@Service
@Transactional
public class ${entity.className}ServiceImpl implements ${entity.className}Service {
 
    @Autowired
    private ${entity.className}Dao ${entity.classNameLowerCase}Dao;
 
    @Override
    public ${entity.className}Dao getRepository() {
        return ${entity.classNameLowerCase}Dao;
    }
 
    <%
    if(!entity.isTree){
    %>
    @Override
    public Page<${entity.className}> findByCondition(${entity.className} ${entity.classNameLowerCase}, SearchVo searchVo, Pageable pageable) {
 
        return ${entity.classNameLowerCase}Dao.findAll(new Specification<${entity.className}>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<${entity.className}> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
 
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
 
                List<Predicate> list = new ArrayList<Predicate>();
 
                // 创建时间
                if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }
 
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }
    <%
    }
    %>
 
    <%
    if(entity.isTree){
    %>
    @Override
    public List<${entity.className}> findByParentIdOrderBySortOrder(String parentId) {
 
        return ${entity.classNameLowerCase}Dao.findByParentIdOrderBySortOrder(parentId);
    }
 
    @Override
    public List<${entity.className}> findByTitleLikeOrderBySortOrder(String title) {
 
        return ${entity.classNameLowerCase}Dao.findByTitleLikeOrderBySortOrder(title);
    }
    <%
    }
    %>
}