
package com.tmerclub.cloud.search.listener;

import com.tmerclub.cloud.api.distribution.feign.DistributionFeignClient;
import com.tmerclub.cloud.api.distribution.vo.DistributionSpuVO;
import com.tmerclub.cloud.common.product.bo.EsDistributionInfoBO;
import com.tmerclub.cloud.common.product.bo.EsProductBO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.search.bo.DistributionInfoBO;
import com.tmerclub.cloud.search.canal.model.CanalBinLogResult;
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.Collections;

/**
 * @Author lth
 * @Date 2021/8/9 16:55
 */
@Component
public class DistributionSpuListener extends BaseCanalBinlogEventThrowExceptionProcessor<DistributionInfoBO> {

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

    @DubboReference
    private DistributionFeignClient distributionFeignClient;

    /**
     *  新增分销商品
     */
    @Override
    protected void processInsertInternal(CanalBinLogResult<DistributionInfoBO> result) {
        updateDistributionSpuInfo(result.getAfterData().getDistributionSpuId());
    }

    /**
     * 更新分销商品
     */
    @Override
    protected void processUpdateInternal(CanalBinLogResult<DistributionInfoBO> result) {
        updateDistributionSpuInfo(result.getAfterData().getDistributionSpuId());
    }

    /**
     * 删除分销商品
     */
    @Override
    protected void processDeleteInternal(CanalBinLogResult<DistributionInfoBO> result) {
        DistributionInfoBO afterData = result.getAfterData();
        EsProductBO esProductBO = new EsProductBO();
        esProductBO.setDistributionSpu(Boolean.FALSE);
        esProductBO.setDistributionInfo(null);
        EsSearchUtil.esUpdateSpuBySpuIds(Collections.singletonList(afterData.getSpuId()), esProductBO);
    }

    /**
     * 更新商品的分销信息
     * @param distributionId
     */
    private void updateDistributionSpuInfo(Long distributionId) {
        ServerResponseEntity<DistributionSpuVO> distributionRes = distributionFeignClient.getByDistributionId(distributionId);
        DistributionSpuVO distributionSpuVO = distributionRes.getData();
        EsProductBO esProductBO = new EsProductBO();
        esProductBO.setDistributionSpu(Boolean.TRUE);

        EsDistributionInfoBO esDistributionInfoBO = new EsDistributionInfoBO();
        esDistributionInfoBO.setDistributionSpuId(distributionSpuVO.getDistributionSpuId());
        esDistributionInfoBO.setState(distributionSpuVO.getState());
        esDistributionInfoBO.setAwardNumbers(distributionSpuVO.getAwardNumbers());
        esDistributionInfoBO.setAwardMode(distributionSpuVO.getAwardMode());
        esDistributionInfoBO.setAwardNumbers(distributionSpuVO.getAwardNumbers());
        esDistributionInfoBO.setParentAwardNumbers(distributionSpuVO.getParentAwardNumbers());
        esDistributionInfoBO.setParentAwardSet(distributionSpuVO.getParentAwardSet());
        esProductBO.setDistributionInfo(esDistributionInfoBO);

        EsSearchUtil.esUpdateSpuBySpuIds(Collections.singletonList(distributionSpuVO.getSpuId()), esProductBO);
    }
}
