package com.fzm.mall.server.admin.mq.consumer.goods_center;

import cn.fzm.chain.simplesdk.constant.TxStatusEnum;
import cn.fzm.chain.simplesdk.model.TxResult;
import com.fzm.mall.server.admin.block_chain.util.ChainUtil;
import com.fzm.mall.server.admin.goods_center.enums.status.SpuStatusEnum;
import com.fzm.mall.server.admin.goods_center.service.ISkuService;
import com.fzm.mall.server.admin.goods_center.service.ISpuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author wangp
 * @date 2021/3/10 17:40
 * @description 数字货币发行
 * @since JDK 1.8
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NftConsumer {

    private final ISpuService spuService;
    private final ChainUtil chainUtil;

    //发行
    @RabbitListener(queues = "ly.issue.nft.queue")
    public void issueCoinsPrice(Map<String, Object> obj) {
        try {
            String goodsId = obj.get("goodsId") + "";
            String hash = obj.get("hash") + "";

            //发行token
            TxResult tr = chainUtil.cycleConfirmTxWithHash(hash);
            log.info("map=========================" + tr.getStatus());
            if (tr.getStatus() != TxStatusEnum.SUCCESS) {
                log.error("NFT发行失败，交易HASH:{},失败原因：{}", hash, tr.getErrMsg());
                spuService.updateStatus(goodsId, SpuStatusEnum.FAIL.getStatus(), tr.getErrMsg().getValue());
//                skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), tr.getErrMsg().getValue());
            } else {
                spuService.updateStatus(goodsId, SpuStatusEnum.WAIT_CHECK.getStatus(), "");
            }
        } catch (Exception e) {
            return;
        }
    }

}
