e5308df8ea871842cb6dfe730c93062b110bd5ca
[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 (pageIndex == -1) {
44       loadAll = true;
45     }
46     
47     if (loadAll) {
48       pageIndex = 0;
49       pageSize = Integer.MAX_VALUE;
50     }
51     
52     Sort sort = convertToSort(orderBy);
53
54     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
55     if (sort != null) {
56       pageRequest = PageRequest.of(pageIndex, pageSize, sort);
57     }
58
59     Page<E> page = this.findAll(spec, pageRequest);
60
61     return page;
62   }
63   
64
65
66
67   public default E selectById(String id) {
68
69     try {
70       Optional<E> entity = this.findById(id);
71
72       if (entity.isPresent()) {
73         return entity.get();
74       }
75     } catch (RuntimeException e) {
76       System.out.println("RuntimeException:" + e.getMessage());
77     } catch (Exception e) {
78       System.out.println("Exception:" + e.getMessage());
79     }
80
81     return null;
82   }
83   
84   public default E insert(E entity) {
85     
86     if (entity.getId() == null || entity.getId().isEmpty()) {
87       entity.setId(generateId());
88     }
89
90     if (entity.getCompanyId() == null || entity.getCompanyId().isEmpty()) {
91       entity.setCompanyId("1");
92     }
93
94     if (entity.getDeleted() == null) {
95       entity.setDeleted(false);
96     }
97     if (entity.getAddAccount() == null) {
98       entity.setAddAccount(UserContext.getUsername());
99     }
100     if (entity.getAddTime() == null) {
101       entity.setAddTime(Calendar.getInstance().getTime());
102     }
103     
104     E e = this.save(entity);
105
106     return e;
107   }
108   
109   public default E update(E entity) {
110
111     if (entity.getEditAccount() == null) {
112       entity.setEditAccount(UserContext.getUsername());
113     }
114     if (entity.getEditTime() == null) {
115       entity.setEditTime(Calendar.getInstance().getTime());
116     }
117     
118     E e = this.save(entity);
119
120     return e;
121   }
122   
123   public default E remove(E entity) {
124     
125     if (entity.getDeleted() == null) {
126       entity.setDeleted(true);
127     }
128     if (entity.getDeleteAccount() == null) {
129       entity.setDeleteAccount(UserContext.getUsername());
130     }
131     if (entity.getDeleteTime() == null) {
132       entity.setDeleteTime(Calendar.getInstance().getTime());
133     }
134     
135     E e = this.save(entity);
136     
137     return e;
138   }
139   
140   public default void delete(String id) {
141     
142     this.deleteById(id);
143   }
144
145 }