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.alibaba.fastjson.JSONArray;
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.model.GoodsSkuUsufruct;
import com.fzm.mall.server.admin.goods_center.service.IGoodsSkuUsufructService;
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;
    private final IGoodsSkuUsufructService goodsSkuUsufructService;

    //发行
    @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;
        }
    }

    //发行
    @RabbitListener(queues = "ly.usu.nft.queue")
    public void usuCoinsPrice(Map<String, Object> obj) {
        try {
            String goodsId = obj.get("goodsId") + "";
            String hash = obj.get("hash") + "";
            String prefix = obj.get("prefix") + "";
            String nos = obj.get("nos") + "";
            System.err.println("obj:" + obj);

            //发行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(), "");
                GoodsSkuUsufruct goodsSkuUsufruct = new GoodsSkuUsufruct();
                goodsSkuUsufruct.setPrefix(prefix);
                goodsSkuUsufruct.setSerialNo(nos.toString());
                goodsSkuUsufructService.save(goodsSkuUsufruct);
            }
        } catch (Exception e) {
            return;
        }
    }

}
