Browse Source

Merge remote-tracking branch 'origin/dev' into dev

纪新园 1 year ago
parent
commit
7946871940

+ 233 - 1
blade-service/blade-los/src/main/java/org/springblade/los/business/sea/service/impl/TemplateImportServiceImpl.java

@@ -13,6 +13,8 @@ import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tool.api.R;
+import org.springblade.los.basic.packages.entity.BPackages;
+import org.springblade.los.basic.packages.service.IBPackagesService;
 import org.springblade.los.basic.ports.entity.BPorts;
 import org.springblade.los.basic.ports.service.IBPortsService;
 import org.springblade.los.basic.vessels.entity.BVessels;
@@ -32,6 +34,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -45,6 +48,7 @@ public class TemplateImportServiceImpl implements ITemplateImportService {
 	private final IBVesselsService bVesselsService;
 
 	private final IBPortsService bPortsService;
+	private final IBPackagesService bPackagesService;
 
 	public static double splitNumericPrefixFromString(String str){
 		str=str.trim();
@@ -132,6 +136,68 @@ public class TemplateImportServiceImpl implements ITemplateImportService {
 		return ObjectUtils.isNotNull(sb) && !sb.toString().trim().isEmpty();
 	}
 
+	public static String getFirstStars(String text){
+		String ret = "";
+		if(ObjectUtils.isNotNull(text)) {
+			for (int i = 0; i<text.length(); i++){
+				if(text.charAt(i)=='*'){
+					ret += '*';
+				}else{
+					break;
+				}
+			}
+		}
+		return ret;
+	}
+
+	public static String getLastStars(String text){
+		String ret = "";
+		if(ObjectUtils.isNotNull(text)) {
+			for (int i = text.length() - 1; i>=0; i--){
+				if(text.charAt(i)=='*'){
+					ret += '*';
+				}else{
+					break;
+				}
+			}
+		}
+		return ret;
+	}
+
+	public static String getStarLines(StringBuilder sb, String starts){
+		StringBuilder ret = new StringBuilder();
+
+		if(ObjectUtils.isNotNull(sb) && ObjectUtils.isNotNull(starts)){
+			String[] lines = sb.toString().split("\n");
+			ArrayList<String> newLines = new ArrayList<String>();
+			Boolean bInStar = false;
+			for (String line : lines) {
+				if(getFirstStars(line).equals(starts)){
+					bInStar = true;
+					if(line.length()>starts.length()){
+						ret.append(line.substring(starts.length() - 1).concat("\r\n"));
+					}
+				}else{
+					if(bInStar && (line.isEmpty() || line.charAt(0)!='*')){
+						ret.append(line.concat("\r\n"));
+					}else{
+						bInStar = false;
+					}
+					if(!bInStar) {
+						newLines.add(line);
+					}
+				}
+			}
+
+			sb.setLength(0);
+			newLines.forEach(line->{
+				sb.append(line.concat("\r\n"));
+			});
+		}
+
+		return ret.toString().trim();
+	}
+
 	public static void resetBillsNullValuesAsEmptyString(Bills bills){
 		//将字符串为null的赋默认值为""
 		Field[] fields = bills.getClass().getDeclaredFields();
@@ -975,7 +1041,6 @@ public class TemplateImportServiceImpl implements ITemplateImportService {
 			throw new RuntimeException("体积不能为空");
 		}
 
-
 		bills.setContainersList(new ArrayList<>());
 		bills.setPreContainersList(new ArrayList<>());
 		bills.setContainersReportsList(new ArrayList<>());
@@ -992,6 +1057,8 @@ public class TemplateImportServiceImpl implements ITemplateImportService {
 	public R<Bills> importHisenseBill(MultipartFile file) throws Exception {
 		Bills bills = new Bills();
 		SeaBillsDetail seaBillsDetail = new SeaBillsDetail();
+		List<Containers> containersList = new ArrayList<>();
+
 		// 创建临时文件
 		Path tempFile = Files.createTempFile("prefix", "suffix");
 		// 将MultipartFile的内容复制到临时文件
@@ -1002,6 +1069,171 @@ public class TemplateImportServiceImpl implements ITemplateImportService {
 		Workbook workbook = new HSSFWorkbook(fis);
 		Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
 
+		String header = getSheetCellValueAsString(sheet, 0, 0);
+		if(!header.toUpperCase().equals("HISENSE B/L INFORMATION")){
+			throw new RuntimeException("这可能不是海信提单文件!");
+		}
+
+		int firstRow = sheet.getFirstRowNum();
+		int lastRow = sheet.getLastRowNum();
+		if(firstRow<=0 && lastRow<0){
+			throw new RuntimeException("文件内容为空!");
+		}
+
+		StringBuilder sbLines = new StringBuilder();
+		for(int r=firstRow; r<=lastRow; r++){
+			String val0 = getSheetCellValueAsString(sheet, r, 0).toUpperCase().replace(" ", "");
+			if(val0.isEmpty()){
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("SHIPPER")) {
+				sbLines.setLength(0);
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("CONSIGNEE")) {
+				if (stringBuilderIsNotEmpty(sbLines)) {
+					System.out.println("shipper:" + sbLines);
+					seaBillsDetail.setHshipperDetails(sbLines.toString().trim());
+				}
+				sbLines.setLength(0);
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("NOTIFYPARTY")) {
+				if (stringBuilderIsNotEmpty(sbLines)) {
+					System.out.println("consignee:" + sbLines);
+					seaBillsDetail.setHconsigneeDetails(sbLines.toString().trim());
+				}
+				sbLines.setLength(0);
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("ALSONOTIFYPARTY")) {
+				if (stringBuilderIsNotEmpty(sbLines)) {
+					System.out.println("notify:" + sbLines);
+					seaBillsDetail.setHnotifyDetails(sbLines.toString().trim());
+				}
+				sbLines.setLength(0);
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("PORTOFLOADING")||val0.equals("PLACEOFRECEIPT")||val0.equals("PORTOFDISCHARGE")||val0.equals("PLACEOFDELIVERY")) {
+				if (val0.equals("PORTOFLOADING") && stringBuilderIsNotEmpty(sbLines)) {
+					System.out.println("notify2:" + sbLines);
+					seaBillsDetail.setHnotify2Details(sbLines.toString().trim());
+				}
+				sbLines.setLength(0);
+				String portName = getSheetCellValueAsString(sheet, r, 1);
+				if (ObjectUtils.isNotNull(portName)) {
+					BPorts ports = bPortsService.getOne(new LambdaQueryWrapper<BPorts>()
+						.eq(BPorts::getTenantId, AuthUtil.getTenantId())
+						.eq(BPorts::getIsDeleted, 0)
+						.eq(BPorts::getEnName, portName)
+						.last("limit 1"));
+					if (ports != null) {
+						if (val0.equals("PORTOFLOADING")){
+							bills.setPolId(ports.getId());
+							bills.setPolCode(ports.getCode());
+							bills.setPolCnName(ports.getCnName());
+							bills.setPolEnName(ports.getEnName());
+							bills.setPolNamePrint(ports.getEnName());
+						}else if(val0.equals("PLACEOFRECEIPT")){
+							bills.setPlaceReceiptId(ports.getId());
+							bills.setPlaceReceiptCode(ports.getCode());
+							bills.setPlaceReceiptName(ports.getEnName());
+							bills.setPlaceReceiptNamePrint(ports.getEnName());
+						}else if(val0.equals("PORTOFDISCHARGE")){
+							bills.setPodId(ports.getId());
+							bills.setPodCode(ports.getCode());
+							bills.setPodCnName(ports.getCnName());
+							bills.setPodEnName(ports.getEnName());
+							bills.setPodNamePrint(ports.getEnName());
+						}else if(val0.equals("PLACEOFDELIVERY")){
+							bills.setPlaceDeliveryId(ports.getId());
+							bills.setPlaceDeliveryCode(ports.getCode());
+							bills.setPlaceDeliveryName(ports.getEnName());
+							bills.setPlaceDeliveryNamePrint(ports.getEnName());
+						}
+					}
+				}
+				System.out.println(val0  + ":" + portName);
+			} else if (val0.equals("SHIPPINGMARK")) {
+				sbLines.setLength(0);
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("DESCRIPTIONOFGOODS")) {
+				if (stringBuilderIsNotEmpty(sbLines)) {
+					System.out.println("marks:" + sbLines);
+					bills.setMarks(sbLines.toString().trim());
+				}
+				sbLines.setLength(0);
+				sbLines.append(getSheetCellValueAsString(sheet, r, 1).concat("\r\n"));
+			} else if (val0.equals("箱型")) {
+				if (stringBuilderIsNotEmpty(sbLines)) {
+					System.out.println("goodsMarks:" + sbLines);
+					bills.setCommodityDescr(sbLines.toString().trim());
+				}
+				sbLines.setLength(0);
+				BPackages packages = bPackagesService.getOne(new LambdaQueryWrapper<BPackages>()
+					.eq(BPackages::getTenantId, AuthUtil.getTenantId())
+					.eq(BPackages::getIsDeleted, 0)
+					.eq(BPackages::getCode, getSheetCellValueAsString(sheet, r, 3))
+					.last("limit 1"));
+				if(ObjectUtils.isNotNull(packages)){
+					bills.setPackingUnitId(packages.getId());
+					bills.setPackingUnit(packages.getCode());
+				}
+				for(int cr=r+1; cr<=lastRow; cr++) {
+					String cntrType = getSheetCellValueAsString(sheet, cr, 0);
+					String cntrNo = getSheetCellValueAsString(sheet, cr, 1);
+					if(cntrType.length()==4 && cntrNo.length()==11) {
+						Containers containers = new Containers();
+						containers.setCntrTypeCode(cntrType);
+						containers.setCntrNo(cntrNo);
+						containers.setSealNo(getSheetCellValueAsString(sheet, cr, 2));
+						containers.setQuantity(BigDecimal.valueOf(getSheetCellValueAsDouble(sheet, cr, 3)));
+						containers.setGrossWeight(BigDecimal.valueOf(getSheetCellValueAsDouble(sheet, cr, 4)));
+						containers.setMeasurement(BigDecimal.valueOf(getSheetCellValueAsDouble(sheet, cr, 5)));
+						if(ObjectUtils.isNotNull(packages)){
+							containers.setPackingUnitId(packages.getId());
+							containers.setPackingUnit(packages.getCode());
+						}
+						containersList.add(containers);
+					}
+				}
+			}
+		}
+
+		// 处理货描中多余的收发通信息
+		sbLines.setLength(0);
+		sbLines.append(ObjectUtils.isNotNull(bills.getCommodityDescr()) ? bills.getCommodityDescr().trim() : "");
+
+		String lines = ObjectUtils.isNotNull(seaBillsDetail.getHshipperDetails()) ? seaBillsDetail.getHshipperDetails().trim() : "";
+		String stars = getLastStars(lines);
+		if(!stars.isEmpty()){
+			seaBillsDetail.setHshipperDetails(lines.substring(0, lines.length() - stars.length()).concat("\r\n").concat(getStarLines(sbLines, stars)));
+		}
+
+		lines = ObjectUtils.isNotNull(seaBillsDetail.getHconsigneeDetails()) ? seaBillsDetail.getHconsigneeDetails().trim() : "";
+		stars = getLastStars(lines);
+		if(!stars.isEmpty()){
+			seaBillsDetail.setHconsigneeDetails(lines.substring(0, lines.length() - stars.length()).concat("\r\n").concat(getStarLines(sbLines, stars)));
+		}
+
+		lines = ObjectUtils.isNotNull(seaBillsDetail.getHnotifyDetails()) ? seaBillsDetail.getHnotifyDetails().trim() : "";
+		stars = getLastStars(lines);
+		if(!stars.isEmpty()){
+			seaBillsDetail.setHnotifyDetails(lines.substring(0, lines.length() - stars.length()).concat("\r\n").concat(getStarLines(sbLines, stars)));
+		}
+
+		lines = ObjectUtils.isNotNull(seaBillsDetail.getHnotify2Details()) ? seaBillsDetail.getHnotify2Details().trim() : "";
+		stars = getLastStars(lines);
+		if(!stars.isEmpty()){
+			seaBillsDetail.setHnotify2Details(lines.substring(0, lines.length() - stars.length()).concat("\r\n").concat(getStarLines(sbLines, stars)));
+		}
+
+		bills.setCommodityDescr(sbLines.toString().trim());
+
+		bills.setContainersList(containersList);
+		bills.setPreContainersList(new ArrayList<>());
+		bills.setContainersReportsList(new ArrayList<>());
+		bills.setFeeCenterListC(new ArrayList<>());
+		bills.setFeeCenterListD(new ArrayList<>());
+		bills.setFilesList(new ArrayList<>());
+		bills.setWaitingBoxList(new ArrayList<>());
+		bills.setDetail(seaBillsDetail);
+		resetBillsNullValuesAsEmptyString(bills);
 		return R.data(bills);
 	}