admin
2021-10-12 664cc2fd39177fd3daa6d3988396c704d130882c
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
package org.yeshi.utils.generater.vo.service;
 
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.generater.entity.BaseData;
import org.yeshi.utils.generater.entity.ClassInfo;
import org.yeshi.utils.generater.util.EntityUtil;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
 
public class ServiceInfoVO extends BaseData {
    private ClassInfo service;
    private ClassInfo dao;
    //方法列表
    private List<ServiceMetodInfoVO> metodInfoList;
    private ClassInfo query;
 
 
    public static class Builder {
        private Class entity;
        private String packageName;
        private ClassInfo dao;
        private ClassInfo service;
        private ClassInfo query;
 
 
        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;
        }
 
 
        private String getRemarksLine(String content) {
            return "\t * " + content + "\n";
        }
 
        //获取方法的注释
        private String getMethodRemarks(String type) {
            StringBuilder builder = new StringBuilder();
            builder.append("/**\n");
            switch (type) {
                case "list":
                    builder.append(getRemarksLine("获取列表"));
                    builder.append(getRemarksLine("@param " + StringUtil.firstCharToLower(query.getName())));
                    builder.append(getRemarksLine("@param page"));
                    builder.append(getRemarksLine("@param pageSize"));
                    builder.append(getRemarksLine("@return"));
                    break;
                default:
                    builder.append(getRemarksLine(""));
            }
 
            builder.append("\t */");
            return builder.toString();
        }
 
        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不能为空");
            }
 
        }
 
 
        public ServiceInfoVO build() throws Exception {
            validParams();
            Field identifyId = EntityUtil.getIdentifyId(entity);
            if (identifyId == null) {
                throw new Exception("尚未找到主键属性");
            }
            ServiceInfoVO serviceData = new ServiceInfoVO();
            serviceData.setDao(dao);
            serviceData.setService(service);
            serviceData.setEntity(new ClassInfo(entity.getSimpleName(), entity.getName()));
            serviceData.setPackageName(packageName);
            serviceData.setQuery(query);
            //设置接口
            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.setNote(getMethodRemarks("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.setNote(getMethodRemarks("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.setNote(getMethodRemarks("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, ","));
            metodInfo.setNote(getMethodRemarks("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.setNote(getMethodRemarks("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.setNote(getMethodRemarks("delete"));
            metodInfoVOList.add(metodInfo);
 
            /*******添加方法结束*******/
            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 getQuery() {
        return query;
    }
 
    public void setQuery(ClassInfo query) {
        this.query = query;
    }
}