| | |
| | | package com.consum.base.controller; |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.consum.base.BaseController; |
| | | import com.consum.base.config.WhWarningImportVerifyHandler; |
| | | import com.consum.base.core.utils.CommonUtil; |
| | | import com.consum.base.pojo.WarnConfImEntity; |
| | | import com.consum.base.pojo.WhWarningConfigParam; |
| | | import com.consum.base.pojo.query.WhWarningConfigQry; |
| | | import com.consum.base.service.BaseWarehouseServiceImpl; |
| | |
| | | import com.consum.model.po.WhWarningConfig; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.walker.db.page.GenericPager; |
| | | import com.walker.file.FileInfo; |
| | | import com.walker.infrastructure.utils.NumberGenerator; |
| | | import com.walker.web.ResponseCode; |
| | | import com.walker.web.ResponseValue; |
| | | import io.swagger.annotations.Api; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | import org.springframework.core.io.InputStreamResource; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @ClassName LWhWarningConfigController |
| | |
| | | } |
| | | return ResponseValue.success(lWhWarningConfigService.select(param)); |
| | | } |
| | | |
| | | /** |
| | | * @return 获取项目导入的模板 |
| | | * @throws IOException |
| | | */ |
| | | @PostMapping("getWarnConfImportTemplate") |
| | | public ResponseEntity<InputStreamResource> getProjectImportTemplate() throws IOException { |
| | | // 从当前项目资源目录获取文件 |
| | | org.springframework.core.io.Resource resource = new ClassPathResource("import/warningConfImp.xls"); |
| | | // 获取文件输入流 |
| | | InputStream inputStream = resource.getInputStream(); |
| | | // 设置HTTP响应头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| | | String encodedFilename = URLEncoder.encode("库存预警配置导入模板.xls", "UTF-8"); |
| | | headers.setContentDispositionFormData("attachment", encodedFilename); |
| | | // 创建InputStreamResource对象,将文件输入流包装起来 |
| | | InputStreamResource resourceToDownload = new InputStreamResource(inputStream); |
| | | // 返回带有文件输入流的ResponseEntity对象 |
| | | return ResponseEntity |
| | | .status(HttpStatus.OK) |
| | | .headers(headers) |
| | | .body(resourceToDownload); |
| | | } |
| | | |
| | | |
| | | @PostMapping("import") |
| | | public ResponseValue warnConfImport(MultipartFile multipartFile) throws Exception { |
| | | String originalFilename = multipartFile.getOriginalFilename(); |
| | | if (!"xls".equals(originalFilename.substring(originalFilename.lastIndexOf(".") + 1))) { |
| | | return ResponseValue.error(ResponseCode.ERROR.getCode(), "文件格式有误!"); |
| | | } |
| | | FinSysTenantUser sysInfo = this.getSysInfo(); |
| | | if (sysInfo == null) { |
| | | return ResponseValue.error(ResponseCode.ERROR.getCode(), "当前登录用户为空"); |
| | | } |
| | | |
| | | ImportParams params = new ImportParams(); |
| | | params.setHeadRows(1); |
| | | params.setNeedVerify(true); |
| | | WhWarningImportVerifyHandler whWarningImportVerifyHandler = new WhWarningImportVerifyHandler(Long.valueOf(sysInfo.getTenantId())); |
| | | params.setVerifyHandler(whWarningImportVerifyHandler); |
| | | |
| | | ExcelImportResult<WarnConfImEntity> result = ExcelImportUtil.importExcelMore(multipartFile.getInputStream(), WarnConfImEntity.class, params); |
| | | |
| | | boolean verfiyFail = result.isVerfiyFail(); |
| | | if (verfiyFail) { |
| | | StringBuilder errmsg = new StringBuilder(); |
| | | List<WarnConfImEntity> failList = result.getFailList(); |
| | | for (int i = 0; i < failList.size() && i < 20; i++) { |
| | | WarnConfImEntity entity = failList.get(i); |
| | | errmsg.append("第") |
| | | .append(entity.getRowNum()) |
| | | .append("行的错误是:") |
| | | .append(entity.getErrorMsg()) |
| | | .append("\r\n"); |
| | | } |
| | | InputStream inputStream = new ByteArrayInputStream(errmsg.toString().getBytes(StandardCharsets.UTF_8)); |
| | | FileInfo fileInfo = uploadFileToLocal(inputStream, NumberGenerator.generatorHexUUID() + ".txt", "-1", 111, 0, "-1"); |
| | | String url = fileInfo.getUrl(); |
| | | return ResponseValue.error(ResponseCode.PARAM_NOT_VALID.getCode(), url); |
| | | } |
| | | |
| | | |
| | | List<WarnConfImEntity> excelList = result.getList(); |
| | | ArrayList<WhWarningConfig> configArrayList = new ArrayList<>(excelList.size()); |
| | | // 按baseWarehouseId属性分组 |
| | | Map<Long, List<WarnConfImEntity>> groupedData = excelList.stream().collect(Collectors.groupingBy(WarnConfImEntity::getBaseWarehouseId)); |
| | | for (Map.Entry<Long, List<WarnConfImEntity>> entry : groupedData.entrySet()) { |
| | | Long baseWarehouseId = entry.getKey(); |
| | | List<WarnConfImEntity> houseModelList = entry.getValue(); |
| | | // 提取id并存储到新列表 |
| | | List<Long> modelIdList = houseModelList.stream() |
| | | .map(WarnConfImEntity::getBaseGoodsModelsId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<Map<String, Object>> queryModelIsExistList = lWhWarningConfigService.queryModelIsExist(0, baseWarehouseId, modelIdList); |
| | | if (CollectionUtils.isEmpty(queryModelIsExistList)) { |
| | | // 全部为新增 |
| | | for (WarnConfImEntity warnConfImEntity : houseModelList) { |
| | | // 新增 |
| | | WhWarningConfig whWarningConfig = new WhWarningConfig(); |
| | | whWarningConfig.setId(NumberGenerator.getLongSequenceNumber()); |
| | | whWarningConfig.setWarehouseType(0); |
| | | whWarningConfig.setBaseWarehouseId(warnConfImEntity.getBaseWarehouseId()); |
| | | whWarningConfig.setBaseGoodsTemplateId(warnConfImEntity.getGoodsTemplateId()); |
| | | whWarningConfig.setBaseGoodsModelsId(warnConfImEntity.getBaseGoodsModelsId()); |
| | | whWarningConfig.setUpperLimit(warnConfImEntity.getUpperLimit()); |
| | | whWarningConfig.setLowerLimit(warnConfImEntity.getLowerLimit()); |
| | | if (whWarningConfig.getBaseGoodsModelsId() == null) { |
| | | whWarningConfig.setGoodsType(1); |
| | | } else { |
| | | whWarningConfig.setGoodsType(2); |
| | | } |
| | | configArrayList.add(whWarningConfig); |
| | | } |
| | | } else { |
| | | // 提取id并存储到新列表 |
| | | List<Long> dbExistModelIdList = queryModelIsExistList.stream() |
| | | .map(map -> Convert.toLong(map.get("baseGoodsModelsId"))) |
| | | .collect(Collectors.toList()); |
| | | for (WarnConfImEntity warnConfImEntity : houseModelList) { |
| | | Long baseGoodsModelsId = warnConfImEntity.getBaseGoodsModelsId(); |
| | | WhWarningConfig whWarningConfig = new WhWarningConfig(); |
| | | whWarningConfig.setWarehouseType(0); |
| | | whWarningConfig.setBaseWarehouseId(warnConfImEntity.getBaseWarehouseId()); |
| | | whWarningConfig.setBaseGoodsModelsId(warnConfImEntity.getBaseGoodsModelsId()); |
| | | whWarningConfig.setUpperLimit(warnConfImEntity.getUpperLimit()); |
| | | whWarningConfig.setLowerLimit(warnConfImEntity.getLowerLimit()); |
| | | |
| | | if (dbExistModelIdList.contains(baseGoodsModelsId)) { |
| | | // 修改 |
| | | limitEditByModeInfo(whWarningConfig); |
| | | } else { |
| | | // 新增 |
| | | whWarningConfig.setId(NumberGenerator.getLongSequenceNumber()); |
| | | whWarningConfig.setBaseGoodsTemplateId(warnConfImEntity.getGoodsTemplateId()); |
| | | if (whWarningConfig.getBaseGoodsModelsId() == null) { |
| | | whWarningConfig.setGoodsType(1); |
| | | } else { |
| | | whWarningConfig.setGoodsType(2); |
| | | } |
| | | configArrayList.add(whWarningConfig); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (!CollectionUtils.isEmpty(configArrayList)) { |
| | | int flag = lWhWarningConfigService.insertBatch(configArrayList); |
| | | } |
| | | return ResponseValue.success("导入成功!", 1); |
| | | } |
| | | |
| | | |
| | | private String KU_CUN_UPD_BY_MODE_INFO = "update WH_WARNING_CONFIG set "; |
| | | |
| | | public int limitEditByModeInfo(WhWarningConfig config) { |
| | | StringBuilder sql = new StringBuilder(KU_CUN_UPD_BY_MODE_INFO); |
| | | Integer lowerLimit = config.getLowerLimit(); |
| | | if (lowerLimit == null) { |
| | | sql.append("lower_limit=null"); |
| | | } else { |
| | | sql.append("lower_limit=" + lowerLimit); |
| | | } |
| | | Integer upperLimit = config.getUpperLimit(); |
| | | if (upperLimit == null) { |
| | | sql.append(",upper_Limit=null"); |
| | | } else { |
| | | sql.append(",upper_Limit=" + upperLimit); |
| | | } |
| | | sql.append(" where 1=1"); |
| | | Integer warehouseType = config.getWarehouseType(); |
| | | if (warehouseType != null) { |
| | | sql.append(" and warehouse_Type=" + warehouseType); |
| | | } |
| | | Long baseWarehouseId = config.getBaseWarehouseId(); |
| | | if (baseWarehouseId != null) { |
| | | sql.append(" and BASE_WAREHOUSE_ID=" + baseWarehouseId); |
| | | } |
| | | Long baseGoodsModelsId = config.getBaseGoodsModelsId(); |
| | | if (baseGoodsModelsId != null) { |
| | | sql.append(" and BASE_GOODS_MODELS_ID=" + baseGoodsModelsId); |
| | | } |
| | | lWhWarningConfigService.update(sql.toString()); |
| | | return 1; |
| | | } |
| | | } |