BController_facade.java
6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.bsth.controller.schedule;
import com.bsth.common.Constants;
import com.bsth.common.ResponseCode;
import com.bsth.control_v2.plan_module.common.dto.PageRequestDto;
import com.bsth.control_v2.plan_module.common.exception.PlanModuleException;
import com.bsth.control_v2.plan_module.common.service.BServiceFacade;
import com.bsth.entity.sys.SysUser;
import com.bsth.service.sys.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class BController_facade<Id extends Serializable, DTO> {
protected abstract BServiceFacade<Id, DTO> getBServiceFacade();
@Autowired
private SysUserService sysUserService;
//---------------- 设置操作用户 ----------------//
public void setCreateUserInfo(DTO dto, Integer userId, Date createDate) {
throw new PlanModuleException("子类必须override setCUserInfo方法!");
}
public void setUpdateUserInfo(DTO dto, Integer userId, Date updateDate) {
throw new PlanModuleException("子类必须override setUpdateUserInfo方法!");
}
//---------------- 扩展的save,update,delete操作 --------------------//
protected boolean isSaveExtend() {
return false;
}
protected DTO saveExtend(DTO dto) {
throw new PlanModuleException("子类如果isSaveExtend()==true,必须覆写saveExtend!");
}
protected boolean isUpdateExtend() {
return false;
}
protected DTO updateExtend(DTO dto) {
throw new PlanModuleException("子类如果isUpdateExtend()==true,必须覆写updateExtend!");
}
protected boolean isDeleteExtend() {
return false;
}
protected void deleteExtend(Id id) {
throw new PlanModuleException("子类如果isDeleteExtend()==true,必须覆写deleteExtend!");
}
//---------------- CRUD 操作 ----------------//
// Create操作
@RequestMapping(method = RequestMethod.POST)
public Map<String, Object> save(@RequestBody DTO dto, HttpSession httpSession) {
String userName = String.valueOf(httpSession.getAttribute(Constants.SESSION_USERNAME));
SysUser sysUser = sysUserService.findByUserName(userName);
setCreateUserInfo(dto, sysUser.getId(), new Date());
setUpdateUserInfo(dto, sysUser.getId(), new Date());
Map<String, Object> rtn = new HashMap<>();
try {
DTO dto_saved;
if (isSaveExtend()) {
dto_saved = saveExtend(dto);
} else {
dto_saved = getBServiceFacade().save(dto);
}
rtn.put("status", ResponseCode.SUCCESS);
rtn.put("data", dto_saved);
} catch (Exception exp) {
rtn.put("status", ResponseCode.ERROR);
rtn.put("msg", exp.getMessage());
}
return rtn;
}
// Update操作
@RequestMapping(value="/{id}", method = RequestMethod.POST)
public Map<String, Object> update(@RequestBody DTO dto, HttpSession httpSession) {
String userName = String.valueOf(httpSession.getAttribute(Constants.SESSION_USERNAME));
SysUser sysUser = sysUserService.findByUserName(userName);
setUpdateUserInfo(dto, sysUser.getId(), new Date());
Map<String, Object> rtn = new HashMap<>();
try {
DTO dto_updated;
if (isUpdateExtend()) {
dto_updated = updateExtend(dto);
} else {
dto_updated = getBServiceFacade().save(dto);
}
rtn.put("status", ResponseCode.SUCCESS);
rtn.put("data", dto_updated);
} catch (Exception exp) {
rtn.put("status", ResponseCode.ERROR);
rtn.put("msg", exp.getMessage());
}
return rtn;
}
// Research操作
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Map<String, Object> findById(@PathVariable("id") Id id) {
Map<String, Object> rtn = new HashMap<>();
try {
DTO dto = getBServiceFacade().findById(id);
rtn.put("status", ResponseCode.SUCCESS);
rtn.put("data", dto);
} catch (Exception exp) {
rtn.put("status", ResponseCode.ERROR);
rtn.put("msg", exp.getMessage());
}
return rtn;
}
// 查询所有操作
@RequestMapping(value = "/all", method = RequestMethod.GET)
public Map<String, Object> list(@RequestParam Map<String, Object> param) {
Map<String, Object> rtn = new HashMap<>();
try {
List<DTO> tList = getBServiceFacade().list(param);
rtn.put("status", ResponseCode.SUCCESS);
rtn.put("data", tList);
} catch (Exception exp) {
rtn.put("status", ResponseCode.ERROR);
rtn.put("msg", exp.getMessage());
}
return rtn;
}
// 分页查询操作
@RequestMapping(method = RequestMethod.GET)
public Map<String, Object> list(
@RequestParam Map<String, Object> map,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(defaultValue = "id") String order,
@RequestParam(defaultValue = "DESC") String direction) {
PageRequestDto pageRequestDto = PageRequestDto.getBuilder()
.setPage(page)
.setSize(size)
.setOrder(order)
.setDirection(direction)
.build();
Map<String, Object> rtn = new HashMap<>();
try {
rtn.put("data", getBServiceFacade().list(map, pageRequestDto));
rtn.put("status", ResponseCode.SUCCESS);
} catch (Exception exp) {
rtn.put("status", ResponseCode.ERROR);
rtn.put("msg", exp.getMessage());
}
return rtn;
}
// Delete操作
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public Map<String, Object> delete(@PathVariable("id") Id id, HttpSession httpSession) {
Map<String, Object> rtn = new HashMap<>();
try {
// 由于种种原因,这里不保存用户和操作时间了
if (isDeleteExtend()) {
deleteExtend(id);
} else {
getBServiceFacade().delete(id);
}
rtn.put("status", ResponseCode.SUCCESS);
} catch (Exception exp) {
rtn.put("status", ResponseCode.ERROR);
rtn.put("msg", exp.getMessage());
}
return rtn;
}
}