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

import com.fzm.mall.server.admin.chain.FzmChain;
import com.fzm.mall.server.admin.finance_center.enums.status.AssetsFlowStatusEnum;
import com.fzm.mall.server.admin.finance_center.enums.status.MerchantIntegralStatusEnum;
import com.fzm.mall.server.admin.finance_center.enums.type.AssetsFlowTypeEnum;
import com.fzm.mall.server.admin.finance_center.model.MerchantAssets;
import com.fzm.mall.server.admin.finance_center.model.MerchantAssetsFlow;
import com.fzm.mall.server.admin.finance_center.service.IMerchantAssetsFlowService;
import com.fzm.mall.server.admin.finance_center.service.IMerchantAssetsService;
import com.fzm.mall.server.admin.finance_center.service.IMerchantIntegralService;
import com.fzm.mall.server.admin.util.UUIDUtil;
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.math.BigDecimal;
import java.util.Map;

/**
 * @author wangp
 * @date 2021/4/28 13:57
 * @description 发行积分
 * @since JDK 1.8
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IssueIntegralConsumer {
    private final IMerchantIntegralService merchantIntegralService;
    private final FzmChain fzmChain;
    private final IMerchantAssetsFlowService merchantAssetsFlowService;

    //发行商户积分
    @RabbitListener(queues = "ly.issue.merchant.integral.queue")
    public void issueMerchantIntegral(Map<String, Object> obj) {
        try {
            String coin = (String) obj.get("coin");
            int amount = (Integer) obj.get("amount");
            String addr = (String) obj.get("tokenAddr");
            String merchantId = (String) obj.get("merchantId");
            Integer status = 0;
            String errMsg = "";

            //商户流水
            MerchantAssetsFlow merchantAssetsFlow = new MerchantAssetsFlow(
                    UUIDUtil.getUUID(),
                    merchantId,
                    coin,
                    new BigDecimal(amount),
                    AssetsFlowTypeEnum.ISSUE.getType(),
                    AssetsFlowStatusEnum.SUCCESS.getStatus(),
                    addr
            );

            Map<String, String> map = fzmChain.IssueCoin(coin, amount, addr);
            if (map.get("status").equals("FAIL")) {
                status = MerchantIntegralStatusEnum.ISSUE_FAIL.getStatus();
                errMsg = map.get("errMsg");
                merchantAssetsFlow.setStatus(AssetsFlowStatusEnum.FAIL.getStatus());
            } else {
                status = MerchantIntegralStatusEnum.WAIT_CHECK.getStatus();
            }

            //更新积分发行状态
            merchantIntegralService.updateStatusByMerchantId(merchantId, status, errMsg);
            //保存流水
            merchantAssetsFlowService.save(merchantAssetsFlow);

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