package com.ruoyi.account.controller;
|
|
|
import com.alibaba.fastjson2.JSON;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.ruoyi.account.api.model.AppUser;
|
import com.ruoyi.account.api.model.UserCoupon;
|
import com.ruoyi.account.api.model.UserPoint;
|
import com.ruoyi.account.api.vo.CouponInfoVo;
|
import com.ruoyi.account.api.vo.PaymentUserCouponVo;
|
import com.ruoyi.account.service.AppUserService;
|
import com.ruoyi.account.service.UserCouponService;
|
import com.ruoyi.account.service.UserPointService;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.utils.bean.BeanUtils;
|
import com.ruoyi.account.api.vo.PaymentUserCoupon;
|
import com.ruoyi.other.api.domain.Goods;
|
import com.ruoyi.other.api.domain.PointSetting;
|
import com.ruoyi.other.api.feignClient.GoodsClient;
|
import com.ruoyi.other.api.feignClient.PointSettingClient;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
import com.ruoyi.common.security.service.TokenService;
|
import com.ruoyi.other.api.domain.CouponInfo;
|
import com.ruoyi.other.api.feignClient.CouponClient;
|
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiParam;
|
|
import java.time.LocalDateTime;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 前端控制器
|
* </p>
|
*
|
* @author luodangjia
|
* @since 2024-11-21
|
*/
|
@RestController
|
@RequestMapping("/user-coupon")
|
public class UserCouponController {
|
@Resource
|
private UserCouponService userCouponService;
|
@Resource
|
private TokenService tokenService;
|
|
@Resource
|
private CouponClient couponClient;
|
@Resource
|
private AppUserService appUserService;
|
|
@Resource
|
private UserPointService userPointService;
|
@Resource
|
private GoodsClient goodsClient;
|
|
@Resource
|
private PointSettingClient pointSettingClient;
|
|
|
|
|
@GetMapping("/mine/list")
|
@ApiOperation(value = "已领取列表", tags = {"小程序-个人中心-优惠劵"})
|
public R<Page<UserCoupon>> minelist(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @ApiParam("1未使用2已使用3已过期") Integer status) {
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
LambdaQueryChainWrapper<UserCoupon> chainWrapper = userCouponService.lambdaQuery()
|
.eq(UserCoupon::getAppUserId, userid)
|
.isNotNull(UserCoupon::getCouponInfo);
|
if(null != status && 1 == status){
|
chainWrapper.isNull(UserCoupon::getUseTime).gt(UserCoupon::getEndTime, LocalDateTime.now());
|
}
|
if(null != status && 2 == status){
|
chainWrapper.isNotNull(UserCoupon::getUseTime);
|
}
|
if(null != status && 3 == status){
|
chainWrapper.isNull(UserCoupon::getUseTime).lt(UserCoupon::getEndTime, LocalDateTime.now());
|
}
|
Page<UserCoupon> page = chainWrapper.page(Page.of(pageNum-1, pageSize));
|
for (UserCoupon record : page.getRecords()) {
|
record.setIdStr(record.getId().toString());
|
String couponInfo = record.getCouponInfo();
|
CouponInfoVo vo = JSON.parseObject(couponInfo, CouponInfoVo.class);
|
// BeanUtils.copyProperties(data,vo);
|
//如果是商品券,将商品名称返回
|
if (vo != null && vo.getCouponType()==4){
|
List<String> goodNames = new ArrayList<>();
|
if (vo.getForGoodIds().equals("-1")){
|
List<Goods> data1 = goodsClient.getAllGoods().getData();
|
List<String> collect = data1.stream().map(Goods::getName).collect(Collectors.toList());
|
goodNames.addAll(collect);
|
}else if(StringUtils.isEmpty(vo.getForGoodIds())){
|
goodNames.addAll(JSON.parseArray(vo.getGoodsNameJson(), String.class));
|
}else {
|
String[] split = vo.getForGoodIds().split(",");
|
R<List<Goods>> goodsById = goodsClient.getGoodsById(split);
|
if (goodsById.getData()!=null){
|
for (Goods datum : goodsById.getData()) {
|
goodNames.add(datum.getName());
|
}
|
}
|
}
|
vo.setGoodNames(goodNames);
|
}
|
|
record.setCouponInfoVo(vo);
|
if(null != record.getUseTime()){
|
record.setStatus(2);
|
}else{
|
if(record.getEndTime().isBefore(LocalDateTime.now())){
|
record.setStatus(3);
|
}else{
|
record.setStatus(1);
|
}
|
}
|
AppUser appUser = appUserService.getById(record.getAppUserId());
|
record.setUserName(appUser.getName());
|
record.setPhone(appUser.getPhone());
|
}
|
return R.ok(page);
|
}
|
|
|
@GetMapping("/mine/list1")
|
@ApiOperation(value = "已领取列表", tags = {"管理后台-优惠劵"})
|
public R<Page<UserCoupon>> list1(@RequestParam Integer pageNum,
|
@RequestParam Integer pageSize,
|
String userName,
|
@ApiParam("1未使用2已使用3已过期") Integer status,
|
String phone,
|
Integer id) {
|
|
LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
|
|
if (!StringUtils.isEmpty(phone)) {
|
queryWrapper.like(AppUser::getPhone, phone);
|
}
|
if (!StringUtils.isEmpty(userName)) {
|
queryWrapper.like(AppUser::getName, userName);
|
}
|
|
List<AppUser> appUserList = appUserService.list(queryWrapper);
|
|
if (CollectionUtils.isEmpty(appUserList)) {
|
return R.ok(Page.of(pageNum, pageSize));
|
}
|
|
List<Long> appUserIds = appUserList.stream().map(AppUser::getId).distinct().collect(Collectors.toList());
|
|
|
Page<UserCoupon> page = userCouponService.lambdaQuery()
|
.isNull(status!=null&&(status==1||status==3),UserCoupon::getUseTime)
|
.isNotNull(status!=null&&status==2,UserCoupon::getUseTime)
|
.eq(UserCoupon::getCouponId, id)
|
.in(!CollectionUtils.isEmpty(appUserIds),UserCoupon::getAppUserId, appUserIds)
|
.lt(status!=null&&status==3,UserCoupon::getEndTime, LocalDateTime.now()).page(Page.of(pageNum-1, pageSize));
|
for (UserCoupon record : page.getRecords()) {
|
record.setIdStr(record.getId().toString());
|
CouponInfo data = couponClient.detail(record.getCouponId()).getData();
|
CouponInfoVo vo = new CouponInfoVo();
|
BeanUtils.copyProperties(data,vo);
|
//如果是商品券,将商品名称返回
|
if (vo.getCouponType()==4){
|
List<String> goodNames = new ArrayList<>();
|
if (vo.getForGoodIds().equals("-1")){
|
goodNames.add("全部商品");
|
}else{
|
String[] split = vo.getForGoodIds().split(",");
|
R<List<Goods>> goodsById = goodsClient.getGoodsById(split);
|
if (goodsById.getData()!=null){
|
for (Goods datum : goodsById.getData()) {
|
goodNames.add(datum.getName());
|
}
|
}
|
vo.setGoodNames(goodNames);
|
}
|
}
|
|
record.setCouponInfoVo(vo);
|
if (record.getUseTime()==null){
|
record.setStatus(1);
|
if (record.getEndTime().isBefore(LocalDateTime.now())){
|
record.setStatus(3);
|
}
|
}else {
|
record.setStatus(2);
|
}
|
|
AppUser appUser = appUserService.getById(record.getAppUserId());
|
record.setUserName(appUser.getName());
|
record.setPhone(appUser.getPhone());
|
}
|
return R.ok(page);
|
}
|
|
|
|
@GetMapping("/get")
|
@ApiOperation(value = "领取或者兑换优惠券", tags = {"小程序-个人中心-优惠劵"})
|
public R<Page<UserCoupon>> get(@RequestParam Integer couponId) {
|
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser byId = appUserService.getById(userid);
|
|
R<CouponInfo> detail = couponClient.detail(couponId);
|
CouponInfo data = detail.getData();
|
//检验当前优惠券是否存在
|
if (data==null){
|
return R.fail("当前优惠券不存在,请刷新后重试");
|
}
|
if(data.getDelFlag() == 1 || data.getShelfStatus() == 0){
|
return R.fail("当前优惠券不存在,请刷新后重试");
|
}
|
long count = userCouponService.count(new LambdaQueryWrapper<UserCoupon>().eq(UserCoupon::getCouponId, couponId).eq(UserCoupon::getDelFlag, 0));
|
if(data.getSendNum() <= count){
|
return R.fail("当前优惠券已全部领取完毕");
|
}
|
count = userCouponService.count(new LambdaQueryWrapper<UserCoupon>().eq(UserCoupon::getCouponId, couponId)
|
.eq(UserCoupon::getAppUserId, userid).eq(UserCoupon::getDelFlag, 0));
|
if(data.getMaxNum() <= count){
|
return R.fail("当前优惠券已达到最大领取限制");
|
}
|
|
if (data.getSendType()!=1&& byId.getLavePoint().compareTo(data.getNeedPoint().intValue()) < 0){
|
return R.fail("当前积分不足,兑换失败");
|
}
|
//检验发放时间
|
LocalDateTime now = LocalDateTime.now();
|
if (now.isBefore(data.getSendStartTime())||now.isAfter(data.getSendEndTime())){
|
return R.fail("领取失败,不在发放有效期");
|
}
|
//如果是积分兑换,增加积分的历史记录
|
if (data.getSendType()!=1){
|
int point = data.getNeedPoint().intValue();
|
Integer lavePoint = byId.getLavePoint();
|
//扣除积分
|
byId.setLavePoint(byId.getLavePoint() - point);
|
appUserService.updateById(byId);
|
|
if(point > 0){
|
UserPoint userPoint = new UserPoint();
|
userPoint.setType(15);
|
userPoint.setVariablePoint(point);
|
userPoint.setAppUserId(userid);
|
userPoint.setObjectId(Long.valueOf(data.getId()));
|
userPoint.setCreateTime(LocalDateTime.now());
|
userPoint.setChangeDirection(-1);
|
userPointService.save(userPoint);
|
}
|
}
|
|
|
//增加优惠券记录,根据时间类型设置开始结束时间
|
UserCoupon userCoupon = new UserCoupon();
|
userCoupon.setAppUserId(userid);
|
userCoupon.setCouponInfo(JSON.toJSONString(data));
|
if (data.getPeriodType()==1) {
|
userCoupon.setStartTime(data.getPeriodStartTime().atTime(0, 0, 0));
|
userCoupon.setEndTime(data.getPeriodEndTime().atTime(23, 59, 59));
|
}else {
|
userCoupon.setStartTime(now);
|
userCoupon.setEndTime(now.plusDays(data.getPeriodDays()));
|
}
|
userCoupon.setCouponId(data.getId());
|
userCouponService.save(userCoupon);
|
return R.ok();
|
|
|
}
|
|
|
|
/**
|
* 获取支付页面用户优惠券列表数据
|
* @param paymentUserCoupon
|
* @return
|
*/
|
@ResponseBody
|
@PostMapping("/getPaymentUserCoupon")
|
public R<List<PaymentUserCouponVo>> getPaymentUserCoupon(@RequestBody PaymentUserCoupon paymentUserCoupon){
|
List<PaymentUserCouponVo> userCoupon = userCouponService.getUserCoupon(paymentUserCoupon.getUserId(), paymentUserCoupon.getType());
|
return R.ok(userCoupon);
|
}
|
|
|
/**
|
* 根据用户优惠券id获取优惠券详情
|
* @param userCouponId
|
* @return
|
*/
|
@ResponseBody
|
@PostMapping("/getCouponInfo")
|
public R<CouponInfoVo> getCouponInfo(@RequestParam("userCouponId") Long userCouponId){
|
UserCoupon userCoupon = userCouponService.getById(userCouponId);
|
String jsonStr = userCoupon.getCouponInfo();
|
CouponInfoVo couponInfoVo = JSON.parseObject(jsonStr, CouponInfoVo.class);
|
return R.ok(couponInfoVo);
|
}
|
|
/**
|
* 获取优惠券发放数量
|
*/
|
@GetMapping("/getCouponCount")
|
public R<Long> getCouponCount(@RequestParam Integer couponId){
|
return R.ok(userCouponService.lambdaQuery().eq(UserCoupon::getCouponId, couponId).count());
|
}
|
|
|
|
|
@ResponseBody
|
@GetMapping("/getCouponInfoInfo")
|
@ApiOperation(value = "获取核销商品券详情", tags = {"小程序-个人中心-门店管理"})
|
public R<UserCoupon> getCouponInfoInfo(String id){
|
UserCoupon userCoupon = userCouponService.getById(id);
|
CouponInfo couponInfo = couponClient.detail(userCoupon.getCouponId()).getData();
|
List<String> goodNames = new ArrayList<>();
|
if(!"null".equals(couponInfo.getGoodsNameJson()) && StringUtils.isNotEmpty(couponInfo.getGoodsNameJson())){
|
goodNames = JSON.parseArray(couponInfo.getGoodsNameJson(), String.class);
|
}else{
|
List<Goods> goods = null;
|
if("-1".equals(couponInfo.getForGoodIds())){
|
goods = goodsClient.getGoodsByType(2).getData();
|
}else{
|
goods = goodsClient.getGoodsById(couponInfo.getForGoodIds().split(",")).getData();
|
}
|
goodNames = goods.stream().map(Goods::getName).collect(Collectors.toList());
|
}
|
|
CouponInfoVo couponInfoVo = new CouponInfoVo();
|
BeanUtils.copyProperties(couponInfo, couponInfoVo);
|
couponInfoVo.setGoodNames(goodNames);
|
userCoupon.setCouponInfoVo(couponInfoVo);
|
if (userCoupon.getUseTime()==null){
|
userCoupon.setStatus(1);
|
if (userCoupon.getEndTime().isBefore(LocalDateTime.now())){
|
userCoupon.setStatus(3);
|
}
|
}else {
|
userCoupon.setStatus(2);
|
}
|
return R.ok(userCoupon);
|
}
|
|
@ResponseBody
|
@PutMapping("/useCoupon/{id}")
|
@ApiOperation(value = "核销商品优惠券", tags = {"小程序-个人中心-门店管理"})
|
public R useCoupon(@PathVariable("id") String id){
|
UserCoupon userCoupon = userCouponService.getById(id);
|
if(null == userCoupon){
|
return R.ok("核销码错误");
|
}
|
if(userCoupon.getUseTime() != null){
|
return R.ok("优惠券已使用");
|
}
|
if(LocalDateTime.now().isAfter(userCoupon.getEndTime())){
|
return R.ok("优惠券已过期");
|
}
|
userCoupon.setStatus(2);
|
userCoupon.setUseTime(LocalDateTime.now());
|
userCouponService.updateById(userCoupon);
|
return R.ok();
|
}
|
|
|
/**
|
* 获取用户优惠券详情
|
* @param id
|
* @return
|
*/
|
@PostMapping("/getUserCoupon")
|
public R<UserCoupon> getUserCoupon(@RequestParam("id") Long id){
|
UserCoupon userCoupon = userCouponService.getById(id);
|
return R.ok(userCoupon);
|
}
|
|
|
/**
|
* 编辑用户优惠券
|
* @return
|
*/
|
@PostMapping("/editUserCoupon")
|
public R editUserCoupon(@RequestBody UserCoupon userCoupon){
|
userCouponService.updateById(userCoupon);
|
return R.ok();
|
}
|
|
|
/**
|
* 编辑用户优惠券使用时间
|
* @param userCoupon
|
* @return
|
*/
|
@PostMapping("/editReturnUse")
|
public R editReturnUse(@RequestBody UserCoupon userCoupon){
|
userCouponService.update(new LambdaUpdateWrapper<UserCoupon>().eq(UserCoupon::getId, userCoupon.getId())
|
.set(UserCoupon::getUseTime, userCoupon.getUseTime()));
|
return R.ok();
|
}
|
}
|