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.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.propertyManagement.business.domain.WxCarport;
import com.propertyManagement.business.domain.WxCommunity;
import com.propertyManagement.business.domain.WxProprietorLedger;
import com.propertyManagement.business.domain.vo.WxCarportVo;
import com.propertyManagement.business.domain.vo.WxProprietorLedgerVo;
import com.propertyManagement.business.mapper.WxCarportMapper;
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 WxCarportImportListener extends AnalysisEventListener<WxCarportVo> implements ExcelListener<WxCarportVo> {

    private final WxProprietorLedgerMapper proprietorLedgerMapper;

    private final WxCarportMapper carportMapper;

    private final WxCommunityMapper communityMapper;

    private final Boolean isUpdateSupport;

    private final Long communityId;

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

    public WxCarportImportListener(Long communityId, Boolean isUpdateSupport) {
        this.proprietorLedgerMapper = SpringUtils.getBean(WxProprietorLedgerMapper.class);
        this.carportMapper = SpringUtils.getBean(WxCarportMapper.class);
        this.communityMapper = SpringUtils.getBean(WxCommunityMapper.class);
        this.isUpdateSupport = isUpdateSupport;
        this.communityId = communityId;
    }

    @Override
    public void invoke(WxCarportVo carportVo, AnalysisContext context) {
        LambdaQueryWrapper<WxCarport> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WxCarport::getCommunityId, carportVo.getCommunityId());
        lqw.eq(WxCarport::getProprietorName, carportVo.getProprietorName());
        lqw.eq(WxCarport::getCarportNumber, carportVo.getCarportNumber());
        WxCarport wxCarport = carportMapper.selectOne(lqw);
        try {
            if (ObjectUtil.isNull(wxCarport)) {
                LambdaQueryWrapper<WxProprietorLedger> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper
                    .eq(WxProprietorLedger::getCommunityId, communityId)
                    .eq(WxProprietorLedger::getName, carportVo.getProprietorName())
                    .eq(WxProprietorLedger::getRoom, carportVo.getRoom());
                List<WxProprietorLedger> proprietorLedgerList = proprietorLedgerMapper.selectList(queryWrapper);
                WxProprietorLedger proprietorLedger = proprietorLedgerList.get(0);
                WxCarport carport = BeanUtil.toBean(carportVo, WxCarport.class);
                carport.setCommunityId(proprietorLedger.getCommunityId());
                carport.setCommunityName(proprietorLedger.getCommunityName());
                carport.setProprietorId(proprietorLedger.getProprietorId());
                carport.setProprietorPhone(proprietorLedger.getPhone());
                carportMapper.insert(carport);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、业主车位 ").append(carport.getCarportNumber()).append(" 导入成功");
            } else if (isUpdateSupport) {
                BeanUtil.copyProperties(carportVo, wxCarport);
                carportMapper.updateById(wxCarport);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、业主车位 ").append(wxCarport.getCarportNumber()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、业主车位 ").append(wxCarport.getCarportNumber()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、业主车位 " + wxCarport.getCarportNumber() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

            @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<WxCarportVo> getList() {
                return null;
            }

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