package com.bweb.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bweb.model.ImportStoreModel;
import com.hai.entity.*;
import com.hai.model.HighMerchantStoreModel;
import com.hai.model.UserInfoModel;
import com.hai.service.CommonService;
import com.hai.service.HighMerchantStoreService;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/26 19:28
 */
public class ImportStoreListener extends AnalysisEventListener<ImportStoreModel>  {

    List<ImportStoreModel> errorData = new ArrayList<>();
    List<HighMerchantStoreModel> successData = new ArrayList<>();
    List<HighMerchantStore> allStoreData = new ArrayList<>();

    HighMerchantStoreModel highMerchantStore;
    HighMerchant highMerchant;
    UserInfoModel userInfoModel;

    private HighMerchantStoreService highMerchantStoreService;
    private CommonService commonService;

    /**
     * @Author 胡锐
     * @Description 解析到的每一条数据都会调用此方法
     * @Date 2021/3/20 21:08
     **/
    @Override
    public void invoke(ImportStoreModel importStoreModel, AnalysisContext analysisContext) {
        if (StringUtils.isBlank(importStoreModel.getStoreName())
            || StringUtils.isBlank(importStoreModel.getStoreKey())
            || StringUtils.isBlank(importStoreModel.getRegionId())
            || StringUtils.isBlank(importStoreModel.getAddress())
            || StringUtils.isBlank(importStoreModel.getLatitude())
            || StringUtils.isBlank(importStoreModel.getLongitude())
        ) {
            importStoreModel.setErrorMessage("有必填项未填写");
            errorData.add(importStoreModel);
            return;
        }

        // 查询区域信息
        String regionName = this.commonService.getRegionName(Long.parseLong(importStoreModel.getRegionId()));
        if (StringUtils.isBlank(regionName)) {
            importStoreModel.setErrorMessage("【"+importStoreModel.getRegionId()+"】,未知的区域id");
            errorData.add(importStoreModel);
            return;
        }

        // 根据门店编码进行校验
        List<HighMerchantStore> collect = allStoreData.stream().filter(o -> o.getStoreKey().equals(importStoreModel.getStoreKey())).collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            importStoreModel.setErrorMessage("门店编号已存在");
            errorData.add(importStoreModel);
            return;
        }

        highMerchantStore = new HighMerchantStoreModel();
        highMerchantStore.setStoreName(importStoreModel.getStoreName());
        highMerchantStore.setStoreKey(importStoreModel.getStoreKey());
        highMerchantStore.setAddress(importStoreModel.getAddress());
        highMerchantStore.setLatitude(importStoreModel.getLatitude());
        highMerchantStore.setLongitude(importStoreModel.getLongitude());
        highMerchantStore.setRegionId(Long.parseLong(importStoreModel.getRegionId()));
        highMerchantStore.setRegionName(regionName);
        highMerchantStore.setCompanyId(highMerchant.getCompanyId());
        highMerchantStore.setMerchantId(highMerchant.getId());
        highMerchantStore.setOperatorId(userInfoModel.getSecUser().getId());
        highMerchantStore.setOperatorName(userInfoModel.getSecUser().getUserName());
        highMerchantStore.setStatus(1);
        highMerchantStore.setCreateTime(new Date());
        highMerchantStore.setUpdateTime(new Date());
        highMerchantStore.setPrestoreType(0);

        successData.add(highMerchantStore);
        allStoreData.add(highMerchantStore);


    }

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

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

    public void initData(HighMerchant highMerchant, UserInfoModel userInfoModel, HighMerchantStoreService highMerchantStoreService, CommonService commonService){
        this.highMerchant = highMerchant;
        this.userInfoModel = userInfoModel;
        this.highMerchantStoreService = highMerchantStoreService;
        this.commonService = commonService;
        this.allStoreData = this.highMerchantStoreService.getMerchantStoreList(new HashMap<>());
    }

}
