Commit c4a74f35 by caiyt

新增接口:赠送优惠券

parent cb214744
package com.soss.web.controller.coupon; package com.soss.web.controller.coupon;
import org.springframework.web.bind.annotation.RequestMapping; import com.soss.common.annotation.Log;
import org.springframework.web.bind.annotation.RestController; import com.soss.common.core.domain.AjaxResult;
import com.soss.common.enums.BusinessType;
import com.soss.system.domain.po.CouponPo;
import com.soss.system.service.ICouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
/** /**
* <p> * <p>
...@@ -14,6 +22,20 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -14,6 +22,20 @@ import org.springframework.web.bind.annotation.RestController;
*/ */
@RestController @RestController
@RequestMapping("/coupon-user") @RequestMapping("/coupon-user")
@Api(tags = "用户领取优惠券记录服务类")
public class CouponUserController { public class CouponUserController {
@Autowired
private ICouponUserService couponUserService;
/**
* 赠送优惠券
*/
@PreAuthorize("@ss.hasPermi('coupon:user:give')")
@Log(title = "赠送优惠券", businessType = BusinessType.INSERT)
@PutMapping("/{userId}/{userPhone}/{couponId}")
@ApiOperation("赠送优惠券")
public AjaxResult giveUserCoupon(@PathVariable Long userId, @PathVariable String userPhone, @PathVariable Integer couponId) {
couponUserService.giveUserCoupon(userId, userPhone, couponId);
return AjaxResult.success();
}
} }
...@@ -8,16 +8,17 @@ import java.time.LocalDateTime; ...@@ -8,16 +8,17 @@ import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
/** /**
* 时间工具类 * 时间工具类
* *
* @author zxq * @author zxq
*/ */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
{
public static String YYYY = "yyyy"; public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM"; public static String YYYY_MM = "yyyy-MM";
...@@ -29,63 +30,52 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -29,63 +30,52 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = { private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/** /**
* 获取当前Date型日期 * 获取当前Date型日期
* *
* @return Date() 当前日期 * @return Date() 当前日期
*/ */
public static Date getNowDate() public static Date getNowDate() {
{
return new Date(); return new Date();
} }
/** /**
* 获取当前日期, 默认格式为yyyy-MM-dd * 获取当前日期, 默认格式为yyyy-MM-dd
* *
* @return String * @return String
*/ */
public static String getDate() public static String getDate() {
{
return dateTimeNow(YYYY_MM_DD); return dateTimeNow(YYYY_MM_DD);
} }
public static final String getTime() public static final String getTime() {
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS); return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
} }
public static final String dateTimeNow() public static final String dateTimeNow() {
{
return dateTimeNow(YYYYMMDDHHMMSS); return dateTimeNow(YYYYMMDDHHMMSS);
} }
public static final String dateTimeNow(final String format) public static final String dateTimeNow(final String format) {
{
return parseDateToStr(format, new Date()); return parseDateToStr(format, new Date());
} }
public static final String dateTime(final Date date) public static final String dateTime(final Date date) {
{
return parseDateToStr(YYYY_MM_DD, date); return parseDateToStr(YYYY_MM_DD, date);
} }
public static final String parseDateToStr(final String format, final Date date) public static final String parseDateToStr(final String format, final Date date) {
{
return new SimpleDateFormat(format).format(date); return new SimpleDateFormat(format).format(date);
} }
public static final Date dateTime(final String format, final String ts) public static final Date dateTime(final String format, final String ts) {
{ try {
try
{
return new SimpleDateFormat(format).parse(ts); return new SimpleDateFormat(format).parse(ts);
} } catch (ParseException e) {
catch (ParseException e)
{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
...@@ -93,8 +83,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -93,8 +83,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 日期路径 即年/月/日 如2018/08/08 * 日期路径 即年/月/日 如2018/08/08
*/ */
public static final String datePath() public static final String datePath() {
{
Date now = new Date(); Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd"); return DateFormatUtils.format(now, "yyyy/MM/dd");
} }
...@@ -102,8 +91,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -102,8 +91,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 日期路径 即年/月/日 如20180808 * 日期路径 即年/月/日 如20180808
*/ */
public static final String dateTime() public static final String dateTime() {
{
Date now = new Date(); Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd"); return DateFormatUtils.format(now, "yyyyMMdd");
} }
...@@ -111,18 +99,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -111,18 +99,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 日期型字符串转化为日期 格式 * 日期型字符串转化为日期 格式
*/ */
public static Date parseDate(Object str) public static Date parseDate(Object str) {
{ if (str == null) {
if (str == null)
{
return null; return null;
} }
try try {
{
return parseDate(str.toString(), parsePatterns); return parseDate(str.toString(), parsePatterns);
} } catch (ParseException e) {
catch (ParseException e)
{
return null; return null;
} }
} }
...@@ -130,8 +113,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -130,8 +113,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 获取服务器启动时间 * 获取服务器启动时间
*/ */
public static Date getServerStartDate() public static Date getServerStartDate() {
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime(); long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time); return new Date(time);
} }
...@@ -139,16 +121,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -139,16 +121,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 计算相差天数 * 计算相差天数
*/ */
public static int differentDaysByMillisecond(Date date1, Date date2) public static int differentDaysByMillisecond(Date date1, Date date2) {
{
return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
} }
/** /**
* 计算两个时间差 * 计算两个时间差
*/ */
public static String getDatePoor(Date endDate, Date nowDate) public static String getDatePoor(Date endDate, Date nowDate) {
{
long nd = 1000 * 24 * 60 * 60; long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60; long nh = 1000 * 60 * 60;
long nm = 1000 * 60; long nm = 1000 * 60;
...@@ -169,8 +149,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -169,8 +149,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 增加 LocalDateTime ==> Date * 增加 LocalDateTime ==> Date
*/ */
public static Date toDate(LocalDateTime temporalAccessor) public static Date toDate(LocalDateTime temporalAccessor) {
{
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant()); return Date.from(zdt.toInstant());
} }
...@@ -178,10 +157,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils ...@@ -178,10 +157,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* 增加 LocalDate ==> Date * 增加 LocalDate ==> Date
*/ */
public static Date toDate(LocalDate temporalAccessor) public static Date toDate(LocalDate temporalAccessor) {
{
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant()); return Date.from(zdt.toInstant());
} }
public static LocalDateTime addDaysAndGetBegin(LocalDateTime targetTime, int days) {
targetTime.plusDays(days);
return targetTime.toLocalDate().atTime(0, 0, 0, 0);
}
} }
...@@ -25,7 +25,7 @@ public class CouponUser extends BaseEntity { ...@@ -25,7 +25,7 @@ public class CouponUser extends BaseEntity {
/** /**
* 用户ID * 用户ID
*/ */
private String userId; private Long userId;
/** /**
* 用户名 * 用户名
......
package com.soss.system.mapper; package com.soss.system.mapper;
import com.soss.system.domain.CouponUser;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
/** /**
...@@ -18,4 +19,20 @@ public interface CouponUserMapper { ...@@ -18,4 +19,20 @@ public interface CouponUserMapper {
* @return * @return
*/ */
int getCouponReceiveCount(@Param("couponId") Integer couponId); int getCouponReceiveCount(@Param("couponId") Integer couponId);
/**
* 保存用户领取优惠券记录
*
* @param couponUser
* @return
*/
int insertCouponUser(CouponUser couponUser);
/**
* 更新用户领取优惠券记录
*
* @param couponUser
* @return
*/
int updateCouponUser(CouponUser couponUser);
} }
...@@ -10,4 +10,5 @@ package com.soss.system.service; ...@@ -10,4 +10,5 @@ package com.soss.system.service;
*/ */
public interface ICouponUserService { public interface ICouponUserService {
void giveUserCoupon(Long userId, String phone, Integer couponId);
} }
...@@ -124,6 +124,8 @@ public class CouponServiceImpl implements ICouponService { ...@@ -124,6 +124,8 @@ public class CouponServiceImpl implements ICouponService {
ruleDesc = "满" + couponPo.getPriceLimit() + "元"; ruleDesc = "满" + couponPo.getPriceLimit() + "元";
} }
} }
Assert.isTrue(couponPo.getDaysLimit() > 0 && couponPo.getUserLimit() != null,
"用户可领取次数限制还未设置完全");
Assert.notNull(couponPo.getUseStartTime(), "优惠券的绝对有效时间还未设置"); Assert.notNull(couponPo.getUseStartTime(), "优惠券的绝对有效时间还未设置");
Assert.notNull(couponPo.getUseEndTime(), "优惠券的绝对有效时间还未设置"); Assert.notNull(couponPo.getUseEndTime(), "优惠券的绝对有效时间还未设置");
Assert.isTrue((couponPo.getCategoryIds() != null && couponPo.getCategoryIds().length > 0) Assert.isTrue((couponPo.getCategoryIds() != null && couponPo.getCategoryIds().length > 0)
......
package com.soss.system.service.impl; package com.soss.system.service.impl;
import com.soss.common.core.domain.entity.SysUser;
import com.soss.common.enums.CouponState;
import com.soss.common.exception.ServiceException;
import com.soss.common.utils.DateUtils;
import com.soss.system.domain.Coupon;
import com.soss.system.domain.CouponRule;
import com.soss.system.domain.CouponUser;
import com.soss.system.mapper.CouponMapper;
import com.soss.system.mapper.CouponRuleMapper;
import com.soss.system.mapper.CouponUserMapper;
import com.soss.system.mapper.SysUserMapper;
import com.soss.system.service.ICouponUserService; import com.soss.system.service.ICouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.time.LocalDateTime;
import java.util.Objects;
/** /**
* <p> * <p>
...@@ -13,5 +29,44 @@ import org.springframework.stereotype.Service; ...@@ -13,5 +29,44 @@ import org.springframework.stereotype.Service;
*/ */
@Service @Service
public class CouponUserServiceImpl implements ICouponUserService { public class CouponUserServiceImpl implements ICouponUserService {
@Autowired
private CouponUserMapper couponUserMapper;
@Autowired
private CouponMapper couponMapper;
@Autowired
private CouponRuleMapper couponRuleMapper;
@Autowired
private SysUserMapper userMapper;
@Override
public void giveUserCoupon(Long userId, String phone, Integer couponId) {
SysUser user = userMapper.selectUserById(userId);
Assert.isTrue(user != null && Objects.equals(user.getPhonenumber(), phone),
"用户信息不匹配[id=" + userId + "][phone=" + phone + "]");
Coupon coupon = couponMapper.selectCouponById(couponId);
Assert.isTrue(coupon != null && Objects.equals(coupon.getState(), CouponState.ONLINE.getState()), "优惠券状态不合法");
CouponRule couponRule = couponRuleMapper.selectCouponRuleById(coupon.getRuleId());
Assert.notNull(couponRule, "未查询到匹配的优惠券规则[id=" + coupon.getRuleId() + "]");
LocalDateTime now = LocalDateTime.now();
if (couponRule.getUseStartTime().isAfter(now) || couponRule.getUseEndTime().isBefore(now)) {
throw new ServiceException("优惠券不在有效期内");
}
CouponUser couponUser = new CouponUser();
couponUser.setUserId(userId);
couponUser.setUserName(user.getUserName());
couponUser.setUserPhone(user.getPhonenumber());
couponUser.setCouponId(couponId);
couponUser.setReceiveTime(now);
if (couponRule.getRelativeTime() != null && couponRule.getRelativeTime() > 0) {
LocalDateTime expiredTime = DateUtils.addDaysAndGetBegin(now, couponRule.getRelativeTime() + 1);
expiredTime = expiredTime.isBefore(couponRule.getUseEndTime()) ? expiredTime : couponRule.getUseEndTime();
couponUser.setExpiredTime(expiredTime);
} else {
couponUser.setExpiredTime(couponRule.getUseEndTime());
}
couponUser.setType(2);
couponUser.setCreatedAt(now);
couponUser.setUpdatedAt(now);
couponUserMapper.insertCouponUser(couponUser);
}
} }
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