| | |
| | | package com.xiaopeng.operation.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.xiaopeng.api.dto.account.UcUserInfo; |
| | | import com.xiaopeng.api.dto.operation.SmsDto; |
| | | import com.xiaopeng.api.dto.operation.XpActivityDto; |
| | | import com.xiaopeng.api.feignClient.account.UserResourcesClient; |
| | | import com.xiaopeng.api.feignClient.account.XpAppUserCouponClient; |
| | | import com.xiaopeng.api.feignClient.operation.SmsClient; |
| | | import com.xiaopeng.api.feignClient.operation.XpCouponClient; |
| | | import com.xiaopeng.api.feignClient.order.XpOrderClient; |
| | | import com.xiaopeng.api.feignClient.pushMessage.SubscribeMessageClient; |
| | | import com.xiaopeng.api.model.account.XpAppUserCoupon; |
| | | import com.xiaopeng.api.model.operation.*; |
| | | import com.xiaopeng.api.query.operation.XpActivityPageListQuery; |
| | | import com.xiaopeng.api.vo.account.AppUserCouponSaveBatchVo; |
| | | import com.xiaopeng.api.vo.operation.SmsVo; |
| | | import com.xiaopeng.api.vo.pushMessage.SendCouponMessageVo; |
| | | import com.xiaopeng.api.vo.pushMessage.SendSubscribeMessageVo; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.enums.MessageTemplateEnum; |
| | | import com.xiaopeng.common.core.utils.bean.BeanUtils; |
| | | import com.xiaopeng.common.core.web.page.PageInfo; |
| | | import com.xiaopeng.operation.mapper.XpActicityMapper; |
| | | import com.xiaopeng.operation.service.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneOffset; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadFactory; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private UserResourcesClient userResourcesClient; |
| | | |
| | | @Resource |
| | | private SubscribeMessageClient subscribeMessageClient; |
| | | |
| | | @Resource |
| | | private XpCouponClient xpCouponClient; |
| | | |
| | | @Resource |
| | | private SmsClient smsClient; |
| | | |
| | | @Value("${sms.appid:1476153389173952512}") |
| | | private String sms_appid; |
| | | |
| | | private static final ThreadPoolExecutor RETRY_EXECUTOR = new ThreadPoolExecutor( |
| | | 4, |
| | | 16, |
| | | 60L, TimeUnit.SECONDS, |
| | | new LinkedBlockingQueue<>(512), |
| | | new ThreadFactory() { |
| | | private final AtomicInteger counter = new AtomicInteger(0); |
| | | @Override |
| | | public Thread newThread(Runnable r) { |
| | | Thread t = new Thread(r, "route-push-retry-" + counter.incrementAndGet()); |
| | | t.setDaemon(true); |
| | | return t; |
| | | } |
| | | }, |
| | | new ThreadPoolExecutor.CallerRunsPolicy() |
| | | ); |
| | | |
| | | |
| | | |
| | |
| | | List<XpActivity> list = xpActicityService.list(new QueryWrapper<XpActivity>().in("status", Arrays.asList(1, 2)).eq("is_deleted", 0)); |
| | | for (XpActivity xpActivity : list) { |
| | | Integer status = xpActivity.getStatus(); |
| | | //即将开始的活动推送socket给前端用户 |
| | | if(1 == status && LocalDateTime.now().isBefore(xpActivity.getStartTime())){ |
| | | |
| | | } |
| | | |
| | | //即将结束的活动推送给前端用户 |
| | | |
| | | //生效的活动修改状态和发放一次优惠券 |
| | | if(1 == status && LocalDateTime.now().isAfter(xpActivity.getStartTime()) && LocalDateTime.now().isBefore(xpActivity.getEndTime())){ |
| | | xpActivity.setStatus(2); |
| | |
| | | for (XpActicityStrategy xpActicityStrategy : list1) { |
| | | pushCoupon(xpActivity, xpActicityStrategy); |
| | | } |
| | | |
| | | } |
| | | //到期的活动修改状态为已结束 |
| | | if(2 == status & LocalDateTime.now().isAfter(xpActivity.getEndTime())){ |
| | |
| | | AppUserCouponSaveBatchVo vo = new AppUserCouponSaveBatchVo(); |
| | | vo.setXpAppUserCoupons(xpAppUserCoupons); |
| | | xpAppUserCouponClient.saveBatch(vo); |
| | | |
| | | log.info("全部白名单用户发送 {}", JSON.toJSONString(xpAppUserCoupons)); |
| | | //发送到账提醒消息 |
| | | pushMessage(xpActivity, xpAppUserCoupons); |
| | | } |
| | | if(null == distributionsTotal){ |
| | | continue; |
| | |
| | | AppUserCouponSaveBatchVo vo = new AppUserCouponSaveBatchVo(); |
| | | vo.setXpAppUserCoupons(xpAppUserCoupons); |
| | | xpAppUserCouponClient.saveBatch(vo); |
| | | //发送到账提醒消息 |
| | | pushMessage(xpActivity, xpAppUserCoupons); |
| | | } |
| | | if(null == distributionsTotal){ |
| | | continue; |
| | |
| | | AppUserCouponSaveBatchVo vo = new AppUserCouponSaveBatchVo(); |
| | | vo.setXpAppUserCoupons(xpAppUserCoupons); |
| | | xpAppUserCouponClient.saveBatch(vo); |
| | | //发送到账提醒消息 |
| | | pushMessage(xpActivity, xpAppUserCoupons); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 推送提醒消息 |
| | | * @param xpActivity |
| | | * @param list |
| | | */ |
| | | private void pushMessage(XpActivity xpActivity, List<XpAppUserCoupon> list) { |
| | | pushWxCouponMessage(xpActivity, list); |
| | | pushSmsCouponMessage(xpActivity, list); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 微信通知 |
| | | */ |
| | | private void pushWxCouponMessage(XpActivity xpActivity, List<XpAppUserCoupon> list) { |
| | | log.info("开始执行优惠券到账微信提醒消息发送 {}", JSON.toJSONString(xpActivity)); |
| | | //微信通知活动开始提醒(0=否,1=是) |
| | | Integer wechatReceivedReminder = xpActivity.getWechatReceivedReminder(); |
| | | if(null != wechatReceivedReminder && 1 == wechatReceivedReminder){ |
| | | RETRY_EXECUTOR.execute(() -> { |
| | | for (XpAppUserCoupon xpAppUserCoupon : list) { |
| | | XpCoupon coupon = xpCouponClient.getCouponById(xpAppUserCoupon.getCouponId()).getData(); |
| | | SendSubscribeMessageVo vo = new SendSubscribeMessageVo(); |
| | | vo.setUserId(xpAppUserCoupon.getAppUserId()); |
| | | vo.setTemplateId(MessageTemplateEnum.C.getTemplateId()); |
| | | String json = MessageTemplateEnum.C.getRemarks(); |
| | | json = json.replace("活动名称", xpAppUserCoupon.getCouponName()); |
| | | json = json.replace("温馨提示", coupon.getCouponDescription()); |
| | | vo.setJson(json); |
| | | R<?> r = subscribeMessageClient.sendSubscribeMessage(vo); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 短信通知 |
| | | * @param xpActivity |
| | | */ |
| | | private void pushSmsCouponMessage(XpActivity xpActivity, List<XpAppUserCoupon> list) { |
| | | log.info("开始执行优惠券短信提醒消息发送 {}", JSON.toJSONString(xpActivity)); |
| | | Integer now = Integer.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("HHmm"))); |
| | | if(StringUtils.hasLength(xpActivity.getSmsStartBlockingTime())){ |
| | | String[] smsStartBlockingTime = xpActivity.getSmsStartBlockingTime().split(","); |
| | | String[] smsEndBlockingTime = xpActivity.getSmsEndBlockingTime().split(","); |
| | | for (int i = 0; i < smsStartBlockingTime.length; i++) { |
| | | Integer start = Integer.valueOf(smsStartBlockingTime[i].replace(":", "")); |
| | | Integer end = Integer.valueOf(smsEndBlockingTime[i].replace(":", "")); |
| | | if(start <= now && now <= end){ |
| | | log.info("当前时间在短信发送时间区间内,不发送短信"); |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | //短信通知到账提醒(0=否,1=是) |
| | | Integer smsReceivedReminder = xpActivity.getSmsReceivedReminder(); |
| | | if(null != smsReceivedReminder && 1 == smsReceivedReminder){ |
| | | RETRY_EXECUTOR.execute(() -> { |
| | | for (XpAppUserCoupon xpAppUserCoupon : list) { |
| | | UcUserInfo ucUserInfo = userResourcesClient.getBaseUserByUid(xpAppUserCoupon.getAppUserId().toString()); |
| | | if(null == ucUserInfo || !StringUtils.hasLength(ucUserInfo.getMobile())){ |
| | | log.info("用户信息不存在,跳过"); |
| | | continue; |
| | | } |
| | | XpCoupon coupon = xpCouponClient.getCouponById(xpAppUserCoupon.getCouponId()).getData(); |
| | | SmsVo vo = new SmsVo(); |
| | | vo.setAppId(sms_appid); |
| | | vo.setPushType(1); |
| | | vo.setReceivers(ucUserInfo.getMobile()); |
| | | vo.setTemplateCode("RTActivityStartTpl"); |
| | | vo.setAppPushConfigCode("RTActivityStartSms"); |
| | | vo.setParams(new HashMap<>()); |
| | | vo.getParams().put("name", xpAppUserCoupon.getCouponName()); |
| | | SmsDto smsDto = smsClient.pushSms(vo); |
| | | if(null == smsDto || smsDto.getCode() != 200){ |
| | | log.info("短信发送失败"); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | } |