package com.yeejoin.amos.boot.module.jg.biz.edit.event.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.boot.module.jg.api.dto.BizRelationDataDto;
import com.yeejoin.amos.boot.module.jg.api.dto.ESDataChangeLogDto;
import com.yeejoin.amos.boot.module.jg.api.dto.FieldChangeMeta;
import com.yeejoin.amos.boot.module.jg.api.dto.JgResumeInfoDto;
import com.yeejoin.amos.boot.module.jg.api.entity.JgBizChangeLog;
import com.yeejoin.amos.boot.module.jg.api.enums.BusinessTypeEnum;
import com.yeejoin.amos.boot.module.jg.biz.dao.ESDataChangeLogDao;
import com.yeejoin.amos.boot.module.jg.biz.edit.event.BaseBizDataChangeEvent;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.JgBizChangeLogServiceImpl;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.JgResumeInfoServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
import org.typroject.tyboot.core.foundation.context.RequestContextModel;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author Administrator
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ChangeLogInsertListener {

    private static final Map<String, String> bizTypeTableMap = new HashMap<>();

    static {
        bizTypeTableMap.put("useRegister", "tzs_jg_use_registration");
        bizTypeTableMap.put("installationNotice", "tzs_jg_installation_notice");
        bizTypeTableMap.put("singleManageEquipEdit", "idx_biz_jg_use_info");
    }

    @Value("${change.log.deal.thread.number:1}")
    private int threadNumber;

    @Value("${change.log.deal.routePath:/mixuap?appId=1742358052905971713&id=1924440797940314113}")
    private String routePath;

    private final ESDataChangeLogDao esDataChangeLogDao;

    private final JgBizChangeLogServiceImpl bizChangeLogService;

    private final JgResumeInfoServiceImpl jgResumeInfoService;


    private final BlockingQueue<BaseBizDataChangeEvent> queue = new LinkedBlockingQueue<>();


    @TransactionalEventListener(value = BaseBizDataChangeEvent.class)
    @Async
    public void handleTransactionalEvent(BaseBizDataChangeEvent event) {
        log.info("日志处理器，收到变更业务数据消息:{}", JSONObject.toJSONString(event));
        queue.add(event);
    }

    @PostConstruct
    public void init() {
        ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
        IntStream.range(0, threadNumber).forEach(i -> {
            executorService.execute(() -> {
                while (true) {
                    try {
                        BaseBizDataChangeEvent event = queue.take();
                        processEvent(event);
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
            });
        });
    }

    private void processEvent(BaseBizDataChangeEvent event) {
        List<FieldChangeMeta> fieldChangeMetas = event.getData();
        RequestContextModel requestContextModel = event.getRequestContext();
        Date date = new Date();
        // 1.主表保存
        JgBizChangeLog changeLog = saveLog(event);
        // 2.子表字段保存
        List<ESDataChangeLogDto> logs = saveColumnsChange2Es(fieldChangeMetas, requestContextModel, date, changeLog);
        // 3.异步记录履历日志
        this.insertCertLog(changeLog, event);
        log.info("es 操作日志数据入库成功：{}条", logs.size());
    }

    @Async
    protected void insertCertLog(JgBizChangeLog changeLog, BaseBizDataChangeEvent event) {
        try {
            BizRelationDataDto data = event.getBizRelationData();
            String bizId = data.getBizId();
            String businessTypeName = BusinessTypeEnum.findNameByEnumName(changeLog.getBizType()).orElse("设备编辑");
            String approvalUnit = data.getUnitName();
            String approvalUnitCode = data.getUnitCode();
            String recUserName = data.getRecUserName();
            String recUserId = event.getRequestContext().getExcutedUserId();
            String changeLogId = String.valueOf(changeLog.getSequenceNbr());
            String equId = (data.getProjectContraptionIds() == null || data.getProjectContraptionIds().isEmpty())
                    ? event.getData().stream()
                    .map(FieldChangeMeta::getChangeId)
                    .filter(Objects::nonNull)
                    .findFirst()
                    .orElse(null)
                    : data.getProjectContraptionIds().iterator().next();
            JgResumeInfoDto dto = new JgResumeInfoDto();
            dto.setApplyNo(bizId);
            dto.setBusinessType(businessTypeName);
            dto.setBusinessId(bizId);
            dto.setEquId(equId);
            dto.setResumeType("1");
            dto.setChangeContent("设备编辑");
            dto.setChangeLogId(changeLogId);
            dto.setApprovalUnit(approvalUnit);
            dto.setApprovalUnitCode(approvalUnitCode);
            dto.setRecDate(new Date());
            dto.setRecUserName(recUserName);
            dto.setRecUserId(recUserId);
            dto.setStatus("正常");
            dto.setRoutePath(routePath);
            if (!event.getBizRelationData().getNeedRecord()) {
                dto.setIsDelete(true);
            }
            jgResumeInfoService.createWithModel(dto);
        } catch (Exception e) {
            log.error("插入设备履历日志异常，bizType: {}, 错误: {}", changeLog.getBizType(), e.getMessage(), e);
        }
    }

    private List<ESDataChangeLogDto> saveColumnsChange2Es(List<FieldChangeMeta> fieldChangeMetas, RequestContextModel requestContextModel, Date date, JgBizChangeLog changeLog) {
        List<ESDataChangeLogDto> logs = fieldChangeMetas.parallelStream().map(changeDataDto -> {
            ESDataChangeLogDto changeLogDto = new ESDataChangeLogDto();
            changeLogDto.setColumnKey(changeDataDto.getColumnKey());
            changeLogDto.setColumnKeyLabel(changeDataDto.getColumnLabel());
            changeLogDto.setBeforeData(changeDataDto.getColumnOldValue());
            changeLogDto.setAfterData(changeDataDto.getColumnNewValue());
            changeLogDto.setUserId(requestContextModel.getExcutedUserId());
            changeLogDto.setCreateDate(date.getTime());
            changeLogDto.setRequestDate(DateUtil.formatDateTime(date));
            changeLogDto.setChangeId(changeDataDto.getChangeId());
            changeLogDto.setColumnFamily(changeDataDto.getColumnFamily());
            changeLogDto.setBizType(changeLog.getBizType());
            changeLogDto.setDisplayOldValue(changeDataDto.getDisplayOldValue());
            changeLogDto.setDisplayNewValue(changeDataDto.getDisplayNewValue());
            changeLogDto.setBatchId(changeLog.getSequenceNbr() + "");
            changeLogDto.setColumnType(changeDataDto.getColumnType());
            return changeLogDto;
        }).collect(Collectors.toList());
        if (!logs.isEmpty()) {
            esDataChangeLogDao.saveAll(logs);
        }
        return logs;
    }

    private JgBizChangeLog saveLog(BaseBizDataChangeEvent event) {
        JgBizChangeLog changeLog = new JgBizChangeLog();
        BeanUtil.copyProperties(event.getBizRelationData(), changeLog);
        changeLog.setBizTable(bizTypeTableMap.get(changeLog.getBizType()));
        changeLog.setRecDate(new Date());
        changeLog.setRecUserId(event.getRequestContext().getExcutedUserId());
        changeLog.setSequenceNbr(Long.parseLong(event.getRequestContext().getTraceId()));
        bizChangeLogService.getBaseMapper().insert(changeLog);
        return changeLog;
    }
}
