package com.fzm.mall.server.front.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.asset.model.TranOrder;
import com.fzm.mall.server.front.asset.service.ITranOrderService;
import com.fzm.mall.server.front.constant.MALLGlobalConfig;
import com.fzm.mall.server.front.enums.QueueTTLTypeEnum;
import com.fzm.mall.server.front.mq.MsgProducer;
import com.fzm.mall.server.front.order.model.po.TranDto;
import com.fzm.mall.server.front.user.model.User;
import com.fzm.mall.server.front.user.service.IUserService;
import com.fzm.mall.server.front.util.ChainUtil;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 订单超时处理
 *
 * @author qxl
 * @date 创建时间：2021年4月13日 下午3:01:41
 */
@Slf4j
@Component
public class TranOrderPayConsumer {

    @Autowired
    private ITranOrderService tranOrderService;

    @Autowired
    private ChainUtil chainUtil;

    @Autowired
    private IUserService userService;

    @Autowired
    private MsgProducer msgProducer;

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = MALLGlobalConfig.projectMq + ".pay.tran.queue"), exchange = @Exchange(value = MALLGlobalConfig.projectMq + "-pay-tran-exchange"), key = MALLGlobalConfig.projectMq + ".pay.tran.key")})
    public void receiver(String message) {
        System.out.println("TranOrderPayConsumer:" + message);
        if (StringUtil.isNullOrEmpty(message)) {
            log.error("orderPayConsumer:message为空");
            return;
        }
        TranOrder byOid = tranOrderService.getByOid(message);
        if (byOid == null) {
            log.error("orderPayConsumer:pid:" + message + "订单不存在");
            return;
        }
        if (byOid.getStatus() != 1) {
            log.error("orderPayConsumer:pid:" + message + "订单状态不成功");
            return;
        }
        User buy = userService.getByUid(byOid.getUid());
        User seller = userService.getByUid(byOid.getSeller());
        String hash = chainUtil.evmTransfer(buy.getAddress(), seller.getAddress(), Long.valueOf(byOid.getCoin().replace(MALLGlobalConfig.GOODS_NFT_PREFIX, "")), byOid.getNumber().longValue());
        TranDto tranDto = new TranDto();
        tranDto.setHash(hash);
        tranDto.setFromAddr(seller.getAddress());
        tranDto.setToAddr(buy.getAddress());
        tranDto.setAmount(new BigDecimal(byOid.getNumber()));
        tranDto.setCoin(byOid.getCoin().replace(MALLGlobalConfig.GOODS_NFT_PREFIX, ""));
        tranDto.setType(1);
        msgProducer.sendTTL(JSONObject.toJSONString(tranDto), QueueTTLTypeEnum.CONFIRM_WITHDRAW_TTL, 5 * 1000);
    }
}
