package com.dsk.component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.common.excel.ExcelUtils;
import com.dsk.common.exception.ServiceException;
import com.dsk.cscec.constant.CbProjectConstants;
import com.dsk.cscec.domain.CbProjectFile;
import com.dsk.cscec.domain.CbQuantitySummary;
import com.dsk.cscec.domain.bo.CbProjectBaseBo;
import com.dsk.cscec.service.CbProjectFileService;
import com.dsk.cscec.service.ICbQuantitySummaryActualService;
import com.dsk.cscec.service.ICbQuantitySummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.FileInputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据解析组件
 *
 * @Author lcl
 * @Data 2024/2/7 17:43
 */
@Component
public class DataAnalysisComponent {

    @Autowired
    private ICbQuantitySummaryService quantitySummaryService;

    /**
     * 工料汇总数据解析
     */
    @Async
    public void quantitySummaryDataAnalysis(CbProjectBaseBo bo) throws Exception {
        //查询工料汇总导入文件
        List<CbProjectFile> fileList = projectFileService.list(Wrappers.<CbProjectFile>lambdaQuery()
                .eq(CbProjectFile::getProjectId, bo.getProjectId())
                .eq(CbProjectFile::getCbType, CbProjectConstants.CB_TYPE_QUANTITY_SUMMARY)
                .eq(CbProjectFile::getCbStage, bo.getCbStage()));
        if (ObjectUtils.isEmpty(fileList)) return;
        //文件处理
        for (CbProjectFile file : fileList) {
            if (file.getDelFlag() == 0) {
                if (file.getFileParseStatus() == 3) break;
                //文件下载
                //解析数据
                List<CbQuantitySummary> importList = new ExcelUtils<>(CbQuantitySummary.class).importExcelAllSheet(new FileInputStream(""), 1);
                if (importList.isEmpty()) {
                    throw new ServiceException("表格中不存在待导入数据!");
                }
                importList = importList.stream().parallel()
                        .filter(item -> !ObjectUtils.isEmpty(item.getCbName()))
                        .peek(item -> {
                            item.setProjectId(1L);
                            item.setCbStage(0);
                            item.setCbProjectFileId(1L);
                        }).collect(Collectors.toList());
                if (importList.isEmpty()) {
                    throw new ServiceException("表格中不存在有效数据数据!");
                }
                //分批次插入
                if (importList.size() > 1000) {
                    int index = 0;
                    int sum = importList.size();
                    while (index < sum) {
                        List<CbQuantitySummary> divideList = importList.subList(index, Math.max((index + 1) * 1000, sum));
                        boolean b = quantitySummaryService.saveBatch(divideList);
                        if (!b) {
                            throw new ServiceException("数据插入失败！");
                        }
                        index += 1000;
                    }
                } else {
                    boolean b = quantitySummaryService.saveBatch(importList);
                    if (!b) {
                        throw new ServiceException("数据插入失败！");
                    }
                }
            } else {
                quantitySummaryService.remove(Wrappers.<CbQuantitySummary>lambdaQuery().eq(CbQuantitySummary::getCbProjectFileId, file.getId()));
            }
        }
    }

    @Autowired
    private CbProjectFileService projectFileService;
}
