1b44fa99182b55e9571bd3252f9e59c217b8c4e3
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.domain.repo;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Optional;
7
8 import javax.persistence.criteria.CriteriaBuilder;
9 import javax.persistence.criteria.CriteriaQuery;
10 import javax.persistence.criteria.Predicate;
11 import javax.persistence.criteria.Root;
12
13 import org.springframework.data.domain.Example;
14 import org.springframework.data.domain.ExampleMatcher;
15 import org.springframework.data.jpa.domain.Specification;
16 import org.springframework.stereotype.Repository;
17 import org.springframework.util.StringUtils;
18
19 import com.supwisdom.institute.backend.common.framework.repo.BaseJpaRepository;
20 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
21 import com.supwisdom.institute.backend.system.domain.entity.Account;
22
23 @Repository
24 public interface AccountRepository extends BaseJpaRepository<Account> {
25   
26 //  public default Page<Account> selectPageList(int pageIndex, int pageSize, Account probe) {
27 //    
28 //    ExampleMatcher matcher = ExampleMatcher.matching()
29 //        .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
30 //        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
31 //        .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact());
32 //    
33 //    PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
34 //    Example<Account> example = Example.of(probe, matcher);
35 //    
36 //    Page<Account> page = this.findAll(example, pageRequest);
37 //    
38 //    return page;
39 //  }
40   
41   
42   @Override
43   public default Specification<Account> convertToSpec(Map<String, Object> mapBean) {
44     
45     Specification<Account> spec = new Specification<Account>() {
46
47       /**
48        * 
49        */
50       private static final long serialVersionUID = 9071470982419099273L;
51
52       @Override
53       public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
54         List<Predicate> predicates = new ArrayList<>();
55         
56         if (mapBean != null) {
57
58           if (MapBeanUtils.getBoolean(mapBean, "deleted") != null) {
59             predicates.add(criteriaBuilder.equal(root.get("deleted"), MapBeanUtils.getBoolean(mapBean, "deleted")));
60           }
61
62           if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "username"))) {
63             predicates.add(criteriaBuilder.like(root.get("username"), "%" + MapBeanUtils.getString(mapBean, "username") + "%"));
64           }
65           if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "name"))) {
66             predicates.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "name") + "%"));
67           }
68           
69           if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "status"))) {
70             predicates.add(criteriaBuilder.equal(root.get("status"), MapBeanUtils.getString(mapBean, "status")));
71           }
72           
73 //          if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "grantTimeBegin"))) {
74 //            String grantTimeBegin = MapBeanUtils.getString(mapBean, "grantTimeBegin");
75 //            Date d = DateUtil.parseDate(grantTimeBegin+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
76 //            
77 //            if (d != null) {
78 //              predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("grantTime"), d));
79 //            }
80 //          }
81 //
82 //          if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "grantTimeEnd"))) {
83 //            String grantTimeEnd = MapBeanUtils.getString(mapBean, "grantTimeEnd");
84 //            Date d = DateUtil.parseDate(grantTimeEnd+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
85 //            
86 //            if (d != null) {
87 //              predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("grantTime"), d));
88 //            }
89 //          }
90
91           List<Predicate> predicatesKeyword = new ArrayList<>();
92           if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "keyword"))) {
93             predicatesKeyword.add(criteriaBuilder.like(root.get("username"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
94             predicatesKeyword.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
95             predicatesKeyword.add(criteriaBuilder.like(root.get("memo"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
96             
97             predicates.add(criteriaBuilder.or(predicatesKeyword.toArray(new Predicate[predicatesKeyword.size()])));
98           }
99         }
100         
101         return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
102       }
103       
104     };
105     
106     return spec;
107   }
108   
109   
110 //  @Override
111 //  public default Page<Account> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
112 //    if (loadAll) {
113 //      pageIndex = 0;
114 //      pageSize = Integer.MAX_VALUE;
115 //    }
116 //    
117 //    Account probe = new Account();
118 //    if (mapBean != null) {
119 //      probe.setDeleted(MapBeanUtils.getBoolean(mapBean, "deleted"));
120 //      probe.setUsername(MapBeanUtils.getString(mapBean, "username"));
121 //      probe.setName(MapBeanUtils.getString(mapBean, "name"));
122 //      probe.setStatus(MapBeanUtils.getString(mapBean, "status"));
123 //    }
124 //    
125 //    ExampleMatcher matcher = ExampleMatcher.matching()
126 //        .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
127 //        .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
128 //        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
129 //        .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact());
130 //    
131 //    PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
132 //    Example<Account> example = Example.of(probe, matcher);
133 //    
134 //    Page<Account> page = this.findAll(example, pageRequest);
135 //    
136 //    return page;
137 //  }
138   
139   /*
140   public default User selectById(String id) {
141     
142     try {
143       Optional<User> entity = this.findById(id);
144       
145       return entity.get();
146     } catch(RuntimeException e) {
147       System.out.println("RuntimeException:"+e.getMessage());
148     } catch(Exception e) {
149       System.out.println("Exception:"+e.getMessage());
150     }
151     
152     return null;
153   }
154   
155   public default User insert(User entity) {
156     
157     if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
158       entity.setCompanyId("1");
159     }
160     
161     entity.setDeleted(false);
162     //entity.setAddAccount(AuthUtil.getRemoteUser());
163     entity.setAddTime(Calendar.getInstance().getTime());
164     
165     User e = this.save(entity);
166     
167     return e;
168   }
169   
170   public default User update(User entity) {
171     
172     //entity.setEditAccount(AuthUtil.getRemoteUser());
173     entity.setEditTime(Calendar.getInstance().getTime());
174     
175     User e = this.save(entity);
176     
177     return e;
178   }
179   */
180   
181   public default Account selectByUsername(String username) {
182     Account probe = new Account();
183     probe.setUsername(username);
184     
185     ExampleMatcher matcher = ExampleMatcher.matching()
186         .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.exact());
187     
188     Example<Account> example = Example.of(probe, matcher);
189     
190     Optional<Account> u = this.findOne(example);
191     
192     if (u.isPresent()) {
193       return u.get();
194     }
195     
196     return null;
197   }
198
199 }