/**
* 保存卡券
*/
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