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