crud.serviceImpl.vm Maven / Gradle / Ivy
The newest version!
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
#if(${generateImport}||${generateExport})
import java.util.Collections;
import java.util.LinkedList;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.stream.Collectors;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.read.listener.ReadListener;
#end
#if(${generateInsert})
import ${basePackage}.${DTOPackage}.${entity}InsertDTO;
#end
#if(${generateUpdate})
import ${basePackage}.${DTOPackage}.${entity}UpdateDTO;
#end
#if(${generateSelect})
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${basePackage}.${DTOPackage}.${entity}SelectDTO;
import java.io.Serializable;
#end
#if(${generateSelect}||${generateInsert})
import ${basePackage}.${VOPackage}.${entity}VO;
#end
/**
* $!{table.comment} 服务实现类
*
* @author ${author}
* @since ${date}
*/
@Service
#if(${kotlin})
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName},${entity}>() implements ${table.serviceName} {
}
#else
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName},${entity}> implements ${table.serviceName} {
#if(${generateSelect})
protected void voPostProcess(List<${entity}VO> dataList) {
}
@Override
public ${entity}VO voById(Serializable id){
${entity}SelectDTO dto = new ${entity}SelectDTO();
dto.setPrimaryKey(id);
return this.oneByDTO(dto);
}
@Override
public ${entity}VO oneByDTO(${entity}SelectDTO s) {
List<${entity}VO> vs = listByDTO(s);
if (vs == null || vs.size()==0 ) {
return null;
}
if(vs.size() > 1) {
throw new RuntimeException("error query => required one but found"+vs.size());
}
return vs.get(0);
}
@Override
public List<${entity}VO> listByDTO(${entity}SelectDTO s){
List<${entity}VO> voList = this.baseMapper.listByMap(s, null);
voPostProcess(voList);
return voList;
}
@Override
public IPage<${entity}VO> pageByDTO(${entity}SelectDTO s, Long current, Long size){
if (current == null || current<1) {
current=1L;
}
if (size == null) {
size=10L;
}
Page<${entity}VO> page = new Page<>(current, size);
List<${entity}VO> voList = this.baseMapper.listByMap(s, page);
voPostProcess(voList);
page.setRecords(voList);
return page;
}
#end
#if(${generateInsert})
@Override
public ${entity}VO insertByDTO(${entity}InsertDTO s) {
${entity} entity = new ${entity}();
BeanUtils.copyProperties(s, entity);
super.save(entity);
${entity}VO vo = new ${entity}VO();
BeanUtils.copyProperties(entity, vo);
return vo;
}
#end
#if(${generateUpdate})
@Override
public boolean updateByDTO(${entity}UpdateDTO s) {
${entity} entity = new ${entity}();
BeanUtils.copyProperties(s, entity);
return super.updateById(entity);
}
#end
#if(${generateDelete})
@Override
public boolean removeById(Serializable id) {
return super.removeById(id);
}
#end
#if(${generateExport})
@Override
public void exportExcel(${entity}SelectDTO s, OutputStream os, Class clazz) {
List<${entity}VO> voList = listByDTO(s);
EasyExcel.write(os, clazz).sheet().doWrite(voList);
}
#end
#if(${generateImport})
@Override
public boolean importExcel(InputStream is, Class clazz) {
List cachedDataList = new LinkedList<>();
ReadListener listener = new ReadListener() {
@Override
public void invoke(U data, AnalysisContext context) {
cachedDataList.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
};
try {
EasyExcel.read(is, clazz, listener).sheet().doRead();
} catch (ExcelAnalysisException e) {
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) e.getCause();
String msg = String.format("第%s行,第%s列数据格式不正确:%s", excelDataConvertException.getRowIndex() + 1, excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
throw new RuntimeException(msg);
}
if (cachedDataList.isEmpty()) return false;
List<${entity}> entityList = cachedDataList.stream()
.map(e->{
${entity} entity = new ${entity}();
BeanUtils.copyProperties(e, entity);
return entity;
}).collect(Collectors.toList());
return super.saveBatch(entityList);
}
@Override
public void exportTemplate(OutputStream os, Class clazz) {
EasyExcel.write(os, clazz).sheet().doWrite(Collections.emptyList());
}
#end
}
#end