优惠券重写

This commit is contained in:
2024-10-24 13:26:18 +08:00
parent 646f201ce1
commit d27e2e01b6
38 changed files with 2423 additions and 1602 deletions

View File

@@ -839,27 +839,6 @@ public class CartService {
cart.setPackFee(BigDecimal.ZERO);
}
if (cart.getIsVip().equals((byte) 1)) {
if (isVip) {
int i1 = activateInRecordService.queryByVipIdAndShopIdAndProId(
Integer.valueOf(tbShopUser.getId()), Integer.valueOf(shopId), Integer.valueOf(cart.getProductId()));
if (i1 < cart.getTotalNumber()) {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("status", "fail");
jsonObject1.put("msg", "会员商品[" + cart.getName() + "],可下单数量为" + i1);
jsonObject1.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), tableCartKey, jsonObject.getString("userId"), true);
continue;
}
} else {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("status", "fail");
jsonObject1.put("msg", "非会员用户不可下单会员商品");
jsonObject1.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), tableCartKey, jsonObject.getString("userId"), true);
continue;
}
}
TbProductSkuWithBLOBs tbProduct;
if (!"-999".equals(cart.getProductId())) {
tbProduct = productSkuMapper.selectByPrimaryKey(Integer.valueOf(cart.getSkuId()));
@@ -1039,29 +1018,7 @@ public class CartService {
}
// 去除餐位费信息
List<TbActivateOutRecord> outRecords = new ArrayList<>();
for (TbCashierCart cashierCart : cashierCartList) {
if (!cashierCart.getProductId().equals("-999") && cashierCart.getIsVip().equals((byte) 1)) {
List<TbActivateInRecord> actInRecords = activateInRecordService.queryAllByVipIdAndShopIdAndProId(
Integer.valueOf(tbShopUser.getId()), Integer.valueOf(orderInfo.getShopId()), Integer.valueOf(cashierCart.getProductId()));
Integer totalNumber = cashierCart.getTotalNumber();
for (TbActivateInRecord actInRecord : actInRecords) {
if (totalNumber > 0) {
if (actInRecord.getOverNum() > totalNumber) {
TbActivateOutRecord outRecord = new TbActivateOutRecord(actInRecord.getId(), actInRecord.getProId(), totalNumber, orderInfo.getId().toString(), "create");
outRecords.add(outRecord);
activateInRecordService.updateOverNumById(actInRecord.getId(), actInRecord.getOverNum() - totalNumber);
break;
} else {
TbActivateOutRecord outRecord = new TbActivateOutRecord(actInRecord.getId(), actInRecord.getProId(), actInRecord.getOverNum(), orderInfo.getId().toString(), "create");
outRecords.add(outRecord);
activateInRecordService.updateOverNumById(actInRecord.getId(), 0);
totalNumber = totalNumber - actInRecord.getOverNum();
}
}
}
}
cashierCart.setUpdatedAt(System.currentTimeMillis());
cashierCart.setOrderId(orderId + "");
if ((!TableConstant.CART_SEAT_ID.equals(cashierCart.getProductId()) && !shopEatTypeInfoDTO.isDineInAfter())
@@ -1080,7 +1037,6 @@ public class CartService {
// cashierCartMapper.deleteByPrimaryKey(seatCartInfo.getId());
// }
if (!CollectionUtils.isEmpty(outRecords)) outRecordMapper.insertBatch(outRecords);
// 打印票据
if (!addOrderDetail.isEmpty() && shopEatTypeInfoDTO.isDineInAfter()) {

View File

@@ -1,156 +0,0 @@
//package com.chaozhanggui.system.cashierservice.service;
//
//import com.chaozhanggui.system.cashierservice.dao.TbCashierCartMapper;
//import com.chaozhanggui.system.cashierservice.dao.TbProductMapper;
//import com.chaozhanggui.system.cashierservice.dao.TbProductSkuMapper;
//import com.chaozhanggui.system.cashierservice.entity.TbCashierCart;
//import com.chaozhanggui.system.cashierservice.entity.TbProduct;
//import com.chaozhanggui.system.cashierservice.entity.TbProductSku;
//import com.chaozhanggui.system.cashierservice.entity.dto.ProductCartDto;
//import com.chaozhanggui.system.cashierservice.entity.vo.CashierCarVo;
//import com.chaozhanggui.system.cashierservice.exception.MsgException;
//import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
//import com.chaozhanggui.system.cashierservice.sign.Result;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.time.Instant;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//
///**
// * @author lyf
// */
//@Service
//@Slf4j
//public class CashierCartService {
// @Resource
// private TbCashierCartMapper cashierCartMapper;
// @Resource
// private TbProductMapper productMapper;
// @Resource
// private TbProductSkuMapper productSkuMapper;
//
// /**
// * 增加购物车
// * @param productCartDto
// * @return
// */
// @Transactional(rollbackFor = Exception.class)
// public Result batchAdd(ProductCartDto productCartDto){
// //首先确认金额
// TbProduct tbProduct = productMapper.selectById(Integer.valueOf(productCartDto.getProductId()));
// if (tbProduct == null){
// return Result.fail("商品信息不存在");
// }
//
// TbCashierCart cashierCart = cashierCartMapper.selectByProduct(productCartDto.getProductId(), productCartDto.getTableId());
// if (cashierCart != null){
// if ("add".equals(productCartDto.getType())){
// TbCashierCart cashierCartNow = new TbCashierCart();
// cashierCartNow.setNumber(cashierCart.getNumber()+1F);
// cashierCartNow.setId(cashierCart.getId());
// cashierCartMapper.updateByPrimaryKeySelective(cashierCartNow);
// return Result.success(CodeEnum.ENCRYPT);
// }else if ("minus".equals(productCartDto.getType())){
// TbCashierCart cashierCartNow = new TbCashierCart();
// cashierCartNow.setNumber(cashierCart.getNumber()-1F);
// cashierCartNow.setId(cashierCart.getId());
// if (cashierCartNow.getNumber() == 0F){
// cashierCartNow.setStatus("clear");
// cashierCartMapper.updateByPrimaryKeySelective(cashierCartNow);
// return Result.success(CodeEnum.ENCRYPT);
// }
// cashierCartMapper.updateByPrimaryKeySelective(cashierCartNow);
// return Result.success(CodeEnum.ENCRYPT);
// }else {
// throw new MsgException("添加购物车失败");
// }
// }
// //增加新的购物车
// TbCashierCart tbCashierCart = new TbCashierCart();
// BeanUtils.copyProperties(productCartDto,tbCashierCart);
// tbCashierCart.setSalePrice(tbProduct.getLowPrice());
// tbCashierCart.setCreatedAt(Instant.now().toEpochMilli());
// tbCashierCart.setUpdatedAt(Instant.now().toEpochMilli());
// tbCashierCart.setTotalNumber(0.00F);
// tbCashierCart.setRefundNumber(0.00F);
// tbCashierCart.setType((byte) 0);
// tbCashierCart.setSkuId(productCartDto.getSkuInfo());
// //购物车状态打开
// tbCashierCart.setStatus("open");
//
// int insert = cashierCartMapper.insertSelective(tbCashierCart);
// if (insert>0){
// return Result.success(CodeEnum.SUCCESS);
// }
// throw new MsgException("添加购物车失败");
// }
//
//
// public Result cartList(Integer tableId){
// HashMap<String, Object> map = new HashMap<>();
// List<TbCashierCart> tbCashierCarts = cashierCartMapper.selectByTableId(tableId);
// BigDecimal total = new BigDecimal("0.00");
// for (TbCashierCart date :tbCashierCarts) {
// Float number = date.getNumber();
// BigDecimal bigDecimalValue = new BigDecimal(number.toString());
// total=total.add(bigDecimalValue.multiply(date.getSalePrice()));
// }
//
// map.put("cartList",tbCashierCarts);
// map.put("total",total);
//
// return Result.success(CodeEnum.ENCRYPT,map);
//
// }
// @Transactional(rollbackFor = Exception.class)
// public Result updateNumber(Integer tableId,String type){
// TbCashierCart cashierCart = cashierCartMapper.selectByPrimaryKey(tableId);
// if (cashierCart == null){
// return Result.fail("商品不存在");
// }
// if ("add".equals(type)){
// TbCashierCart cashierCartNow = new TbCashierCart();
// cashierCartNow.setNumber(cashierCart.getNumber()+1F);
// cashierCartNow.setId(cashierCart.getId());
// cashierCartMapper.updateByPrimaryKeySelective(cashierCartNow);
// return Result.success(CodeEnum.ENCRYPT);
// }else if ("minus".equals(type)){
// TbCashierCart cashierCartNow = new TbCashierCart();
// cashierCartNow.setNumber(cashierCart.getNumber()-1F);
// cashierCartNow.setId(cashierCart.getId());
// if (cashierCartNow.getNumber() == 0F){
// cashierCartNow.setStatus("clear");
// cashierCartMapper.updateByPrimaryKeySelective(cashierCartNow);
// return Result.success(CodeEnum.ENCRYPT);
// }
// cashierCartMapper.updateByPrimaryKeySelective(cashierCartNow);
// return Result.success(CodeEnum.ENCRYPT);
// }else {
// throw new MsgException("更改商品失败");
// }
//
// }
// @Transactional(rollbackFor = Exception.class)
// public Result clearCart(Integer tableId){
// List<CashierCarVo> cashierCarVos = cashierCartMapper.selectByTableIdOpen(tableId);
// if (cashierCarVos.isEmpty()){
// return Result.fail("购物车内无商品");
// }
// List<Integer> ids = new ArrayList<>();
// for (CashierCarVo date :cashierCarVos) {
// ids.add(date.getId());
// }
// int i = cashierCartMapper.updateByIdsStatus(ids, Instant.now().toEpochMilli());
// if (i != ids.size()){
// throw new MsgException("清空购物车失败");
// }
// return Result.success(CodeEnum.ENCRYPT);
// }
//}

View File

@@ -1,5 +1,6 @@
package com.chaozhanggui.system.cashierservice.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
@@ -82,7 +83,9 @@ public class PayService {
@Autowired
TbShopPayTypeMapper tbShopPayTypeMapper;
@Autowired
private TbActivateProductMapper actProductMapper;
private TbCouponProductMapper couProductMapper;
@Autowired
private TbShopCouponMapper couponMapper;
@Resource
private TbActivateInRecordMapper activateInRecordMapper;
@@ -475,7 +478,6 @@ public class PayService {
.eq(TbOrderDetail::getStatus, "unpaid")
.set(TbOrderDetail::getStatus, "closed"));
outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(), "closed");
log.info("更新购物车:{}", cartCount);
JSONObject jsonObject = new JSONObject();
@@ -1013,7 +1015,6 @@ public class PayService {
//更新子单状态
tbOrderDetailMapper.updateStatusByOrderIdAndStatus(orderInfo.getId(), "closed");
outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(), "closed");
//修改主单状态
orderInfo.setStatus("closed");
orderInfo.setPayType("wx_lite");
@@ -1098,7 +1099,6 @@ public class PayService {
orderInfo.setPayAmount(orderInfo.getOrderAmount());
orderInfo.setPaidTime(System.currentTimeMillis());
tbOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);
outRecordMapper.updateByOrderIdAndStatus(orderInfo.getId(), "closed");
JSONObject jsonObject = new JSONObject();
jsonObject.put("token", 0);
@@ -1176,8 +1176,18 @@ public class PayService {
PageHelper.startPage(page, pageSize);
List<TbActivate> list = tbActivateMapper.selectByShopId(shopId);
for (TbActivate tbActivate : list) {
if (tbActivate.getIsGiftPro() == 1) {
tbActivate.setGives(actProductMapper.queryProsByActivateId(tbActivate.getId()));
if (tbActivate.getIsUseCoupon() == 1) {
TbShopCoupon coupon = couponMapper.queryById(tbActivate.getCouponId());
if(coupon!=null){
if(coupon.getType()==1){
//满减
tbActivate.setCouponDesc(""+coupon.getFullAmount() + "" + coupon.getDiscountAmount()+" * "+tbActivate.getNum()+"");
} else if (coupon.getType() == 2) {
//商品
tbActivate.setCouponDesc("商品券");
tbActivate.setGives(couProductMapper.queryProsByActivateId(coupon.getId(),tbActivate.getNum()));
}
}
}
}
PageInfo pageInfo = new PageInfo(list);
@@ -1292,25 +1302,68 @@ public class PayService {
public BigDecimal giveActivate(TbShopUser tbShopUser, BigDecimal memAmount, Integer flowId) {
TbActivate activate = tbActivateMapper.selectByAmount(tbShopUser.getShopId(), memAmount);
if (ObjectUtil.isNotEmpty(activate) && ObjectUtil.isNotNull(activate)) {
if (activate.getIsGiftPro() != null && activate.getIsGiftPro() == 1) {
List<TbActivateProduct> tbActivateProducts = actProductMapper.queryAllByActivateId(activate.getId());
List<TbActivateInRecord> actGiveRecords = new ArrayList<>();
for (TbActivateProduct actPro : tbActivateProducts) {
TbActivateInRecord record = new TbActivateInRecord(Integer.valueOf(tbShopUser.getId()), actPro.getProductId(), actPro.getNum(), Integer.valueOf(tbShopUser.getShopId()), activate.getId(), flowId);
actGiveRecords.add(record);
if (activate.getIsUseCoupon() == 1) {
TbShopCoupon tbShopCoupon = couponMapper.queryById(activate.getCouponId());
Date start = new Date();
Date end = new Date();
if ("fixed".equals(tbShopCoupon.getValidityType())) {
//固定时间
end = DateUtil.offsetDay(new Date(),tbShopCoupon.getValidDays());
} else if ("custom".equals(tbShopCoupon.getValidityType())) {
//自定义时间
start = tbShopCoupon.getValidStartTime();
end = tbShopCoupon.getValidEndTime();
}
if (tbShopCoupon != null) {
List<TbActivateInRecord> actGiveRecords = new ArrayList<>();
if(tbShopCoupon.getType() == 1) {
//满减
TbActivateInRecord record = new TbActivateInRecord();
record.setVipUserId(Integer.valueOf(tbShopUser.getId()));
record.setCouponId(tbShopCoupon.getId());
record.setName("" + tbShopCoupon.getFullAmount() + "" + tbShopCoupon.getDiscountAmount());
record.setFullAmount(tbShopCoupon.getFullAmount());
record.setDiscountAmount(tbShopCoupon.getDiscountAmount());
record.setType(1);
record.setNum(activate.getNum());
record.setOverNum(activate.getNum());
record.setShopId(Integer.valueOf(tbShopUser.getShopId()));
record.setSourceActId(activate.getId());
record.setSourceFlowId(flowId);
record.setUseStartTime(start);
record.setUseEndTime(end);
record.setCouponJson(activateInRecordService.getCouponJson(activate,tbShopCoupon,null));
actGiveRecords.add(record);
} else if (tbShopCoupon.getType() == 2) {
//商品卷
List<TbCouponProduct> tbCouponProducts = couProductMapper.queryAllByCouponId(tbShopCoupon.getId());
for (TbCouponProduct actPro : tbCouponProducts) {
TbActivateInRecord record = new TbActivateInRecord();
record.setVipUserId(Integer.valueOf(tbShopUser.getId()));
record.setCouponId(tbShopCoupon.getId());
record.setName("商品卷");
record.setType(2);
record.setProId(actPro.getProductId());
record.setNum(actPro.getNum()*tbShopCoupon.getNumber());
record.setOverNum(actPro.getNum()*tbShopCoupon.getNumber());
record.setShopId(Integer.valueOf(tbShopUser.getShopId()));
record.setSourceActId(activate.getId());
record.setSourceFlowId(flowId);
record.setUseStartTime(start);
record.setUseEndTime(end);
record.setCouponJson(activateInRecordService.getCouponJson(activate,tbShopCoupon,actPro));
actGiveRecords.add(record);
}
}
activateInRecordMapper.insertBatch(actGiveRecords);
tbShopCoupon.setLeftNumber(tbShopCoupon.getLeftNumber()-activate.getNum());
couponMapper.update(tbShopCoupon);
}
activateInRecordMapper.insertBatch(actGiveRecords);
}
BigDecimal amount = BigDecimal.ZERO;
switch (activate.getHandselType()) {
case "GD":
amount = activate.getHandselNum();
break;
case "RATIO":
amount = memAmount.multiply(activate.getHandselNum());
break;
}
BigDecimal amount = activate.getGiftAmount() == null ?
BigDecimal.ZERO : new BigDecimal(activate.getGiftAmount());
tbShopUser.setAmount(tbShopUser.getAmount().add(amount));
tbShopUser.setUpdatedAt(System.currentTimeMillis());
tbShopUserMapper.updateByPrimaryKeySelective(tbShopUser);

View File

@@ -234,16 +234,6 @@ public class ProductService {
}
});
groupList.sort(Comparator.comparingInt(TbProductGroup::getIsSale).reversed());
TbShopUser tbShopUser = tbShopUserMapper.selectByUserIdAndShopId(userId, shopId);
if (tbShopUser != null) {
TbProductGroup vipProGroup = new TbProductGroup();
vipProGroup.setName("会员商品");
List<TbProduct> vipPros = activateInRecordService.queryByVipIdAndShopId(Integer.valueOf(tbShopUser.getId()), Integer.valueOf(shopId));
if(!CollectionUtils.isEmpty(vipPros)){
vipProGroup.setProducts(handleDate(vipPros, true, 1, true));
groupList.add(0, vipProGroup);
}
}
groupList.add(0, hot);
concurrentMap.put("productInfo", groupList);
}

View File

@@ -1,11 +1,10 @@
package com.chaozhanggui.system.cashierservice.service;
import com.chaozhanggui.system.cashierservice.entity.TbActivateInRecord;
import com.chaozhanggui.system.cashierservice.entity.TbProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.entity.TbActivate;
import com.chaozhanggui.system.cashierservice.entity.TbCouponProduct;
import com.chaozhanggui.system.cashierservice.entity.TbShopCoupon;
import org.springframework.stereotype.Service;
/**
* 活动商品赠送表(TbActivateInRecord)表服务接口
@@ -13,44 +12,14 @@ import java.util.List;
* @author ww
* @since 2024-08-22 11:13:40
*/
public interface TbActivateInRecordService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
TbActivateInRecord queryById(Integer id);
List<TbProduct> queryByVipIdAndShopId(Integer vipUserId, Integer shopId);
int queryByVipIdAndShopIdAndProId(Integer vipUserId, Integer shopId,Integer productId);
List<TbActivateInRecord> queryAllByVipIdAndShopIdAndProId(Integer vipUserId, Integer shopId,Integer productId);
/**
* 新增数据
*
* @param tbActivateInRecord 实例对象
* @return 实例对象
*/
TbActivateInRecord insert(TbActivateInRecord tbActivateInRecord);
/**
* 修改数据
*
* @param tbActivateInRecord 实例对象
* @return 实例对象
*/
TbActivateInRecord update(TbActivateInRecord tbActivateInRecord);
int updateOverNumById(Integer id,Integer overNum);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Integer id);
@Service
public class TbActivateInRecordService {
public String getCouponJson(TbActivate activate, TbShopCoupon tbShopCoupon, TbCouponProduct couProduct){
JSONObject result = new JSONObject();
result.put("activate",JSONObject.toJSON(activate));
result.put("coupon",JSONObject.toJSON(tbShopCoupon));
result.put("couProduct",JSONObject.toJSON(couProduct));
return result.toJSONString();
}
}

View File

@@ -1,45 +0,0 @@
package com.chaozhanggui.system.cashierservice.service;
import com.chaozhanggui.system.cashierservice.entity.TbActivateOutRecord;
/**
* 活动赠送商品使用记录表(TbActivateOutRecord)表服务接口
*
* @author ww
* @since 2024-08-22 11:21:56
*/
public interface TbActivateOutRecordService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
TbActivateOutRecord queryById(Integer id);
/**
* 新增数据
*
* @param tbActivateOutRecord 实例对象
* @return 实例对象
*/
TbActivateOutRecord insert(TbActivateOutRecord tbActivateOutRecord);
/**
* 修改数据
*
* @param tbActivateOutRecord 实例对象
* @return 实例对象
*/
TbActivateOutRecord update(TbActivateOutRecord tbActivateOutRecord);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Integer id);
}

View File

@@ -0,0 +1,15 @@
package com.chaozhanggui.system.cashierservice.service;
import com.chaozhanggui.system.cashierservice.entity.dto.CouponDto;
import com.chaozhanggui.system.cashierservice.sign.Result;
import org.springframework.context.annotation.Primary;
/**
* 优惠券(TbShopCoupon)表服务接口
*
* @author ww
* @since 2024-10-23 15:37:37
*/
public interface TbShopCouponService {
Result find(CouponDto param);
}

View File

@@ -4,21 +4,16 @@ import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.dao.TbShopInfoMapper;
import com.chaozhanggui.system.cashierservice.dao.TbShopUserMapper;
import com.chaozhanggui.system.cashierservice.dao.TbUserInfoMapper;
import com.chaozhanggui.system.cashierservice.entity.TbShopInfo;
import com.chaozhanggui.system.cashierservice.entity.TbShopUser;
import com.chaozhanggui.system.cashierservice.entity.TbUserInfo;
import com.chaozhanggui.system.cashierservice.entity.vo.IntegralVo;
import com.chaozhanggui.system.cashierservice.entity.vo.OpenMemberVo;
import com.chaozhanggui.system.cashierservice.exception.MsgException;
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.util.CacheMap;
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
import com.chaozhanggui.system.cashierservice.util.MD5Util;
import com.chaozhanggui.system.cashierservice.util.RedisUtils;
import com.chaozhanggui.system.cashierservice.wxUtil.WxAccountUtil;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
@@ -62,21 +57,6 @@ public class UserService {
}
public static void main(String[] args) {
IntegralVo integralVo = new IntegralVo();
integralVo.setNum(new BigDecimal("5254"));
integralVo.setOpenId("or1l864NBOoJZhC5x_yeziZ26j6c");
integralVo.setType("sub");
JSONObject object = (JSONObject) JSONObject.toJSON(integralVo);
object.put("userSign", "02c03d79c36b4c01b217ffb1baef9009");
JSONObject jsonObject = JSONUtil.sortJSONObject(object, CacheMap.map);
System.out.println("加密前字符串:" + jsonObject.toJSONString());
String sign = MD5Util.encrypt(jsonObject.toJSONString());
System.out.println("加密后签名:" + sign);
}
public String getSubQrCode(String shopId) throws Exception {

View File

@@ -0,0 +1,128 @@
package com.chaozhanggui.system.cashierservice.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.entity.dto.CouponDto;
import com.chaozhanggui.system.cashierservice.entity.vo.TbUserCouponVo;
import com.chaozhanggui.system.cashierservice.service.TbShopCouponService;
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
import com.chaozhanggui.system.cashierservice.sign.Result;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
* 优惠券(TbShopCoupon)表服务实现类
*
* @author ww
* @since 2024-10-23 15:37:37
*/
@Primary
@Service
public class TbShopCouponServiceImpl implements TbShopCouponService {
@Resource
private TbOrderDetailMapper orderDetailMapper;
@Resource
private TbShopUserMapper shopUserMapper;
@Resource
private TbShopCouponMapper couponMapper;
@Resource
private TbOrderInfoMapper orderInfoMapper;
@Resource
private TbActivateInRecordMapper inRecordMapper;
@Resource
private TbActivateOutRecordMapper outRecordMapper;
@Override
public Result find(CouponDto param) {
TbShopUser tbShopUser = shopUserMapper.selectByUserIdAndShopId(param.getUserId().toString(), param.getShopId().toString());
if (param.getOrderId() != null) {
TbOrderInfo tbOrderInfo = orderInfoMapper.selectByPrimaryKey(param.getOrderId());
List<TbOrderDetail> tbOrderDetails = orderDetailMapper.selectAllByOrderId(param.getOrderId());
Set<Integer> pros = tbOrderDetails.stream().map(TbOrderDetail::getProductId).collect(Collectors.toSet());
if (CollectionUtil.isNotEmpty(tbOrderDetails)) {
List<TbUserCouponVo> tbUserCouponVos = inRecordMapper.queryByVipIdAndShopId(Integer.valueOf(tbShopUser.getId()), param.getShopId());
if (CollectionUtil.isNotEmpty(tbUserCouponVos)) {
String week = DateUtil.dayOfWeekEnum(new Date()).toChinese("");
LocalTime now = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
//券id 券使用描述
Map<Integer, JsonObject> coupons = new HashMap<>();
for (TbUserCouponVo tbUserCouponVo : tbUserCouponVos) {
if (!coupons.containsKey(tbUserCouponVo.getCouponId())) {
JsonObject json=new JsonObject();
boolean isUse = true;
TbShopCoupon tbShopCoupon = couponMapper.queryById(tbUserCouponVo.getCouponId());
StringBuilder useRestrictions = new StringBuilder("每天 ");
if(tbShopCoupon.getType().equals(1)){
if(tbOrderInfo.getOrderAmount().compareTo(new BigDecimal(tbShopCoupon.getFullAmount()))<0){
isUse=false;
}
}
if(StringUtils.isNotBlank(tbShopCoupon.getUserDays())){
String[] split = tbShopCoupon.getUserDays().split(",");
if (split.length != 7) {
useRestrictions = new StringBuilder(tbShopCoupon.getUserDays() + " ");
}
if(!tbShopCoupon.getUserDays().contains(week)){
isUse=false;
}
}
if (tbShopCoupon.getUseTimeType().equals("custom")) {
if(now.isBefore(tbShopCoupon.getUseStartTime()) || now.isAfter(tbShopCoupon.getUseEndTime())){
isUse=false;
}
useRestrictions.append(
tbShopCoupon.getUseStartTime().format(formatter)
+ "-"
+ tbShopCoupon.getUseEndTime().format(formatter));
} else {
useRestrictions.append("全时段");
}
useRestrictions.append(" 可用");
json.addProperty("isUse",isUse);
json.addProperty("useRestrictions",useRestrictions.toString());
coupons.put(tbUserCouponVo.getCouponId(), json);
}
JsonObject couponJson = coupons.get(tbUserCouponVo.getCouponId());
tbUserCouponVo.setUseRestrictions(couponJson.get("useRestrictions").toString());
if(tbUserCouponVo.getType().equals(1)){
tbUserCouponVo.setUse(couponJson.get("isUse").getAsBoolean());
} else if (tbUserCouponVo.getType().equals(2) && couponJson.get("isUse").getAsBoolean()) {
if(!pros.contains(tbUserCouponVo.getProId())){
tbUserCouponVo.setUse(false);
}
}
}
tbUserCouponVos.sort(Comparator.comparing(TbUserCouponVo::isUse).reversed().thenComparing(TbUserCouponVo::getExpireTime));
return new Result(CodeEnum.SUCCESS, tbUserCouponVos);
}
}
} else {
if (param.getStatus().equals(1)) {
return new Result(CodeEnum.SUCCESS, inRecordMapper.queryByVipIdAndShopId(Integer.valueOf(tbShopUser.getId()), param.getShopId()));
} else if (param.getStatus().equals(-1)) {
return new Result(CodeEnum.SUCCESS, inRecordMapper.queryByVipIdAndShopIdExpire(Integer.valueOf(tbShopUser.getId()), param.getShopId()));
} else if (param.getStatus().equals(2)) {
return new Result(CodeEnum.SUCCESS, outRecordMapper.queryByVipIdAndShopId(Integer.valueOf(tbShopUser.getId()), param.getShopId()));
}
}
return new Result(CodeEnum.SUCCESS);
}
}