//package com.tzy.listener.mq;
//
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson2.JSON;
//import com.rabbitmq.client.Channel;
//import com.tzy.base.dto.DictBaseDto;
//import com.tzy.base.service.SysBaseService;
//import com.tzy.common.constant.Constants;
//import com.tzy.common.constant.MqConstans;
//import com.tzy.common.core.domain.entity.SysDictData;
//import com.tzy.common.exception.ServiceException;
//import com.tzy.coupon.card.service.IAppUserCardRecordService;
//import com.tzy.sportcard.api.bean.act.UserListActParam;
//import com.tzy.sportcard.api.service.GroupApiService;
//import com.tzy.sportcard.base.service.CommonCacheService;
//import com.tzy.sportcard.group.service.AppActService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.Queue;
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.List;
//
//@Slf4j
//@Component
//public class UserInviteCodeConsumer {
//        @Autowired
//        private IAppUserCardRecordService appUserCardRecordService;
//        @Resource
//        private CommonCacheService commonCacheService;
//        @Resource
//        private GroupApiService groupApiService;
//        @Resource
//        private AppActService appActService;
//        @Autowired
//        private SysBaseService sysBaseService;
//        @RabbitHandler
//        @RabbitListener(queuesToDeclare = @Queue(MqConstans.QUEUE_USER_INVITE_CODE))
//        public void userListActConsumer(String data, Channel channel, Message message) throws IOException {
//            log.info("用户邀请码绑定消费:{}", data);
//            try {
//                //从data 的json结构解析出userId inviteCode parentUserId
//                JSONObject sendData = JSONObject.parseObject(data);
//                Long userId = sendData.getLong("userId");
//                String inviteCode = sendData.getString("inviteCode");
//                Long parentUserId = sendData.getLong("parentUserId");
//                //查询数据字典 从中拿到商家邀请码配置的优惠券 并且发送给用户
//
//                List<SysDictData> sysDictDatas = commonCacheService.getCommonDictData(Constants.DICT_TYPE_INVITE_CODE,
//                        null,"1", null, -1);
//                //遍历优惠券列表  找到用户邀请的优惠券
//                String couponDetail = null;
//                String couponRemark = null;
//                for (SysDictData sysDictData : sysDictDatas) {
//                    if (sysDictData.getDictLabel().equals(inviteCode)) {
//                        couponDetail = sysDictData.getDictValue();
//                        couponRemark = sysDictData.getRemark();
//                        JSONObject couponJson = JSONObject.parseObject(couponDetail);
//                        Integer couponId = couponJson.getInteger("couponId");
//                        Integer couponCount = couponJson.getInteger("couponCount");
//                        int count = appUserCardRecordService.selectAppUserCardRecordCount(couponId.longValue());
//                        if(count >= couponCount) {
//                            log.error("用户{}绑定优惠券{}失败,优惠券不够", userId, inviteCode);
//                            //throw new ServiceException("来晚了");
//                        }else{
//                            appUserCardRecordService.addUserCoupon(couponId, userId.intValue(), couponRemark, couponId.longValue(), Constants.DICT_TYPE_INVITE_CODE);
//                            log.info("用户{}绑定邀请码{}成功", userId, inviteCode);
//                        }
//                    }
//                }
//
//            } catch (Exception e) {
//                log.error("用户邀请码绑定消费异常:{}", data, e);
//            }
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        }
//
//    }
