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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fzm.mall.server.front.asset.model.UserAsset;
import com.fzm.mall.server.front.asset.model.UserAssetRecord;
import com.fzm.mall.server.front.asset.model.po.AssetRecord;
import com.fzm.mall.server.front.asset.service.IUserAssetRecordService;
import com.fzm.mall.server.front.asset.service.IUserAssetService;
import com.fzm.mall.server.front.constant.MALLGlobalConfig;
import com.fzm.mall.server.front.enums.CoinNameEnum;
import com.fzm.mall.server.front.order.model.Pay;
import com.fzm.mall.server.front.user.model.Member;
import com.fzm.mall.server.front.user.model.OprationPopularizeCommission;
import com.fzm.mall.server.front.user.service.IMemberService;
import com.fzm.mall.server.front.user.service.IOprationPopularizeCommissionService;
import com.fzm.mall.server.front.user.service.IUserService;
import com.fzm.mall.server.front.util.OrderUtil;
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;

@Slf4j
@Component
public class RewardConsumer {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IOprationPopularizeCommissionService oprationPopularizeCommissionService;

    @Autowired
    private IUserAssetService userAssetService;

    @Autowired
    private IUserAssetRecordService userAssetRecordService;

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = MALLGlobalConfig.projectMq+".register.reward.queue"), exchange = @Exchange(value = MALLGlobalConfig.projectMq+"-register-reward-exchange"), key = MALLGlobalConfig.projectMq+".register.reward.key") })
    public void receiver(String message) {
        if(!StringUtil.isNullOrEmpty(message)){
            Member member = memberService.getByUid(message);
            Integer level = 1;
            if(member != null){
                level = member.getMemberLevel();
            }
            OprationPopularizeCommission oprationPopularizeCommission = oprationPopularizeCommissionService.getByLevel(level);
            if(oprationPopularizeCommission!=null&&oprationPopularizeCommission.getRegisterAward().compareTo(BigDecimal.ZERO)==1){
                //奖励
                UserAsset coin = userAssetService.getCoin(message, CoinNameEnum.MALLCOIN.value());
                userAssetService.addCoin(coin.getId(),oprationPopularizeCommission.getRegisterAward());
                //生成记录
                UserAssetRecord assetRecord = new UserAssetRecord();
                assetRecord.setUid(message);
                assetRecord.setFlowId(OrderUtil.getFlowId());
                assetRecord.setStatus(2);
                assetRecord.setCoin(CoinNameEnum.MALLCOIN.value());
                assetRecord.setType(22);
                assetRecord.setAmount(oprationPopularizeCommission.getRegisterAward());
                userAssetRecordService.save(assetRecord);
            }
        }
    }

}
