diff --git a/yxt-base-biz/src/main/java/com/yxt/base/apiadmin/base/BaseGoodsSpuRest.java b/yxt-base-biz/src/main/java/com/yxt/base/apiadmin/base/BaseGoodsSpuRest.java index 78e8af1e65..4643996f1d 100644 --- a/yxt-base-biz/src/main/java/com/yxt/base/apiadmin/base/BaseGoodsSpuRest.java +++ b/yxt-base-biz/src/main/java/com/yxt/base/apiadmin/base/BaseGoodsSpuRest.java @@ -8,7 +8,10 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; import java.util.List; /** @@ -111,6 +114,18 @@ public class BaseGoodsSpuRest { return baseGoodsSpuService.selByCodeAndDept(code,deptSid); } + + @ApiOperation(value = "商品信息导入") + @PostMapping("/importGoodsSpuData") + public ResultBean importGoodsSpuData( + @RequestParam(value = "file") MultipartFile file, + @RequestParam("orgPath") String orgPath, + @RequestParam("userSid") String userSid, + HttpServletRequest request) throws IOException { + return baseGoodsSpuService.importGoodsSpuData(file,orgPath,userSid,request); + } + + //------------------------------- 行政类商品管理 -------------------------------- @ApiOperation("保存修改行政商品信息") diff --git a/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/BaseGoodsSpuService.java b/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/BaseGoodsSpuService.java index 2332242bcf..075ce225b6 100644 --- a/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/BaseGoodsSpuService.java +++ b/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/BaseGoodsSpuService.java @@ -34,11 +34,23 @@ import com.yxt.common.base.utils.StringUtils; import com.yxt.common.core.query.PagerQuery; import com.yxt.common.core.result.ResultBean; import com.yxt.common.core.vo.PagerVo; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.InputStream; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author wangpengfei @@ -588,7 +600,7 @@ public class BaseGoodsSpuService extends MybatisBaseService> selPurchaseAssetInfo(String orgPath,String name) { + public ResultBean> selPurchaseAssetInfo(String orgPath, String name) { ResultBean rb = ResultBean.fireFail(); List list = new ArrayList<>(); String useOrgSid = ""; @@ -603,7 +615,7 @@ public class BaseGoodsSpuService extends MybatisBaseService goodsSpus = baseMapper.selPurchaseAssetInfo(useOrgSid,name); + List goodsSpus = baseMapper.selPurchaseAssetInfo(useOrgSid, name); if (!goodsSpus.isEmpty()) { goodsSpus.stream().forEach(details -> { YxtBaseFormCommon formCommon = new YxtBaseFormCommon(); @@ -639,4 +651,269 @@ public class BaseGoodsSpuService extends MybatisBaseService split = Arrays.asList(orgPath.split("/")); + if (split.size() > 1) { + //获取本级sid获取本级部门信息 + SysOrganizationVo sysOrganization1 = sysOrganizationFeign.fetchBySid(split.get(split.size() - 1)).getData(); + useOrgSid = sysOrganization1.getSid(); + } else { + SysOrganizationVo sysOrganization = sysOrganizationFeign.fetchBySid(split.get(0)).getData(); + useOrgSid = sysOrganization.getSid(); + } + + int size = 0; + try { + String temp = request.getSession().getServletContext().getRealPath(File.separator) + "temp";// 临时目录 + File tempFile = new File(temp); + if (!tempFile.exists()) { + tempFile.mkdirs(); + } + String fileName = file.getOriginalFilename(); + if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { + return rb.fail().setMsg("上传文件不正确"); + } + boolean isExcel2003 = true; + if (fileName.matches("^.+\\.(?i)(xlsx)$")) { + isExcel2003 = false; + } + InputStream is = file.getInputStream(); + Workbook wb = null; + if (isExcel2003) { + wb = new HSSFWorkbook(is); + } else { + wb = new XSSFWorkbook(is); + } + Sheet sheet = wb.getSheetAt(0); + ReturnImportMsg returnMsg = getSleepSheetVal(sheet); + Set setMsg = returnMsg.getSetMsg(); + StringBuffer sbMsg = new StringBuffer(); + List list = returnMsg.getList(); + if (!list.isEmpty() && list.size() > 0) { + for (ImportDataVo vo : list) { + + } + } + if (null != setMsg && setMsg.size() > 0) { + for (String s : setMsg) { + sbMsg.append(s).append(","); + } + sbMsg.delete(sbMsg.length() - 1, sbMsg.length()); + if (StringUtils.isNotBlank(sbMsg.toString())) { + return rb.setMsg(sbMsg.toString()); + } + } + size = list.size(); + } catch (Exception e) { + e.printStackTrace(); + } + return rb.success().setMsg("成功导入" + size + "条数据"); + } + + private ReturnImportMsg getSleepSheetVal(Sheet sheet) { + ReturnImportMsg returnMsg = new ReturnImportMsg(); + List voList = new ArrayList<>(); + Set setMsg = new HashSet<>(); + Row row1 = sheet.getRow(0); + Map header = new HashMap<>(); + Map map = new HashMap<>(); + for (int i = 0; i < 13; i++) { + String trim = new String(); + try { + trim = getCellVal(row1.getCell(i)).toString().trim(); + String temp = getCellVal(row1.getCell(i)).toString().trim(); + if (StringUtils.isNotBlank(temp)) { + header.put(i, temp); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + for (int r = 1; r <= sheet.getLastRowNum(); r++) { + ImportDataVo info = new ImportDataVo(); + Row row = sheet.getRow(r); + if (row == null) { + continue; + } + for (int i = 0; i < header.size(); i++) { +// String trim = new String(); + try { +// trim = getCellVal(row.getCell(i)).toString().trim(); + String temp = ""; + if (null != getCellVal(row.getCell(i))) { + temp = getCellVal(row.getCell(i)).toString().trim(); + } + String head = header.get(i); + String field = ""; + if (StringUtils.isNotBlank(temp)) { + field = temp; + } + if (head.equals("*排序号")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行排序号不能为空"); + } else { + if (!isNum(field)) { + setMsg.add("第" + (r + 1) + "行排序号请填写数字"); + } else { + int parseInt = Integer.parseInt(field); + field = String.valueOf(parseInt); + } + } + info.setSortNo(field); + } + if (head.equals("*商品名称")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行商品名称不能为空"); + } + info.setGoodsName(field); + } + if (head.equals("*图号")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行图号不能为空"); + } + info.setGoodsCode(field); + } + if (head.equals("商品条码")) { + info.setBarCode(field); + } + if (head.equals("*商品类别")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行商品类别不能为空"); + } + info.setGoodsTypeName(field); + } + if (head.equals("*品牌")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行品牌不能为空"); + } + info.setBrandName(field); + } + if (head.equals("*厂家")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行厂家不能为空"); + } + info.setManufacturerName(field); + } + if (head.equals("规格")) { + info.setIndexes(field); + } + if (head.equals("*计量单位")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行计量单位不能为空"); + } + info.setGoodsUnitName(field); + } + if (head.equals("*库存上限")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行库存上限不能为空"); + } else { + if (!isNum(field)) { + setMsg.add("第" + (r + 1) + "行库存上限请填写数字"); + } else { + int parseInt = Integer.parseInt(field); + field = String.valueOf(parseInt); + } + } + info.setInventoryAlertUpperLimit(field); + } + if (head.equals("*库存下限")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行库存下限不能为空"); + } else { + if (!isNum(field)) { + setMsg.add("第" + (r + 1) + "行库存下限请填写数字"); + } else { + int parseInt = Integer.parseInt(field); + field = String.valueOf(parseInt); + } + } + info.setInventoryAlertLowerLimit(field); + } + if (head.equals("*商品等级")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行商品等级不能为空"); + } + info.setGoodsLevelValue(field); + } + if (head.equals("*商品类型")) { + if (StringUtils.isBlank(field)) { + setMsg.add("第" + (r + 1) + "行商品类型不能为空"); + } + info.setGoodsClassValue(field); + } + } catch (Exception e) { + e.printStackTrace(); + continue; + } + } + voList.add(info); + } + returnMsg.setList(voList); + returnMsg.setSetMsg(setMsg); + return returnMsg; + } + + public Object getCellVal(Cell cell) { + Object obj = null; + if (cell != null) { + switch (cell.getCellTypeEnum()) { + case BOOLEAN: + obj = cell.getBooleanCellValue(); + break; + case ERROR: + obj = cell.getErrorCellValue(); + break; + case NUMERIC: + obj = cell.getNumericCellValue(); + break; + case STRING: + obj = cell.getStringCellValue(); + break; + default: + break; + } + } + return obj; + } + + public boolean isNum(String s) { + if (org.apache.commons.lang3.StringUtils.isNotBlank(s)) { + if (s.contains(".")) { + return isNumeric(s); + } else { + return isNumericFirst(s); + } + } + return false; + } + + public boolean isNumeric(String s) { + String substring = s.substring(0, s.lastIndexOf(".")); + String substringLast = s.substring(s.lastIndexOf(".") + 1, s.length()); + if (substring != null && !"".equals(substring.trim()) || substringLast != null && !"".equals(substringLast.trim())) { + boolean matches = substring.matches("^[0-9]*$"); + boolean b = substringLast.matches("^[0-9]*$"); + if (matches && b) { + return true; + } else { + return false; + } + } else + return false; + } + + public boolean isNumericFirst(String str) { + Pattern pattern = Pattern.compile("[0-9]*"); + System.out.println(str); + Matcher isNum = pattern.matcher(str); + if (!isNum.matches()) { + return false; + } + return true; + } + + } diff --git a/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/ImportDataVo.java b/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/ImportDataVo.java new file mode 100644 index 0000000000..af36798b22 --- /dev/null +++ b/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/ImportDataVo.java @@ -0,0 +1,22 @@ +package com.yxt.base.biz.base.basegoodsspu; + +import lombok.Data; + +@Data +public class ImportDataVo { + + private String sortNo;//排序 + private String goodsName;//商品名称 + private String goodsCode;//图号 + private String barCode;//条形码 + private String goodsTypeName;//商品分类sid + private String brandName;//品牌 + private String manufacturerName;//厂家名称 + private String indexes;//规格 + private String goodsUnitName;//计量单位名称 + private String inventoryAlertUpperLimit;//库存上限 + private String inventoryAlertLowerLimit;//库存下限 + private String goodsClassValue;//商品类型 + private String goodsLevelValue;//商品等级 + +} diff --git a/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/ReturnImportMsg.java b/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/ReturnImportMsg.java new file mode 100644 index 0000000000..dfdcba9c88 --- /dev/null +++ b/yxt-base-biz/src/main/java/com/yxt/base/biz/base/basegoodsspu/ReturnImportMsg.java @@ -0,0 +1,14 @@ +package com.yxt.base.biz.base.basegoodsspu; + +import lombok.Data; + +import java.util.List; +import java.util.Set; + +@Data +public class ReturnImportMsg { + + private Set setMsg; + private List list; + +}