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.enums.ChainStatusEnum;
import com.fzm.mall.server.admin.block_chain.util.ChainUtil;
import com.fzm.mall.server.admin.chain.FzmChain;
import com.fzm.mall.server.admin.goods_center.enums.status.SkuStatusEnum;
import com.fzm.mall.server.admin.goods_center.enums.status.SpuStatusEnum;
import com.fzm.mall.server.admin.goods_center.model.Sku;
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 CoinsConsumer {

    private final ISpuService spuService;
    private final FzmChain fzmChain;
    private final ChainUtil chainUtil;
    private final ISkuService skuService;

    //发行
    @RabbitListener(queues = "ly.issue.coins.queue")
    public void issueCoinsPrice(Map<String, Object> obj) {
        try {
            String hash = (String) obj.get("hash");
            int stock = Integer.parseInt(obj.get("stock") + "");
            String goodsId = (String) obj.get("goodsId");
            String skuId = (String) obj.get("skuId");
            int subCount = (int) obj.get("subCount");
            int total = (int) obj.get("total");

            //异步确认
            TxResult tr = chainUtil.cycleConfirmTxWithHash(hash);
            log.info("issue.coins.queue发行=========================" + tr.getStatus());
            if (tr.getStatus() != TxStatusEnum.SUCCESS) {
                log.error("NFT发行失败，交易HASH:{},失败原因：{},交易参数：{}", hash, tr.getErrMsg(), obj);
                spuService.updateStatus(goodsId, SpuStatusEnum.DRAFT.getStatus(), tr.getErrMsg().getValue());
                skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), tr.getErrMsg().getValue());
            } else {
                log.info("NFT发行成功");
                Integer status = spuService.getByGoodsId(goodsId).getStatus();
                if (subCount == total && !status.equals(SpuStatusEnum.DRAFT.getStatus())) {
                    spuService.updateStatus(goodsId, SpuStatusEnum.WAIT_CHECK.getStatus(), "");
                }
                skuService.updateCirculation(skuId, stock);
            }
        } catch (Exception e) {
            log.info("issue.coins.queue异常=========================" + e.getMessage());
        }
        /*try {
            String coinName = obj.get("coinName") + "";
            int stock = (int) ((long) obj.get("stock"));
            String tokenAddr = obj.get("tokenAddr") + "";
            String goodsId = obj.get("goodsId") + "";
            String skuId = obj.get("skuId") + "";
            int subCount = (int) obj.get("subCount");
            int total = (int) obj.get("total");

            //判断是否发行过
            if (fzmChain.coinExist(coinName)) {
                spuService.updateStatus(goodsId, SpuStatusEnum.FAIL.getStatus(), "TOKEN已存在");
                skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), "TOKEN已存在");
                return;
            }

            //发行token
            Map<String, String> map = fzmChain.IssueCoin(coinName, stock, tokenAddr);
            log.info("map=========================" + map);
            if (map.get("status").equals("FAIL")) {
                spuService.updateStatus(goodsId, SpuStatusEnum.FAIL.getStatus(), map.get("errMsg"));
                skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), map.get("errMsg"));
            } else {
                Integer status = spuService.getByGoodsId(goodsId).getStatus();
                if (subCount == total && !status.equals(SpuStatusEnum.FAIL.getStatus())) {
                    spuService.updateStatus(goodsId, SpuStatusEnum.WAIT_CHECK.getStatus(), "");
                }
                skuService.updateCirculation(skuId, stock);
            }

        } catch (Exception e) {
            return;
        }*/

    }

    @RabbitListener(queues = "ly.change.coins.queue")
    public void changeCoinsPrice(Map<String, Object> obj) {
        try {
            String hash = obj.get("hash") + "";
            String skuId = obj.get("skuId") + "";
            int cir = Integer.parseInt(obj.get("cir") + "");
            //异步确认
            TxResult tr = chainUtil.cycleConfirmTxWithHash(hash);
            log.info("change.coins.queue发行=========================" + tr.getStatus());
            if (tr.getStatus() != TxStatusEnum.SUCCESS) {
                log.error("NFT增发失败，交易HASH:{},失败原因：{}", hash, tr.getErrMsg());
                Sku sku = skuService.getBySkuId(skuId);
                skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), tr.getErrMsg().getValue());
            } else {
                skuService.updateCirculation(skuId, cir);
            }
        } catch (Exception e) {
            return;
        }
        /*try {
            String coinName = obj.get("coinName") + "";
            int change = (int) obj.get("change");
            String issueAddr = obj.get("issueAddr") + "";
            String addrKey = obj.get("addrKey") + "";
            String goodsId = obj.get("goodsId") + "";
            String skuId = obj.get("skuId") + "";

            if (change > 0) {
                Map<String, String> map = fzmChain.coinBurn(coinName, issueAddr, addrKey, change);
                if (map.get("status").equals("FAIL")) {
                    spuService.updateStatus(goodsId, SpuStatusEnum.FAIL.getStatus(), map.get("errMsg"));
                    skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), map.get("errMsg"));
                } else {
                    skuService.updateCirculation(skuId, change * -1);
                }
            } else if (change < 0) {
                Map<String, String> map = fzmChain.coinMint(coinName, issueAddr, addrKey, change * -1);
                if (map.get("status").equals("FAIL")) {
                    spuService.updateStatus(goodsId, SpuStatusEnum.FAIL.getStatus(), map.get("errMsg"));
                    skuService.updateStatus(skuId, SkuStatusEnum.ISSUE_FAIL.getStatus(), map.get("errMsg"));
                } else {
                    skuService.updateCirculation(skuId, change * -1);
                }
            }
        } catch (Exception e) {
            return;
        }*/
    }
}
