1 package com.supwisdom.institute.backend.system.domain.repo;
3 import java.util.ArrayList;
7 import java.util.Optional;
9 import javax.persistence.criteria.CriteriaBuilder;
10 import javax.persistence.criteria.CriteriaQuery;
11 import javax.persistence.criteria.Predicate;
12 import javax.persistence.criteria.Root;
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;
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;
28 public interface AccountRepository extends BaseJpaRepository<Account> {
30 // public default Page<Account> selectPageList(int pageIndex, int pageSize, Account probe) {
32 // ExampleMatcher matcher = ExampleMatcher.matching()
33 // .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
34 // .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
35 // .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact());
37 // PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
38 // Example<Account> example = Example.of(probe, matcher);
40 // Page<Account> page = this.findAll(example, pageRequest);
47 public default Specification<Account> convertToSpec(Map<String, Object> mapBean) {
49 Specification<Account> spec = new Specification<Account>() {
54 private static final long serialVersionUID = 9071470982419099273L;
57 public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
58 List<Predicate> predicates = new ArrayList<>();
60 if (mapBean != null) {
62 if (MapBeanUtils.getBoolean(mapBean, "deleted") != null) {
63 predicates.add(criteriaBuilder.equal(root.get("deleted"), MapBeanUtils.getBoolean(mapBean, "deleted")));
66 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "username"))) {
67 predicates.add(criteriaBuilder.like(root.get("username"), "%" + MapBeanUtils.getString(mapBean, "username") + "%"));
69 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "name"))) {
70 predicates.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "name") + "%"));
73 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "status"))) {
74 predicates.add(criteriaBuilder.equal(root.get("status"), MapBeanUtils.getString(mapBean, "status")));
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");
82 // predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("grantTime"), d));
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");
91 // predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("grantTime"), d));
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") + "%"));
101 predicates.add(criteriaBuilder.or(predicatesKeyword.toArray(new Predicate[predicatesKeyword.size()])));
105 return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
115 // public default Page<Account> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
118 // pageSize = Integer.MAX_VALUE;
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"));
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());
135 // PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
136 // Example<Account> example = Example.of(probe, matcher);
138 // Page<Account> page = this.findAll(example, pageRequest);
144 public default User selectById(String id) {
147 Optional<User> entity = this.findById(id);
150 } catch(RuntimeException e) {
151 System.out.println("RuntimeException:"+e.getMessage());
152 } catch(Exception e) {
153 System.out.println("Exception:"+e.getMessage());
159 public default User insert(User entity) {
161 if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
162 entity.setCompanyId("1");
165 entity.setDeleted(false);
166 //entity.setAddAccount(AuthUtil.getRemoteUser());
167 entity.setAddTime(Calendar.getInstance().getTime());
169 User e = this.save(entity);
174 public default User update(User entity) {
176 //entity.setEditAccount(AuthUtil.getRemoteUser());
177 entity.setEditTime(Calendar.getInstance().getTime());
179 User e = this.save(entity);
185 public default Account selectByUsername(String username) {
186 Account probe = new Account();
187 probe.setUsername(username);
189 ExampleMatcher matcher = ExampleMatcher.matching()
190 .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.exact());
192 Example<Account> example = Example.of(probe, matcher);
194 Optional<Account> u = this.findOne(example);