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