package com.bweb.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bweb.model.ImportCouponModel;
import com.bweb.model.ImportFleetOilCardUserModel;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.MemberValidateUtil;
import com.hai.entity.HighCoupon;
import com.hai.entity.HighFleetOilCard;
import com.hai.entity.HighFleetOilCardUser;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCouponCodeService;
import com.hai.service.HighFleetOilCardService;
import com.hai.service.HighFleetOilCardUserService;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 导入车队油卡客户
 * @className: ImportFleetOilCardUserListener
 * @author: HuRui
 * @date: 2022/12/19
 **/
public class ImportFleetOilCardUserListener extends AnalysisEventListener<ImportFleetOilCardUserModel> {

    List<ImportFleetOilCardUserModel> errorData = new ArrayList<>();
    List<HighFleetOilCardUser> successData = new ArrayList<>();
    List<HighFleetOilCardUser> allData = new ArrayList<>();

    UserInfoModel userInfoModel;
    HighFleetOilCard fleetOilCard;
    HighFleetOilCardUser fleetOilCardUser;

    private HighFleetOilCardUserService fleetOilCardUserService;

    /**
     * @Author 胡锐
     * @Description 解析到的每一条数据都会调用此方法
     * @Date 2021/3/20 21:08 
     **/
    @Override
    public void invoke(ImportFleetOilCardUserModel fleetOilCardUserModel, AnalysisContext analysisContext) {

        if (StringUtils.isBlank(fleetOilCardUserModel.getContactName())
                || StringUtils.isBlank(fleetOilCardUserModel.getContactPhone())) {
            fleetOilCardUserModel.setErrMsg("有必填项未填写");
            errorData.add(fleetOilCardUserModel);
            return;
        }

        if (MemberValidateUtil.validatePhone(fleetOilCardUserModel.getContactPhone()) == false) {
            fleetOilCardUserModel.setErrMsg("客户电话格式校验未通过");
            errorData.add(fleetOilCardUserModel);
            return;
        }

        List<HighFleetOilCardUser> userList = allData.stream()
                            .filter(o -> o.getContactPhone().equals(fleetOilCardUserModel.getContactPhone()))
                            .collect(Collectors.toList());
        if (userList.size() > 0) {
            fleetOilCardUserModel.setErrMsg("客户电话：" + fleetOilCardUserModel.getContactPhone() + " 已存在，请勿重复录入");
            errorData.add(fleetOilCardUserModel);
            return;
        }

        fleetOilCardUser = new HighFleetOilCardUser();
        fleetOilCardUser.setContactName(fleetOilCardUserModel.getContactName());
        fleetOilCardUser.setContactPhone(fleetOilCardUserModel.getContactPhone());
        fleetOilCardUser.setOpUserId(userInfoModel.getSecUser().getId());
        fleetOilCardUser.setOpUserName(userInfoModel.getSecUser().getUserName());
        successData.add(fleetOilCardUser);
        allData.add(fleetOilCardUser);
    }

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

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

    public void initData(UserInfoModel userInfoModel,HighFleetOilCard fleetOilCard, HighFleetOilCardUserService fleetOilCardUserService){
        this.userInfoModel = userInfoModel;
        this.fleetOilCard = fleetOilCard;
        this.fleetOilCardUserService = fleetOilCardUserService;
        this.allData = this.fleetOilCardUserService.getUserListByCard(fleetOilCard.getId());
    }

}
