package com.bweb.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bweb.model.ExportOutRechargeOrder;
import com.bweb.model.ImportCouponModel;
import com.hai.common.utils.DateUtil;
import com.hai.entity.OutRechargeOrder;
import com.hai.service.OutRechargeOrderService;
import org.apache.commons.lang3.StringUtils;

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

public class ImportRechargeOrderListener extends AnalysisEventListener<ExportOutRechargeOrder> {

    List<ExportOutRechargeOrder> errorData = new ArrayList<>();
    List<OutRechargeOrder> successData = new ArrayList<>();

    private OutRechargeOrderService outRechargeOrderService;

    /**
    * @Author Sum1Dream
    * @name invoke.java
    * @Description // 解析到的每一条数据都会调用此方法
    * @Date 4:03 下午 2022/1/24
    * @Param [com.bweb.model.ExportOutRechargeOrder, com.alibaba.excel.context.AnalysisContext]
    * @return void
    */
    @Override
    public void invoke(ExportOutRechargeOrder data, AnalysisContext context) {
        if (StringUtils.isBlank(data.getOrderNo())
                || StringUtils.isBlank(data.getRechargeStatusName()) ) {
            data.setErrorMessage("有必填项未填写");
            errorData.add(data);
            return;
        }

        OutRechargeOrder rechargeOrder = outRechargeOrderService.findByOrderNo(data.getOrderNo());

        if (Objects.equals(data.getRechargeStatusName(), "成功")) {
            outRechargeOrderService.finishOrder(rechargeOrder.getId());
        } else if (Objects.equals(data.getRechargeStatusName(), "失败")){
            try {
                outRechargeOrderService.rechargeOrderToRefund(rechargeOrder.getId());
            } catch (Exception exception) {
                data.setErrorMessage("退款失败。");
                errorData.add(data);
                return;
            }
        }


        successData.add(rechargeOrder);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (successData.size() > 0) {
            try {
//                outRechargeOrderService.updateOrderList(successData);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.successData.clear();
    }

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

    public void initData(OutRechargeOrderService outRechargeOrderService) {
        this.outRechargeOrderService = outRechargeOrderService;
    }
}
