Authored by chenjian

订单分享领券判断当天是否领过

package com.yoho.activity.common.bo;
public class OrderShareCouponVO {
//是否成功获取到优惠码
private boolean drawCouponSuccess = false;
//优惠券ID
private String couponId;
//优惠券名称
private String couponName;
//优惠码
private String couponCode;
public String getCouponId() {
return couponId;
}
public void setCouponId(String couponId) {
this.couponId = couponId;
}
public String getCouponName() {
return couponName;
}
public void setCouponName(String couponName) {
this.couponName = couponName;
}
public String getCouponCode() {
return couponCode;
}
public void setCouponCode(String couponCode) {
this.couponCode = couponCode;
}
public boolean isDrawCouponSuccess() {
return drawCouponSuccess;
}
public void setDrawCouponSuccess(boolean drawCouponSuccess) {
this.drawCouponSuccess = drawCouponSuccess;
}
}
... ...
... ... @@ -23,7 +23,12 @@ public class Constant {
// 已注册用户领取的人数
public final static String USED_REGISTER_GET_TIME_MEM_KEY = "yh:users:used_register_get_time_";
// 用户分享领券新用户
public final static int ORDER_SHARE_USERTYPE_NEW = 0;
// 用户分享领券老用户
public final static int ORDER_SHARE_USERTYPE_OLD = 1;
//订单分享用户领券返回码
public final static int ORDER_SHARE_MOBILE_ERROR = 1;
public final static int ORDER_SHARE_USER_UNREGISTER = 2;
... ... @@ -37,4 +42,7 @@ public class Constant {
public final static int ORDER_SHARE_REGISTER_ERROR = 6;
public final static int ORDER_SHARE_SENDCOUPON_ERROR = 7;
//当天已经领取过
public final static int ORDER_SHARE_ALREADYDRAW_ERROR = 8;
}
... ...
... ... @@ -129,4 +129,16 @@ public final class DateUtils {
return second;
}
/**
* 获取当天零点时间
* @return
*/
public static int getTodayZero() {
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
return (int)(today.getTime().getTime() / 1000);
}
}
... ...
package com.yoho.coupon.dal;
import org.apache.ibatis.annotations.Param;
import com.yoho.coupon.dal.model.UserCouponHistory;
public interface IUserCouponHistoryDAO {
... ... @@ -21,4 +23,13 @@ public interface IUserCouponHistoryDAO {
* @return
*/
UserCouponHistory selectBySelective(UserCouponHistory record);
/**
* 查看某用户某段时间以来领券次数
* @param uid
* @param activityId
* @param createTime
* @return
*/
int selectCountAfter(@Param("uid") int uid, @Param("activityId") int activityId, @Param("createTime") int createTime);
}
\ No newline at end of file
... ...
... ... @@ -20,6 +20,13 @@
from user_coupon_history
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectCountAfter" resultType="java.lang.Integer">
select count(1)
from user_coupon_history
where uid = #{uid,jdbcType=INTEGER}
and activity_id = #{activityId,jdbcType=INTEGER}
and create_time > #{createTime,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user_coupon_history
where id = #{id,jdbcType=INTEGER}
... ...
... ... @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
import com.yoho.activity.common.ApiResponse;
import com.yoho.activity.common.bo.OrderShareActivityBO;
import com.yoho.activity.common.bo.OrderShareBO;
import com.yoho.activity.common.bo.OrderShareCouponVO;
import com.yoho.activity.common.constatns.Constant;
import com.yoho.activity.common.convert.OrderShareActivityConvert;
import com.yoho.activity.common.helper.SendSMSHelper;
... ... @@ -265,6 +266,14 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
return respBO;
}
//判断老用户今天是否已经领券过
if(isDrawCouponToday(profileInfo.getUid(), activityInfo.getId())) {
respBO.setReturnCode(Constant.ORDER_SHARE_ALREADYDRAW_ERROR);
respBO.setReturnMsg("您今天已经领过咯,速度去有货购潮流!");
log.warn("unable to draw more today, uid: {}, activityId: {}", profileInfo.getUid(), activityInfo.getId());
return respBO;
}
// 4、判断老用户是否达到分享限制
if (isUpToOrderShareLimit(orderCode, activityInfo.getId())) {
respBO.setReturnCode(Constant.ORDER_SHARE_OLDUSER_UPLIMIT);
... ... @@ -275,18 +284,18 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
// 发送优惠券
// 获取老人优惠券
OrderShareCoupon coupon = orderShareCouponDAO.selectCouponByDateAndType(DateUtils.getToday("yyyy-MM-dd"), 1);
String couponCode = sendCoupon(coupon.getCouponId(), String.valueOf(profileInfo.getUid()));
if (StringUtils.isEmpty(couponCode)) {
OrderShareCouponVO couponVO = drawCoupon(profileInfo.getUid(), Constant.ORDER_SHARE_USERTYPE_OLD);
if (!couponVO.isDrawCouponSuccess()) {
respBO.setReturnCode(Constant.ORDER_SHARE_SENDCOUPON_ERROR);
respBO.setReturnMsg("优惠券已发光");
log.error("orderShare sendCoupon failed, uid: {}, couponId: {}", profileInfo.getUid(), coupon.getCouponId());
log.error("orderShare sendCoupon failed, uid: {}, couponId: {}", profileInfo.getUid(), couponVO.getCouponId());
return respBO;
}
log.info("orderShare drawCoupon success, uid: {}, couponCode: {}", profileInfo.getUid(), couponVO.getCouponCode());
// 4、记录用户领券的记录
respBO.setCoupon(couponCode);
updateOrderShareRecord(activityInfo.getId(), profileInfo.getUid(), orderCode, coupon.getCouponId(), 1);
respBO.setCoupon(couponVO.getCouponCode());
updateOrderShareRecord(activityInfo.getId(), profileInfo.getUid(), orderCode, couponVO.getCouponCode(), Constant.ORDER_SHARE_USERTYPE_OLD);
return respBO;
}
... ... @@ -333,20 +342,20 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
// 3、发送优惠券
// 获取新人优惠券
OrderShareCoupon coupon = orderShareCouponDAO.selectCouponByDateAndType(null, 0);
String couponCode = sendCoupon(coupon.getCouponId(), String.valueOf(model.getUid()));
if (StringUtils.isEmpty(couponCode)) {
OrderShareCouponVO couponVO = drawCoupon(model.getUid(), Constant.ORDER_SHARE_USERTYPE_NEW);
if (!couponVO.isDrawCouponSuccess()) {
respBO.setReturnCode(Constant.ORDER_SHARE_SENDCOUPON_ERROR);
respBO.setReturnMsg("优惠券已发光");
log.error("orderShare sendCoupon failed, uid: {}, couponId: {}", coupon.getCouponId(), model.getUid());
log.error("orderShare drawCoupon failed, uid: {}", model.getUid());
return respBO;
}
log.info("orderShare drawCoupon success, uid: {}, couponCode: {}", model.getUid(), couponVO.getCouponCode());
// 4、记录用户领券的记录
respBO.setCoupon(couponCode);
updateOrderShareRecord(activityInfo.getId(), model.getUid(), orderCode, coupon.getCouponId(), 0);
respBO.setCoupon(couponVO.getCouponCode());
updateOrderShareRecord(activityInfo.getId(), model.getUid(), orderCode, couponVO.getCouponCode(), Constant.ORDER_SHARE_USERTYPE_NEW);
//发送短信
sendSMSHelper.sendNoticeSms(mobile, "【Yoho!Buy有货】恭喜您获得一张"+ coupon.getCouponName() +",您的登录账户是"+ mobile +",密码是"+ registerReqBO.getPassword() +";下载Yoho!Buy有货手机端http://a.app.qq.com/o/simple.jsp?pkgname=com.yoho,更多惊喜等着您!");
sendSMSHelper.sendNoticeSms(mobile, "【Yoho!Buy有货】恭喜您获得一张"+ couponVO.getCouponName() +",您的登录账户是"+ mobile +",密码是"+ registerReqBO.getPassword() +";下载Yoho!Buy有货手机端http://a.app.qq.com/o/simple.jsp?pkgname=com.yoho,更多惊喜等着您!");
return respBO;
}
... ... @@ -427,6 +436,23 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
}
/**
* 检查老用户当天是否已经领取过用户券
* @param uid
* @param activityId
* @return
*/
private boolean isDrawCouponToday(int uid, int activityId) {
int todayZero = DateUtils.getTodayZero();
int count = userCouponHistoryDAO.selectCountAfter(uid, activityId, todayZero);
if(count > 0) {
log.warn("the user have draw coupon today, uid: {}, count: {}", uid, count);
return false;
}
log.info("the user haven't draw coupon today, uid: {}, fromTime: {}", uid, todayZero);
return true;
}
/**
* 检查老用户是否超出领取优惠券限制(默认限制5次)
*
* @param shareHistoryInfo
... ... @@ -448,6 +474,14 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
return false;
}
/**
* 修改领券记录
* @param activityId
* @param uid
* @param orderCode
* @param couponId
* @param userType
*/
private void updateOrderShareRecord(int activityId, int uid, long orderCode, String couponId, int userType) {
addCouponHistory(activityId, uid, String.valueOf(orderCode), couponId, userType);
UserShareHistory shareHistoryInfo = getShareHistoryByOrderAndActivity(orderCode, activityId);
... ... @@ -457,4 +491,44 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
}
updateShareHistory(shareHistoryInfo.getId(), userType);
}
/**
* 领取券
* @param uid
* @param userType
* @return
*/
private OrderShareCouponVO drawCoupon(int uid, int userType) {
log.info("draw orderShare coupon: uid: {}, userType: {}", uid, userType);
OrderShareCouponVO couponVO = new OrderShareCouponVO();
//新人券、老人全不一样
OrderShareCoupon coupon = null;
if(userType == Constant.ORDER_SHARE_USERTYPE_OLD) {
coupon = orderShareCouponDAO.selectCouponByDateAndType(DateUtils.getToday("yyyy-MM-dd"), Constant.ORDER_SHARE_USERTYPE_OLD);
}
else if(userType == Constant.ORDER_SHARE_USERTYPE_NEW) {
coupon = orderShareCouponDAO.selectCouponByDateAndType(null, Constant.ORDER_SHARE_USERTYPE_NEW);
}
if(coupon == null) {
log.error("no orderShare coupon for user, uid: {}, userType: {}", uid, userType);
return couponVO;
}
couponVO.setCouponId(coupon.getCouponId());
couponVO.setCouponName(coupon.getCouponName());
//发送优惠券
log.info("orderShare sendCoupon, uid: {}, couponId: {}", uid, coupon.getCouponId());
String couponCode = sendCoupon(coupon.getCouponId(), String.valueOf(uid));
if(StringUtils.isEmpty(couponCode)) {
log.error("sendCoupon failed, uid: {}, couponId: {}", uid, coupon.getCouponId());
return couponVO;
}
couponVO.setCouponCode(couponCode);
couponVO.setDrawCouponSuccess(true); //领券成功
log.info("draw orderShare coupon result: uid: {}, coupon code: {}", uid, couponCode);
return couponVO;
}
}
... ...