admin
2022-03-29 fac5d01bfcddfc8edef0a5fd3d401b1fe383fe16
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
package org.yeshi.utils.generater.vo.service;
 
import org.springframework.stereotype.Service;
import org.yeshi.utils.FreemarkerUtils;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.bean.BeanUtil;
import org.yeshi.utils.generater.entity.BaseData;
import org.yeshi.utils.generater.entity.ClassInfo;
import org.yeshi.utils.generater.util.EntityUtil;
 
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
public class ServiceImplInfoVO extends BaseData {
 
    private List<String> importList;
    private ClassInfo service;
    private ClassInfo dao;
    //方法列表
    private List<ServiceMetodInfoVO> metodInfoList;
    private ClassInfo daoQuery;
    private ClassInfo query;
 
 
    public static class Builder {
        private Class entity;
        private String packageName;
        private ClassInfo dao;
        private ClassInfo service;
        private ClassInfo query;
        private ClassInfo daoQuery;
        private ClassInfo exception;
 
        public Builder setException(ClassInfo exception) {
            this.exception = exception;
            return this;
        }
 
        public Builder setEntity(Class entity) {
            this.entity = entity;
            return this;
        }
 
        public Builder setPackageName(String packageName) {
            this.packageName = packageName;
            return this;
        }
 
        public Builder setDao(ClassInfo dao) {
            this.dao = dao;
            return this;
        }
 
        public Builder setService(ClassInfo service) {
            this.service = service;
            return this;
        }
 
        public Builder setQuery(ClassInfo query) {
            this.query = query;
            return this;
        }
 
        public Builder setDaoQuery(ClassInfo daoQuery) {
            this.daoQuery = daoQuery;
            return this;
        }
 
        private void validParams() throws Exception {
            if (entity == null) {
                throw new Exception("entity不能为空");
            }
            if (packageName == null) {
                throw new Exception("packageName不能为空");
            }
            if (dao == null) {
                throw new Exception("dao不能为空");
            }
            if (service == null) {
                throw new Exception("service不能为空");
            }
            if (query == null) {
                throw new Exception("query不能为空");
            }
            if (daoQuery == null) {
                throw new Exception("daoQuery不能为空");
            }
        }
 
        private String getMethodContentTemplatePath(String type) {
            String basePath = "generater/service/method/impl/";
            if (dao.getName().endsWith("Mapper")) {
                basePath += "mybatis/";
            } else {
                basePath += "mongo/";
            }
 
            switch (type) {
                case "list":
                    return basePath + "list.ftl";
                case "count":
                    return basePath + "count.ftl";
                case "add":
                    return basePath + "add.ftl";
                case "save":
                    return basePath + "save.ftl";
                case "get":
                    return basePath + "selectByPrimaryKey.ftl";
                case "update":
                    return basePath + "updateSlective.ftl";
                case "delete":
                    return basePath + "delete.ftl";
            }
            return null;
        }
 
        public ServiceImplInfoVO build() throws Exception {
            validParams();
            Field identifyId = EntityUtil.getIdentifyId(entity);
            if (identifyId == null) {
                throw new Exception("尚未找到主键属性");
            }
 
            ServiceImplInfoVO serviceData = new ServiceImplInfoVO();
            serviceData.setDao(dao);
            serviceData.setService(service);
            serviceData.setEntity(new ClassInfo(entity.getSimpleName(), entity.getName()));
            serviceData.setPackageName(packageName);
            serviceData.setDaoQuery(daoQuery);
            serviceData.setQuery(query);
            List<String> importList = new ArrayList<>();
            //设置接口
            List<ServiceMetodInfoVO> metodInfoVOList = new ArrayList<>();
            /*******添加方法开始*******/
            List<String> params = null;
            ServiceMetodInfoVO metodInfo = null;
            //list方法
            params = new ArrayList<>();
            params.add(query.getName() + " " + StringUtil.firstCharToLower(query.getName()));
            params.add(" int page");
            params.add(" int pageSize");
            metodInfo = new ServiceMetodInfoVO("public", String.format("List<%s>", serviceData.getEntity().getName()), "list", StringUtil.concat(params, ","));
            metodInfo.setContent(FreemarkerUtils.generateInputStream(serviceData, getClass().getClassLoader().getResourceAsStream(getMethodContentTemplatePath("list"))));
            metodInfoVOList.add(metodInfo);
            //count方法
            params = new ArrayList<>();
            params.add(query.getName() + " " + StringUtil.firstCharToLower(query.getName()));
            metodInfo = new ServiceMetodInfoVO("public", "long", "count", StringUtil.concat(params, ","));
            metodInfo.setContent(FreemarkerUtils.generateInputStream(serviceData, getClass().getClassLoader().getResourceAsStream(getMethodContentTemplatePath("count"))));
            metodInfoVOList.add(metodInfo);
            //get方法
            params = new ArrayList<>();
            params.add(identifyId.getType().getSimpleName() + " id");
            metodInfo = new ServiceMetodInfoVO("public", serviceData.getEntity().getName(), "get", StringUtil.concat(params, ","));
            metodInfo.setContent(FreemarkerUtils.generateInputStream(serviceData, getClass().getClassLoader().getResourceAsStream(getMethodContentTemplatePath("get"))));
            metodInfoVOList.add(metodInfo);
            //add方法
            params = new ArrayList<>();
            params.add(serviceData.getEntity().getName() + " " + StringUtil.firstCharToLower(serviceData.getEntity().getName()));
            metodInfo = new ServiceMetodInfoVO("public", "void", "add", StringUtil.concat(params, ","));
            if (exception != null) {
                metodInfo.setExceptions(exception.getName());
                importList.add(exception.getClazz());
            } else {
                metodInfo.setExceptions("Exception");
                importList.add(Exception.class.getName());
            }
            metodInfo.setContent(FreemarkerUtils.generateInputStream(serviceData, getClass().getClassLoader().getResourceAsStream(getMethodContentTemplatePath("add"))));
            metodInfoVOList.add(metodInfo);
            //update方法
            params = new ArrayList<>();
            params.add(serviceData.getEntity().getName() + " " + StringUtil.firstCharToLower(serviceData.getEntity().getName()));
            metodInfo = new ServiceMetodInfoVO("public", "void", "update", StringUtil.concat(params, ","));
            metodInfo.setContent(FreemarkerUtils.generateInputStream(serviceData, getClass().getClassLoader().getResourceAsStream(getMethodContentTemplatePath("update"))));
            metodInfoVOList.add(metodInfo);
            //delete方法
            params = new ArrayList<>();
            params.add(String.format("List<%s> idList", identifyId.getType().getSimpleName()));
            metodInfo = new ServiceMetodInfoVO("public", String.format("void", serviceData.getEntity().getName()), "delete", StringUtil.concat(params, ","));
            metodInfo.setContent(FreemarkerUtils.generateInputStream(serviceData, getClass().getClassLoader().getResourceAsStream(getMethodContentTemplatePath("delete"))));
            metodInfoVOList.add(metodInfo);
 
            /*******添加方法结束*******/
 
 
            importList.add(Resource.class.getName());
            importList.add(Service.class.getName());
            importList.add(Date.class.getName());
            importList.add(BeanUtil.class.getName());
            importList.add(List.class.getName());
            importList.add(serviceData.getDao().getClazz());
            importList.add(entity.getName());
            importList.add(service.getClazz());
            importList.add(query.getClazz());
            importList.add(daoQuery.getClazz());
 
            serviceData.setImportList(importList);
            serviceData.setMetodInfoList(metodInfoVOList);
            return serviceData;
        }
 
    }
 
    public ClassInfo getService() {
        return service;
    }
 
    public void setService(ClassInfo service) {
        this.service = service;
    }
 
    public ClassInfo getDao() {
        return dao;
    }
 
    public void setDao(ClassInfo dao) {
        this.dao = dao;
    }
 
    public List<ServiceMetodInfoVO> getMetodInfoList() {
        return metodInfoList;
    }
 
    public void setMetodInfoList(List<ServiceMetodInfoVO> metodInfoList) {
        this.metodInfoList = metodInfoList;
    }
 
    public ClassInfo getDaoQuery() {
        return daoQuery;
    }
 
    public void setDaoQuery(ClassInfo daoQuery) {
        this.daoQuery = daoQuery;
    }
 
    public ClassInfo getQuery() {
        return query;
    }
 
    public void setQuery(ClassInfo query) {
        this.query = query;
    }
 
    public List<String> getImportList() {
        return importList;
    }
 
    public void setImportList(List<String> importList) {
        this.importList = importList;
    }
}