
package com.tmerclub.cloud.search.listener;

import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.search.bo.ShopDetailBO;
import com.tmerclub.cloud.search.canal.model.CanalBinLogResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Objects;

/**
 * @author YXF
 * @date 2021/01/21
 */
@Component
public class ShopDetailCanalListener extends BaseCanalBinlogEventThrowExceptionProcessor<ShopDetailBO> {

    private static final Logger log = LoggerFactory.getLogger(ShopDetailCanalListener.class);
    @DubboReference
    private SpuFeignClient spuFeignClient;

    /**
     * 更新店铺
     * @param result
     */
    @Override
    protected void processUpdateInternal(CanalBinLogResult<ShopDetailBO> result) {
        ShopDetailBO beforeData = result.getBeforeData();
        ShopDetailBO afterData = result.getAfterData();
        // 删除店铺缓存
        RedisUtil.del(CacheNames.SHOP_DETAIL_ID_KEY + CacheNames.UNION + afterData.getShopId());
        boolean isBlank = Objects.isNull(beforeData.getShopName()) && StrUtil.isBlank(beforeData.getShopLogo()) && Objects.isNull(beforeData.getShopStatus());
        if (isBlank) {
            return;
        }
        // 更新商品信息
        ServerResponseEntity<Void> spuRes = spuFeignClient.updateSpuUpdateTimeByShopIds(Collections.singletonList(afterData.getShopId()), SysTypeEnum.MULTISHOP.value());
        if (!spuRes.isSuccess()) {
            throw new LuckException(spuRes.getMsg());
        }
    }
}
