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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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[] hashs = (String[]) obj.get("hashs");
            String prefix = obj.get("prefix") + "";
            String nos = obj.get("nos") + "";
            String merchantId = obj.get("merchantId") + "";
            String commodityPassId = obj.get("commodityPassId") + "";
            System.err.println("obj:" + obj);

            int length = hashs.length;

            //发行token
            for (int i = 0; i < length; i++) {
                TxResult tr = chainUtil.cycleConfirmTxWithHash(hashs[i]);
                log.info("map=========================" + tr.getStatus());
                if (tr.getStatus() != TxStatusEnum.SUCCESS) {
                    log.error("NFT发行失败，交易HASH:{},失败原因：{}", hashs[i], tr.getErrMsg());
                    spuService.updateStatus(goodsId, SpuStatusEnum.FAIL.getStatus(), tr.getErrMsg().getValue());
//                skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), tr.getErrMsg().getValue());
                    return;
                } else {
                    if (i + 1 == length) {
                        LambdaQueryWrapper<GoodsSkuUsufruct> qw = new LambdaQueryWrapper<>();
                        qw.eq(GoodsSkuUsufruct::getPrefix, prefix);
                        GoodsSkuUsufruct usufruct = goodsSkuUsufructService.getOne(qw);
                        goodsSkuUsufructService.removeById(usufruct);

                        spuService.updateStatus(goodsId, SpuStatusEnum.WAIT_CHECK.getStatus(), "");
                        GoodsSkuUsufruct goodsSkuUsufruct = new GoodsSkuUsufruct();
                        goodsSkuUsufruct.setPrefix(prefix);
                        goodsSkuUsufruct.setSerialNo(nos.toString());
                        goodsSkuUsufruct.setCommodityPassId(commodityPassId);
                        goodsSkuUsufruct.setMerchantId(merchantId);
                        goodsSkuUsufructService.save(goodsSkuUsufruct);
                    }
                }
            }
        } catch (Exception e) {
            return;
        }
    }

}
