package org.eco.common.excel.core;

import cn.hutool.core.util.StrUtil;
import lombok.Setter;
import org.eco.common.excel.entity.ExcelResultRes;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认excel返回对象
 *
 * @author wgk
 */
@Setter
public class DefaultExcelResult<T> implements ExcelResult<T> {

    /**
     * 数据对象list
     */
    private List<T> list;

    /**
     * 错误信息列表
     */
    private List<String> errorList;

    public DefaultExcelResult() {
        this.list = new ArrayList<>();
        this.errorList = new ArrayList<>();
    }

    public DefaultExcelResult(List<T> list, List<String> errorList) {
        this.list = list;
        this.errorList = errorList;
    }

    public DefaultExcelResult(ExcelResult<T> excelResult) {
        this.list = excelResult.getList();
        this.errorList = excelResult.getErrorList();
    }

    @Override
    public List<T> getList() {
        return list;
    }

    @Override
    public List<String> getErrorList() {
        return errorList;
    }

    /**
     * 获取导入回执
     *
     * @return 导入回执
     */
    @Override
    public ExcelResultRes getAnalysis() {
        int successCount = list.size();
        int errorCount = errorList.size();
        if (successCount == 0) {
            return ExcelResultRes.builder().logInfo("读取失败，未解析到数据").status("0").build();
        } else {
            if (errorCount == 0) {
                return ExcelResultRes.builder().logInfo(StrUtil.format("恭喜您，全部读取成功！共{}条", successCount)).status("1").build();
            } else {
                return ExcelResultRes.builder().build();
            }
        }
    }
}
