package com.chinaweal.aiccs.aiccs.nonIocalCirculation.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.EOtCase;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.EOtPermit;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.dto.EOTCaseDto;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.entity.dto.EOTPermitDto;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.service.EOtCaseService;
import com.chinaweal.aiccs.aiccs.nonIocalCirculation.service.EOtPermitService;
import com.chinaweal.aiccs.common.util.StringUtils;
import org.springframework.beans.BeanUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lee
 * @since 2021-08-20 3:29
 */
public class EOTPermitExcelListener extends AnalysisEventListener<EOTPermitDto> {
    //Logger

    /**
     * 每隔10条存储数据库，实际使用中可以1000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 1000;
    List<EOtPermit> list = new ArrayList<>();

    private EOtPermitService eOtPermitService;

    public EOTPermitExcelListener(EOtPermitService eOtPermitService){
        this.eOtPermitService = eOtPermitService;
    }

    /**
     * 这个每一条数据解析都会来调用
     * @param eotPermitDto
     * @param analysisContext
     */
    @Override
    public void invoke(EOTPermitDto eotPermitDto, AnalysisContext analysisContext) {
        if (eotPermitDto != null) {  //如果excel不为空
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            if(StringUtils.isNotBlank(eotPermitDto.getValfromStr())){
                LocalDate date = LocalDate.parse(eotPermitDto.getValfromStr(), df);
                eotPermitDto.setValfrom(date);
            }
            if(StringUtils.isNotBlank(eotPermitDto.getValtoStr())){
                LocalDate date = LocalDate.parse(eotPermitDto.getValtoStr(), df);
                eotPermitDto.setValto(date);
            }
            if(StringUtils.isNotBlank(eotPermitDto.getCandateStr())){
                LocalDate date = LocalDate.parse(eotPermitDto.getCandateStr(), df);
                eotPermitDto.setCandate(date);
            }
            if(StringUtils.isNotBlank(eotPermitDto.getRevdateStr())){
                LocalDate date = LocalDate.parse(eotPermitDto.getRevdateStr(), df);
                eotPermitDto.setRevdate(date);
            }
            if(StringUtils.isNotBlank(eotPermitDto.getInvaliddateStr())){
                LocalDate date = LocalDate.parse(eotPermitDto.getInvaliddateStr(), df);
                eotPermitDto.setInvaliddate(date);
            }
            EOtPermit eOtPermit = new EOtPermit();
            BeanUtils.copyProperties(eotPermitDto,eOtPermit);
            eOtPermit.setInsertTime(LocalDateTime.now());
            list.add(eOtPermit);
            //调用service方法，将数据存入数据库中
            // 达到BATCH_COUNT了，需要去存储一次数据库
            if(list.size()>=BATCH_COUNT) {
                saveData();
                list.clear();
            }
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
//        LOGGER.info("所有数据解析完成！");
    }

    private void saveData() {

//        System.out.println(list.size()+"条数据，开始存储数据库！");
//        System.out.println(list);
        eOtPermitService.saveBatch(list);
        for (EOtPermit eOtPermit : list) {
            eOtPermitService.save(eOtPermit);
        }
    }
}
