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;
|
}
|
}
|