优惠券模块开启事务

优惠券记录开发
This commit is contained in:
jieyuu 2024-08-07 11:14:25 +08:00
parent aec4b87b2a
commit 589c6dee0b
7 changed files with 226 additions and 16 deletions

View File

@ -3,8 +3,10 @@ package net.jieyuu;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @SpringBootApplication
@EnableTransactionManagement
@MapperScan("net.jieyuu.mapper") @MapperScan("net.jieyuu.mapper")
public class CouponApplication { public class CouponApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -1,9 +1,16 @@
package net.jieyuu.controller; package net.jieyuu.controller;
import org.springframework.web.bind.annotation.RequestMapping; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.jieyuu.enums.BizCodeEnum;
import net.jieyuu.service.CouponRecordService;
import net.jieyuu.utils.JsonData;
import net.jieyuu.vo.CouponRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController; import java.util.Map;
/** /**
* <p> * <p>
@ -14,8 +21,31 @@ import org.springframework.web.bind.annotation.RestController;
* @since 2024-07-15 * @since 2024-07-15
*/ */
@RestController @RestController
@RequestMapping("/couponRecordDO") @RequestMapping("/api/coupon_record/v1")
public class CouponRecordController { public class CouponRecordController {
@Autowired
private CouponRecordService couponRecordService;
@ApiOperation(value = "个人优惠券分页")
@GetMapping("page")
public JsonData page(@ApiParam("当前页")
@RequestParam(value = "page", defaultValue = "1") int page,
@ApiParam("显示多少条")
@RequestParam(value = "size", defaultValue = "20") int size) {
Map<String, Object> pageResult = couponRecordService.page(page, size);
return JsonData.buildSuccess(pageResult);
}
@ApiOperation("获取优惠券详情")
@GetMapping("detail/{record_id}")
public JsonData getCouponRecordDetail(@ApiParam("记录id") @PathVariable("record_id") long recordId) {
CouponRecordVO couponRecordVO = couponRecordService.findById(recordId);
return couponRecordVO == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS) : JsonData.buildSuccess(couponRecordVO);
}
} }

View File

@ -0,0 +1,33 @@
package net.jieyuu.service;
import net.jieyuu.model.CouponRecordDO;
import com.baomidou.mybatisplus.extension.service.IService;
import net.jieyuu.vo.CouponRecordVO;
import java.util.Map;
/**
* <p>
* 服务类
* </p>
*
* @author jieyuu
* @since 2024-07-15
*/
public interface CouponRecordService extends IService<CouponRecordDO> {
/**
* 分页查询领券记录
* @param page
* @param size
* @return
*/
public Map<String, Object>page(int page, int size);
/**
* 根据id查询详情
* @param recordId
* @return
*/
CouponRecordVO findById(long recordId);
}

View File

@ -0,0 +1,74 @@
package net.jieyuu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.jieyuu.interceptor.LoginInterceptor;
import net.jieyuu.model.CouponRecordDO;
import net.jieyuu.mapper.CouponRecordMapper;
import net.jieyuu.model.LoginUser;
import net.jieyuu.service.CouponRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.jieyuu.vo.CouponRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* <p>
* 服务实现类
* </p>
*
* @author jieyuu
* @since 2024-07-15
*/
@Service
public class CouponRecordServiceImpl extends ServiceImpl<CouponRecordMapper, CouponRecordDO> implements CouponRecordService {
@Autowired
private CouponRecordMapper couponRecordMapper;
@Override
public Map<String, Object> page(int page, int size) {
LoginUser loginUser = LoginInterceptor.threadLocal.get();
Page<CouponRecordDO> pageInfo = new Page<>(page, size);
IPage<CouponRecordDO> couponDOIPage = couponRecordMapper.selectPage(pageInfo, new QueryWrapper<CouponRecordDO>()
.eq("user_id", loginUser.getId())
.orderByDesc("create_time"));
Map<String, Object> pageMap = new HashMap<>(3);
pageMap.put("total_record", couponDOIPage.getTotal());
pageMap.put("total_page", couponDOIPage.getPages());
pageMap.put("current_data", couponDOIPage.getRecords().stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));
return pageMap;
}
@Override
public CouponRecordVO findById(long recordId) {
LoginUser loginUser = LoginInterceptor.threadLocal.get();
CouponRecordDO couponRecordDO = couponRecordMapper.selectOne(new QueryWrapper<CouponRecordDO>()
.eq("id", recordId)
.eq("user_id", loginUser.getId()));
if (couponRecordDO == null) {
return null;
}
return beanProcess(couponRecordDO);
}
private CouponRecordVO beanProcess(CouponRecordDO couponRecordDO) {
CouponRecordVO couponRecordVO = new CouponRecordVO();
BeanUtils.copyProperties(couponRecordDO, couponRecordVO);
return couponRecordVO;
}
}

View File

@ -16,7 +16,6 @@ import net.jieyuu.mapper.CouponMapper;
import net.jieyuu.model.CouponRecordDO; import net.jieyuu.model.CouponRecordDO;
import net.jieyuu.model.LoginUser; import net.jieyuu.model.LoginUser;
import net.jieyuu.service.CouponService; import net.jieyuu.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.jieyuu.utils.CommonUtil; import net.jieyuu.utils.CommonUtil;
import net.jieyuu.utils.JsonData; import net.jieyuu.utils.JsonData;
import net.jieyuu.vo.CouponVO; import net.jieyuu.vo.CouponVO;
@ -24,16 +23,12 @@ import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import java.time.Duration; import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -46,6 +41,7 @@ import java.util.stream.Collectors;
*/ */
@Service @Service
@Slf4j @Slf4j
public class CouponServiceImpl implements CouponService { public class CouponServiceImpl implements CouponService {
@Autowired @Autowired
@ -54,9 +50,6 @@ public class CouponServiceImpl implements CouponService {
@Autowired @Autowired
private CouponRecordMapper couponRecordMapper; private CouponRecordMapper couponRecordMapper;
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired @Autowired
private RedissonClient redissonClient; private RedissonClient redissonClient;
@ -90,6 +83,7 @@ public class CouponServiceImpl implements CouponService {
* @return * @return
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public JsonData addCoupon(long couponId, CouponCategoryEnum category) { public JsonData addCoupon(long couponId, CouponCategoryEnum category) {
LoginUser loginUser = LoginInterceptor.threadLocal.get(); LoginUser loginUser = LoginInterceptor.threadLocal.get();

View File

@ -0,0 +1,79 @@
package net.jieyuu.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class CouponRecordVO implements Serializable {
private Long id;
/**
* 优惠券id
*/
@JsonProperty("coupon_id")
private Long couponId;
/**
* 使用状态 可用 NEW,已使用USED,过期 EXPIRED;
*/
@JsonProperty("use_state")
private String useState;
/**
* 用户id
*/
@JsonProperty("user_id")
private Long userId;
/**
* 用户昵称
*/
@JsonProperty("user_name")
private String userName;
/**
* 优惠券标题
*/
@JsonProperty("coupon_title")
private String couponTitle;
/**
* 优惠券开始有效时间
*/
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", locale = "zh", timezone = "GMT+8")
@JsonProperty("start_time")
private Date startTime;
/**
* 优惠券失效时间
*/
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", locale = "zh", timezone = "GMT+8")
@JsonProperty("end_time")
private Date endTime;
/**
* 订单id
*/
@JsonProperty("order_id")
private Long orderId;
/**
* 抵扣价格
*/
private BigDecimal price;
/**
* 满多少才可以使用
*/
@JsonProperty("condition_price")
private BigDecimal conditionPrice;
}

View File

@ -1,7 +1,5 @@
package net.jieyuu.vo; package net.jieyuu.vo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;