MagicTabelCreateService.java
4.96 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
package cn.com.glsx.entmicro.service;
import cn.com.glsx.entmicro.entity.magic.MagicTemplateConfigInfo;
import cn.com.glsx.entmicro.entity.magic.TableConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author zhouhaibao
* @version 1.0
* @description: TODO
* @date 2022/1/13 11:15
*/
@Service
public class MagicTabelCreateService {
@Autowired
private MagicTemplateConfigInfoService magicTemplateConfigInfoService;
public void tableCreate(Integer merchantId, Integer type) {
TableConfig byMerchantIdAndType = magicTemplateConfigInfoService.findByMerchantIdAndType(merchantId, type);
if (byMerchantIdAndType == null) {
return;
}
byMerchantIdAndType.setMagicTemplateConfigInfoList(addDefaultColumn(byMerchantIdAndType.getMagicTemplateConfigInfoList()));
magicTemplateConfigInfoService.createTable(byMerchantIdAndType);
}
public String tabelCreateSql(Integer merchantId, Integer type) {
TableConfig byMerchantIdAndType = magicTemplateConfigInfoService.findByMerchantIdAndType(merchantId, type);
if (byMerchantIdAndType == null) {
return null;
}
byMerchantIdAndType.setMagicTemplateConfigInfoList(addDataDefaultColumn(byMerchantIdAndType.getMagicTemplateConfigInfoList()));
StringBuffer sb = new StringBuffer();
sb.append("CREATE TABLE IF NOT EXISTS " + byMerchantIdAndType.getMagicTemplateConfig().getTableName());
sb.append("(");
sb.append(" `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',\n");
List<MagicTemplateConfigInfo> magicTemplateConfigInfoList = byMerchantIdAndType.getMagicTemplateConfigInfoList();
magicTemplateConfigInfoList.forEach(index -> {
if (index.getFieldPrimary() == 0) {
sb.append(index.getCloumnName() + " ").append(index.getFieldTypeName() + "");
if (index.getFieldLength() != null) {
sb.append("(" + index.getFieldLength() + ") ");
}
if(!StringUtils.isEmpty(index.getFieldComment())) {
sb.append(" COMMENT ").append("'").append(index.getFieldComment() + "', ");
}
}
});
sb.append("PRIMARY KEY (`id`))");
return sb.toString();
}
private List<MagicTemplateConfigInfo> addDefaultColumn(List<MagicTemplateConfigInfo> byMerchantIdAndType) {
Integer index = 0;
if (!byMerchantIdAndType.isEmpty()){
MagicTemplateConfigInfo info = byMerchantIdAndType.stream().max(Comparator.comparing(MagicTemplateConfigInfo::getCloumnIndex)).get();
index = info.getCloumnIndex()+1;
}
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"created_by", "int", "创建者", 11, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"created_time", "datetime", "创建时间", null, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"updated_by", "int", "更新者", 11, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"updated_time", "datetime", "更新时间", null, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"del_flag", "int", "状态", 2, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"import_type", "int", "导入类型:0-魔方导入,1-企微导入", 2, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"import_flag", "varchar", "导入批次时间戳", 13, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"customer_type", "int", "是否新增:0=新增,1=非新增", 2, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"wait_renewal_flag", "int", "是否待续保客户:0-未知,1-是,2-否", 2, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index++,"wait_regular_maintain_flag", "int", "是否待定保客户:0-未知,1-是,2-否", 2, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo(index,"id", "int", "主键", 11, 1));
byMerchantIdAndType = byMerchantIdAndType.stream().sorted(Comparator.comparing(MagicTemplateConfigInfo::getCloumnIndex)).collect(Collectors.toList());
return byMerchantIdAndType;
}
private List<MagicTemplateConfigInfo> addDataDefaultColumn(List<MagicTemplateConfigInfo> byMerchantIdAndType) {
byMerchantIdAndType.add(new MagicTemplateConfigInfo("merchant_id", "int", "商户号", 11, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo("one_id", "varchar", "oneId", 64, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo("customer_id", "int", "客户id", 11, 0));
byMerchantIdAndType.add(new MagicTemplateConfigInfo("data_type", "int", "数据类型", 11, 0));
return byMerchantIdAndType;
}
}