package com.jeeplus.test.utils.excel;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jeeplus.common.beanvalidator.BeanValidators;
import com.jeeplus.core.excel.ExcelListener;
import com.jeeplus.core.mapstruct.EntityWrapper;
import com.jeeplus.test.wdt.merchantCompany.service.MerchantCompanyService;
import com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.List;

@Slf4j
public class MerchantCompanyExcelListener extends ExcelListener <MerchantCompanyDTO> {

    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     *
     * @param service
     */
    public MerchantCompanyExcelListener(IService service, EntityWrapper wrapper) {
        super ( service, wrapper );
    }


    /**
     * 加上存储数据库
     */
    public void saveData() {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder ( );
            List <MerchantCompanyDTO> list = this.getCachedDataList ( );
            for (MerchantCompanyDTO merchantCompany : list) {
                //获取商户信息
                MerchantCompanyDTO byCompanyName = SpringUtil.getBean(MerchantCompanyService.class).getByCompanyName(merchantCompany.getCompanyName());
                try {
                    if ( null == byCompanyName ) {
                        Validator validator = SpringUtil.getBean ( Validator.class );
                        BeanValidators.validateWithException ( validator, merchantCompany );
                        ((MerchantCompanyService) this.getService ( )).saveOrUpdate ( merchantCompany );
                        successNum++;
                    } else {
                        failureMsg.append ( "<br/>公司名称 " + byCompanyName.getCompanyName ( ) + " 已存在; " );
                        failureNum++;
                    }
                } catch (ConstraintViolationException ex) {
                    failureMsg.append ( "<br/>公司名称 " + merchantCompany.getCompanyName ( ) + " 导入失败：" );
                    List <String> messageList = BeanValidators.extractPropertyAndMessageAsList ( ex, ": " );
                    for (String message : messageList) {
                        failureMsg.append ( message + "; " );
                        failureNum++;
                    }
                } catch (Exception ex) {
                    failureNum++;
                    failureMsg.append ( "<br/>公司名称 " + merchantCompany.getCompanyName ( )+ " 导入失败：" + ex.getMessage ( ) );
                }
                if ( failureNum > 0 ) {
                    failureMsg.insert ( 0, "，失败 " + failureNum + " 条公司信息，导入信息如下：" );
                }
            }
            this.setMessage ( "已成功导入 " + successNum + " 条公司信息" + failureMsg );
            log.info ( this.getMessage ( ) );
        } catch (Exception e) {
            e.printStackTrace ( );
            this.setMessage ( "导入公司信息失败！失败信息：" + e.getMessage ( ) );
            log.info ( this.getMessage ( ) );
        }
    }
}
