package org.jeecg.modules.excelutil;

import cn.com.ctop.common.module.utils.SpringUtils;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.ctop.service.impl.BytedanceCustomerDdTbtjDataServiceImpl;
import org.jeecg.modules.excelutil.entity.BytedanceCustomTbtjDataEntity;

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

public class BytedanceCustomTbtjDataExcelListener extends AnalysisEventListener<BytedanceCustomTbtjDataEntity> {
    //自定义用于暂时存储data。
    //可以通过实例获取该值
    private List<BytedanceCustomTbtjDataEntity> datas = new ArrayList<>();
    private static final int BATCH_COUNT = 200;

    @Override
    public void invoke(BytedanceCustomTbtjDataEntity object, AnalysisContext context) {
        //数据存储到list，供批量处理，或后续自己业务逻辑处理。
        Date date = object.getStateDate();
        object.setDateString(DateUtils.formatDate(date,"yyyy-MM-dd HH:mm:ss"));
        datas.add(object);
        if(null!=datas&&datas.size()>=BATCH_COUNT){
            saveDate();
            datas.clear();
        }
    }

    private void saveDate() {
        BytedanceCustomerDdTbtjDataServiceImpl dataService = SpringUtils.getApplicationContext().getBean(BytedanceCustomerDdTbtjDataServiceImpl.class);
        dataService.replaceEntityList(datas);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 解析结束销毁不用的资源
         datas.clear();
    }

    public List<BytedanceCustomTbtjDataEntity> getDatas() {
        return datas;
    }

    public void setDatas(List<BytedanceCustomTbtjDataEntity> datas) {
        this.datas = datas;
    }
}
