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