Commit c4a74f35 by caiyt

新增接口:赠送优惠券

parent cb214744
package com.soss.web.controller.coupon;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.soss.common.annotation.Log;
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>
......@@ -14,6 +22,20 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/coupon-user")
@Api(tags = "用户领取优惠券记录服务类")
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,7 +8,9 @@ import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
......@@ -16,8 +18,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
*
* @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_MM = "yyyy-MM";
......@@ -38,8 +39,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
*
* @return Date() 当前日期
*/
public static Date getNowDate()
{
public static Date getNowDate() {
return new Date();
}
......@@ -48,44 +48,34 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
*
* @return String
*/
public static String getDate()
{
public static String getDate() {
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime()
{
public static final String getTime() {
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow()
{
public static final String dateTimeNow() {
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format)
{
public static final String dateTimeNow(final String format) {
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);
}
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);
}
public static final Date dateTime(final String format, final String ts)
{
try
{
public static final Date dateTime(final String format, final String ts) {
try {
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e)
{
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
......@@ -93,8 +83,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* 日期路径 即年/月/日 如2018/08/08
*/
public static final String datePath()
{
public static final String datePath() {
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
......@@ -102,8 +91,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* 日期路径 即年/月/日 如20180808
*/
public static final String dateTime()
{
public static final String dateTime() {
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
......@@ -111,18 +99,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* 日期型字符串转化为日期 格式
*/
public static Date parseDate(Object str)
{
if (str == null)
{
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try
{
try {
return parseDate(str.toString(), parsePatterns);
}
catch (ParseException e)
{
} catch (ParseException e) {
return null;
}
}
......@@ -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();
return new Date(time);
}
......@@ -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)));
}
/**
* 计算两个时间差
*/
public static String getDatePoor(Date endDate, Date nowDate)
{
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
......@@ -169,8 +149,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* 增加 LocalDateTime ==> Date
*/
public static Date toDate(LocalDateTime temporalAccessor)
{
public static Date toDate(LocalDateTime temporalAccessor) {
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
......@@ -178,10 +157,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* 增加 LocalDate ==> Date
*/
public static Date toDate(LocalDate temporalAccessor)
{
public static Date toDate(LocalDate temporalAccessor) {
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
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 {
/**
* 用户ID
*/
private String userId;
private Long userId;
/**
* 用户名
......
package com.soss.system.mapper;
import com.soss.system.domain.CouponUser;
import org.apache.ibatis.annotations.Param;
/**
......@@ -18,4 +19,20 @@ public interface CouponUserMapper {
* @return
*/
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;
*/
public interface ICouponUserService {
void giveUserCoupon(Long userId, String phone, Integer couponId);
}
......@@ -124,6 +124,8 @@ public class CouponServiceImpl implements ICouponService {
ruleDesc = "满" + couponPo.getPriceLimit() + "元";
}
}
Assert.isTrue(couponPo.getDaysLimit() > 0 && couponPo.getUserLimit() != null,
"用户可领取次数限制还未设置完全");
Assert.notNull(couponPo.getUseStartTime(), "优惠券的绝对有效时间还未设置");
Assert.notNull(couponPo.getUseEndTime(), "优惠券的绝对有效时间还未设置");
Assert.isTrue((couponPo.getCategoryIds() != null && couponPo.getCategoryIds().length > 0)
......
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.time.LocalDateTime;
import java.util.Objects;
/**
* <p>
......@@ -13,5 +29,44 @@ import org.springframework.stereotype.Service;
*/
@Service
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