sgj
8 天以前 71fe4f1f6a75c86640a726b9c230e8aaae2a28e7
fzzy-igdss-core/src/main/java/com/fzzy/igds/service/ExportService.java
@@ -15,6 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@@ -31,6 +32,329 @@
    @Resource
    private FileService fileService;
    /**
     * 解析Excel数据
     *
     * @param workbook
     * @return
     */
    private static List<InoutRecord> parseExcel(Workbook workbook) throws Exception {
        List<InoutRecord> resultDataList = new ArrayList<>();
        // 解析sheet
        for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
            Sheet sheet = workbook.getSheetAt(sheetNum);
            // 校验sheet是否合法
            if (sheet == null) {
                continue;
            }
            // 获取第一行数据,不用处理
            int firstRowNum = sheet.getFirstRowNum();
            // 解析每一行的数据,构造数据对象
            int rowStart = firstRowNum + 2;
            int rowEnd = sheet.getPhysicalNumberOfRows();
            //定义变量,用于判断数据是否解析完
            boolean flag = false;
            for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (null == row) {
                    //此行为空,则之后不再解析,判定为解析完成
                    flag = true;
                    break;
                }
                InoutRecord resultData = convertRowToData(row);
                resultDataList.add(resultData);
            }
            if (flag) {
                break;
            }
        }
        return resultDataList;
    }
    /**
     * 根据文件后缀名类型获取对应的工作簿对象
     *
     * @param inputStream 读取文件的输入流
     * @param fileType    文件后缀名类型(xls或xlsx)
     * @return 包含文件数据的工作簿对象
     * @throws IOException
     */
    public static Workbook getWorkbook(InputStream inputStream, String fileType) throws IOException {
        Workbook workbook = null;
        if (fileType.equalsIgnoreCase(XLS)) {
            workbook = new HSSFWorkbook(inputStream);
        } else if (fileType.equalsIgnoreCase(XLSX)) {
            workbook = new XSSFWorkbook(inputStream);
        }
        return workbook;
    }
    /**
     * 提取每一行中需要的数据,构造成为一个结果数据对象
     *
     * @param row
     * @return
     */
    private static InoutRecord convertRowToData(Row row) throws Exception {
        InoutRecord resultData = new InoutRecord();
        Cell cell;
        int cellNum = 0;
        //第一列序号,不做处理
        cell = row.getCell(cellNum++);
        //日期
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setCustomerName("");
        } else {
            cell.setCellType(CellType.STRING);
            String time = cell.getStringCellValue().trim().replaceAll(" ", "");
            if (StringUtils.isNotEmpty(time)) {
                resultData.setRegisterTime(parseFlexibleDate(time));
            }
        }
        //类型
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setType("");
        } else {
            cell.setCellType(CellType.STRING);
            String type = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(type)) {
                resultData.setType(type);
            }
        }
        //车牌号
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setPlateNum("");
        } else {
            cell.setCellType(CellType.STRING);
            String plateNum = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(plateNum)) {
                resultData.setPlateNum(plateNum);
            }
        }
        //承运人
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setUserName("");
        } else {
            cell.setCellType(CellType.STRING);
            String userName = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(userName)) {
                resultData.setUserName(userName);
            }
        }
        //往来单位
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setCustomerName("");
        } else {
            cell.setCellType(CellType.STRING);
            String customerName = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(customerName)) {
                resultData.setCustomerName(customerName);
            }
        }
        //通知单编码
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setNoticeId("");
        } else {
            cell.setCellType(CellType.STRING);
            String noticeId = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(noticeId)) {
                resultData.setNoticeId(noticeId);
            }
        }
        //装卸仓库
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setDepotId("");
        } else {
            cell.setCellType(CellType.STRING);
            String depotName = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(depotName)) {
                resultData.setDepotId(depotName);
            }
        }
        //粮食品种
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setFoodVariety("");
        } else {
            cell.setCellType(CellType.STRING);
            String foodVariety = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(foodVariety)) {
                resultData.setFoodVariety(FoodVariety.getCode(foodVariety.trim()));
            }
        }
        //粮食年份
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setFoodYear("");
        } else {
            cell.setCellType(CellType.STRING);
            String year = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(year)) {
                resultData.setFoodYear(year);
            }
        }
        //满车重量
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setFullWeight(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String fullWeight = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(fullWeight)) {
                resultData.setFullWeight(Double.valueOf(fullWeight));
            }
        }
        //空车重量
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setEmptyWeight(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String emptyWeight = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(emptyWeight)) {
                resultData.setEmptyWeight(Double.valueOf(emptyWeight));
            }
        }
        //扣重
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setDeOther(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String deOther = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(deOther)) {
                resultData.setDeOther(Double.valueOf(deOther));
            }
        }
        //结算重量
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setSettleWeight(0.0);
            resultData.setRecordWeight(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String recordWeight = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(recordWeight)) {
                resultData.setSettleWeight(Double.valueOf(recordWeight));
                resultData.setRecordWeight(Double.valueOf(recordWeight));
            }
        }
        //单价
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setPrice(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String price = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(price)) {
                resultData.setPrice(Double.valueOf(price));
            }
        }
        //水分
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setPerWet(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String perWet = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(perWet)) {
                resultData.setPerWet(Double.valueOf(perWet));
            }
        }
        //杂质
        cell = row.getCell(cellNum++);
        if (cell == null || cell.getCellType() == CellType.BLANK) {
            resultData.setPerImpurity(0.0);
        } else {
            cell.setCellType(CellType.STRING);
            String perImpurity = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(perImpurity)) {
                resultData.setPerImpurity(Double.valueOf(perImpurity));
            }
        }
        //备注
        cell = row.getCell(cellNum++);
        if (null == cell || cell.getCellType() == CellType.BLANK) {
            resultData.setRemarks("");
        } else {
            cell.setCellType(CellType.STRING);
            String remarks = cell.getStringCellValue().trim();
            if (StringUtils.isNotEmpty(remarks)) {
                resultData.setRemarks(remarks);
            }
        }
        if (null != resultData.getFullWeight() && null != resultData.getEmptyWeight()) {
            resultData.setNetWeight(resultData.getFullWeight() - resultData.getEmptyWeight());
        }
        return resultData;
    }
    /**
     * 多种格式解析字符串日期
     *
     * @param dateStr 日期字符串
     * @author sgj
     * @since 2026/03/10
     */
    private static Date parseFlexibleDate(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        // 定义支持的日期格式
        String[] parsePatterns = new String[]{
                "yyyy-MM-dd",      // 2026-01-06
                "yyyy/MM/dd",      // 2026/01/06
//                "yyyy年MM月dd日",   // 2026 年 01 月 06 日
                "yyyy-MM-dd HH:mm:ss",  // 2026-01-06 12:00:00
                "yyyy/MM/dd HH:mm:ss",  // 2026/01/06 12:00:00
                "yyyy-MM-dd HH:mm",     // 2026-01-06 12:00
                "yyyy/MM/dd HH:mm"      // 2026/01/06 12:00
        };
        try {
            return DateUtils.parseDateStrictly(dateStr, parsePatterns);
        } catch (Exception e) {
            // 如果所有格式都解析失败,返回 null
            return null;
        }
    }
    /**
     * 读取Excel文件内容
@@ -74,296 +398,4 @@
            }
        }
    }
    /**
     * 解析Excel数据
     *
     * @param workbook
     * @return
     */
    private static List<InoutRecord> parseExcel(Workbook workbook) throws Exception{
        List<InoutRecord> resultDataList = new ArrayList<>();
        // 解析sheet
        for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
            Sheet sheet = workbook.getSheetAt(sheetNum);
            // 校验sheet是否合法
            if (sheet == null) {
                continue;
            }
            // 获取第一行数据,不用处理
            int firstRowNum = sheet.getFirstRowNum();
            // 解析每一行的数据,构造数据对象
            int rowStart = firstRowNum + 2;
            int rowEnd = sheet.getPhysicalNumberOfRows();
            //定义变量,用于判断数据是否解析完
            boolean flag = false;
            for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (null == row) {
                    //此行为空,则之后不再解析,判定为解析完成
                    flag = true;
                    break;
                }
                InoutRecord resultData = convertRowToData(row);
                resultDataList.add(resultData);
            }
            if(flag){
                break;
            }
        }
        return resultDataList;
    }
    /**
     * 根据文件后缀名类型获取对应的工作簿对象
     * @param inputStream 读取文件的输入流
     * @param fileType 文件后缀名类型(xls或xlsx)
     * @return 包含文件数据的工作簿对象
     * @throws IOException
     */
    public static Workbook getWorkbook(InputStream inputStream, String fileType) throws IOException {
        Workbook workbook = null;
        if (fileType.equalsIgnoreCase(XLS)) {
            workbook = new HSSFWorkbook(inputStream);
        } else if (fileType.equalsIgnoreCase(XLSX)) {
            workbook = new XSSFWorkbook(inputStream);
        }
        return workbook;
    }
    /**
     * 提取每一行中需要的数据,构造成为一个结果数据对象
     *
     * @param row
     * @return
     */
    private static InoutRecord convertRowToData(Row row) throws Exception {
        InoutRecord resultData = new InoutRecord();
        Cell cell;
        int cellNum = 0;
        //第一列序号,不做处理
        cell = row.getCell(cellNum++);
        //日期
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setCustomerName("");
        }else {
            cell.setCellType(CellType.STRING);
            String time = cell.getStringCellValue().trim().replaceAll(" ","");
            if(StringUtils.isNotEmpty(time)){
                resultData.setRegisterTime(DateUtils.parseDate(time,"yyyy-MM-dd"));
            }
        }
        //类型
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setType("");
        }else {
            cell.setCellType(CellType.STRING);
            String type = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(type)){
                resultData.setType(type);
            }
        }
        //车牌号
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPlateNum("");
        }else {
            cell.setCellType(CellType.STRING);
            String plateNum = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(plateNum)){
                resultData.setPlateNum(plateNum);
            }
        }
        //承运人
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setUserName("");
        }else {
            cell.setCellType(CellType.STRING);
            String userName = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(userName)){
                resultData.setUserName(userName);
            }
        }
        //往来单位
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setCustomerName("");
        }else {
            cell.setCellType(CellType.STRING);
            String customerName = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(customerName)){
                resultData.setCustomerName(customerName);
            }
        }
        //通知单编码
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setNoticeId("");
        }else {
            cell.setCellType(CellType.STRING);
            String noticeId = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(noticeId)){
                resultData.setNoticeId(noticeId);
            }
        }
        //装卸仓库
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setDepotId("");
        }else {
            cell.setCellType(CellType.STRING);
            String depotName = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(depotName)){
                resultData.setDepotId(depotName);
            }
        }
        //粮食品种
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setFoodVariety("");
        }else {
            cell.setCellType(CellType.STRING);
            String foodVariety = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(foodVariety)){
                resultData.setFoodVariety(FoodVariety.getCode(foodVariety.trim()));
            }
        }
        //粮食年份
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setFoodYear("");
        }else {
            cell.setCellType(CellType.STRING);
            String year = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(year)){
                resultData.setFoodYear(year);
            }
        }
        //满车重量
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setFullWeight(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String fullWeight = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(fullWeight)){
                resultData.setFullWeight(Double.valueOf(fullWeight));
            }
        }
        //空车重量
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setEmptyWeight(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String emptyWeight = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(emptyWeight)){
                resultData.setEmptyWeight(Double.valueOf(emptyWeight));
            }
        }
        //扣重
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setDeOther(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String deOther = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(deOther)){
                resultData.setDeOther(Double.valueOf(deOther));
            }
        }
        //结算重量
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setSettleWeight(0.0);
            resultData.setRecordWeight(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String recordWeight = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(recordWeight)){
                resultData.setSettleWeight(Double.valueOf(recordWeight));
                resultData.setRecordWeight(Double.valueOf(recordWeight));
            }
        }
        //单价
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPrice(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String price = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(price)){
                resultData.setPrice(Double.valueOf(price));
            }
        }
        //水分
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPerWet(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String perWet = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(perWet)){
                resultData.setPerWet(Double.valueOf(perWet));
            }
        }
        //杂质
        cell = row.getCell(cellNum++);
        if(cell == null || cell.getCellType() == CellType.BLANK){
            resultData.setPerImpurity(0.0);
        }else {
            cell.setCellType(CellType.STRING);
            String perImpurity = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(perImpurity)){
                resultData.setPerImpurity(Double.valueOf(perImpurity));
            }
        }
        //备注
        cell = row.getCell(cellNum++);
        if(null ==  cell || cell.getCellType() == CellType.BLANK){
            resultData.setRemarks("");
        }else {
            cell.setCellType(CellType.STRING);
            String remarks = cell.getStringCellValue().trim();
            if(StringUtils.isNotEmpty(remarks)){
                resultData.setRemarks(remarks);
            }
        }
        if(null != resultData.getFullWeight() && null != resultData.getEmptyWeight()){
            resultData.setNetWeight(resultData.getFullWeight() - resultData.getEmptyWeight());
        }
        return resultData;
    }
}