微信小程序转发链接交互逻辑

    /**
     * 保存卡券
     */
    public void sendAndSave(CouponDTO couponDTO) {
        Long cardId = couponDTO.getCardId();
        Long userId = SecurityUtils.getCurrentUserId();

        //formId不能为空
        if (StringUtils.isEmpty(couponDTO.getFormId())) {
            throw new BaseAppException("formId不能为空");
        }
        //当前用户是否是卡的创建人
        Card card = cardRepository.findOne(cardId);
        if(userId.equals(card.getCreateId())){
            return;
        }

        //如果当前用户已经点开过
        List<Coupon> couponForCurrentIds = couponRepository.findByCardIdAndReceiveId(cardId,userId);

        //当前用户添加十张卡券(不考虑使用情况)[当前用户]
        List<Coupon> sumCouponForCurrentIds = couponRepository.findByReceiveId(userId);

        String templateId = applicationProperties.getTemplateId();//模板消息id
        String accessToken = getAccessToken().get("accessToken");
        Card cardCoupon = new Card();
        cardCoupon.setId(couponDTO.getCardId());
        if(couponForCurrentIds.size() == 0 && sumCouponForCurrentIds.size() < 10){
            //给当前用户发送微信信息
            String openIdForCurrentUser = SecurityUtils.getCurrentUser().getOpenId();
            String formIdForCurrentUser =  couponDTO.getFormId();
            templateItem(openIdForCurrentUser, templateId, accessToken, formIdForCurrentUser);

            //保存当前用户的券记录
            Coupon coupon = new Coupon();
            coupon.setReceiveId(SecurityUtils.getCurrentUserId());
            coupon.setCard(cardCoupon);
            save(coupon);
        }


        //制卡人是否已收到券信息
        List<Coupon> couponForCreateIds = couponRepository.findByCardIdAndReceiveId(cardId,card.getCreateId());

        //当前用户添加十张卡券(不考虑使用情况)[当前用户]
        List<Coupon> sumCouponForCreateIds = couponRepository.findByReceiveId(userId);

        if(couponForCreateIds.size()==0 && sumCouponForCreateIds.size() < 10){
            //给制卡人发送微信信息
            String openIdForCreateId = userInfoRepository.findOne(card.getCreateId()).getOpenId();
            //创建贺卡用户模板 创建者的formId从表单表里面取
            Form form = formRepository.findByCreateIdAndCardId(card.getCreateId(),cardId);
            //form为空或超过7天,无法发送模板消息
            if (form == null || form.getFailureTime().toInstant().toEpochMilli() < ZonedDateTime.now().toInstant().toEpochMilli()) {
                return;
            }
            String formIdForCreateId = form.getFormId();
            templateItem(openIdForCreateId, templateId, accessToken, formIdForCreateId);

            //保存制卡人的券记录
            Coupon couponForCreateId = new Coupon();
            couponForCreateId.setReceiveId(card.getId());
            couponForCreateId.setCard(cardCoupon);
            save(couponForCreateId);
        }
        //修改分享状态
        card.setState(1);
        cardRepository.save(card);
    }

转载于:https://my.oschina.net/zhangshsURL/blog/1596242

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值