package org.yeshi.utils.generater.vo.service; 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 ServiceInfoVO extends BaseData { private List importList; private ClassInfo service; //方法列表 private List metodInfoList; private ClassInfo query;; public static class Builder { private Class entity; private String packageName; private ClassInfo service; private ClassInfo query; private ClassInfo exception; public Builder setEntity(Class entity) { this.entity = entity; return this; } public Builder setPackageName(String packageName) { this.packageName = packageName; return this; } public Builder setService(ClassInfo service) { this.service = service; return this; } public Builder setQuery(ClassInfo query) { this.query = query; return this; } public Builder setException(ClassInfo exception) { this.exception = exception; 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 (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.setService(service); serviceData.setEntity(new ClassInfo(entity.getSimpleName(), entity.getName())); serviceData.setPackageName(packageName); serviceData.setQuery(query); //设置接口 List metodInfoVOList = new ArrayList<>(); List importList = new ArrayList<>(); /*******添加方法开始*******/ List 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")); if (exception != null) { metodInfo.setExceptions(exception.getName()); importList.add(exception.getClazz()); } else { metodInfo.setExceptions("Exception"); importList.add(Exception.class.getName()); } 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); importList.add(Resource.class.getName()); importList.add(Date.class.getName()); importList.add(BeanUtil.class.getName()); importList.add(List.class.getName()); importList.add(entity.getName()); importList.add(service.getClazz()); importList.add(query.getClazz()); serviceData.setImportList(importList); return serviceData; } } public ClassInfo getService() { return service; } public void setService(ClassInfo service) { this.service = service; } public List getMetodInfoList() { return metodInfoList; } public void setMetodInfoList(List metodInfoList) { this.metodInfoList = metodInfoList; } public ClassInfo getQuery() { return query; } public void setQuery(ClassInfo query) { this.query = query; } public List getImportList() { return importList; } public void setImportList(List importList) { this.importList = importList; } }