package com.sw.mes.store.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.excel.ExcelListener;
import com.sw.mes.core.excel.ExcelResult;
import com.sw.mes.core.utils.CodeSenderUtils;
import com.sw.mes.core.utils.SpringUtils;
import com.sw.mes.store.dto.WarehouseInputRecordBillDetailDto;
import com.sw.mes.store.entity.WarehouseInputRecordBill;
import com.sw.mes.store.entity.WarehouseInputRecordBillDetail;
import com.sw.mes.store.service.WarehouseInputRecordBillDetailService;
import com.sw.mes.store.service.WarehouseInputRecordBillService;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

public class OutPutImportListener extends AnalysisEventListener<WarehouseInputRecordBillDetailDto> implements ExcelListener<WarehouseInputRecordBillDetailDto> {
    private WarehouseInputRecordBillService warehouseInputRecordBillService;
    private WarehouseInputRecordBillDetailService warehouseInputRecordBillDetailService;
    private final Boolean isUpdateSupport;

    private final String operName;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();
    private int successNum = 0;
    private int failureNum = 0;
    public OutPutImportListener(Boolean isUpdateSupport){
        this.warehouseInputRecordBillService = SpringUtils.getBean(WarehouseInputRecordBillService.class);
        this.warehouseInputRecordBillDetailService = SpringUtils.getBean(WarehouseInputRecordBillDetailService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operName = BaseContextHandler.getUsername();
    }

    @Override
    public void invoke(WarehouseInputRecordBillDetailDto warehouseInputRecordBillDetailDto, AnalysisContext analysisContext) {
        try {
            WarehouseInputRecordBillDetail entity = new WarehouseInputRecordBillDetail();
            BeanUtils.copyProperties(warehouseInputRecordBillDetailDto,entity);
            this.warehouseInputRecordBillDetailService.saveOrUpdate(entity);
            successNum++;
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、 物料编码：" + warehouseInputRecordBillDetailDto.getMaterialModelCode() + "导入失败：";
            failureMsg.append(msg).append(e.getMessage());
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public ExcelResult<WarehouseInputRecordBillDetailDto> getExcelResult() {
        return new ExcelResult<WarehouseInputRecordBillDetailDto>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WarehouseInputRecordBillDetailDto> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
