package com.rongwei.bscommon.sys.utils;

import cn.hutool.core.util.ObjectUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
@Component
public class FileCheckUtil {

    @Autowired
    private BusinessExcelUtil excelUtil;

    public String fileCheckDetail(MultipartFile file,String pageId) {
        String errorMsg = "";
        String fileName = file.getOriginalFilename();
        if (!fileName.matches("^.+\\.(?i)(xlsx)$")) {
            errorMsg = "当前只支持xlsx文件格式，请下载模板后重新上传！";
        }
        Map<String,Object> map = new HashMap<>(16);
        map.put("pageid",pageId);
        // 获取模板文件
        XSSFWorkbook wb = excelUtil.getTemplate(map);
        if(ObjectUtil.isNull(wb)){
            errorMsg = "请先上传模板文件！";
        }
        // 解析上传的文件
        List<String> colums = excelUtil.getColums(file);
        String errMsg = excelUtil.checkFile(colums,wb);
        if(StringUtils.isNotBlank(errMsg)){
            errorMsg = errMsg;
        }
        return errorMsg;
    }

}
