package com.bweb.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bweb.excel.model.ImportCompleteGasOrderReceiptModel;
import com.hfkj.entity.BsGasOrderReceipt;
import com.hfkj.service.gas.BsGasOrderReceiptService;
import com.hfkj.sysenum.gas.GasOrderReceiptStatusEnum;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Auther: 胡锐
 * @Description: 导入监听器
 * @Date: 2021/3/20 21:03
 */
public class ImportCompleteGasOrderReceiptListener extends AnalysisEventListener<ImportCompleteGasOrderReceiptModel> {
    List<ImportCompleteGasOrderReceiptModel> errorData = new ArrayList<>();
    List<BsGasOrderReceipt> successData = new ArrayList<>();
    private BsGasOrderReceiptService gasOrderReceiptService;

    /**
     * @Author 胡锐
     * @Description 解析到的每一条数据都会调用此方法
     * @Date 2021/3/20 21:08 
     **/
    @Override
    public void invoke(ImportCompleteGasOrderReceiptModel model, AnalysisContext analysisContext) {
        if (StringUtils.isNotBlank(model.getOrderNo())) {
            // 订单开票
            BsGasOrderReceipt receipt = gasOrderReceiptService.getDetailByOrderNo(model.getOrderNo().trim(),GasOrderReceiptStatusEnum.status1);
            if (receipt == null) {
                //model.setErrorMessage("未找到交易单号开票申请数据");
                //errorData.add(model);
                return;
            }
            receipt.setStatus(GasOrderReceiptStatusEnum.status2.getStatus());
            successData.add(receipt);
        }
    }

    /**
     * @Author 胡锐
     * @Description 所有数据解析完成了 都会来调用此方法
     * @Date 2021/3/20 21:08 
     **/
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (!successData.isEmpty()) {
            for (BsGasOrderReceipt receipt : successData) {
                gasOrderReceiptService.success(receipt.getApplyNo());
            }
        }
        this.successData.clear();
    }

    public List<ImportCompleteGasOrderReceiptModel> getErrorData() {
        return errorData;
    }

    public void initData(BsGasOrderReceiptService gasOrderReceiptService) {
        this.gasOrderReceiptService = gasOrderReceiptService;
    }
}
