懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.generator.controller;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import javax.servlet.http.HttpServletResponse;
8 import org.apache.commons.io.IOUtils;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.security.access.prepost.PreAuthorize;
11 import org.springframework.validation.annotation.Validated;
12 import org.springframework.web.bind.annotation.DeleteMapping;
13 import org.springframework.web.bind.annotation.GetMapping;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.PostMapping;
16 import org.springframework.web.bind.annotation.PutMapping;
17 import org.springframework.web.bind.annotation.RequestBody;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RestController;
20 import com.jcdm.common.annotation.Log;
21 import com.jcdm.common.core.controller.BaseController;
22 import com.jcdm.common.core.domain.AjaxResult;
23 import com.jcdm.common.core.page.TableDataInfo;
24 import com.jcdm.common.core.text.Convert;
25 import com.jcdm.common.enums.BusinessType;
26 import com.jcdm.generator.domain.GenTable;
27 import com.jcdm.generator.domain.GenTableColumn;
28 import com.jcdm.generator.service.IGenTableColumnService;
29 import com.jcdm.generator.service.IGenTableService;
30
31 /**
32  * 代码生成 操作处理
33  * 
34  * @author jc
35  */
36 @RestController
37 @RequestMapping("/tool/gen")
38 public class GenController extends BaseController
39 {
40     @Autowired
41     private IGenTableService genTableService;
42
43     @Autowired
44     private IGenTableColumnService genTableColumnService;
45
46     /**
47      * 查询代码生成列表
48      */
49     @PreAuthorize("@ss.hasPermi('tool:gen:list')")
50     @GetMapping("/list")
51     public TableDataInfo genList(GenTable genTable)
52     {
53         startPage();
54         List<GenTable> list = genTableService.selectGenTableList(genTable);
55         return getDataTable(list);
56     }
57
58     /**
59      * 修改代码生成业务
60      */
61     @PreAuthorize("@ss.hasPermi('tool:gen:query')")
62     @GetMapping(value = "/{tableId}")
63     public AjaxResult getInfo(@PathVariable Long tableId)
64     {
65         GenTable table = genTableService.selectGenTableById(tableId);
66         List<GenTable> tables = genTableService.selectGenTableAll();
67         List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
68         Map<String, Object> map = new HashMap<String, Object>();
69         map.put("info", table);
70         map.put("rows", list);
71         map.put("tables", tables);
72         return success(map);
73     }
74
75     /**
76      * 查询数据库列表
77      */
78     @PreAuthorize("@ss.hasPermi('tool:gen:list')")
79     @GetMapping("/db/list")
80     public TableDataInfo dataList(GenTable genTable)
81     {
82         startPage();
83         List<GenTable> list = genTableService.selectDbTableList(genTable);
84         return getDataTable(list);
85     }
86
87     /**
88      * 查询数据表字段列表
89      */
90     @PreAuthorize("@ss.hasPermi('tool:gen:list')")
91     @GetMapping(value = "/column/{tableId}")
92     public TableDataInfo columnList(Long tableId)
93     {
94         TableDataInfo dataInfo = new TableDataInfo();
95         List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
96         dataInfo.setRows(list);
97         dataInfo.setTotal(list.size());
98         return dataInfo;
99     }
100
101     /**
102      * 导入表结构(保存)
103      */
104     @PreAuthorize("@ss.hasPermi('tool:gen:import')")
105     @Log(title = "代码生成", businessType = BusinessType.IMPORT)
106     @PostMapping("/importTable")
107     public AjaxResult importTableSave(String tables)
108     {
109         String[] tableNames = Convert.toStrArray(tables);
110         // 查询表信息
111         List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
112         genTableService.importGenTable(tableList);
113         return success();
114     }
115
116     /**
117      * 修改保存代码生成业务
118      */
119     @PreAuthorize("@ss.hasPermi('tool:gen:edit')")
120     @Log(title = "代码生成", businessType = BusinessType.UPDATE)
121     @PutMapping
122     public AjaxResult editSave(@Validated @RequestBody GenTable genTable)
123     {
124         genTableService.validateEdit(genTable);
125         genTableService.updateGenTable(genTable);
126         return success();
127     }
128
129     /**
130      * 删除代码生成
131      */
132     @PreAuthorize("@ss.hasPermi('tool:gen:remove')")
133     @Log(title = "代码生成", businessType = BusinessType.DELETE)
134     @DeleteMapping("/{tableIds}")
135     public AjaxResult remove(@PathVariable Long[] tableIds)
136     {
137         genTableService.deleteGenTableByIds(tableIds);
138         return success();
139     }
140
141     /**
142      * 预览代码
143      */
144     @PreAuthorize("@ss.hasPermi('tool:gen:preview')")
145     @GetMapping("/preview/{tableId}")
146     public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException
147     {
148         Map<String, String> dataMap = genTableService.previewCode(tableId);
149         return success(dataMap);
150     }
151
152     /**
153      * 生成代码(下载方式)
154      */
155     @PreAuthorize("@ss.hasPermi('tool:gen:code')")
156     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
157     @GetMapping("/download/{tableName}")
158     public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
159     {
160         byte[] data = genTableService.downloadCode(tableName);
161         genCode(response, data);
162     }
163
164     /**
165      * 生成代码(自定义路径)
166      */
167     @PreAuthorize("@ss.hasPermi('tool:gen:code')")
168     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
169     @GetMapping("/genCode/{tableName}")
170     public AjaxResult genCode(@PathVariable("tableName") String tableName)
171     {
172         genTableService.generatorCode(tableName);
173         return success();
174     }
175
176     /**
177      * 同步数据库
178      */
179     @PreAuthorize("@ss.hasPermi('tool:gen:edit')")
180     @Log(title = "代码生成", businessType = BusinessType.UPDATE)
181     @GetMapping("/synchDb/{tableName}")
182     public AjaxResult synchDb(@PathVariable("tableName") String tableName)
183     {
184         genTableService.synchDb(tableName);
185         return success();
186     }
187
188     /**
189      * 批量生成代码
190      */
191     @PreAuthorize("@ss.hasPermi('tool:gen:code')")
192     @Log(title = "代码生成", businessType = BusinessType.GENCODE)
193     @GetMapping("/batchGenCode")
194     public void batchGenCode(HttpServletResponse response, String tables) throws IOException
195     {
196         String[] tableNames = Convert.toStrArray(tables);
197         byte[] data = genTableService.downloadCode(tableNames);
198         genCode(response, data);
199     }
200
201     /**
202      * 生成zip文件
203      */
204     private void genCode(HttpServletResponse response, byte[] data) throws IOException
205     {
206         response.reset();
207         response.addHeader("Access-Control-Allow-Origin", "*");
208         response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
209         response.setHeader("Content-Disposition", "attachment; filename=\"JCDM.zip\"");
210         response.addHeader("Content-Length", "" + data.length);
211         response.setContentType("application/octet-stream; charset=UTF-8");
212         IOUtils.write(data, response.getOutputStream());
213     }
214 }