shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package com.product.org.admin.controller;
 
import com.product.common.lang.StringUtils;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.product.core.permission.PermissionService;
import com.product.module.sys.config.SystemErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import com.product.core.config.CoreConst;
import com.product.core.controller.support.AbstractBaseController;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.entity.RequestParameterEntity;
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.sys.entity.SystemUser;
import com.product.module.sys.version.ApiVersion;
import com.product.org.admin.config.CmnConst;
import com.product.org.admin.config.SystemCode;
import com.product.org.admin.service.ProcedureService;
import com.product.org.admin.service.idel.IProcedureService;
import com.product.util.BaseUtil;
 
/**
 * Copyright © LX-BASE
 * 
 * @Title: ProcedureController
 * @Project: LX-BASE-SERVER
 * @Date: 2020-05-29 11:42:06
 * @Author: Xin.Luo
 * @Description: 存储过程管理
 */
@RestController
@RequestMapping("/api/procedure")
public class ProcedureController extends AbstractBaseController {
    
    @Autowired
    public ProcedureService procedureService;
    
    @Autowired
    public BaseDao baseDao = null;
    /**
     * 数据权限验证
     */
    @Autowired
    PermissionService permissionService;
 
    public BaseDao getBaseDao() {
        return baseDao;
    }
 
    public void setBaseDao(BaseDao baseDao) {
        this.baseDao = baseDao;
    }
 
    /**
     *    解析存储过程
     * @param response
     * @param request
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/runSql/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String testSql(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
        try {
            FieldSetEntity fse = null;
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            if (bean != null) {
                RequestParameterEntity reqp = (RequestParameterEntity) bean;
                fse = reqp.getFormData();
            }
            if (fse == null) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_DATAMODEL_PROCEDURE.equals(fse.getTableName())) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
                return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
            }
            IProcedureService service = (IProcedureService) getProxyInstance(procedureService);
            FieldSetEntity field = service.runProcedure(fse);
            // 返回field
            if (field != null) {
                return OK_List(field);
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText());
            return this.error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText());
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_RUN_SQL_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_SQL_PROCEDURE_FAIL.getText());
        }
    }
 
    /**
     *     保存存储过程
     * @param response
     * @param request
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveProcedure/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String saveProcedure(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
        try {
 
            // 获取参数
            FieldSetEntity fse = null;
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            if (bean != null) {
                RequestParameterEntity reqp = (RequestParameterEntity) bean;
                fse = reqp.getFormData();
            }
            // 判断参数是否为空
            if (fse == null) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            String procedure_sql = fse.getString(CmnConst.PROCEDURE_SQL);
            String uuid = fse.getString(CmnConst.UUID);
            FieldSetEntity proField = new FieldSetEntity();
            proField.setTableName(CmnConst.PRODUCT_SYS_DATAMODEL_PROCEDURE);
            procedure_sql = procedure_sql.replaceAll("\n", "  ");// 替换为空格
            procedure_sql = procedure_sql.replaceAll("\r", "  ");// 替换为空格
            proField.setValue(CmnConst.PROCEDURE_SQL, procedure_sql);
            if (StringUtils.isEmpty(uuid)) {
                fse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 创建人
                fse.setValue(CmnConst.CREATED_UTC_DATETIME, transitionDate(new Date()));// 创建时间
            } else {
                // 超级管理员为1
                SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
                int type;
                if (currentUser.getUserType() == 1) {
                    type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER;
                } else {
                    type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG;
                }
                // 数据操作的权限验证
                if (!permissionService.validDataPermission(fse, type)) {
                    SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
                    return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
                }
                fse.setValue(CmnConst.UUID, uuid);
                fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 修改人
                fse.setValue(CmnConst.UPDATED_UTC_DATETIME, transitionDate(new Date()));// 修改时间
            }
            IProcedureService service = (IProcedureService) getProxyInstance(procedureService);
            String pro_uuid = service.saveProcedure(fse);
            if (StringUtils.isEmpty(pro_uuid)) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getText());
                return this.error(SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getText());
            }
            return OK();
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_PROCEDURE_FAIL.getText());
        }
    }
 
    /**
     *     删除存储过程
     * @param response
     * @param request
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delProcedure/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String delProcedure(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
        try {
            // 获取参数
            FieldSetEntity fse = null;
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            if (bean != null) {
                RequestParameterEntity reqp = (RequestParameterEntity) bean;
                fse = reqp.getFormData();
            }
            // 判断参数是否为空
            if (fse == null) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            // 超级管理员为1
            SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
            int type;
            if (currentUser.getUserType() == 1) {
                type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER;
            } else {
                type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG;
            }
            // 数据操作的权限验证
            if (!permissionService.validDataPermission(fse, type)) {
                SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
                return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
            }
            if (fse.getTableName() == null || !CmnConst.PRODUCT_SYS_DATAMODEL_PROCEDURE.equals(fse.getTableName())) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
                return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText());
            }
            IProcedureService service = (IProcedureService) getProxyInstance(procedureService);
            boolean success = service.deleteProcedure(fse);
            if (success) {
                return OK();
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getValue(), SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getText());
            return this.error(SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getValue(), SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getText());
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getValue(), SystemCode.SYSTEM_PROCEDURE_FILTER_ERROR.getText());
        }
    }
 
    /**
     *     获取存储过程详情
     * @param response
     * @param request
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProcedureInfo/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String getProcedureInfo(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
        try {
            // 获取参数
            FieldSetEntity fse = null;
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            if (bean != null) {
                RequestParameterEntity reqp = (RequestParameterEntity) bean;
                fse = reqp.getFormData();
            }
            // 判断参数是否为空
            if (fse == null) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            String uuid = fse.getString(CmnConst.UUID);
            if (BaseUtil.strIsNull(uuid)) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            // 超级管理员为1
            SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
            int type;
            if (currentUser.getUserType() == 1) {
                type = CoreConst.DATA_PERMISSION_VALID_TYPE_USER;
            } else {
                type = CoreConst.DATA_PERMISSION_VALID_TYPE_ORG;
            }
            // 数据操作的权限验证
            if (!permissionService.validDataPermission(fse, type)) {
                SpringMVCContextHolder.getSystemLogger().error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
                return this.error(SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getValue(), SystemErrorCode.SYSTEM_NOT_OPER_PERMISSION.getText());
            }
            FieldSetEntity field = procedureService.getProcedureInfo(uuid);
            // 返回field
            if (field != null) {
                return OK_List(field);
            }
            SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText());
            return this.error(SystemCode.SYSTEM_SQL_START_FAIL.getValue(), SystemCode.SYSTEM_SQL_START_FAIL.getText());
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_PROCEDUREINFO_FAIL.getValue(),
                    SystemCode.SYSTEM_PROCEDUREINFO_FAIL.getText());
        }
    }
 
    /**
     *     存储过程列表
     * @param response
     * @param request
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProcedureAll/{version}", method = RequestMethod.POST)
    @ApiVersion(1)
    public String getProcedureAll(HttpServletResponse response, HttpServletRequest request, HttpSession session) {
        try {
            // 获取参数
            FieldSetEntity fse = null;
            Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
            if (bean != null) {
                RequestParameterEntity reqp = (RequestParameterEntity) bean;
                fse = reqp.getFormData();
            }
            // 判断参数是否为空
            if (fse == null) {
                SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
                return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText());
            }
            return OK_List(procedureService.getProcedureAll(fse));
        } catch (BaseException e) {
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(e.getCode(), e.getMessageInfo());
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            return this.error(SystemCode.SYSTEM_PROCEDUREALL_FAIL.getValue(), SystemCode.SYSTEM_PROCEDUREALL_FAIL.getText());
        }
    }
 
    // 时间处理为字符串
    public String transitionDate(Date date) {
        // yyyy表示年数,MM表示月,dd表示日
        // HH表示24小时制的小时(hh表示12小时的小时),mm表示分钟,ss表示秒,SSS表示毫秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}