mirror of
https://gitee.com/xiaonuobase/snowy.git
synced 2026-03-22 10:47:16 +08:00
277 lines
16 KiB
Plaintext
277 lines
16 KiB
Plaintext
/*
|
||
* Copyright [2022] [https://www.xiaonuo.vip]
|
||
*
|
||
* Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||
*
|
||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||
* 2.请不要删除和修改Snowy源码头部的版权声明。
|
||
* 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
|
||
* 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
|
||
* 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
|
||
* 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
|
||
*/
|
||
package ${packageName}.${moduleName}.modular.${busName}.service.impl;
|
||
|
||
import cn.hutool.core.bean.BeanUtil;
|
||
import cn.hutool.core.collection.CollectionUtil;
|
||
import cn.hutool.core.collection.CollStreamUtil;
|
||
import cn.hutool.core.date.DatePattern;
|
||
import cn.hutool.core.date.DateTime;
|
||
import cn.hutool.core.date.DateUtil;
|
||
import cn.hutool.core.io.FileUtil;
|
||
import cn.hutool.core.util.ObjectUtil;
|
||
import cn.hutool.core.util.StrUtil;
|
||
import cn.hutool.json.JSONArray;
|
||
import cn.hutool.json.JSONObject;
|
||
import cn.hutool.json.JSONUtil;
|
||
import com.alibaba.excel.EasyExcel;
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
import jakarta.servlet.http.HttpServletResponse;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
import ${packageName}.common.enums.CommonSortOrderEnum;
|
||
import ${packageName}.common.exception.CommonException;
|
||
import ${packageName}.common.page.CommonPageRequest;
|
||
import java.math.BigDecimal;
|
||
import java.util.Date;
|
||
import ${packageName}.${moduleName}.modular.${busName}.entity.${className};
|
||
import ${packageName}.${moduleName}.modular.${busName}.mapper.${className}Mapper;
|
||
import ${packageName}.${moduleName}.modular.${busName}.param.${className}AddParam;
|
||
import ${packageName}.${moduleName}.modular.${busName}.param.${className}EditParam;
|
||
import ${packageName}.${moduleName}.modular.${busName}.param.${className}IdParam;
|
||
import ${packageName}.${moduleName}.modular.${busName}.param.${className}PageParam;
|
||
import ${packageName}.${moduleName}.modular.${busName}.service.${className}Service;
|
||
|
||
import vip.xiaonuo.common.util.CommonDownloadUtil;
|
||
import vip.xiaonuo.common.util.CommonResponseUtil;
|
||
|
||
import java.io.File;
|
||
import java.io.Serializable;
|
||
import java.io.IOException;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* ${functionName}Service接口实现类
|
||
*
|
||
* @author ${authorName}
|
||
* @date ${genTime}
|
||
**/
|
||
@Service
|
||
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {
|
||
|
||
@Override
|
||
public Page<${className}> page(${className}PageParam ${classNameFirstLower}PageParam) {
|
||
QueryWrapper<${className}> queryWrapper = new QueryWrapper<${className}>().checkSqlInjection();
|
||
<% for(var i = 0; i < configList.~size; i++) { %>
|
||
<% if(configList[i].needPage) { %>
|
||
<% if(configList[i].effectType == 'datepicker') { %>
|
||
if(ObjectUtil.isNotEmpty(${classNameFirstLower}PageParam.getStart${configList[i].fieldNameCamelCaseFirstUpper}()) && ObjectUtil.isNotEmpty(${classNameFirstLower}PageParam.getEnd${configList[i].fieldNameCamelCaseFirstUpper}())) {
|
||
queryWrapper.lambda().between(${className}::get${configList[i].fieldNameCamelCaseFirstUpper}, ${classNameFirstLower}PageParam.getStart${configList[i].fieldNameCamelCaseFirstUpper}(), ${classNameFirstLower}PageParam.getEnd${configList[i].fieldNameCamelCaseFirstUpper}());
|
||
}
|
||
<% } else { %>
|
||
if(ObjectUtil.isNotEmpty(${classNameFirstLower}PageParam.get${configList[i].fieldNameCamelCaseFirstUpper}())) {
|
||
queryWrapper.lambda().${configList[i].needPageType}(${className}::get${configList[i].fieldNameCamelCaseFirstUpper}, ${classNameFirstLower}PageParam.get${configList[i].fieldNameCamelCaseFirstUpper}());
|
||
}
|
||
<% } %>
|
||
<% } %>
|
||
<% } %>
|
||
if(ObjectUtil.isAllNotEmpty(${classNameFirstLower}PageParam.getSortField(), ${classNameFirstLower}PageParam.getSortOrder())) {
|
||
CommonSortOrderEnum.validate(${classNameFirstLower}PageParam.getSortOrder());
|
||
queryWrapper.orderBy(true, ${classNameFirstLower}PageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
|
||
StrUtil.toUnderlineCase(${classNameFirstLower}PageParam.getSortField()));
|
||
} else {
|
||
<% if(hasSortCodeField) { %>
|
||
queryWrapper.lambda().orderByAsc(${className}::getSortCode);
|
||
<% } else { %>
|
||
queryWrapper.lambda().orderByAsc(${className}::get${dbTableKeyFirstUpper});
|
||
<% } %>
|
||
}
|
||
return this.page(CommonPageRequest.defaultPage(), queryWrapper);
|
||
}
|
||
|
||
@Transactional(rollbackFor = Exception.class)
|
||
@Override
|
||
public void add(${className}AddParam ${classNameFirstLower}AddParam) {
|
||
${className} ${classNameFirstLower} = BeanUtil.toBean(${classNameFirstLower}AddParam, ${className}.class);
|
||
<% for(var i = 0; i < uniqueFieldList.~size; i++) { %>
|
||
<% if(!uniqueFieldList[i].needTableId) { %>
|
||
if(this.count(new LambdaQueryWrapper<${className}>().eq(${className}::get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}, ${classNameFirstLower}.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}())) > 0) {
|
||
throw new CommonException("存在重复的${uniqueFieldList[i].fieldRemark},值为:{}", ${classNameFirstLower}.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}());
|
||
}
|
||
<% } %>
|
||
<% } %>
|
||
this.save(${classNameFirstLower});
|
||
}
|
||
|
||
@Transactional(rollbackFor = Exception.class)
|
||
@Override
|
||
public void edit(${className}EditParam ${classNameFirstLower}EditParam) {
|
||
${className} ${classNameFirstLower} = this.queryEntity(${classNameFirstLower}EditParam.get${dbTableKeyFirstUpper}());
|
||
BeanUtil.copyProperties(${classNameFirstLower}EditParam, ${classNameFirstLower});
|
||
<% for(var i = 0; i < uniqueFieldList.~size; i++) { %>
|
||
<% if(!uniqueFieldList[i].needTableId) { %>
|
||
if(this.count(new LambdaQueryWrapper<${className}>().ne(${className}::get${dbTableKeyFirstUpper}, ${classNameFirstLower}.get${dbTableKeyFirstUpper}()).eq(${className}::get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}, ${classNameFirstLower}.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}())) > 0) {
|
||
throw new CommonException("存在重复的${uniqueFieldList[i].fieldRemark},值为:{}", ${classNameFirstLower}.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}());
|
||
}
|
||
<% } %>
|
||
<% } %>
|
||
this.updateById(${classNameFirstLower});
|
||
}
|
||
|
||
@Transactional(rollbackFor = Exception.class)
|
||
@Override
|
||
public void delete(List<${className}IdParam> ${classNameFirstLower}IdParamList) {
|
||
// 执行删除
|
||
this.removeByIds(CollStreamUtil.toList(${classNameFirstLower}IdParamList, ${className}IdParam::get${dbTableKeyFirstUpper}));
|
||
}
|
||
|
||
@Override
|
||
public ${className} detail(${className}IdParam ${classNameFirstLower}IdParam) {
|
||
return this.queryEntity(${classNameFirstLower}IdParam.get${dbTableKeyFirstUpper}());
|
||
}
|
||
|
||
@Override
|
||
public ${className} queryEntity(Serializable ${dbTableKeyCamelCase}) {
|
||
${className} ${classNameFirstLower} = this.getById(${dbTableKeyCamelCase});
|
||
if(ObjectUtil.isEmpty(${classNameFirstLower})) {
|
||
throw new CommonException("${functionName}不存在,${dbTableKeyCamelCase}值为:{}", ${dbTableKeyCamelCase});
|
||
}
|
||
return ${classNameFirstLower};
|
||
}
|
||
|
||
@Override
|
||
public void downloadImportTemplate(HttpServletResponse response) throws IOException {
|
||
File tempFile = null;
|
||
try {
|
||
List<${className}EditParam> dataList = CollectionUtil.newArrayList();
|
||
String fileName = "${functionName}导入模板_" + DateUtil.format(DateTime.now(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx";
|
||
tempFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
|
||
EasyExcel.write(tempFile.getPath(), ${className}EditParam.class).sheet("${functionName}").doWrite(dataList);
|
||
CommonDownloadUtil.download(tempFile, response);
|
||
} catch (Exception e) {
|
||
log.error(">>> ${functionName}导入模板下载失败:", e);
|
||
CommonResponseUtil.renderError(response, "${functionName}导入模板下载失败");
|
||
} finally {
|
||
FileUtil.del(tempFile);
|
||
}
|
||
}
|
||
|
||
@Transactional(rollbackFor = Exception.class)
|
||
@Override
|
||
public JSONObject importData(MultipartFile file) {
|
||
try {
|
||
int successCount = 0;
|
||
int errorCount = 0;
|
||
JSONArray errorDetail = JSONUtil.createArray();
|
||
// 创建临时文件
|
||
File tempFile = FileUtil.writeBytes(file.getBytes(), FileUtil.file(FileUtil.getTmpDir() +
|
||
FileUtil.FILE_SEPARATOR + "${classNameFirstLower}ImportTemplate.xlsx"));
|
||
// 读取excel
|
||
List<${className}EditParam> ${classNameFirstLower}EditParamList = EasyExcel.read(tempFile).head(${className}EditParam.class).sheet()
|
||
.headRowNumber(1).doReadSync();
|
||
List<${className}> allDataList = this.list();
|
||
for (int i = 0; i < ${classNameFirstLower}EditParamList.size(); i++) {
|
||
JSONObject jsonObject = this.doImport(allDataList, ${classNameFirstLower}EditParamList.get(i), i);
|
||
if(jsonObject.getBool("success")) {
|
||
successCount += 1;
|
||
} else {
|
||
errorCount += 1;
|
||
errorDetail.add(jsonObject);
|
||
}
|
||
}
|
||
return JSONUtil.createObj()
|
||
.set("totalCount", ${classNameFirstLower}EditParamList.size())
|
||
.set("successCount", successCount)
|
||
.set("errorCount", errorCount)
|
||
.set("errorDetail", errorDetail);
|
||
} catch (Exception e) {
|
||
log.error(">>> ${functionName}导入失败:", e);
|
||
throw new CommonException("${functionName}导入失败");
|
||
}
|
||
}
|
||
|
||
public JSONObject doImport(List<${className}> allDataList, ${className}EditParam ${classNameFirstLower}EditParam, int i) {
|
||
<% for(var i = 0; i < requiredFieldList.~size; i++) { %>
|
||
${requiredFieldList[i].fieldJavaType} ${requiredFieldList[i].fieldNameCamelCase} = ${classNameFirstLower}EditParam.get${requiredFieldList[i].fieldNameCamelCaseFirstUpper}();
|
||
<% } %>
|
||
if(ObjectUtil.hasEmpty(${requiredFieldStr})) {
|
||
return JSONUtil.createObj().set("index", i + 1).set("success", false).set("msg", "必填字段存在空值");
|
||
} else {
|
||
try {
|
||
int index = CollStreamUtil.toList(allDataList, ${className}::get${dbTableKeyFirstUpper}).indexOf(${classNameFirstLower}EditParam.get${dbTableKeyFirstUpper}());
|
||
${className} ${classNameFirstLower};
|
||
boolean isAdd = false;
|
||
if(index == -1) {
|
||
isAdd = true;
|
||
${classNameFirstLower} = new ${className}();
|
||
} else {
|
||
${classNameFirstLower} = allDataList.get(index);
|
||
}
|
||
<% if(uniqueFieldList.~size > 1) { %>
|
||
if(isAdd) {
|
||
<% for(var i = 0; i < uniqueFieldList.~size; i++) { %>
|
||
<% if(!uniqueFieldList[i].needTableId) { %>
|
||
boolean repeat${uniqueFieldList[i].fieldNameCamelCaseFirstUpper} = allDataList.stream().anyMatch(tempData -> ObjectUtil
|
||
.isNotEmpty(tempData.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}()) && tempData.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}().equals(${classNameFirstLower}EditParam.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}()));
|
||
if(repeat${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}) {
|
||
return JSONUtil.createObj().set("index", i + 1).set("success", false).set("msg", "新增数据时字段【${uniqueFieldList[i].fieldRemark}(${uniqueFieldList[i].fieldNameCamelCase})】与数据库中数据重复,值为:" + ${classNameFirstLower}EditParam.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}());
|
||
}
|
||
<% } %>
|
||
<% } %>
|
||
} else {
|
||
<% for(var i = 0; i < uniqueFieldList.~size; i++) { %>
|
||
<% if(!uniqueFieldList[i].needTableId) { %>
|
||
boolean repeat${uniqueFieldList[i].fieldNameCamelCaseFirstUpper} = allDataList.stream().anyMatch(tempData -> ObjectUtil
|
||
.isNotEmpty(tempData.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}()) && tempData.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}()
|
||
.equals(${classNameFirstLower}EditParam.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}()) && !tempData.get${dbTableKeyFirstUpper}().equals(${classNameFirstLower}.get${dbTableKeyFirstUpper}()));
|
||
if(repeat${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}) {
|
||
return JSONUtil.createObj().set("index", i + 1).set("success", false).set("msg", "更新数据时字段【${uniqueFieldList[i].fieldRemark}(${uniqueFieldList[i].fieldNameCamelCase})】与数据库中数据重复,值为:" + ${classNameFirstLower}EditParam.get${uniqueFieldList[i].fieldNameCamelCaseFirstUpper}());
|
||
}
|
||
<% } %>
|
||
<% } %>
|
||
}
|
||
<% } %>
|
||
BeanUtil.copyProperties(${classNameFirstLower}EditParam, ${classNameFirstLower});
|
||
if(isAdd) {
|
||
allDataList.add(${classNameFirstLower});
|
||
} else {
|
||
allDataList.remove(index);
|
||
allDataList.add(index, ${classNameFirstLower});
|
||
}
|
||
this.saveOrUpdate(${classNameFirstLower});
|
||
return JSONUtil.createObj().set("success", true);
|
||
} catch (Exception e) {
|
||
log.error(">>> 数据导入异常:", e);
|
||
return JSONUtil.createObj().set("success", false).set("index", i + 1).set("msg", "数据导入异常");
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void exportData(List<${className}IdParam> ${classNameFirstLower}IdParamList, HttpServletResponse response) throws IOException {
|
||
File tempFile = null;
|
||
try {
|
||
List<${className}EditParam> dataList;
|
||
if(ObjectUtil.isNotEmpty(${classNameFirstLower}IdParamList)) {
|
||
List<Serializable> ${dbTableKeyCamelCase}List = CollStreamUtil.toList(${classNameFirstLower}IdParamList, ${className}IdParam::get${dbTableKeyFirstUpper});
|
||
dataList = BeanUtil.copyToList(this.listByIds(${dbTableKeyCamelCase}List), ${className}EditParam.class);
|
||
} else {
|
||
dataList = BeanUtil.copyToList(this.list(), ${className}EditParam.class);
|
||
}
|
||
String fileName = "${functionName}_" + DateUtil.format(DateTime.now(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx";
|
||
tempFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
|
||
EasyExcel.write(tempFile.getPath(), ${className}EditParam.class).sheet("${functionName}").doWrite(dataList);
|
||
CommonDownloadUtil.download(tempFile, response);
|
||
} catch (Exception e) {
|
||
log.error(">>> ${functionName}导出失败:", e);
|
||
CommonResponseUtil.renderError(response, "${functionName}导出失败");
|
||
} finally {
|
||
FileUtil.del(tempFile);
|
||
}
|
||
}
|
||
}
|