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.KuaishouChannelRegInfoServiceImpl;
import org.jeecg.modules.excelutil.entity.KuaishouChannelRegInfoEntity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Override
    public void invoke(Object object, AnalysisContext context) {
        //数据存储到list，供批量处理，或后续自己业务逻辑处理。
        datas.add(object);
        //根据自己业务做处理
        doSomething(object);
    }

    private void doSomething(Object object) {
        //入库调用接口
        KuaishouChannelRegInfoEntity regInfo = (KuaishouChannelRegInfoEntity) object;
        regInfo.setCreateTime(new Date());
        regInfo.setUpdateTime(new Date());
        regInfo.setChannelCode(regInfo.getChannelCode() + "_" + regInfo.getChildChannelCode());
        String date = regInfo.getStateDate();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        try {
            date = DateUtils.formatDate(dateFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        regInfo.setStateDate(date);
        KuaishouChannelRegInfoServiceImpl regInfoService = SpringUtils.getApplicationContext().getBean(KuaishouChannelRegInfoServiceImpl.class);
        regInfoService.saveOrUpdateBychannelCodeAndDate(regInfo);
    }

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

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

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