package com.propertyManagement.business.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.propertyManagement.business.domain.WxCommunity;
import com.propertyManagement.business.domain.WxProprietorLedger;
import com.propertyManagement.business.domain.vo.WxProprietorLedgerVo;
import com.propertyManagement.business.mapper.WxCommunityMapper;
import com.propertyManagement.business.mapper.WxProprietorLedgerMapper;
import com.propertyManagement.common.excel.ExcelListener;
import com.propertyManagement.common.excel.ExcelResult;
import com.propertyManagement.common.exception.ServiceException;
import com.propertyManagement.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 业主台账自定义导入
 *
 * @author liushuai
 */
@Slf4j
public class WxProprietorLedgerImportListener extends AnalysisEventListener<WxProprietorLedgerVo> implements ExcelListener<WxProprietorLedgerVo> {

    private final WxProprietorLedgerMapper proprietorLedgerMapper;

    private final WxCommunityMapper communityMapper;

    private final Boolean isUpdateSupport;

    private final Long communityId;

    private final String communityName;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WxProprietorLedgerImportListener(Long communityId, Boolean isUpdateSupport) {
        this.proprietorLedgerMapper = SpringUtils.getBean(WxProprietorLedgerMapper.class);
        this.communityMapper = SpringUtils.getBean(WxCommunityMapper.class);
        WxCommunity community = communityMapper.selectById(communityId);
        this.isUpdateSupport = isUpdateSupport;
        this.communityId = communityId;
        this.communityName = community.getCommunityName();
    }

    @Override
    public void invoke(WxProprietorLedgerVo proprietorLedgerVo, AnalysisContext context) {
        LambdaQueryWrapper<WxProprietorLedger> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WxProprietorLedger::getCommunityId, communityId);
        lqw.eq(WxProprietorLedger::getName, proprietorLedgerVo.getName());
        lqw.eq(WxProprietorLedger::getRoom, proprietorLedgerVo.getRoom());
        WxProprietorLedger proprietorLedger = proprietorLedgerMapper.selectOne(lqw);
        try {
            if (ObjectUtil.isNull(proprietorLedger)) {
                WxProprietorLedger wxProprietorLedger = BeanUtil.toBean(proprietorLedgerVo, WxProprietorLedger.class);
                wxProprietorLedger.setCommunityId(communityId);
                wxProprietorLedger.setCommunityName(communityName);
                proprietorLedgerMapper.insert(wxProprietorLedger);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、业主台账 ").append(wxProprietorLedger.getName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                BeanUtil.copyProperties(proprietorLedgerVo, proprietorLedger);
                proprietorLedgerMapper.updateById(proprietorLedger);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、业主台账 ").append(proprietorLedger.getName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、业主台账 ").append(proprietorLedger.getName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、业主台账 " + proprietorLedger.getName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

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

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

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