package com.bweb.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bweb.model.ImportCouponModel;
import com.hai.common.utils.DateUtil;
import com.hai.entity.HighCoupon;
import com.hai.entity.HighCouponCode;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCouponCodeService;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @Auther: 胡锐
 * @Description: 导入监听器
 * @Date: 2021/3/20 21:03
 */
public class ImportCouponListener extends AnalysisEventListener<ImportCouponModel> {

    List<ImportCouponModel> errorData = new ArrayList<>();
    List<HighCouponCode> successData = new ArrayList<>();
    List<HighCouponCode> allCouponCode = new ArrayList<>();
    List<HighCouponCode> allCouponCodeKey = new ArrayList<>();
    HighCouponCode highCouponCode;
    HighCoupon highCoupon;
    UserInfoModel userInfoModel;

    private HighCouponCodeService highCouponCodeService;

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

        List<HighCouponCode> collect = allCouponCode.stream().filter(o -> o.getSalesCode().equals(importCouponModel.getSalesCode())).collect(Collectors.toList());
        if (collect.size() > 0) {
            importCouponModel.setErrorMessage("销售码已存在");
            errorData.add(importCouponModel);
            return;
        }

        if (StringUtils.isNotBlank(importCouponModel.getCodeKey())) {
            List<HighCouponCode> codeList = allCouponCodeKey.stream().filter(o -> o.getCodeKey().equals(importCouponModel.getCodeKey())).collect(Collectors.toList());
            if (codeList.size() > 0) {
                importCouponModel.setErrorMessage("卡券编号id已存在");
                errorData.add(importCouponModel);
                return;
            }
        }

        highCouponCode = new HighCouponCode();
        highCouponCode.setCouponId(highCoupon.getId());
        highCouponCode.setMerchantId(highCoupon.getMerchantId());
        highCouponCode.setCodeKey(importCouponModel.getCodeKey());
        highCouponCode.setSalesCode(importCouponModel.getSalesCode());
        highCouponCode.setIsAssignAgent(false);

        // 销售截止时间
        Calendar salesEndTime = Calendar.getInstance();
        salesEndTime.setTime(DateUtil.format(importCouponModel.getSalesEndTime(), "yyyy/MM/dd"));
        salesEndTime.set(Calendar.HOUR_OF_DAY, 23);
        salesEndTime.set(Calendar.MINUTE, 59);
        salesEndTime.set(Calendar.SECOND, 59);
        highCouponCode.setSalesEndTime(salesEndTime.getTime());

        // 使用截止时间
        Calendar useEndTime = Calendar.getInstance();
        useEndTime.setTime(DateUtil.format(importCouponModel.getUseEndTime(), "yyyy/MM/dd"));
        useEndTime.set(Calendar.HOUR_OF_DAY, 23);
        useEndTime.set(Calendar.MINUTE, 59);
        useEndTime.set(Calendar.SECOND, 59);
        highCouponCode.setUseEndTime(useEndTime.getTime());

        highCouponCode.setStatus(1);
        highCouponCode.setOperatorId(userInfoModel.getSecUser().getId());
        highCouponCode.setOperatorName(userInfoModel.getSecUser().getUserName());
        highCouponCode.setCreateTime(new Date());
        successData.add(highCouponCode);
        allCouponCode.add(highCouponCode);
    }

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

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

    public void initData(HighCoupon highCoupon, UserInfoModel userInfoModel, HighCouponCodeService highCouponCodeService){
        this.highCoupon = highCoupon;
        this.userInfoModel = userInfoModel;
        this.highCouponCodeService = highCouponCodeService;
        this.allCouponCode = this.highCouponCodeService.getCouponCodeList(new HashMap<>());
        this.allCouponCodeKey = allCouponCode.stream().filter(o -> !Objects.equals(o.getCodeKey(), null)).collect(Collectors.toList());
    }

}
