# SalesKpiOperatingStatementParseServiceByM3.java

    package cloud.wconnect.workperformance.service.saleskpi.accounting;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.SystemPlatformEnum;
    import cloud.wconnect.commonlib.exception.InternalServerErrorException;
    import cloud.wconnect.commonlib.exception.ParseReportException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.CellUtils;
    import cloud.wconnect.commonlib.util.FileCheckUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.UploadUtils;
    import cloud.wconnect.workperformance.pojo.bo.saleskpi.OperatingStatementBO;
    import cloud.wconnect.workperformance.service.saleskpi.ISalesKpiMonthlyService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import org.apache.commons.lang3.StringUtils;
    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.ss.usermodel.WorkbookFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.IOException;
    import java.io.InputStream;
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    import java.util.List;

    /**
     * 服务实现类：报告处理
     *
     * @author zc
     * @date 2023/09/09
     */
    @Component
    @DS("work_performance")
    public class SalesKpiOperatingStatementParseServiceByM3 implements ISalesKpiOperatingStatementParseService {
        @Resource
        private ISalesKpiMonthlyService salesKpiMonthlyService;

        @Value("${file-max-size.import-report.sales-kpi}")
        private long fileMaxSize;

        /**
         * 判断是否支持
         *
         * @param source 数据源
         * @return boolean
         * @author zc
         * @date 2024/8/21
         */
        @Override
        public boolean supports(String source) {
            return SystemPlatformEnum.M3.getDesc().equals(source);
        }

        /**
         * 校验文件内容是否合法
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @author zc
         * @date 2024/8/21
         */
        @Override
        public void checkFile(PropertyDTO propertyDTO, MultipartFile file) {
            // 限制文件大小
            FileCheckUtils.checkFileSize(file, fileMaxSize);
            // 校验文件格式
            UploadUtils.checkFileFormat(file, Lists.newArrayList("xls", "xlsx"));
        }

        /**
         * 处理数据
         *
         * @param propertyDTO 资产
         * @param file        文件
         * @param date        报告数据日期(yyyyMM)
         * @author zc
         * @date 2024/8/21
         */
        @Override
        public void handleData(PropertyDTO propertyDTO, MultipartFile file, long date) {
            try (InputStream in = file.getInputStream()) {
                // 解析报告数据
                List<OperatingStatementBO> operatingStatementList = parse(propertyDTO, in);
                // 处理报告数据
                salesKpiMonthlyService.handleAccountingReport(propertyDTO.getId(), date, operatingStatementList);
            } catch (Exception e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle import Sales KPI M3 report error.", e);
            }
        }

        /**
         * 解析报告
         *
         * @param propertyDTO 资产
         * @param in          报告
         * @return OperatingStatementBO 集合
         * @author zc
         * @date 2023/9/9
         */
        @Override
        public List<OperatingStatementBO> parse(PropertyDTO propertyDTO, InputStream in) throws ParseReportException {
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(propertyDTO
                    .getInternationalization().getNumber().getCalculationRule());

            try (Workbook workbook = WorkbookFactory.create(in)) {
                Sheet sheet = workbook.getSheetAt(0);
                int lastRowNum = sheet.getLastRowNum();

                List<OperatingStatementBO> fileDataBOList = Lists.newArrayListWithExpectedSize(lastRowNum);
                for (int rowIndex = 0; rowIndex <= lastRowNum; rowIndex++) {
                    Row row = sheet.getRow(rowIndex);
                    String segment = CellUtils.convertCellStr(row.getCell(6));
                    if (StringUtils.isBlank(segment)) {
                        continue;
                    }

                    String actual = CellUtils.convertCellStr(row.getCell(0));
                    String target = CellUtils.convertCellStr(row.getCell(2));
                    fileDataBOList.add(OperatingStatementBO.builder()
                            .segment(segment)
                            .actual(StringUtils.isBlank(actual) ? null : new BigDecimal(actual)
                                    .setScale(BaseConstant.DATA_PRECISION, roundingMode))
                            .target(StringUtils.isBlank(target) ? null : new BigDecimal(target)
                                    .setScale(BaseConstant.DATA_PRECISION, roundingMode))
                            .build());
                }
                return fileDataBOList;
            } catch (IOException e) {
                throw new InternalServerErrorException(BaseErrorCodeEnum.SYSTEM_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.SYSTEM_ERROR.getCode(),
                        "Handle parse Sales KPI Operating Statement M3 report error", e);
            }
        }
    }