You've already forked FrameTour-BE
优惠券
This commit is contained in:
@@ -1,9 +1,44 @@
|
|||||||
package com.ycwl.basic.controller.mobile;
|
package com.ycwl.basic.controller.mobile;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import com.ycwl.basic.service.mobile.CouponRecordService;
|
||||||
|
import com.ycwl.basic.utils.ApiResponse;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/mobile/controller/v1")
|
@RequestMapping("/api/mobile/coupon/v1")
|
||||||
public class AppCouponController {
|
public class AppCouponController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CouponRecordService couponRecordService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据memberId和faceId查找优惠券记录
|
||||||
|
*/
|
||||||
|
@GetMapping("/records")
|
||||||
|
public ApiResponse<List<CouponRecordEntity>> getCouponRecords(
|
||||||
|
@RequestParam Long memberId,
|
||||||
|
@RequestParam Long faceId) {
|
||||||
|
List<CouponRecordEntity> records = couponRecordService.queryByMemberIdAndFaceId(memberId, faceId);
|
||||||
|
return ApiResponse.success(records);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 领取优惠券
|
||||||
|
*/
|
||||||
|
@PostMapping("/claim")
|
||||||
|
public ApiResponse<CouponRecordEntity> claimCoupon(
|
||||||
|
@RequestParam Long memberId,
|
||||||
|
@RequestParam Long faceId,
|
||||||
|
@RequestParam Integer type) {
|
||||||
|
try {
|
||||||
|
CouponRecordEntity record = couponRecordService.claimCoupon(memberId, faceId, type);
|
||||||
|
return ApiResponse.success(record);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return ApiResponse.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,4 +9,8 @@ import java.util.List;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface CouponRecordMapper extends BaseMapper<CouponRecordEntity> {
|
public interface CouponRecordMapper extends BaseMapper<CouponRecordEntity> {
|
||||||
List<CouponRecordEntity> queryByUserWithGoodsId(Long scenicId, Long memberId, String goodsId);
|
List<CouponRecordEntity> queryByUserWithGoodsId(Long scenicId, Long memberId, String goodsId);
|
||||||
|
|
||||||
|
List<CouponRecordEntity> queryByMemberIdAndFaceId(Long memberId, Long faceId);
|
||||||
|
|
||||||
|
CouponRecordEntity queryByMemberIdAndFaceIdAndType(Long memberId, Long faceId, Integer type);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,10 @@
|
|||||||
|
package com.ycwl.basic.model.mobile.coupon.req;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ClaimCouponReq {
|
||||||
|
private Long memberId;
|
||||||
|
private Long faceId;
|
||||||
|
private Integer type;
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
package com.ycwl.basic.model.pc.couponRecord.req;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class CouponRecordPageQueryReq {
|
||||||
|
private Integer pageNum = 1;
|
||||||
|
private Integer pageSize = 10;
|
||||||
|
private Long scenicId;
|
||||||
|
private String couponName;
|
||||||
|
private Integer couponType;
|
||||||
|
}
|
@@ -0,0 +1,23 @@
|
|||||||
|
package com.ycwl.basic.model.pc.couponRecord.resp;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class CouponRecordPageResp {
|
||||||
|
private Integer id;
|
||||||
|
private Integer couponId;
|
||||||
|
private String couponName;
|
||||||
|
private Integer couponType;
|
||||||
|
private String couponTypeName;
|
||||||
|
private Long scenicId;
|
||||||
|
private String scenicName;
|
||||||
|
private Long memberId;
|
||||||
|
private Long faceId;
|
||||||
|
private Integer status;
|
||||||
|
private String statusName;
|
||||||
|
private Date createTime;
|
||||||
|
private Date usedTime;
|
||||||
|
private Long usedOrderId;
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
package com.ycwl.basic.service.mobile;
|
||||||
|
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface CouponRecordService {
|
||||||
|
|
||||||
|
List<CouponRecordEntity> queryByMemberIdAndFaceId(Long memberId, Long faceId);
|
||||||
|
|
||||||
|
CouponRecordEntity claimCoupon(Long memberId, Long faceId, Integer type);
|
||||||
|
}
|
@@ -0,0 +1,69 @@
|
|||||||
|
package com.ycwl.basic.service.mobile.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.ycwl.basic.mapper.CouponMapper;
|
||||||
|
import com.ycwl.basic.mapper.CouponRecordMapper;
|
||||||
|
import com.ycwl.basic.model.pc.coupon.entity.CouponEntity;
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity;
|
||||||
|
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||||
|
import com.ycwl.basic.repository.FaceRepository;
|
||||||
|
import com.ycwl.basic.service.mobile.CouponRecordService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CouponRecordServiceImpl implements CouponRecordService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CouponRecordMapper couponRecordMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CouponMapper couponMapper;
|
||||||
|
@Autowired
|
||||||
|
private FaceRepository faceRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CouponRecordEntity> queryByMemberIdAndFaceId(Long memberId, Long faceId) {
|
||||||
|
return couponRecordMapper.queryByMemberIdAndFaceId(memberId, faceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public CouponRecordEntity claimCoupon(Long memberId, Long faceId, Integer type) {
|
||||||
|
// 检查是否已经领取过该类型的优惠券
|
||||||
|
CouponRecordEntity existingRecord = couponRecordMapper.queryByMemberIdAndFaceIdAndType(memberId, faceId, type);
|
||||||
|
if (existingRecord != null) {
|
||||||
|
throw new RuntimeException("该用户已经领取过此类型的优惠券");
|
||||||
|
}
|
||||||
|
FaceEntity face = faceRepository.getFace(faceId);
|
||||||
|
if (face == null) {
|
||||||
|
throw new RuntimeException("人脸数据不存在");
|
||||||
|
}
|
||||||
|
// 查找可用的优惠券
|
||||||
|
Long scenicId = face.getScenicId();
|
||||||
|
QueryWrapper<CouponEntity> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("scenic_id", scenicId)
|
||||||
|
.eq("type", type)
|
||||||
|
.eq("status", 1); // 开启状态
|
||||||
|
CouponEntity coupon = couponMapper.selectOne(queryWrapper);
|
||||||
|
|
||||||
|
if (coupon == null) {
|
||||||
|
throw new RuntimeException("未找到可领取的优惠券");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建优惠券记录
|
||||||
|
CouponRecordEntity record = new CouponRecordEntity();
|
||||||
|
record.setCouponId(coupon.getId());
|
||||||
|
record.setMemberId(memberId);
|
||||||
|
record.setFaceId(faceId);
|
||||||
|
record.setStatus(0); // 有效状态
|
||||||
|
record.setCreateTime(new Date());
|
||||||
|
|
||||||
|
couponRecordMapper.insert(record);
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,10 @@
|
|||||||
|
package com.ycwl.basic.service.pc;
|
||||||
|
|
||||||
|
import com.github.pagehelper.PageInfo;
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.req.CouponRecordPageQueryReq;
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.resp.CouponRecordPageResp;
|
||||||
|
import com.ycwl.basic.utils.ApiResponse;
|
||||||
|
|
||||||
|
public interface CouponRecordService {
|
||||||
|
ApiResponse<PageInfo<CouponRecordPageResp>> pageQuery(CouponRecordPageQueryReq query);
|
||||||
|
}
|
@@ -0,0 +1,30 @@
|
|||||||
|
package com.ycwl.basic.service.pc.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
|
import com.github.pagehelper.PageInfo;
|
||||||
|
import com.ycwl.basic.mapper.CouponRecordMapper;
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity;
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.req.CouponRecordPageQueryReq;
|
||||||
|
import com.ycwl.basic.model.pc.couponRecord.resp.CouponRecordPageResp;
|
||||||
|
import com.ycwl.basic.service.pc.CouponRecordService;
|
||||||
|
import com.ycwl.basic.utils.ApiResponse;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CouponRecordServiceImpl extends ServiceImpl<CouponRecordMapper, CouponRecordEntity> implements CouponRecordService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CouponRecordMapper couponRecordMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApiResponse<PageInfo<CouponRecordPageResp>> pageQuery(CouponRecordPageQueryReq query) {
|
||||||
|
PageHelper.startPage(query.getPageNum(), query.getPageSize());
|
||||||
|
List<CouponRecordPageResp> list = couponRecordMapper.selectByPageQuery(query);
|
||||||
|
PageInfo<CouponRecordPageResp> pageInfo = new PageInfo<>(list);
|
||||||
|
return ApiResponse.success(pageInfo);
|
||||||
|
}
|
||||||
|
}
|
@@ -524,6 +524,7 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
order.setCouponId(null);
|
order.setCouponId(null);
|
||||||
if (couponRecord != null) {
|
if (couponRecord != null) {
|
||||||
if (couponRecord.isUsable()) {
|
if (couponRecord.isUsable()) {
|
||||||
|
log.info("优惠券可用,优惠券记录ID:{},优惠券ID:{}", couponRecord.getId(), couponRecord.getCouponId());
|
||||||
order.setCouponId(couponRecord.getCouponId());
|
order.setCouponId(couponRecord.getCouponId());
|
||||||
order.setCouponRecordId(couponRecord.getId());
|
order.setCouponRecordId(couponRecord.getId());
|
||||||
order.setCouponPrice(couponRecord.getCoupon().calculateDiscountPrice(order.getPrice()));
|
order.setCouponPrice(couponRecord.getCoupon().calculateDiscountPrice(order.getPrice()));
|
||||||
|
@@ -5,4 +5,18 @@
|
|||||||
resultType="com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity">
|
resultType="com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity">
|
||||||
select * from coupon_record where member_id = #{memberId} and coupon_id in (select id from coupon where scenic_id = #{scenicId} and FIND_IN_SET(#{goodsId},config_ids))
|
select * from coupon_record where member_id = #{memberId} and coupon_id in (select id from coupon where scenic_id = #{scenicId} and FIND_IN_SET(#{goodsId},config_ids))
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="queryByMemberIdAndFaceId"
|
||||||
|
resultType="com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity">
|
||||||
|
select * from coupon_record where member_id = #{memberId} and face_id = #{faceId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="queryByMemberIdAndFaceIdAndType"
|
||||||
|
resultType="com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity">
|
||||||
|
select cr.*
|
||||||
|
from coupon_record cr
|
||||||
|
inner join coupon c on cr.coupon_id = c.id
|
||||||
|
where cr.member_id = #{memberId} and cr.face_id = #{faceId} and c.type = #{type}
|
||||||
|
limit 1
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
Reference in New Issue
Block a user