Commit d6ee16bd authored by shiyu's avatar shiyu

签到提醒

parent a163d0e5
......@@ -215,6 +215,11 @@ public interface CommConsts {
*/
public final static double CHANNEL_COST_REBATE = 0.01;
/**
* 一次性订阅消息,签到提醒targetId为0
*/
public final static Long SIGN_IN_TARGET_ID = 0L;
/**
* 自测账号,不发送短信
*/
......
......@@ -15,6 +15,7 @@ public class DisposableSubscribeRecordEnum {
GROUPPURASE_SURPLUS_AMOUNT_PAY(4, "e6-VlkWBST_sN3PQ2Yx3iuPkxQL5Hf6cfMia3MDNinc", "尾款支付提醒"),
GROUPPURASE_CLOSER_TO_SUCCESS(5, "ekZ3s3OL6ZlWnORvvMOONfWNZsbm6DTlMNyv_2j2ijk", "拼团待成团提醒"),
GROUPPURASE_RATE(6, "nmpPtwVrFo261tcRbMvCK8NeAztDnrUubwh-04J4_Ow", "拼团进度提醒"),
SIGN_IN_REMIND(7, "bFVaiBLSMYQBfIrDpYZpwuWzh8IriIVhSdZrY0ewlWk", "签到提醒"),
;
private int type;
......
package com.wwdz.ch.core.entity;
import com.xxdxxs.entity.Entity;
import lombok.Data;
@Data
public class SignInNoticeMsg implements Entity {
/**
* 用户id
*/
private Long userId;
/**
* 用户openid
*/
private String openId;
/**
* 消息内容
*/
private String content;
}
......@@ -1077,4 +1077,40 @@ public class SendMsgServiceImpl implements SendMsgService {
}
return Result.failed();
}
@Override
public Result sendSignInNoticeMsg(SignInNoticeMsg signInNoticeMsg) {
String openId = signInNoticeMsg.getOpenId();
String token = wxAppletApi.getAppletAccessToken();
String url = SEND_SUBSCRIBE_MSG_URL + "?access_token=" + token;
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
map.put("touser", openId);
map.put("template_id", DisposableSubscribeRecordEnum.TypeEnum.SIGN_IN_REMIND.getTemplateId());
Map<String, Object> miniprogramMap = new HashMap<>();
miniprogramMap.put("appid", APPLET_APPID);
map.put("page", CommConsts.HOMEPAGE_URL);
Map<String, Object> paramMap = new LinkedHashMap<>();
paramMap.put("thing10", new HashMap() {{
put("value", "今天尚未签到");
}});
paramMap.put("thing4", new HashMap() {{
put("value", signInNoticeMsg.getContent());
}});
map.put("data", paramMap);
logger.info("============ 拼团签到提醒订阅消息内容 : {}", JsonUtils.fromMap(map));
okHttpUtil.addParams(map);
okHttpUtil.post(true);
String responseStr = okHttpUtil.async();
logger.info("openid : {}, 发送签到进度提醒订阅消息 response :{}", openId, responseStr);
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
if (!"0".equals(errorCode)) {
logger.error("openid : {}, 发送签到提醒订阅消息, errorCode : {}, errmsg : {} ", openId, errorCode, errmsg);
return Result.failed(responseStr);
}
logger.info("============= 发送签到提醒订阅消息内容成功 ===========");
return Result.success();
}
}
......@@ -147,4 +147,12 @@ public interface SendMsgService {
* @return
*/
Result exchangeNoticeMsg(ExchangeNoticeMsg exchangeNoticeMsg);
/**
* 签到提醒
* @param signInNoticeMsg
* @return
*/
Result sendSignInNoticeMsg(SignInNoticeMsg signInNoticeMsg);
}
......@@ -2,12 +2,14 @@ package com.wwdz.ch.wx.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.consts.DisposableSubscribeRecordEnum;
import com.wwdz.ch.core.consts.DistributionEnum;
import com.wwdz.ch.core.consts.SigninEnum;
import com.wwdz.ch.core.entity.SignDateRecord;
import com.wwdz.ch.core.entity.SupplierItemVo;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.*;
import com.wwdz.ch.db.dao.DisposableSubscribeRecordDao;
import com.wwdz.ch.db.dao.SignInShareImageDao;
import com.wwdz.ch.db.dao.UserSignInDao;
import com.wwdz.ch.db.dao.distribution.DistributionOrderDao;
......@@ -61,6 +63,9 @@ public class UserSigninServiceImpl implements UserSigninService {
@Autowired
RedisUtils redisUtils;
@Autowired
DisposableSubscribeRecordDao disposableSubscribeRecordDao;
@Override
public Result signIn(UserSignInRequestDto dto) {
try {
......@@ -425,6 +430,11 @@ public class UserSigninServiceImpl implements UserSigninService {
DayOfWeek dayOfWeek = today.getDayOfWeek();
String weekDay = dayOfWeek.getDisplayName(java.time.format.TextStyle.FULL, java.util.Locale.CHINESE);
map.put("weekDay", weekDay);
if (disposableSubscribeRecordDao.isSubscribed(dto.getUserId(), CommConsts.SIGN_IN_TARGET_ID, DisposableSubscribeRecordEnum.TypeEnum.SIGN_IN_REMIND.getType())) {
map.put("isSubscribed", true);
} else {
map.put("isSubscribed", false);
}
return Result.success(map);
} catch (Exception e) {
logger.error("获取分享信息失败 error : {}", e);
......
......@@ -41,6 +41,7 @@ public class DisposableSubscribeRecordServiceImpl implements DisposableSubscribe
DisposableSubscribeRecord disposableSubscribeRecord = new DisposableSubscribeRecord();
EntityMapper.copyAttribute(dto, disposableSubscribeRecord);
disposableSubscribeRecord.setType(DisposableSubscribeRecordEnum.TypeEnum.getTypeByTemplateId(templateId));
//签到消息订阅,targetId前端固定传0
disposableSubscribeRecord.setOpenid(openid);
disposableSubscribeRecord.setIsValid(true);
disposableSubscribeRecord.setCreateTime(new Date());
......
package com.wwdz.ch.wx.job;
import com.github.pagehelper.PageInfo;
import com.wwdz.ch.core.api.WxAppletApi;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.consts.DisposableSubscribeRecordEnum;
import com.wwdz.ch.core.consts.DistributionEnum;
import com.wwdz.ch.core.entity.AuctionOfferNoticeMsg;
import com.wwdz.ch.core.entity.SignInNoticeMsg;
import com.wwdz.ch.core.notify.AliSmsSender;
import com.wwdz.ch.core.service.SendMsgService;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.RedisUtils;
import com.wwdz.ch.db.dao.DisposableSubscribeRecordDao;
import com.wwdz.ch.db.dao.FollowFansRecordDao;
import com.wwdz.ch.db.dao.UserSignInDao;
import com.wwdz.ch.db.dao.distribution.ShopWhiteListDao;
import com.wwdz.ch.db.dao.distribution.SupplierItemDao;
import com.wwdz.ch.db.domain.FollowFansRecord;
import com.wwdz.ch.db.domain.UserSignIn;
import com.wwdz.ch.db.domain.distribution.DisposableSubscribeRecord;
import com.wwdz.ch.db.domain.distribution.SpecialPerformance;
import com.wwdz.ch.db.domain.distribution.SupplierItem;
import com.wwdz.ch.db.dto.request.FollowFansRecordRequestDto;
import com.wwdz.ch.db.dto.request.distribution.DisposableSubscribeRecordRequestDto;
import com.wwdz.ch.db.dto.request.distribution.SupplierItemRequestDto;
import com.xxdxxs.utils.DateUtils;
import com.xxdxxs.utils.JsonUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* 藏品上新通知
*/
@Component
public class SignInNoticeJob {
private static final Logger logger = LoggerFactory.getLogger(SignInNoticeJob.class);
private final static String SIGN_IN_NOTICE_KEY = "SIGN_IN_NOTICE_KEY";
@Autowired
RedissonClient redissonClient;
@Autowired
RedisUtils redisUtils;
@Autowired
DisposableSubscribeRecordDao disposableSubscribeRecordDao;
@Autowired
UserSignInDao userSignInDao;
@Autowired
WxAppletApi wxAppletApi;
@Autowired
SendMsgService sendMsgService;
@Value("${spring.profiles.active}")
private String env;
/**
* 每天下午两点执行一次
*/
@Scheduled(cron = "0 0 14 * * ?")
public void execute() {
/* if ("dev".equals(env)) {
logger.info(">>>>>>>>>>>>>>>>>>>>>>> dev环境, 不执行发送签到提醒通知定时作业<<<<<<<<<<<<<<<<<<<<<");
return;
}*/
RLock lock = redissonClient.getLock(SIGN_IN_NOTICE_KEY);
if (!lock.tryLock()) {
logger.warn("发送签到提醒通知,当前服务实例获取锁成功: {} 获取锁失败,锁被占用不执行", Thread.currentThread().getId());
return;
}
logger.info(">>>>>>>>>>>>>>>>>>>>>>> 发送商品上新通知定时作业, 开始执行 <<<<<<<<<<<<<<<<<<<<<");
try {
//发送专场开始订阅通知
DisposableSubscribeRecordRequestDto disposableSubscribeRecordRequestDto = new DisposableSubscribeRecordRequestDto();
disposableSubscribeRecordRequestDto.setType(DisposableSubscribeRecordEnum.TypeEnum.SIGN_IN_REMIND.getType());
boolean hasNextPage = true;
int page = 1;
while (hasNextPage) {
List<DisposableSubscribeRecord> disposableSubscribeRecordList = disposableSubscribeRecordDao.findList(disposableSubscribeRecordRequestDto);
PageInfo pageInfo = new PageInfo(disposableSubscribeRecordList);
for (DisposableSubscribeRecord disposableSubscribeRecord : disposableSubscribeRecordList) {
long userId = disposableSubscribeRecord.getUserId();
int needConsecutiveDays = 7;
int consecutiveDays = 0;
//检查昨天是否打过卡
UserSignIn yesterdaySignIn = userSignInDao.findYesterdayRecord(userId);
//查询今天的打卡记录
UserSignIn todayRecord = userSignInDao.findTodayRecord(userId);
if (yesterdaySignIn == null && todayRecord == null) {
//连续签到天数
consecutiveDays = 0;
needConsecutiveDays = 7;
} else if (yesterdaySignIn == null && todayRecord != null) {
consecutiveDays = todayRecord.getConsecutiveDays();
needConsecutiveDays = 6;
} else {
if (todayRecord != null) {
consecutiveDays = todayRecord.getConsecutiveDays();
} else {
consecutiveDays = yesterdaySignIn.getConsecutiveDays();
}
if (consecutiveDays < 7) {
needConsecutiveDays = 7 - consecutiveDays;
} else if (consecutiveDays >= 7 && consecutiveDays < 14) {
needConsecutiveDays = 14 - consecutiveDays;
} else if (consecutiveDays >=14 && consecutiveDays < 21) {
needConsecutiveDays = 21 - consecutiveDays;
} else {
needConsecutiveDays = 7;
}
}
if (todayRecord == null) {
String content = "您再签到" + needConsecutiveDays + "天,即可获取奖励";
sendSignInNoticeMsg(disposableSubscribeRecord.getTemplateId(), disposableSubscribeRecord.getOpenid(), content);
}
}
if (pageInfo.isHasNextPage()) {
page = page + 1;
disposableSubscribeRecordRequestDto.setPage(page);
hasNextPage = pageInfo.isHasNextPage();
logger.info("要发送专场开始订阅消息的用户数据总页数为 : {}, 当前为 : {}", pageInfo.getPages(), page);
} else {
logger.info("============= 发送专场开始订阅消息循环结束 =============");
break;
}
}
} catch (Exception e) {
logger.error("商品上新通知定时作业 error {}", e);
} finally {
if (lock != null && lock.isHeldByCurrentThread()) {
lock.unlock();
logger.info("======================== 线程id: {} , 发送商品上新通知定时作业执行完毕, 释放锁成功 ========================", Thread.currentThread().getId());
}
}
}
private void sendSignInNoticeMsg(String templateId, String openId, String msg){
try {
SignInNoticeMsg signInNoticeMsg = new SignInNoticeMsg();
signInNoticeMsg.setOpenId(openId);
signInNoticeMsg.setContent(msg);
Result result = sendMsgService.sendSignInNoticeMsg(signInNoticeMsg);
if (!result.getSuccess()) {
String errorCode = JsonUtils.getValueByPath(result.getErrmsg(), "errcode");
//用户为订阅消息,43101用户未订阅消息;43108并发下发消息给同一个粉丝,这两种情况表示用户不需要被通知
if (!"43101".equals(errorCode) && !"43108".equals(errorCode)) {
logger.error("发送签到订阅消息失败");
}
}
//订阅状态改为无效
disposableSubscribeRecordDao.updateInvalid(openId, CommConsts.SIGN_IN_TARGET_ID, templateId);
} catch (Exception e) {
logger.error("发送签到订阅消息失败:{}", e);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment