优惠券使用 以及 退券

This commit is contained in:
wangw 2024-10-25 11:19:25 +08:00
parent b1e7095357
commit 84a2007b26
6 changed files with 97 additions and 47 deletions

View File

@ -17,6 +17,7 @@ import java.util.List;
public interface TbActivateInRecordMapper extends BaseMapper<TbActivateInRecord> {
@Select("SELECT" +
" inRecord.coupon_id as id," +
" inRecord.coupon_id as couponId," +
" pro.id as proId," +
" CASE" +
@ -37,5 +38,10 @@ public interface TbActivateInRecordMapper extends BaseMapper<TbActivateInRecord>
" and inRecord.use_end_time &gt; now()" +
" order by inRecord.use_end_time asc")
List<TbUserCouponVo> queryByVipIdAndShopId(@Param("vipUserId") Integer vipUserId, @Param("shopId") Integer shopId);
@Select("update tb_activate_in_record" +
" set over_num = #{overNum}" +
" where id = #{id}")
int updateOverNum(@Param("id") Integer id, @Param("overNum") Integer overNum);
}

View File

@ -2,6 +2,8 @@ package cn.ysk.cashier.mybatis.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.ysk.cashier.mybatis.entity.TbActivateOutRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* 活动赠送商品使用记录表(TbActivateOutRecord)表数据库访问层
@ -11,5 +13,9 @@ import cn.ysk.cashier.mybatis.entity.TbActivateOutRecord;
*/
public interface TbActivateOutRecordMapper extends BaseMapper<TbActivateOutRecord> {
@Select("update tb_activate_out_record" +
" set ref_num = ref_num + #{refNum}" +
" where id = #{id}")
int updateRefNum(@Param("id") Integer id, @Param("refNum") Integer refNum);
}

View File

@ -1,6 +1,7 @@
package cn.ysk.cashier.mybatis.service;
import cn.ysk.cashier.dto.CouponDto;
import cn.ysk.cashier.mybatis.entity.TbActivateOutRecord;
import cn.ysk.cashier.mybatis.entity.TbCouponProduct;
import cn.ysk.cashier.mybatis.vo.TbShopCouponVo;
import com.baomidou.mybatisplus.extension.service.IService;
@ -24,11 +25,15 @@ public interface TbShopCouponService extends IService<TbShopCoupon> {
TbShopCoupon findById (Integer id);
ResponseEntity<Object> find(CouponDto param);
boolean update(TbShopCouponVo param);
boolean delete(Integer id);
ResponseEntity<Object> find(CouponDto param);
boolean use(Integer shopId,Integer orderId,Integer vipUserId,List<TbActivateOutRecord> param);
boolean refund(List<TbActivateOutRecord> param);
List<TbCouponProduct> findActivatePros(Integer couponId);
}

View File

@ -6,9 +6,12 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.ysk.cashier.dto.CouponDto;
import cn.ysk.cashier.dto.TbShopCouponQueryCriteria;
import cn.ysk.cashier.mybatis.entity.TbActivateInRecord;
import cn.ysk.cashier.mybatis.entity.TbActivateOutRecord;
import cn.ysk.cashier.mybatis.entity.TbCouponProduct;
import cn.ysk.cashier.mybatis.entity.TbShopCoupon;
import cn.ysk.cashier.mybatis.mapper.TbActivateInRecordMapper;
import cn.ysk.cashier.mybatis.mapper.TbActivateOutRecordMapper;
import cn.ysk.cashier.mybatis.mapper.TbShopCouponMapper;
import cn.ysk.cashier.mybatis.service.TbActivateInRecordService;
import cn.ysk.cashier.mybatis.service.TbCouponProductService;
@ -66,6 +69,8 @@ public class TbShopCouponServiceImpl extends ServiceImpl<TbShopCouponMapper, TbS
private TbOrderDetailRepository orderDetailRepository;
@Autowired
private TbActivateInRecordMapper inRecordMapper;
@Autowired
private TbActivateOutRecordMapper outRecordMapper;
@Override
@ -97,6 +102,43 @@ public class TbShopCouponServiceImpl extends ServiceImpl<TbShopCouponMapper, TbS
return tbShopCouponVo;
}
@Override
public boolean update(TbShopCouponVo param) {
if (param.getId() == null) {
tbShopCouponmapper.insert(param);
} else {
tbShopCouponmapper.updateById(param);
}
if (CollectionUtil.isNotEmpty(param.getProducts())) {
List<Integer> collect = new ArrayList<>();
for (TbCouponProduct product : param.getProducts()) {
product.setCouponId(param.getId());
if (product.getId() != null) collect.add(product.getId());
}
QueryWrapper<TbCouponProduct> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("coupon_id", param.getId());
List<TbCouponProduct> actProducts = couProductService.list(queryWrapper);
for (TbCouponProduct actProduct : actProducts) {
if (!collect.contains(actProduct.getId())) {
couProductService.removeById(actProduct.getId());
}
}
couProductService.saveOrUpdateBatch(param.getProducts());
} else {
LambdaQueryWrapper<TbCouponProduct> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TbCouponProduct::getCouponId, param.getId());
couProductService.remove(queryWrapper);
}
return true;
}
@Override
public boolean delete(Integer id) {
tbShopCouponmapper.deleteById(id);
couProductService.remove(new LambdaQueryWrapper<TbCouponProduct>().eq(TbCouponProduct::getCouponId, id));
return true;
}
@Override
public ResponseEntity<Object> find(CouponDto param) {
TbShopUser tbShopUser = shopUserRepository.selectByUserIdAndShopId(param.getUserId().toString(), param.getShopId().toString());
@ -170,45 +212,51 @@ public class TbShopCouponServiceImpl extends ServiceImpl<TbShopCouponMapper, TbS
return new ResponseEntity<>(HttpStatus.OK);
}
/**
* 使用券
* @param shopId
* @param orderId
* @param vipUserId
* @param param giveId useNum 必传
* @return
*/
@Override
public boolean update(TbShopCouponVo param) {
if (param.getId() == null) {
tbShopCouponmapper.insert(param);
} else {
tbShopCouponmapper.updateById(param);
}
if (CollectionUtil.isNotEmpty(param.getProducts())) {
List<Integer> collect = new ArrayList<>();
for (TbCouponProduct product : param.getProducts()) {
product.setCouponId(param.getId());
if (product.getId() != null) collect.add(product.getId());
}
QueryWrapper<TbCouponProduct> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("coupon_id", param.getId());
List<TbCouponProduct> actProducts = couProductService.list(queryWrapper);
for (TbCouponProduct actProduct : actProducts) {
if (!collect.contains(actProduct.getId())) {
couProductService.removeById(actProduct.getId());
}
}
couProductService.saveOrUpdateBatch(param.getProducts());
} else {
LambdaQueryWrapper<TbCouponProduct> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TbCouponProduct::getCouponId, param.getId());
couProductService.remove(queryWrapper);
public boolean use(Integer shopId,Integer orderId,Integer vipUserId,List<TbActivateOutRecord> param) {
for (TbActivateOutRecord outRecord : param) {
TbActivateInRecord inRecord = inRecordMapper.selectById(outRecord.getGiveId());
inRecord.setOverNum(inRecord.getOverNum() - outRecord.getUseNum());
inRecordMapper.updateOverNum(inRecord.getId(), inRecord.getOverNum());
outRecord.setType(inRecord.getType());
outRecord.setShopId(shopId);
outRecord.setOrderId(orderId.toString());
outRecord.setVipUserId(vipUserId);
outRecord.setStatus("closed");
outRecord.setCreateTime(new Date());
outRecordMapper.insert(outRecord);
}
return true;
}
/**
* 退还券
* @param param giveId和 refNum 必传
* @return
*/
@Override
public boolean delete(Integer id) {
tbShopCouponmapper.deleteById(id);
couProductService.remove(new LambdaQueryWrapper<TbCouponProduct>().eq(TbCouponProduct::getCouponId, id));
public boolean refund(List<TbActivateOutRecord> param) {
for (TbActivateOutRecord outRecord : param) {
outRecord.setUpdateTime(new Date());
outRecordMapper.updateRefNum(outRecord.getId(),outRecord.getRefNum());
TbActivateInRecord inRecord = inRecordMapper.selectById(outRecord.getGiveId());
inRecord.setOverNum(inRecord.getOverNum() + outRecord.getRefNum());
inRecordMapper.updateOverNum(inRecord.getId(), inRecord.getOverNum());
}
return true;
}
@Override
public List<TbCouponProduct> findActivatePros(Integer couponId) {
QueryWrapper<TbCouponProduct> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("coupon_id", couponId);

View File

@ -400,22 +400,6 @@ public class TbOrderInfoServiceImpl implements TbOrderInfoService {
@Transactional(rollbackFor = Exception.class)
public void upOrderStatus(TbOrderInfo tbOrderInfo) {
tbOrderInfo.setStatus("cancelled");
//订单取消 赠送商品数量返回
QueryWrapper<TbActivateOutRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("order_id", tbOrderInfo.getId());
queryWrapper.eq("status", "create");
List<TbActivateOutRecord> outRecords = outRecordService.list(queryWrapper);
for (TbActivateOutRecord outRecord : outRecords) {
TbActivateInRecord inRecord = inRecordService.getById(outRecord.getGiveId());
inRecord.setOverNum(inRecord.getOverNum() + outRecord.getUseNum());
inRecordService.updateById(inRecord);
}
LambdaUpdateWrapper<TbActivateOutRecord> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(TbActivateOutRecord::getOrderId, tbOrderInfo.getId())
.eq(TbActivateOutRecord::getStatus, "create")
.set(TbActivateOutRecord::getStatus, "cancel");
outRecordService.update(lambdaUpdateWrapper);
List<TbOrderDetail> details = tbOrderDetailRepository.searchDetailByOrderId(tbOrderInfo.getId());
for (TbOrderDetail detail : details) {
detail.setStatus("cancelled");

View File

@ -6,6 +6,7 @@ import java.util.Date;
@Data
public class TbUserCouponVo {
private Integer id;
private Integer couponId;
private Integer proId;
//优惠券名称