2be2c31e0d410b79ce7b1bd3152a8603533c939b
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.common.framework.repo;
2
3 import java.util.Calendar;
4 import java.util.Map;
5 import java.util.Optional;
6
7 import org.springframework.data.domain.Page;
8 import org.springframework.data.domain.PageRequest;
9 import org.springframework.data.domain.Sort;
10 import org.springframework.data.jpa.domain.Specification;
11 import org.springframework.data.jpa.repository.JpaRepository;
12 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
13 import org.springframework.data.repository.NoRepositoryBean;
14
15 import com.supwisdom.institute.backend.common.core.transmit.user.UserContext;
16 import com.supwisdom.institute.backend.common.framework.entity.ABaseEntity;
17 import com.supwisdom.institute.backend.common.util.UUIDUtils;
18
19 @NoRepositoryBean
20 public interface BaseJpaRepository<E extends ABaseEntity> extends JpaRepository<E, String>, JpaSpecificationExecutor<E> {
21   
22
23   /**
24    * 生成主键值。 默认使用方法
25    * 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。 
26    * @param entity 要持久化的对象 
27    */
28   public default String generateId() {
29     return UUIDUtils.create();
30   }
31   
32   public default Specification<E> convertToSpec(Map<String, Object> mapBean) {
33     return null;
34   }
35   public default Sort convertToSort(Map<String, String> orderBy) {
36     return null;
37   }
38
39   public default Page<E> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
40     
41     Specification<E> spec = convertToSpec(mapBean);
42     
43     if (loadAll) {
44       pageIndex = 0;
45       pageSize = Integer.MAX_VALUE;
46     }
47     
48     Sort sort = convertToSort(orderBy);
49
50     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
51     if (sort != null) {
52       pageRequest = PageRequest.of(pageIndex, pageSize, sort);
53     }
54
55     Page<E> page = this.findAll(spec, pageRequest);
56
57     return page;
58   }
59   
60
61
62
63   public default E selectById(String id) {
64
65     try {
66       Optional<E> entity = this.findById(id);
67
68       if (entity.isPresent()) {
69         return entity.get();
70       }
71     } catch (RuntimeException e) {
72       System.out.println("RuntimeException:" + e.getMessage());
73     } catch (Exception e) {
74       System.out.println("Exception:" + e.getMessage());
75     }
76
77     return null;
78   }
79   
80   public default E insert(E entity) {
81     
82     if (entity.getId() == null || entity.getId().isEmpty()) {
83       entity.setId(generateId());
84     }
85
86     if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
87       entity.setCompanyId("1");
88     }
89
90     if (entity.getDeleted() == null) {
91       entity.setDeleted(false);
92     }
93     if (entity.getAddAccount() == null) {
94       entity.setAddAccount(UserContext.getUsername());
95     }
96     if (entity.getAddTime() == null) {
97       entity.setAddTime(Calendar.getInstance().getTime());
98     }
99     
100     E e = this.save(entity);
101
102     return e;
103   }
104   
105   public default E update(E entity) {
106
107     if (entity.getEditAccount() == null) {
108       entity.setEditAccount(UserContext.getUsername());
109     }
110     if (entity.getEditTime() == null) {
111       entity.setEditTime(Calendar.getInstance().getTime());
112     }
113     
114     E e = this.save(entity);
115
116     return e;
117   }
118   
119   public default E remove(E entity) {
120     
121     if (entity.getDeleted() == null) {
122       entity.setDeleted(true);
123     }
124     if (entity.getDeleteAccount() == null) {
125       entity.setDeleteAccount(UserContext.getUsername());
126     }
127     if (entity.getDeleteTime() == null) {
128       entity.setDeleteTime(Calendar.getInstance().getTime());
129     }
130     
131     E e = this.save(entity);
132     
133     return e;
134   }
135   
136   public default void delete(String id) {
137     
138     this.deleteById(id);
139   }
140
141 }