admin
2021-10-12 664cc2fd39177fd3daa6d3988396c704d130882c
src/main/java/org/yeshi/utils/generater/vo/service/ServiceInfoVO.java
File was renamed from src/main/java/org/yeshi/utils/generater/vo/ServiceInfoVO.java
@@ -1,9 +1,11 @@
package org.yeshi.utils.generater.vo;
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;
@@ -12,6 +14,7 @@
    private ClassInfo dao;
    //方法列表
    private List<ServiceMetodInfoVO> metodInfoList;
    private ClassInfo query;
    public static class Builder {
@@ -48,12 +51,62 @@
        }
        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<>();
            /*******添加方法开始*******/
@@ -65,35 +118,42 @@
            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("String id");
            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("List<String> idList");
            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;
        }
@@ -124,5 +184,11 @@
        this.metodInfoList = metodInfoList;
    }
    public ClassInfo getQuery() {
        return query;
    }
    public void setQuery(ClassInfo query) {
        this.query = query;
    }
}