package com.red.circle.console.app.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.red.circle.console.app.excel.model.bank.UserBankBalanceReadModel;
import com.red.circle.tool.core.num.ArithmeticUtils;
import com.red.circle.tool.core.text.StringUtils;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 用户工资导入监听器
 *
 * @author zongpubin on 2023/7/11
 */
@Getter
public class UserBankBalanceReadModelExcelImportListener extends
    AnalysisEventListener<UserBankBalanceReadModel> {

  private int rowCount = 0;
  private String sheetName;
  private final List<UserBankBalanceReadModel> dataList = new ArrayList<>();

  @Override
  public void invoke(UserBankBalanceReadModel data, AnalysisContext context) {
    rowCount++;
    UserBankBalanceReadModel newModel = extractedModel(data);
    if (Objects.nonNull(newModel)) {
      dataList.add(data);
    }
  }

  @Override
  public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    ReadSheetHolder readSheetHolder = context.readSheetHolder();
    sheetName = readSheetHolder.getSheetName();
  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
  }

  private UserBankBalanceReadModel extractedModel(UserBankBalanceReadModel data) {
    if (StringUtils.isBlank(data.getSysOrigin())) {
      throw new IllegalArgumentException("第" + rowCount + "行系统不能为空!");
    }

    if (StringUtils.isBlank(data.getUserId())) {
      throw new IllegalArgumentException("第" + rowCount + "行用户ID不能为空!");
    }
    if (!isDigits(data.getUserId())) {
      throw new IllegalArgumentException("第" + rowCount + "行用户ID格式不对，只能有数字!");
    }

    if (StringUtils.isBlank(data.getType())) {
      throw new IllegalArgumentException("第" + rowCount + "行type不能为空!");
    }
    if (!Objects.equals(data.getType(), "0") && !Objects.equals(data.getType(), "1")) {
      throw new IllegalArgumentException("第" + rowCount + "行type格式不对，只能是0和1 !");
    }

    if (StringUtils.isBlank(data.getAmount())) {
      throw new IllegalArgumentException("第" + rowCount + "行金额不能为空!");
    }

    try {
      if (!isNumeric(data.getAmount())) {
        throw new IllegalArgumentException("第" + rowCount + "行金额格式不对!");
      }
      BigDecimal amount = new BigDecimal(data.getAmount());
      if (!ArithmeticUtils.gtZero(amount)) {
        throw new IllegalArgumentException("第" + rowCount + "行金额需要大于0!");
      }
    } catch (Exception ex) {
      // ignore
    }
    return data;
  }

  private boolean isNumeric(String str) {
    Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");
    return pattern.matcher(str).matches();
  }

  /**
   * 校验一个字符串是否只包含纯数字
   */
  private boolean isDigits(String data) {
    String regex = "[0-9]+";
    Pattern pattern = Pattern.compile(regex);
    return pattern.matcher(data).matches();
  }

}

