diff --git a/maintain-business/src/main/java/com/maintain/business/controller/ErpMaterialController.java b/maintain-business/src/main/java/com/maintain/business/controller/ErpMaterialController.java index de2388d9976d5f499f77edbc7237bffea54da33f..590126baa923a553c65b6f856df600db121f21c1 100644 --- a/maintain-business/src/main/java/com/maintain/business/controller/ErpMaterialController.java +++ b/maintain-business/src/main/java/com/maintain/business/controller/ErpMaterialController.java @@ -1,12 +1,19 @@ package com.maintain.business.controller; +import java.util.ArrayList; import java.util.List; import java.util.Arrays; +import com.maintain.business.domain.bo.ErpCarBo; +import com.maintain.business.domain.vo.ErpCarVo; +import com.maintain.business.listener.ErpCarImportListener; +import com.maintain.business.listener.ErpMaterialImportListener; +import com.maintain.common.excel.ExcelResult; import lombok.RequiredArgsConstructor; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.*; import cn.dev33.satoken.annotation.SaCheckPermission; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.validation.annotation.Validated; import com.maintain.common.annotation.RepeatSubmit; @@ -22,6 +29,7 @@ import com.maintain.business.domain.vo.ErpMaterialVo; import com.maintain.business.domain.bo.ErpMaterialBo; import com.maintain.business.service.IErpMaterialService; import com.maintain.common.core.page.TableDataInfo; +import org.springframework.web.multipart.MultipartFile; /** * 物资 @@ -46,6 +54,27 @@ public class ErpMaterialController extends BaseController { return iErpMaterialService.queryPageList(bo, pageQuery); } + /** + * 物料导入模板下载 + */ + @PostMapping("/importTemplate") + public void importTemplate(ErpMaterialBo bo, HttpServletResponse response) { + ExcelUtil.exportExcel(new ArrayList<>(), "物料导入", ErpMaterialVo.class, response); + } + + /** + * 导入数据 + * + * @param file 导入文件 + * @param updateSupport 是否更新已存在数据 + */ + @Log(title = "物料导入", businessType = BusinessType.IMPORT) + @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public R importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception { + ExcelResult result = ExcelUtil.importExcel(file.getInputStream(), ErpMaterialVo.class, new ErpMaterialImportListener(updateSupport)); + return R.ok(result.getAnalysis()); + } + /** * 导出物资列表 */ diff --git a/maintain-business/src/main/java/com/maintain/business/domain/vo/ErpMaterialVo.java b/maintain-business/src/main/java/com/maintain/business/domain/vo/ErpMaterialVo.java index e0338d3827fb39c0055f9c87b5749fd1353326c6..0e67124fcad8c551b09ea89c5f1405f070a9f199 100644 --- a/maintain-business/src/main/java/com/maintain/business/domain/vo/ErpMaterialVo.java +++ b/maintain-business/src/main/java/com/maintain/business/domain/vo/ErpMaterialVo.java @@ -28,17 +28,6 @@ public class ErpMaterialVo implements Serializable { */ private Long id; - /** - * 物资分类ID - */ - private Long materialTypeId; - - /** - * 物资分类名称 - */ - @ExcelProperty(value = "物资分类名称") - private String materialTypeName; - /** * 部门ID */ @@ -48,30 +37,35 @@ public class ErpMaterialVo implements Serializable { * 部门名称 */ private String deptName; + /** - * 仓库ID + * 仓库货位ID */ - private Long warehouseId; + private Long warehouseLocationId; + /** - * 仓库名称 + * 物资编码 */ - private String warehouseName; + @ExcelProperty(value = "物资编码") + private String materialCode; + /** - * 仓库货位ID + * 物资名称 */ - private Long warehouseLocationId; + @ExcelProperty(value = "物资名称") + private String materialName; /** - * 仓库货位编码 + * 物资规格 */ - private String warehouseLocationCode; + @ExcelProperty(value = "物资规格") + private String materialSpecifications; /** - * 仓库货位类型 + * 物资单位 */ - @ExcelProperty(value = "仓库货位类型") - @ColumnWidth(30) - private String warehouseLocationType; + @ExcelProperty(value = "物资单位") + private String materialUnit; /** * 供应商ID @@ -85,28 +79,37 @@ public class ErpMaterialVo implements Serializable { private String vendorName; /** - * 物资编码 + * 物资分类ID */ - @ExcelProperty(value = "物资编码") - private String materialCode; + private Long materialTypeId; /** - * 物资名称 + * 物资分类名称 */ - @ExcelProperty(value = "物资名称") - private String materialName; + @ExcelProperty(value = "物资分类名称") + private String materialTypeName; /** - * 物资规格 + * 仓库ID */ - @ExcelProperty(value = "物资规格") - private String materialSpecifications; + private Long warehouseId; /** - * 物资单位 + * 仓库名称 */ - @ExcelProperty(value = "物资单位") - private String materialUnit; + private String warehouseName; + + /** + * 仓库货位编码 + */ + private String warehouseLocationCode; + + /** + * 仓库货位类型 + */ + @ExcelProperty(value = "仓库货位类型") + @ColumnWidth(30) + private String warehouseLocationType; /** * 物资品牌 @@ -130,7 +133,7 @@ public class ErpMaterialVo implements Serializable { /** * 质保期(月) */ - @ExcelProperty(value = "质保期") + @ExcelProperty(value = "质保期(月)") private Integer materialWarrantyPeriod; /** diff --git a/maintain-business/src/main/java/com/maintain/business/listener/ErpMaterialImportListener.java b/maintain-business/src/main/java/com/maintain/business/listener/ErpMaterialImportListener.java new file mode 100644 index 0000000000000000000000000000000000000000..47e754d72de023e12362cc614fead61d10642c6f --- /dev/null +++ b/maintain-business/src/main/java/com/maintain/business/listener/ErpMaterialImportListener.java @@ -0,0 +1,245 @@ +package com.maintain.business.listener; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.maintain.business.domain.*; +import com.maintain.business.domain.vo.ErpCarVo; +import com.maintain.business.domain.vo.ErpMaterialVo; +import com.maintain.business.mapper.*; +import com.maintain.common.enums.IsDeleteStatus; +import com.maintain.common.excel.ExcelListener; +import com.maintain.common.excel.ExcelResult; +import com.maintain.common.exception.ServiceException; +import com.maintain.common.helper.LoginHelper; +import com.maintain.common.utils.StringUtils; +import com.maintain.common.utils.ValidatorUtils; +import com.maintain.common.utils.spring.SpringUtils; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; + +/** + * 车辆自定义导入 + * + * @author liushuai + */ +@Slf4j +public class ErpMaterialImportListener extends AnalysisEventListener implements ExcelListener { + + private final ErpMaterialMapper materialMapper; + private final ErpWarehouseMapper warehouseMapper; + private final ErpMaterialVendorMapper materialVendorMapper; + private final ErpMaterialTypeMapper materialTypeMapper; + private final ErpWarehouseLocationMapper warehouseLocationMapper; + + private final Boolean isUpdateSupport; + + private final String operName; + + private int successNum = 0; + private int failureNum = 0; + private final StringBuilder successMsg = new StringBuilder(); + private final StringBuilder failureMsg = new StringBuilder(); + + public ErpMaterialImportListener(Boolean isUpdateSupport) { + this.materialMapper = SpringUtils.getBean(ErpMaterialMapper.class); + this.warehouseMapper = SpringUtils.getBean(ErpWarehouseMapper.class); + this.materialVendorMapper = SpringUtils.getBean(ErpMaterialVendorMapper.class); + this.materialTypeMapper = SpringUtils.getBean(ErpMaterialTypeMapper.class); + this.warehouseLocationMapper = SpringUtils.getBean(ErpWarehouseLocationMapper.class); + this.isUpdateSupport = isUpdateSupport; + this.operName = LoginHelper.getUsername(); + } + + @Override + public void invoke(ErpMaterialVo materialImportVo, AnalysisContext context) { + LambdaQueryWrapper materialLambdaQueryWrapper = new LambdaQueryWrapper<>(); + materialLambdaQueryWrapper.eq(ErpMaterial::getMaterialCode, materialImportVo.getMaterialCode()) + .eq(ErpMaterial::getMaterialName, materialImportVo.getMaterialName()).eq(ErpMaterial::getIsDelete, IsDeleteStatus.NO.getCode()); + ErpMaterial material = materialMapper.selectOne(materialLambdaQueryWrapper); + try { + // 验证是否存在 + if (ObjectUtil.isNull(material)) { + material = BeanUtil.toBean(materialImportVo, ErpMaterial.class); + ValidatorUtils.validate(material); + /** + * 物资类型 + */ + if (StringUtils.isNotEmpty(material.getMaterialTypeName())) { + List materialTypes = materialTypeMapper.selectList(new LambdaQueryWrapper().eq(ErpMaterialType::getTypeName, material.getMaterialTypeName())); + if (materialTypes.isEmpty()) { + ErpMaterialType materialType = new ErpMaterialType(); + materialType.setTypeName(material.getMaterialTypeName()); + materialType.setParentId(1L); + materialType.setAncestors("0,1"); + materialTypeMapper.insert(materialType); + material.setMaterialTypeId(materialType.getTypeId()); + }else { + material.setMaterialTypeId(materialTypes.get(0).getTypeId()); + } + } + /** + * 物资仓库 + */ + if (StringUtils.isNotEmpty(material.getWarehouseName())) { + ErpWarehouse erpWarehouse = warehouseMapper.selectOne(new LambdaQueryWrapper().eq(ErpWarehouse::getName, material.getWarehouseName())); + if (ObjectUtil.isNull(erpWarehouse)) { + ErpWarehouse warehouse = new ErpWarehouse(); + warehouse.setCode(material.getWarehouseName()); + warehouse.setName(material.getWarehouseName()); + warehouseMapper.insert(warehouse); + material.setWarehouseId(warehouse.getId()); + }else { + material.setWarehouseId(erpWarehouse.getId()); + } + } + /** + * 供应商 + */ + if (StringUtils.isNotEmpty(material.getVendorName())) { + ErpMaterialVendor erpMaterialVendor = materialVendorMapper.selectOne(new LambdaQueryWrapper().eq(ErpMaterialVendor::getVendorName, material.getVendorName())); + if (ObjectUtil.isNull(erpMaterialVendor)) { + ErpMaterialVendor vendor = new ErpMaterialVendor(); + vendor.setVendorName(material.getVendorName()); + materialVendorMapper.insert(vendor); + material.setVendorId(vendor.getId()); + } else { + material.setVendorId(erpMaterialVendor.getId()); + } + } + /** + * 仓库货位 + */ + if (StringUtils.isNotEmpty(material.getWarehouseLocationType())) { + ErpWarehouseLocation warehouseLocation = warehouseLocationMapper.selectOne(new LambdaQueryWrapper().eq(ErpWarehouseLocation::getLocationType, material.getWarehouseLocationType())); + if (ObjectUtil.isNull(warehouseLocation)) { + ErpWarehouseLocation location = new ErpWarehouseLocation(); + location.setLocationCode(material.getWarehouseLocationType()); + location.setLocationType(material.getWarehouseLocationType()); + warehouseLocationMapper.insert(location); + material.setWarehouseLocationCode(location.getLocationCode()); + material.setWarehouseLocationId(location.getId()); + } else { + material.setWarehouseLocationCode(warehouseLocation.getLocationCode()); + material.setWarehouseLocationId(warehouseLocation.getId()); + } + } + materialMapper.insert(material); + successNum++; + successMsg.append("
").append(successNum).append("、物料 ").append(material.getMaterialName()).append(" 导入成功"); + } else if (isUpdateSupport) { + Long materialId = material.getId(); + material = BeanUtil.toBean(materialImportVo, ErpMaterial.class); + material.setId(materialId); + ValidatorUtils.validate(material); + /** + * 物资类型 + */ + if (StringUtils.isNotEmpty(material.getMaterialTypeName())) { + List materialTypes = materialTypeMapper.selectList(new LambdaQueryWrapper().eq(ErpMaterialType::getTypeName, material.getMaterialTypeName())); + if (materialTypes.isEmpty()) { + ErpMaterialType materialType = new ErpMaterialType(); + materialType.setTypeName(material.getMaterialTypeName()); + materialType.setParentId(1L); + materialType.setAncestors("0,1"); + materialTypeMapper.insert(materialType); + material.setMaterialTypeId(materialType.getTypeId()); + }else { + material.setMaterialTypeId(materialTypes.get(0).getTypeId()); + } + } + /** + * 物资仓库 + */ + if (StringUtils.isNotEmpty(material.getWarehouseName())) { + ErpWarehouse erpWarehouse = warehouseMapper.selectOne(new LambdaQueryWrapper().eq(ErpWarehouse::getName, material.getWarehouseName())); + if (ObjectUtil.isNull(erpWarehouse)) { + ErpWarehouse warehouse = new ErpWarehouse(); + warehouse.setCode(material.getWarehouseName()); + warehouse.setName(material.getWarehouseName()); + warehouseMapper.insert(warehouse); + material.setWarehouseId(warehouse.getId()); + }else { + material.setWarehouseId(erpWarehouse.getId()); + } + } + /** + * 供应商 + */ + if (StringUtils.isNotEmpty(material.getVendorName())) { + ErpMaterialVendor erpMaterialVendor = materialVendorMapper.selectOne(new LambdaQueryWrapper().eq(ErpMaterialVendor::getVendorName, material.getVendorName())); + if (ObjectUtil.isNull(erpMaterialVendor)) { + ErpMaterialVendor vendor = new ErpMaterialVendor(); + vendor.setVendorName(material.getVendorName()); + materialVendorMapper.insert(vendor); + material.setVendorId(vendor.getId()); + } else { + material.setVendorId(erpMaterialVendor.getId()); + } + } + /** + * 仓库货位 + */ + if (StringUtils.isNotEmpty(material.getWarehouseLocationType())) { + ErpWarehouseLocation warehouseLocation = warehouseLocationMapper.selectOne(new LambdaQueryWrapper().eq(ErpWarehouseLocation::getLocationType, material.getWarehouseLocationType())); + if (ObjectUtil.isNull(warehouseLocation)) { + ErpWarehouseLocation location = new ErpWarehouseLocation(); + location.setLocationCode(material.getWarehouseLocationType()); + location.setLocationType(material.getWarehouseLocationType()); + warehouseLocationMapper.insert(location); + material.setWarehouseLocationId(location.getId()); + } else { + material.setWarehouseLocationId(warehouseLocation.getId()); + } + } + materialMapper.updateById(material); + successNum++; + successMsg.append("
").append(successNum).append("、物料 ").append(material.getMaterialName()).append(" 更新成功"); + } else { + failureNum++; + failureMsg.append("
").append(failureNum).append("、物料 ").append(material.getMaterialName()).append(" 已存在"); + } + } catch (Exception e) { + failureNum++; + String msg = "
" + failureNum + "、物料 " + material.getMaterialName() + " 导入失败:"; + failureMsg.append(msg).append(e.getMessage()); + log.error(msg, e); + } + + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + + } + + @Override + public ExcelResult getExcelResult() { + return new ExcelResult() { + + @Override + public String getAnalysis() { + if (failureNum > 0) { + failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); + throw new ServiceException(failureMsg.toString()); + } else { + successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); + } + return successMsg.toString(); + } + + @Override + public List getList() { + return null; + } + + @Override + public List getErrorList() { + return null; + } + }; + } +} diff --git a/maintain-business/src/main/java/com/maintain/business/service/impl/ErpMaterialServiceImpl.java b/maintain-business/src/main/java/com/maintain/business/service/impl/ErpMaterialServiceImpl.java index 63a70a52763222621e57d5492c9daacea9e4610f..be342484c12029e9108564938264c74516930a3c 100644 --- a/maintain-business/src/main/java/com/maintain/business/service/impl/ErpMaterialServiceImpl.java +++ b/maintain-business/src/main/java/com/maintain/business/service/impl/ErpMaterialServiceImpl.java @@ -93,6 +93,7 @@ public class ErpMaterialServiceImpl implements IErpMaterialService { lqw.eq(StringUtils.isNotBlank(bo.getIsEnable()), ErpMaterial::getIsEnable, bo.getIsEnable()); lqw.eq(StringUtils.isNotBlank(bo.getIsUniversal()), ErpMaterial::getIsUniversal, bo.getIsUniversal()); lqw.eq(ErpMaterial::getIsDelete, IsDeleteStatus.NO.getCode()); + lqw.orderByDesc(ErpMaterial::getCreateTime); return lqw; } diff --git a/maintain-ui/src/utils/request.js b/maintain-ui/src/utils/request.js index 083a0462a5eadc4189a7a5ce07d718ecdd7a07ff..769b3de3c9033cae2908e4157f117f3fc12cf0d1 100644 --- a/maintain-ui/src/utils/request.js +++ b/maintain-ui/src/utils/request.js @@ -9,7 +9,7 @@ import { saveAs } from 'file-saver' let downloadLoadingInstance; // 是否显示重新登录 -export let isRelogin = { show: false }; +export let isRelogin = { show: true }; axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' // 对应国际化资源文件后缀 @@ -19,7 +19,7 @@ const service = axios.create({ // axios中请求配置有baseURL选项,表示请求URL公共部分 baseURL: process.env.VUE_APP_BASE_API, // 超时 - timeout: 50000 + timeout: 10000 }) // request拦截器 diff --git a/maintain-ui/src/views/business/material/index.vue b/maintain-ui/src/views/business/material/index.vue index 1fceba9a6b2e5311c9c31d0bc69a04304b6eb23d..05651e7835c3458db8729bd3e45dbe3b87cb3467 100644 --- a/maintain-ui/src/views/business/material/index.vue +++ b/maintain-ui/src/views/business/material/index.vue @@ -120,6 +120,16 @@ v-hasPermi="['business:material:export']" >导出 + + 导入 + @@ -310,6 +320,36 @@ 取 消 + + + + + +
将文件拖到此处,或点击上传
+
+ + + + 仅允许导入xls、xlsx格式文件。 + 下载模板 +
+
+ +
@@ -321,6 +361,7 @@ import '@riophae/vue-treeselect/dist/vue-treeselect.css' import { listWarehouseLocation } from '../../../api/business/warehouseLocation' import { listMaterialVendor, listMaterialVendorAll } from '../../../api/business/materialVendor' import { listSiteWarehouse, listWarehouseAll } from '@/api/business/siteWarehouse' +import {getToken} from "@/utils/auth"; export default { name: "Material", @@ -375,6 +416,21 @@ export default { isUniversal: undefined, isCheck: undefined, }, + // 导入参数 + upload: { + // 是否显示弹出层 + open: false, + // 弹出层标题 + title: "", + // 是否禁用上传 + isUploading: false, + // 是否更新已经存在的数据 + updateSupport: 0, + // 设置上传的请求头部 + headers: { Authorization: "Bearer " + getToken() }, + // 上传的地址 + url: process.env.VUE_APP_BASE_API + "business/material/importData" + }, // 表单参数 form: {}, // 表单校验 @@ -589,7 +645,32 @@ export default { this.download('business/material/export', { ...this.queryParams }, `物资列表_${new Date().getTime()}.xlsx`) - } + }, + /** 导入按钮操作 */ + handleImport() { + this.upload.title = "物资导入"; + this.upload.open = true; + }, + /** 下载模板操作 */ + importTemplate() { + this.download('business/material/importTemplate', {}, `物资导入模板`) + }, + // 文件上传中处理 + handleFileUploadProgress(event, file, fileList) { + this.upload.isUploading = true; + }, + // 文件上传成功处理 + handleFileSuccess(response, file, fileList) { + this.upload.open = false; + this.upload.isUploading = false; + this.$refs.upload.clearFiles(); + this.$alert("
" + response.msg + "
", "导入结果", { dangerouslyUseHTMLString: true }); + this.getList(); + }, + // 提交上传文件 + submitFileForm() { + this.$refs.upload.submit(); + }, } };