package com.bweb.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bweb.model.ImportFleetOilCardCarModel;
import com.hai.entity.HighFleetOilCard;
import com.hai.entity.HighFleetOilCardCar;
import com.hai.model.UserInfoModel;
import com.hai.service.HighFleetOilCardCarService;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class ImportFleetOilCardCarListener extends AnalysisEventListener<ImportFleetOilCardCarModel> {

    List<ImportFleetOilCardCarModel> errorData = new ArrayList<>();
    List<HighFleetOilCardCar> successData = new ArrayList<>();

    UserInfoModel userInfoModel;
    HighFleetOilCard fleetOilCard;
    HighFleetOilCardCar fleetOilCardCar;

    private HighFleetOilCardCarService fleetOilCardCarService;

    /**
     * @Author 胡锐
     * @Description 解析到的每一条数据都会调用此方法
     * @Date 2021/3/20 21:08 
     **/
    @Override
    public void invoke(ImportFleetOilCardCarModel fleetOilCardCarModel, AnalysisContext analysisContext) {
        if (StringUtils.isBlank(fleetOilCardCarModel.getContactName())
                || StringUtils.isBlank(fleetOilCardCarModel.getContactPhone())
                || StringUtils.isBlank(fleetOilCardCarModel.getCarLicensePlate())) {
            fleetOilCardCarModel.setErrMsg("有必填项未填写");
            errorData.add(fleetOilCardCarModel);
            return;
        }

        fleetOilCardCar = new HighFleetOilCardCar();
        fleetOilCardCar.setFleetOilCardId(fleetOilCard.getId());
        fleetOilCardCar.setFleetOilCardNo(fleetOilCard.getCardNo());
        fleetOilCardCar.setContactName(fleetOilCardCarModel.getContactName());
        fleetOilCardCar.setContactPhone(fleetOilCardCarModel.getContactPhone());
        fleetOilCardCar.setCarLicensePlate(fleetOilCardCarModel.getCarLicensePlate());
        fleetOilCardCar.setOpUserId(userInfoModel.getSecUser().getId());
        fleetOilCardCar.setOpUserName(userInfoModel.getSecUser().getUserName());
        fleetOilCardCar.setStatus(1);
        successData.add(fleetOilCardCar);
    }

    /**
     * @Author 胡锐
     * @Description 所有数据解析完成了 都会来调用此方法
     * @Date 2021/3/20 21:08 
     **/
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (successData.size() > 0) {
            try {
                fleetOilCardCarService.batchInsert(successData);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.successData.clear();
    }

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

    public void initData(UserInfoModel userInfoModel, HighFleetOilCardCarService fleetOilCardCarService, HighFleetOilCard fleetOilCard){
        this.userInfoModel = userInfoModel;
        this.fleetOilCardCarService = fleetOilCardCarService;
        this.fleetOilCard = fleetOilCard;
    }

}
