| | |
| | | import com.zy.crm.system.service.DicService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.ss.usermodel.DataFormatter; |
| | | import org.apache.poi.ss.usermodel.Row; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | |
| | | @ManagerAuth(memo = "ç²æ¹åä½Excel导å
¥") |
| | | @Transactional |
| | | public R cstmrExcelImport(MultipartFile file) throws IOException { |
| | | try (InputStream inStream = file.getInputStream()) { |
| | | String fileMime = file.getContentType(); |
| | | int excelVersion = 2007; |
| | | if ("application/vnd.ms-excel".equals(fileMime)) { |
| | | excelVersion = 2003; |
| | | } |
| | | Workbook book = null; |
| | | if (excelVersion == 2003) { |
| | | book = new HSSFWorkbook(inStream); |
| | | } |
| | | else { // å½ excel æ¯ 2007 æ¶ |
| | | book = new XSSFWorkbook(inStream); |
| | | } |
| | | Sheet sheet = book.getSheetAt(0); |
| | | int totalRows = sheet.getPhysicalNumberOfRows() - 1; // æ» |
| | | Long userId = getUserId(); |
| | | Long hostId = getHostId(); |
| | | Long deptId = getDeptId(); |
| | | Date now = new Date(); |
| | | for (int i = 2; i < totalRows; i++) { |
| | | Cstmr cstmr = new Cstmr(); |
| | | Row row = sheet.getRow(i); |
| | | // 客æ·ä»£å· |
| | | String uuid = row.getCell(0).getStringCellValue(); |
| | | if (!Cools.isEmpty(uuid)) { |
| | | if (cstmrService.selectByUuid(hostId, uuid) != null) { |
| | | throw new CoolException(uuid + "客æ·ä»£å·å·²åå¨ï¼è¯·éæ°å¯¼å
¥"); |
| | | } else { |
| | | cstmr.setUuid(uuid); |
| | | } |
| | | } else { |
| | | cstmr.setUuid(cstmrService.getUuid(hostId)); |
| | | } |
| | | // 客æ·ä»£å· |
| | | String name = row.getCell(1).getStringCellValue(); |
| | | if (Cools.isEmpty(name)) { |
| | | throw new CoolException("第" + i + "è¡å®¢æ·åç§°ä¸è½ä¸ºç©ºï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setName(name); |
| | | // 客æ·ç±»å« |
| | | String type = row.getCell(2).getStringCellValue(); |
| | | if (Cools.isEmpty(type)) { |
| | | throw new CoolException("第" + i + "è¡å®¢æ·ç±»å«ä¸è½ä¸ºç©ºï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | CstmrType cstmrType = cstmrTypeService.selectByName(hostId, type); |
| | | if (null == cstmrType) { |
| | | throw new CoolException("第" + i + "è¡å®¢æ·ç±»å«ä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setCstmrType(cstmrType.getId()); |
| | | // 客æ·ç±»å« |
| | | cstmr.setRela(row.getCell(3).getStringCellValue()); |
| | | // çµè¯ |
| | | cstmr.setTel(row.getCell(4).getStringCellValue()); |
| | | // 客æ·è系人 |
| | | cstmr.setContacts(row.getCell(5).getStringCellValue()); |
| | | // 夿³¨ |
| | | cstmr.setRemarks(row.getCell(6).getStringCellValue()); |
| | | // ç |
| | | String province = row.getCell(7).getStringCellValue(); |
| | | Dic dicProvince = dicService.selectByName(province); |
| | | if (Cools.isEmpty(dicProvince)) { |
| | | throw new CoolException("第" + i + "è¡çåä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setProvince(dicProvince.getCode()); |
| | | // å¸ |
| | | String city = row.getCell(8).getStringCellValue(); |
| | | Dic dicCity = dicService.selectByName(city); |
| | | if (Cools.isEmpty(dicCity)) { |
| | | throw new CoolException("第" + i + "è¡å¸åä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setCity(dicCity.getCode()); |
| | | // åº |
| | | String district = row.getCell(9).getStringCellValue(); |
| | | Dic dicDistrict = dicService.selectByName(district); |
| | | if (Cools.isEmpty(dicDistrict)) { |
| | | throw new CoolException("第" + i + "è¡å¿åä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setDistrict(dicDistrict.getCode()); |
| | | // 详ç»å°å |
| | | cstmr.setAddr(row.getCell(10).getStringCellValue()); |
| | | // åºå |
| | | cstmr.setType0(row.getCell(11).getStringCellValue()); |
| | | cstmr.setHostId(hostId); |
| | | cstmr.setDeptId(deptId); |
| | | cstmr.setUserId(userId); |
| | | cstmr.setStatus(1); |
| | | cstmr.setCreateBy(userId); |
| | | cstmr.setCreateTime(now); |
| | | cstmr.setUpdateBy(userId); |
| | | cstmr.setUpdateTime(now); |
| | | if (!cstmrService.insert(cstmr)) { |
| | | throw new CoolException("ä¿å失败ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | InputStream inStream = file.getInputStream(); |
| | | String fileMime = file.getContentType(); |
| | | int excelVersion = 2007; |
| | | if ("application/vnd.ms-excel".equals(fileMime)) { |
| | | excelVersion = 2003; |
| | | } |
| | | |
| | | Workbook book = null; |
| | | if (excelVersion == 2003) { |
| | | book = new HSSFWorkbook(inStream); |
| | | } |
| | | else { // å½ excel æ¯ 2007 æ¶ |
| | | book = new XSSFWorkbook(inStream); |
| | | } |
| | | Sheet sheet = book.getSheetAt(0); |
| | | int totalRows = sheet.getLastRowNum() + 1; // æ» |
| | | Long userId = getUserId(); |
| | | Long hostId = getHostId(); |
| | | Long deptId = getDeptId(); |
| | | Date now = new Date(); |
| | | DataFormatter dataFormatter = new DataFormatter(); |
| | | for (int i = 2; i < totalRows; i++) { |
| | | Cstmr cstmr = new Cstmr(); |
| | | Row row = sheet.getRow(i); |
| | | // 客æ·ä»£å· |
| | | String uuid = dataFormatter.formatCellValue(row.getCell(0)); |
| | | if (!Cools.isEmpty(uuid)) { |
| | | if (cstmrService.selectByUuid(hostId, uuid) != null) { |
| | | throw new CoolException(uuid + "客æ·ä»£å·å·²åå¨ï¼è¯·éæ°å¯¼å
¥"); |
| | | } else { |
| | | cstmr.setUuid(uuid); |
| | | } |
| | | } else { |
| | | cstmr.setUuid(cstmrService.getUuid(hostId)); |
| | | } |
| | | // 客æ·ä»£å· |
| | | String name = dataFormatter.formatCellValue(row.getCell(1)); |
| | | if (Cools.isEmpty(name)) { |
| | | throw new CoolException("第" + i + "è¡å®¢æ·åç§°ä¸è½ä¸ºç©ºï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setName(name); |
| | | // 客æ·ç±»å« |
| | | String type = dataFormatter.formatCellValue(row.getCell(2)); |
| | | if (Cools.isEmpty(type)) { |
| | | throw new CoolException("第" + i + "è¡å®¢æ·ç±»å«ä¸è½ä¸ºç©ºï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | CstmrType cstmrType = cstmrTypeService.selectByName(hostId, type); |
| | | if (null == cstmrType) { |
| | | throw new CoolException("第" + i + "è¡å®¢æ·ç±»å«ä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setCstmrType(cstmrType.getId()); |
| | | // 客æ·å
³ç³» |
| | | cstmr.setRela(dataFormatter.formatCellValue(row.getCell(3))); |
| | | // çµè¯ |
| | | cstmr.setTel(dataFormatter.formatCellValue(row.getCell(4))); |
| | | // 客æ·è系人 |
| | | cstmr.setContacts(dataFormatter.formatCellValue(row.getCell(5))); |
| | | // 夿³¨ |
| | | cstmr.setRemarks(dataFormatter.formatCellValue(row.getCell(6))); |
| | | // ç |
| | | String province = dataFormatter.formatCellValue(row.getCell(7)); |
| | | Dic dicProvince = dicService.selectByName(province); |
| | | if (Cools.isEmpty(dicProvince)) { |
| | | throw new CoolException("第" + i + "è¡çåä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setProvince(dicProvince.getCode()); |
| | | // å¸ |
| | | String city = dataFormatter.formatCellValue(row.getCell(8)); |
| | | Dic dicCity = dicService.selectByName(city); |
| | | if (Cools.isEmpty(dicCity)) { |
| | | throw new CoolException("第" + i + "è¡å¸åä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setCity(dicCity.getCode()); |
| | | // åº |
| | | String district = dataFormatter.formatCellValue(row.getCell(9)); |
| | | Dic dicDistrict = dicService.selectByName(district); |
| | | if (Cools.isEmpty(dicDistrict)) { |
| | | throw new CoolException("第" + i + "è¡å¿åä¸åæ³ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | cstmr.setDistrict(dicDistrict.getCode()); |
| | | // 详ç»å°å |
| | | cstmr.setAddr(dataFormatter.formatCellValue(row.getCell(10))); |
| | | // åºå |
| | | cstmr.setType0(dataFormatter.formatCellValue(row.getCell(11))); |
| | | cstmr.setHostId(hostId); |
| | | cstmr.setDeptId(deptId); |
| | | cstmr.setUserId(userId); |
| | | cstmr.setStatus(1); |
| | | cstmr.setCreateBy(userId); |
| | | cstmr.setCreateTime(now); |
| | | cstmr.setUpdateBy(userId); |
| | | cstmr.setUpdateTime(now); |
| | | if (!cstmrService.insert(cstmr)) { |
| | | throw new CoolException("ä¿å失败ï¼è¯·éæ°å¯¼å
¥"); |
| | | } |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |