颓废太子
2021-11-24 4689659b197a13cce2a492bed6c752a39164e1c0
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
package com.java110.code;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.RestTemplate;
import com.java110.db.dao.IQueryServiceDAO;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.http.converter.StringHttpMessageConverter;
 
import java.nio.charset.Charset;
 
public class TableToJson {
 
    //show create table c_orders  用这个语句获取
    public static final String createTableSql = "CREATE TABLE `coupon_user_detail` (\n" +
            "  `uo_id` varchar(30) NOT NULL COMMENT '记录表',\n" +
            "  `coupon_id` varchar(30) NOT NULL COMMENT '优惠券ID',\n" +
            "  `coupon_name` varchar(64) NOT NULL COMMENT '优惠券名称',\n" +
            "  `user_id` varchar(30) NOT NULL COMMENT '用户ID',\n" +
            "  `user_name` varchar(64) NOT NULL COMMENT '用户名称',\n" +
            "  `obj_type` varchar(12) NOT NULL COMMENT '抵消对象类型 1001 物业费用 2002 临时停车费',\n" +
            "  `obj_id` varchar(30) NOT NULL COMMENT '抵消对象',\n" +
            "  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
            "  `status_cd` varchar(2) NOT NULL DEFAULT '0' COMMENT '数据状态,详细参考c_status表,S 保存,0, 在用 1失效',\n" +
            "  PRIMARY KEY (`uo_id`)\n" +
            ")";
 
    public static void main(String[] args) {
 
        String desc = "用户抵消记录";
        String id = "uoId";
        String name = "couponUserDetail";
        String shareName = "acct"; //生成到那个服务下
        String shareColumn = "uo_id";
        String shareParam = "uoId";
        //业务名称 desc 业务编码名称生成后类名 name 主键 id  需要放到那个服务 shareName
        String newSql = createTableSql.substring(createTableSql.indexOf("(") + 1, createTableSql.lastIndexOf(")"));
        String tableName = createTableSql.substring(createTableSql.indexOf("TABLE") + 5, createTableSql.indexOf("("));
        tableName = tableName.replaceAll("`", "").trim();
        newSql = newSql.replaceAll("\n", "");
        String[] rowSqls = newSql.split("',");
        JSONObject param = new JSONObject();
        param.put("autoMove", true);
        param.put("desc", desc);
        param.put("id", id);
        param.put("name", name);
        param.put("shareColumn", shareColumn);
        param.put("shareName", shareName);
        param.put("shareParam", shareParam);
        param.put("tableName", tableName);
        JSONObject paramColumn = new JSONObject();
        JSONArray requireds = new JSONArray();
        JSONObject required = null;
        String key = "";
        for (String rowSql : rowSqls) {
            required = new JSONObject();
            key = rowSql.trim();
            key = key.substring(0, key.indexOf(" "));
            key = key.replaceAll("`", "");
            if ("UNIQUE".equals(key)) {
                continue;
            }
            if ("KEY".equals(key)) {
                continue;
            }
            if ("b_id".equals(key)) {
                continue;
            }
            if ("create_time".equals(key)) {
                continue;
            }
            if (rowSql.toLowerCase().contains("not null")) {
                required.put("code", StringUtil.lineToHump(key));
                String comment = rowSql.contains("COMMENT") ? rowSql.substring(rowSql.indexOf("COMMENT '") + 9) : StringUtil.lineToHump(key);
                comment = comment.trim();
                if (comment.contains(",")) {
                    comment = comment.split(",")[0];
                }
                if (comment.contains(" ")) {
                    comment = comment.split(" ")[0];
                }
 
                required.put("msg", comment + "不能为空");
                requireds.add(required);
            }
            paramColumn.put(StringUtil.lineToHump(key), key);
        }
        param.put("param", paramColumn);
        param.put("required", requireds);
        System.out.println(param.toJSONString());
    }
}