package com.chinaweal.aiccs.aiccs.revoke.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.revoke.entity.AnnouncementTaskListDetail;
import com.chinaweal.aiccs.aiccs.revoke.entity.dto.AnnouncementTaskListDetailImportDto;
import com.chinaweal.aiccs.aiccs.revoke.service.AnnouncementTaskListDetailService;
import com.chinaweal.aiccs.common.util.StringUtils;
import org.springframework.beans.BeanUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 公告任务名单详细名录导入监听器
 *
 * @author system
 * @since 2026/2/27
 */
public class AnnouncementTaskListDetailExcelListener extends AnalysisEventListener<AnnouncementTaskListDetailImportDto> {

    /**
     * 每隔100条存储数据库
     */
    private static final int BATCH_COUNT = 100;

    private List<AnnouncementTaskListDetail> list = new ArrayList<>();

    private AnnouncementTaskListDetailService announcementTaskListDetailService;

    private EBaseinfoService eBaseinfoService;

    private String taskListId;

    private String createUserId;

    public AnnouncementTaskListDetailExcelListener(AnnouncementTaskListDetailService announcementTaskListDetailService,
                                                   EBaseinfoService eBaseinfoService,
                                                   String taskListId,
                                                   String createUserId) {
        this.announcementTaskListDetailService = announcementTaskListDetailService;
        this.eBaseinfoService = eBaseinfoService;
        this.taskListId = taskListId;
        this.createUserId = createUserId;
    }

    /**
     * 每一条数据解析都会来调用
     *
     * @param importDto       导入DTO
     * @param analysisContext 分析上下文
     */
    @Override
    public void invoke(AnnouncementTaskListDetailImportDto importDto, AnalysisContext analysisContext) {
        if (importDto != null) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            if (StringUtils.isNotBlank(importDto.getRevoketimeStr())) {
                LocalDate date = LocalDate.parse(importDto.getRevoketimeStr(), df);
                importDto.setRevoketime(date);
            }

            AnnouncementTaskListDetail detail = new AnnouncementTaskListDetail();
            BeanUtils.copyProperties(importDto, detail);
            detail.setTaskListId(taskListId);
            detail.setCreateTime(LocalDateTime.now());
            detail.setCreateUserId(createUserId);
            detail.setDelFlag("0");

            // 通过统一社会信用代码查询E_BASEINFO表获取市场主体主键、市场主体类型、所属行业
            if (StringUtils.isNotBlank(importDto.getUniscid())) {
                LambdaQueryWrapper<EBaseinfo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(EBaseinfo::getUniscid, importDto.getUniscid());
                EBaseinfo eBaseinfo = eBaseinfoService.getOne(wrapper);

                if (eBaseinfo != null) {
                    detail.setPripid(eBaseinfo.getPripid());
                    detail.setEnttype(eBaseinfo.getEnttype());
                    detail.setIndustryphy(eBaseinfo.getIndustryphy());
                    detail.setIndustryco(eBaseinfo.getIndustryco());
                }
            }

            list.add(detail);

            // 达到BATCH_COUNT了，需要去存储一次数据库
            if (list.size() >= BATCH_COUNT) {
                saveData();
                list.clear();
            }
        }
    }

    /**
     * 所有数据解析完成了都会来调用
     *
     * @param analysisContext 分析上下文
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        if (!list.isEmpty()) {
            announcementTaskListDetailService.saveBatch(list);
        }
    }
}