package com.yihu.jw.base.service.word;

import com.yihu.jw.base.endpoint.common.excel.AExcelReader;
import com.yihu.jw.base.service.word.excelImport.BaseSensitiveExcelDO;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import java.util.HashSet;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2020/7/7
 */
@Component
public class BaseSensitiveExcelReader extends AExcelReader {

    @Override
    public void read(Workbook rwb) throws Exception {
        try{
            Sheet sheetAt = rwb.getSheetAt(0);
//            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;
            BaseSensitiveExcelDO baseSensitiveExcelDO;
            getRepeat().put("id", new HashSet<>());
            getRepeat().put("sensitiveWords", new HashSet<>());
            getRepeat().put("explanation", new HashSet<>());
//            while (sheets.hasNext()){
//                Sheet sheet = sheets.next();

                if ((rows = sheetAt.getLastRowNum()) == 0) {
                   return;
                }
            correctLs.clear();
                for (int i = 1; i <= rows; i++) {
                    baseSensitiveExcelDO = new BaseSensitiveExcelDO();
                    baseSensitiveExcelDO.setId(getCellCont(sheetAt, i, 0));
                    baseSensitiveExcelDO.setSensitiveWord(getCellCont(sheetAt, i, 1));
                    baseSensitiveExcelDO.setExplanation(getCellCont(sheetAt, i, 2));
                    baseSensitiveExcelDO.validate(repeat);
                    correctLs.add(baseSensitiveExcelDO);
                    errorLs.add(baseSensitiveExcelDO);
                }
                j++;
//            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }
}
