package org.dromara.payment.bill.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.core.IExcelListener;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.payment.bill.domain.bo.AddBillBo;
import org.dromara.payment.bill.domain.bo.MerBillBo;
import org.dromara.payment.bill.domain.bo.MerBillDetailBo;
import org.dromara.payment.bill.service.IMerBillService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;

/**
 * 派单批量打款导入
 *
 * @author Lion Li
 */
@Slf4j
public class BillImportListener extends AnalysisEventListener<Map<Integer, String>> implements IExcelListener<Map<Integer, String>,MerBillDetailBo> {

    private final IMerBillService billService;

    private AddBillBo bo;

    private ExcelResult<MerBillDetailBo> result;

    private int row = 0;

    public BillImportListener(AddBillBo bo) {
//        this.bo = MapstructUtils.convert(bo,AddBillBo.class);
        this.bo = bo;
        this.billService = SpringUtils.getBean(IMerBillService.class);
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        try {
            if(row == 1){
                bo.setNo(data.get(1));
            }else if (row > 2){
                if(StringUtils.isNotBlank(data.get(0)) && StringUtils.isNotBlank(data.get(1)) && StringUtils.isNotBlank(data.get(2))){
                    int size = data.size();
                    if(bo.getDetails() == null){
                        bo.setDetails(new ArrayList<>());
                    }
                    if(bo.getDetails().size() >= 500){
                            throw new ServiceException("单批次最大支持500条订单");
                    }
                    MerBillDetailBo detail = new MerBillDetailBo();
                    detail.setWorkerName(data.get(0));
                    detail.setCardId(data.get(1));
                    detail.setPassage(this.bo.getPassage());
                    detail.setMoneyDecimal(new BigDecimal(data.get(2)));
                    if(size > 3){
                        detail.setBankCard(data.get(3));
                    }
                    if(size > 4){
                        detail.setBankType(data.get(4));
                    }
                    bo.getDetails().add(detail);
                }
            }
        } catch (Exception e) {
            log.error("第"+(row+2)+"行读取excel出错（"+ JsonUtils.toJsonString(data) +"）：", e);
            throw  e;
        }
        row++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        result = billService.onlinePaymentImport(bo);
    }

    @Override
    public ExcelResult<MerBillDetailBo> getExcelResult() {
        return result;
    }
}
