1 package com.supwisdom.institute.backend.system.domain.repo;
3 import java.util.ArrayList;
6 import java.util.Optional;
8 import javax.persistence.criteria.CriteriaBuilder;
9 import javax.persistence.criteria.CriteriaQuery;
10 import javax.persistence.criteria.Predicate;
11 import javax.persistence.criteria.Root;
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;
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;
24 public interface AccountRepository extends BaseJpaRepository<Account> {
26 // public default Page<Account> selectPageList(int pageIndex, int pageSize, Account probe) {
28 // ExampleMatcher matcher = ExampleMatcher.matching()
29 // .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
30 // .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
31 // .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact());
33 // PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
34 // Example<Account> example = Example.of(probe, matcher);
36 // Page<Account> page = this.findAll(example, pageRequest);
43 public default Specification<Account> convertToSpec(Map<String, Object> mapBean) {
45 Specification<Account> spec = new Specification<Account>() {
50 private static final long serialVersionUID = 9071470982419099273L;
53 public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
54 List<Predicate> predicates = new ArrayList<>();
56 if (mapBean != null) {
58 if (MapBeanUtils.getBoolean(mapBean, "deleted") != null) {
59 predicates.add(criteriaBuilder.equal(root.get("deleted"), MapBeanUtils.getBoolean(mapBean, "deleted")));
62 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "username"))) {
63 predicates.add(criteriaBuilder.like(root.get("username"), "%" + MapBeanUtils.getString(mapBean, "username") + "%"));
65 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "name"))) {
66 predicates.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "name") + "%"));
69 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "status"))) {
70 predicates.add(criteriaBuilder.equal(root.get("status"), MapBeanUtils.getString(mapBean, "status")));
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");
78 // predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("grantTime"), d));
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");
87 // predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("grantTime"), d));
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") + "%"));
97 predicates.add(criteriaBuilder.or(predicatesKeyword.toArray(new Predicate[predicatesKeyword.size()])));
101 return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
111 // public default Page<Account> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
114 // pageSize = Integer.MAX_VALUE;
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"));
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());
131 // PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
132 // Example<Account> example = Example.of(probe, matcher);
134 // Page<Account> page = this.findAll(example, pageRequest);
140 public default User selectById(String id) {
143 Optional<User> entity = this.findById(id);
146 } catch(RuntimeException e) {
147 System.out.println("RuntimeException:"+e.getMessage());
148 } catch(Exception e) {
149 System.out.println("Exception:"+e.getMessage());
155 public default User insert(User entity) {
157 if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
158 entity.setCompanyId("1");
161 entity.setDeleted(false);
162 //entity.setAddAccount(AuthUtil.getRemoteUser());
163 entity.setAddTime(Calendar.getInstance().getTime());
165 User e = this.save(entity);
170 public default User update(User entity) {
172 //entity.setEditAccount(AuthUtil.getRemoteUser());
173 entity.setEditTime(Calendar.getInstance().getTime());
175 User e = this.save(entity);
181 public default Account selectByUsername(String username) {
182 Account probe = new Account();
183 probe.setUsername(username);
185 ExampleMatcher matcher = ExampleMatcher.matching()
186 .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.exact());
188 Example<Account> example = Example.of(probe, matcher);
190 Optional<Account> u = this.findOne(example);