admin
2022-03-31 36754ba47da7a3277d5be183a523c912a1dc4cef
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
package com.ks.codegenerator.controller;
 
import com.ks.codegenerator.utils.AndroidBuilder;
import com.ks.codegenerator.utils.ServiceBuilder;
import com.ks.codegenerator.vo.ServiceVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
 
/**
 * @author hxh
 * @title: GeneratorController
 * @description: 自动化代码生成
 * @date 2021/11/13 14:43
 */
@RequestMapping("api/generator")
@Controller
public class GeneratorController {
 
    //生成service
    @RequestMapping("createService")
    public void service(ServiceVO vo, PrintWriter out, HttpServletRequest request) {
        if (StringUtil.isNullOrEmpty(vo.getName()) || StringUtil.isNullOrEmpty(vo.getPks()) || StringUtil.isNullOrEmpty(vo.getSubpks()) || StringUtil.isNullOrEmpty(vo.getChineseName())) {
            out.print(JsonUtil.loadFalseResult("参数不完整"));
            return;
        }
 
        if (StringUtil.isNullOrEmpty(vo.getSqlDataBaseName()) || StringUtil.isNullOrEmpty(vo.getSqlTablePrefix()) || StringUtil.isNullOrEmpty(vo.getMongoDataBaseName())) {
            out.print(JsonUtil.loadFalseResult("参数不完整"));
            return;
        }
 
 
        String rootPath = request.getServletContext().getRealPath("cache");
        try {
            String path = new ServiceBuilder().setCacheDir(rootPath).setName(vo.getName()).setPks(vo.getPks()).setSubpks(vo.getSubpks()).setProjectChineseName(vo.getChineseName()).setMongoDatabaseName(vo.getMongoDataBaseName()).setSqlDatabaseName(vo.getSqlDataBaseName()).setSqlTablePrefix(vo.getSqlTablePrefix()).build();
            out.print(JsonUtil.loadTrueResult(path));
        } catch (Exception e) {
            e.printStackTrace();
            out.print(JsonUtil.loadFalseResult(e.getMessage()));
        }
    }
 
    @RequestMapping("createAndroid")
    public void android(String name, String pks, PrintWriter out, HttpServletRequest request) {
        if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(pks)) {
            out.print(JsonUtil.loadFalseResult("项目名称,项目包名不能为空"));
            return;
        }
        String rootPath = request.getServletContext().getRealPath("cache");
        try {
            String path = new AndroidBuilder().setCacheDir(rootPath).setName(name).setPks(pks).build();
            out.print(JsonUtil.loadTrueResult(path));
        } catch (Exception e) {
            e.printStackTrace();
            out.print(JsonUtil.loadFalseResult(e.getMessage()));
        }
    }
 
    @RequestMapping("downloadZIP")
    public void downloadServiceZIP(String name, HttpServletResponse response) throws IOException {
        if (!new File(name).exists()) {
            response.sendError(406, "文件已被删除");
            return;
        }
        response.setHeader("content-disposition", "attachment;filename=" + new File(name).getName());
        //4.获取要下载的文件输入流
        InputStream in = new FileInputStream(new File(name).getAbsolutePath());
        int len = 0;
        //5.创建数据缓冲区
        byte[] buffer = new byte[1024];
        //6.通过response对象获取OutputStream流
        OutputStream out = response.getOutputStream();
        //7.将FileInputStream流写入到buffer缓冲区
        while ((len = in.read(buffer)) > 0) {
            //8.使用OutputStream将缓冲区的数据输出到客户端浏览器
            out.write(buffer, 0, len);
        }
        in.close();
    }
 
 
}