package com.tmerclub.cloud.search.listener;

import com.tmerclub.cloud.api.order.bo.EsAllotOrderBO;
import com.tmerclub.cloud.api.order.feign.AllotOrderFeignClient;
import com.tmerclub.cloud.search.bo.AllotOrderBO;
import com.tmerclub.cloud.search.canal.model.CanalBinLogResult;
import com.tmerclub.cloud.search.constant.EsIndexEnum;
import com.tmerclub.cloud.search.util.EsSearchUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author gaozijie
 * @since 2023-11-24
 */
@Component
public class AllotOrderCanalListener extends BaseCanalBinlogEventThrowExceptionProcessor<AllotOrderBO>{

    private static final Logger log = LoggerFactory.getLogger(AllotOrderCanalListener.class);

    @DubboReference
    private AllotOrderFeignClient allotOrderFeignClient;

    @Override
    protected void processInsertInternal(CanalBinLogResult<AllotOrderBO> result) {
        // 新增mongo和es数据
        Long allotOrderId = result.getPrimaryKey();
        EsAllotOrderBO esAllotOrderBO = allotOrderFeignClient.saveMongoAndGetEsInfo(allotOrderId);
        if (Objects.isNull(esAllotOrderBO)) {
            log.error("调拨订单信息异常的订单id为：{}", allotOrderId);
            return;
        }
        EsSearchUtil.esSave(esAllotOrderBO.getAllotOrderId(), EsIndexEnum.ALLOT_ORDER, esAllotOrderBO);
    }

    @Override
    protected void processUpdateInternal(CanalBinLogResult<AllotOrderBO> result) {
        // 更新mongo和es数据
        Long allotOrderId = result.getAfterData().getAllotOrderId();
        EsAllotOrderBO esAllotOrderBO = allotOrderFeignClient.saveMongoAndGetEsInfo(allotOrderId);
        if (Objects.isNull(esAllotOrderBO)) {
            log.error("调拨订单信息异常的订单id为：{}", allotOrderId);
            return;
        }
        EsSearchUtil.esUpdate(esAllotOrderBO.getAllotOrderId(), EsIndexEnum.ALLOT_ORDER, esAllotOrderBO);
    }

}
