693c10af226a195be552f47ed95e4338d6b3f097
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.domain.repo;
2
3 import java.util.Map;
4 import java.util.Optional;
5
6 import org.springframework.data.domain.Example;
7 import org.springframework.data.domain.ExampleMatcher;
8 import org.springframework.data.domain.Page;
9 import org.springframework.data.domain.PageRequest;
10 import org.springframework.stereotype.Repository;
11
12 import com.supwisdom.institute.backend.common.framework.repo.BaseJpaRepository;
13 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
14 import com.supwisdom.institute.backend.system.domain.entity.Resource;
15
16 @Repository
17 public interface ResourceRepository extends BaseJpaRepository<Resource> {
18
19   @Override
20   public default Page<Resource> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
21     if (loadAll) {
22       pageIndex = 0;
23       pageSize = Integer.MAX_VALUE;
24     }
25     
26     Resource probe = new Resource();
27     if (mapBean != null) {
28       probe.setDeleted(MapBeanUtils.getBoolean(mapBean, "deleted"));
29       probe.setCode(MapBeanUtils.getString(mapBean, "code"));
30       probe.setName(MapBeanUtils.getString(mapBean, "name"));
31       probe.setMemo(MapBeanUtils.getString(mapBean, "memo"));
32       probe.setStatus(MapBeanUtils.getString(mapBean, "status"));
33     }
34     
35     ExampleMatcher matcher = ExampleMatcher.matching()
36         .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
37         .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.contains())
38         .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
39         .withMatcher("memo", ExampleMatcher.GenericPropertyMatchers.contains())
40         .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact());
41     
42     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
43     Example<Resource> example = Example.of(probe, matcher);
44     
45     Page<Resource> page = this.findAll(example, pageRequest);
46     
47     return page;
48   }
49
50   public default Resource selectByCode(String code) {
51     Resource probe = new Resource();
52     probe.setCode(code);
53     
54     ExampleMatcher matcher = ExampleMatcher.matching()
55         .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.exact());
56     
57     Example<Resource> example = Example.of(probe, matcher);
58     
59     Optional<Resource> o = this.findOne(example);
60     
61     if (o.isPresent()) {
62       return o.get();
63     }
64     
65     return null;
66   }
67   
68 }