feat: 霸王餐相关接口

This commit is contained in:
2024-10-26 09:14:29 +08:00
parent 2f324a741a
commit 3573f6a0d6
10 changed files with 528 additions and 196 deletions

View File

@@ -12,9 +12,11 @@ import com.chaozhanggui.system.cashierservice.constant.TableConstant;
import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.Enum.OrderUseTypeEnum;
import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.entity.dto.OrderPayDTO;
import com.chaozhanggui.system.cashierservice.entity.dto.ReturnGroupOrderDto;
import com.chaozhanggui.system.cashierservice.entity.dto.ShopEatTypeInfoDTO;
import com.chaozhanggui.system.cashierservice.entity.vo.ShopUserListVo;
import com.chaozhanggui.system.cashierservice.entity.vo.TbUserCouponVo;
import com.chaozhanggui.system.cashierservice.exception.MsgException;
import com.chaozhanggui.system.cashierservice.mapper.MpCashierCartMapper;
import com.chaozhanggui.system.cashierservice.mapper.MpOrderDetailMapper;
@@ -34,6 +36,7 @@ import com.chaozhanggui.system.cashierservice.thirdpay.resp.WxScanPayResp;
import com.chaozhanggui.system.cashierservice.thirdpay.service.ThirdPayService;
import com.chaozhanggui.system.cashierservice.util.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@@ -51,6 +54,7 @@ import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
@@ -184,10 +188,14 @@ public class PayService {
private MpCashierCartMapper mpCashierCartMapper;
private final MpShopTableService mpShopTableService;
private final TbFreeDineConfigService freeDineConfigService;
private final TbShopCouponService shopCouponService;
public PayService(@Qualifier("tbShopSongOrderServiceImpl") TbShopSongOrderService shopSongOrderService, MpShopTableService mpShopTableService) {
public PayService(@Qualifier("tbShopSongOrderServiceImpl") TbShopSongOrderService shopSongOrderService, MpShopTableService mpShopTableService, TbFreeDineConfigService freeDineConfigService, TbShopCouponService shopCouponService) {
this.shopSongOrderService = shopSongOrderService;
this.mpShopTableService = mpShopTableService;
this.freeDineConfigService = freeDineConfigService;
this.shopCouponService = shopCouponService;
}
@Transactional(rollbackFor = Exception.class)
@@ -211,58 +219,41 @@ public class PayService {
return Result.success(CodeEnum.SUCCESS, mapper.readTree(wxScanPayResp.getObjData().getPayInfo()));
}
@Transactional(rollbackFor = Exception.class)
public Result payOrder(String openId, String orderId, String payType, String ip) throws Exception {
if (ObjectUtil.isEmpty(openId) || Objects.isNull(openId)) {
return Result.fail("付款用户[userId]参数不能为空");
}
TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(Integer.valueOf(orderId));
/**
* 校验订单支付状态是否正常
* @param orderInfo 订单信息
*/
private void checkOrderPayState(TbOrderInfo orderInfo) {
if (!"unpaid".equals(orderInfo.getStatus()) && !"paying".equals(orderInfo.getStatus())) {
return Result.fail("订单状态异常,不允许支付");
throw new MsgException("订单状态异常,不允许支付");
}
if (!OrderUseTypeEnum.DINE_IN_AFTER.getValue().equals(orderInfo.getUseType()) && System.currentTimeMillis() - orderInfo.getCreatedAt() > 60 * 15 * 1000) {
return Result.fail("订单十五分钟内有效,当前已超时,请重新下单。");
throw new MsgException("订单十五分钟内有效,当前已超时,请重新下单。");
}
if (ObjectUtil.isNull(orderInfo.getMerchantId()) || ObjectUtil.isEmpty(orderInfo.getMerchantId())) {
return Result.fail("没有对应的商户");
throw new MsgException("没有对应的商户");
}
List<TbCashierCart> cashierCarts = tbCashierCartMapper.selectByOrderId(orderId, null);
if (ObjectUtil.isEmpty(cashierCarts) || ObjectUtil.isNull(cashierCarts)) {
return Result.fail("购物车信息不存在");
}
}
StringBuffer body = new StringBuffer();
for (TbCashierCart cashierCart : cashierCarts) {
body.append(cashierCart.getName());
}
TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getMerchantId()));
if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) {
return Result.fail("支付通道不存在");
}
if ("aliPay".equals(payType) && StrUtil.isBlank(thirdApply.getAlipaySmallAppid())) {
return Result.fail("店铺未配置支付宝小程序appId");
}
String userId = String.valueOf(TokenUtil.getUserId());
TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(orderId);
/**
* 保存订单支付详情
* @param orderInfo 订单信息
* @param payType 支付类型
* @return 订单支付
*/
private TbOrderPayment createOrderPayment(TbOrderInfo orderInfo, String payType) {
TbOrderPayment payment = tbOrderPaymentMapper.selectByOrderId(String.valueOf(orderInfo.getId()));
String payName = "wechatPay".equals(payType) ? "微信支付" : "aliPay".equals(payType) ? "支付宝支付" : "未知支付";
if (ObjectUtil.isEmpty(payment) || payment == null) {
payment = new TbOrderPayment();
payment.setPayTypeId("ysk");
payment.setAmount(orderInfo.getOrderAmount());
payment.setPaidAmount(orderInfo.getPayAmount());
payment.setHasRefundAmount(BigDecimal.ZERO);
if ("wechatPay".equals(payType)) {
payment.setPayName("微信支付");
} else if ("aliPay".equals(payType)) {
payment.setPayName("支付宝支付");
}
payment.setPayName(payName);
payment.setPayType(payType);
payment.setReceived(payment.getAmount());
payment.setChangeFee(BigDecimal.ZERO);
@@ -272,39 +263,89 @@ public class PayService {
payment.setCreatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.insert(payment);
} else {
if (payType.equals("wechatPay")) {
payment.setPayName("微信支付");
} else if (payType.equals("aliPay")) {
payment.setPayName("支付宝支付");
}
payment.setPayName(payName);
payment.setPayType(payType);
payment.setUpdatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.updateByPrimaryKey(payment);
}
if ("ysk".equals(thirdPayType)) {
PayReq req = new PayReq();
return payment;
req.setAppId(thirdApply.getAppId());
req.setTimestamp(System.currentTimeMillis());
req.setIp(ip);
req.setMercOrderNo(orderInfo.getOrderNo());
req.setNotifyUrl(callBackurl);
req.setPayAmt(payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).toPlainString());
req.setPayType("03");
req.setPayWay("WXZF");
req.setSubject("扫码点餐");
req.setUserId(openId);
}
private JSONObject yskPay(TbMerchantThirdApply thirdApply, TbOrderInfo orderInfo, TbOrderPayment payment, String openId, String userId, String ip) {
PayReq req = new PayReq();
req.setAppId(thirdApply.getAppId());
req.setTimestamp(System.currentTimeMillis());
req.setIp(ip);
req.setMercOrderNo(orderInfo.getOrderNo());
req.setNotifyUrl(callBackurl);
req.setPayAmt(payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).toPlainString());
req.setPayType("03");
req.setPayWay("WXZF");
req.setSubject("扫码点餐");
req.setUserId(openId);
Map<String, Object> map = BeanUtil.transBeanMap(req);
req.setSign(MD5Util.encrypt(map, thirdApply.getAppToken(), true));
log.info("开始发送银收客支付请求,请求: {}", req);
ResponseEntity<String> response = restTemplate.postForEntity(url.concat("trans/pay"), req, String.class);
log.info("银收客支付请求结束,响应: {}", response);
if (response.getStatusCodeValue() == 200 && ObjectUtil.isNotEmpty(response.getBody())) {
JSONObject object = JSONObject.parseObject(response.getBody());
if (object.get("code").equals("0")) {
payment.setTradeNumber(object.getJSONObject("data").get("orderNumber").toString());
payment.setUpdatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.updateByPrimaryKeySelective(payment);
orderInfo.setStatus("paying");
orderInfo.setPayOrderNo(payment.getTradeNumber());
orderInfo.setUserId(userId);
tbOrderInfoMapper.updateByPrimaryKey(orderInfo);
Map<String, Object> map = BeanUtil.transBeanMap(req);
req.setSign(MD5Util.encrypt(map, thirdApply.getAppToken(), true));
//清除缓存购物车数据
redisUtil.deleteByKey(RedisCst.getTableCartKey(orderInfo.getTableId(), orderInfo.getTableId(), orderInfo.getUserId()));
tbCashierCartMapper.updateStatusByOrderId(orderInfo.getId().toString(), "final");
return object.getJSONObject("data");
} else {
throw new MsgException(object.getString("msg"));
}
}
throw new MsgException("支付失败");
}
ResponseEntity<String> response = restTemplate.postForEntity(url.concat("trans/pay"), req, String.class);
if (response.getStatusCodeValue() == 200 && ObjectUtil.isNotEmpty(response.getBody())) {
JSONObject object = JSONObject.parseObject(response.getBody());
if (object.get("code").equals("0")) {
payment.setTradeNumber(object.getJSONObject("data").get("orderNumber").toString());
private JsonNode fstPay(String body, TbMerchantThirdApply thirdApply, TbOrderInfo orderInfo, TbOrderPayment payment, String openId, String userId, String ip) throws JsonProcessingException {
String reqbody;
if (body.length() > 15) {
reqbody = body.substring(0, 6).concat("....").concat(body.substring(body.length() - 6, body.length()));
} else {
reqbody = body;
}
String smallAppid = thirdApply.getSmallAppid();
String appId = thirdApply.getAppId();
String appToken = thirdApply.getAppToken();
if ("aliPay".equals(orderInfo.getPayType())){
smallAppid = thirdApply.getAlipaySmallAppid();
}
String convertPayType = "aliPay".equals(orderInfo.getPayType()) ? "ALIPAY" : "WECHAT";
PublicResp<WxScanPayResp> publicResp = thirdPayService.scanpay(thirdUrl,
appId,
reqbody,
reqbody,
payment.getAmount().setScale(2, RoundingMode.DOWN).multiply(new BigDecimal(100)).longValue(),
convertPayType,
smallAppid,
openId,
ip,
DateUtils.getsdfTimesSS(),
thirdApply.getStoreId(),
callFSTBack,
null,
appToken);
if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) {
if ("000000".equals(publicResp.getCode())) {
WxScanPayResp wxScanPayResp = publicResp.getObjData();
if ("TRADE_AWAIT".equals(wxScanPayResp.getState())) {
payment.setTradeNumber(wxScanPayResp.getPayOrderId());
payment.setUpdatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.updateByPrimaryKeySelective(payment);
orderInfo.setStatus("paying");
@@ -313,84 +354,87 @@ public class PayService {
tbOrderInfoMapper.updateByPrimaryKey(orderInfo);
//清除缓存购物车数据
redisUtil.deleteByKey(RedisCst.getTableCartKey(orderInfo.getTableId(), orderInfo.getTableId(), orderInfo.getUserId()));
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("status", "success");
jsonObject1.put("msg", "成功");
jsonObject1.put("type", "");
jsonObject1.put("data", new JSONArray());
jsonObject1.put("amount", 0);
// AppWebSocketServer.AppSendInfo(jsonObject1,key, "",false);
tbCashierCartMapper.updateStatusByOrderId(orderId.toString(), "final");
return Result.success(CodeEnum.SUCCESS, object.getJSONObject("data"));
redisUtil.deleteByKey(RedisCst.getTableCartKey(orderInfo.getShopId(), orderInfo.getTableId(), orderInfo.getUserId()));
tbCashierCartMapper.updateStatusByOrderId(orderInfo.getId().toString(), "final");
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(wxScanPayResp.getPayInfo());
} else {
return Result.fail(object.getString("msg"));
}
}
} else {
String reqbody = "";
if (body.length() > 15) {
reqbody = body.substring(0, 6).concat("....").concat(body.substring(body.length() - 6, body.length())).toString();
} else {
reqbody = body.toString();
}
String smallAppid = thirdApply.getSmallAppid();
String appId = thirdApply.getAppId();
String appToken = thirdApply.getAppToken();
if ("aliPay".equals(payType)){
smallAppid = thirdApply.getAlipaySmallAppid();
}
String convertPayType = "aliPay".equals(payType) ? "ALIPAY" : "WECHAT";
PublicResp<WxScanPayResp> publicResp = thirdPayService.scanpay(thirdUrl,
appId,
reqbody,
reqbody,
payment.getAmount().setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).longValue(),
convertPayType,
smallAppid,
openId,
ip,
DateUtils.getsdfTimesSS(),
thirdApply.getStoreId(),
callFSTBack,
null,
appToken);
if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) {
if ("000000".equals(publicResp.getCode())) {
WxScanPayResp wxScanPayResp = publicResp.getObjData();
if ("TRADE_AWAIT".equals(wxScanPayResp.getState())) {
payment.setTradeNumber(wxScanPayResp.getPayOrderId());
payment.setUpdatedAt(System.currentTimeMillis());
tbOrderPaymentMapper.updateByPrimaryKeySelective(payment);
orderInfo.setStatus("paying");
orderInfo.setPayOrderNo(payment.getTradeNumber());
orderInfo.setUserId(userId);
tbOrderInfoMapper.updateByPrimaryKey(orderInfo);
//清除缓存购物车数据
redisUtil.deleteByKey(RedisCst.getTableCartKey(orderInfo.getShopId(), orderInfo.getTableId(), orderInfo.getUserId()));
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("status", "success");
jsonObject1.put("msg", "成功");
jsonObject1.put("type", "");
jsonObject1.put("data", new JSONArray());
jsonObject1.put("amount", 0);
// AppWebSocketServer.AppSendInfo(jsonObject1,key, "",false);
tbCashierCartMapper.updateStatusByOrderId(orderId.toString(), "final");
ObjectMapper mapper = new ObjectMapper();
return Result.success(CodeEnum.SUCCESS, mapper.readTree(wxScanPayResp.getPayInfo()));
} else {
return Result.fail(publicResp.getMsg());
}
throw new MsgException(publicResp.getMsg());
}
}
}
log.error("福商通支付失败,响应: {}", publicResp);
throw new MsgException("支付失败");
}
private void freeDinePay(OrderPayDTO payDTO, String productName, TbOrderInfo orderInfo, TbMerchantThirdApply thirdApply, String userId) {
// 获取店铺霸王餐配置
TbFreeDineConfig freeDineConfig = freeDineConfigService.getById(orderInfo.getShopId());
if (freeDineConfig == null || freeDineConfig.getEnable() == null || freeDineConfig.getEnable() == 0) {
throw new MsgException("店铺未开启霸王餐配置");
}
return Result.fail("失败");
// 校验订单金额是否满足
if (orderInfo.getOriginAmount().compareTo(freeDineConfig.getRechargeThreshold()) < 0) {
throw new MsgException("当前订单金额未满足霸王餐最低标准");
}
// 校验优惠券积分同享
if (payDTO.getCouponId() != null && freeDineConfig.getWithCoupon() == 0) {
throw new MsgException("当前店铺未开启与优惠券同享");
}
if (payDTO.getUsePoints() && freeDineConfig.getWithPoints() == 0) {
throw new MsgException("当前店铺未开启与积分同享");
}
BigDecimal shouldPayAmount = orderInfo.getOriginAmount().multiply(BigDecimal.valueOf(freeDineConfig.getRechargeTimes()));
if (payDTO.getCouponId() != null) {
List<TbUserCouponVo> couponList = shopCouponService.getActivateCouponByAmount(Integer.valueOf(orderInfo.getShopId()), userId, shouldPayAmount);
if (couponList.stream().noneMatch(item -> item.getCouponId().equals(payDTO.getCouponId()))) {
throw new MsgException("此优惠券不可用");
}
}
// TODO 霸王餐积分
if (payDTO.getUsePoints()) {
}
TbShopInfo shopInfo = mpsh.selectById(orderInfo.getShopId());
}
@Transactional(rollbackFor = Exception.class)
public Result payOrder(String openId, OrderPayDTO payDTO, String ip) throws Exception {
TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(payDTO.getOrderId());
checkOrderPayState(orderInfo);
List<TbCashierCart> cashierCarts = tbCashierCartMapper.selectByOrderId(String.valueOf(payDTO.getOrderId()), null);
if (ObjectUtil.isEmpty(cashierCarts) || ObjectUtil.isNull(cashierCarts)) {
return Result.fail("购物车信息不存在");
}
StringBuilder body = new StringBuilder();
for (TbCashierCart cashierCart : cashierCarts) {
body.append(cashierCart.getName());
}
TbMerchantThirdApply thirdApply = tbMerchantThirdApplyMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getMerchantId()));
if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) {
return Result.fail("支付通道不存在");
}
if ("aliPay".equals(payDTO.getPayType()) && StrUtil.isBlank(thirdApply.getAlipaySmallAppid())) {
return Result.fail("店铺未配置支付宝小程序appId");
}
String userId = String.valueOf(TokenUtil.getUserId());
TbOrderPayment payment = createOrderPayment(orderInfo, payDTO.getPayType());
if ("ysk".equals(thirdPayType)) {
return Result.successWithData(yskPay(thirdApply, orderInfo, payment, openId, userId, ip));
} else {
return Result.successWithData(fstPay(body.toString(), thirdApply, orderInfo, payment, openId, userId, ip));
}
}

View File

@@ -0,0 +1,13 @@
package com.chaozhanggui.system.cashierservice.service;
import com.chaozhanggui.system.cashierservice.entity.TbFreeDineConfig;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author Administrator
* @description 针对表【tb_free_dine_config(霸王餐配置信息表)】的数据库操作Service
* @createDate 2024-10-25 14:22:41
*/
public interface TbFreeDineConfigService extends IService<TbFreeDineConfig> {
}

View File

@@ -1,11 +1,14 @@
package com.chaozhanggui.system.cashierservice.service;
import com.chaozhanggui.system.cashierservice.entity.TbActivateOutRecord;
import com.chaozhanggui.system.cashierservice.entity.TbOrderInfo;
import com.chaozhanggui.system.cashierservice.entity.dto.CouponDto;
import com.chaozhanggui.system.cashierservice.entity.dto.CouponUseDto;
import com.chaozhanggui.system.cashierservice.entity.vo.TbUserCouponVo;
import com.chaozhanggui.system.cashierservice.sign.Result;
import org.springframework.context.annotation.Primary;
import java.math.BigDecimal;
import java.util.List;
/**
@@ -15,6 +18,12 @@ import java.util.List;
* @since 2024-10-23 15:37:37
*/
public interface TbShopCouponService {
/**
* 根据金额获取可用优惠券
* @param amount 订单金额
*/
List<TbUserCouponVo> getActivateCouponByAmount(Integer shopId, String userId, BigDecimal amount);
Result find(CouponDto param);
boolean use(Integer shopId,Integer orderId,Integer vipUserId,List<TbActivateOutRecord> param);

View File

@@ -0,0 +1,24 @@
package com.chaozhanggui.system.cashierservice.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaozhanggui.system.cashierservice.entity.TbFreeDineConfig;
import com.chaozhanggui.system.cashierservice.service.TbFreeDineConfigService;
import com.chaozhanggui.system.cashierservice.mapper.TbFreeDineConfigMapper;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
/**
* @author Administrator
* @description 针对表【tb_free_dine_config(霸王餐配置信息表)】的数据库操作Service实现
* @createDate 2024-10-25 14:22:41
*/
@Service
@Primary
public class TbFreeDineConfigServiceImpl extends ServiceImpl<TbFreeDineConfigMapper, TbFreeDineConfig>
implements TbFreeDineConfigService{
}

View File

@@ -5,7 +5,6 @@ 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.dto.CouponUseDto;
import com.chaozhanggui.system.cashierservice.entity.vo.TbUserCouponVo;
import com.chaozhanggui.system.cashierservice.service.TbShopCouponService;
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
@@ -14,9 +13,6 @@ 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;
@@ -47,6 +43,73 @@ public class TbShopCouponServiceImpl implements TbShopCouponService {
@Resource
private TbActivateOutRecordMapper outRecordMapper;
public List<TbUserCouponVo> getActivateCouponByAmount(Integer shopId, String userId, BigDecimal amount) {
TbShopUser tbShopUser = shopUserMapper.selectByUserIdAndShopId(userId, String.valueOf(shopId));
List<TbUserCouponVo> tbUserCouponVos = inRecordMapper.queryByVipIdAndShopId(Integer.valueOf(tbShopUser.getId()), shopId);
ArrayList<TbUserCouponVo> canUseCoupon = new ArrayList<>();
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 (tbUserCouponVo.getType() != null && tbUserCouponVo.getType() == 1) {
if (!coupons.containsKey(tbUserCouponVo.getCouponId())) {
setCouponInfo(coupons, tbUserCouponVo, amount, week, now, formatter);
}
JsonObject couponJson = coupons.get(tbUserCouponVo.getCouponId());
tbUserCouponVo.setUseRestrictions(couponJson.get("useRestrictions").toString());
if (tbUserCouponVo.getType().equals(1)) {
tbUserCouponVo.setUse(couponJson.get("isUse").getAsBoolean());
}
canUseCoupon.add(tbUserCouponVo);
}
}
canUseCoupon.sort(Comparator.comparing(TbUserCouponVo::isUse).reversed().thenComparing(TbUserCouponVo::getExpireTime));
}
return canUseCoupon;
}
private void setCouponInfo( Map<Integer, JsonObject> coupons, TbUserCouponVo tbUserCouponVo, BigDecimal amount, String week, LocalTime now, DateTimeFormatter formatter) {
JsonObject json = new JsonObject();
boolean isUse = true;
TbShopCoupon tbShopCoupon = couponMapper.queryById(tbUserCouponVo.getCouponId());
StringBuilder useRestrictions = new StringBuilder("每天 ");
if (tbShopCoupon.getType().equals(1)) {
if (amount.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);
}
@Override
public Result find(CouponDto param) {
TbShopUser tbShopUser = shopUserMapper.selectByUserIdAndShopId(param.getUserId().toString(), param.getShopId().toString());
@@ -65,47 +128,14 @@ public class TbShopCouponServiceImpl implements TbShopCouponService {
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);
setCouponInfo(coupons, tbUserCouponVo, tbOrderInfo.getAmount(), week, now, formatter);
}
JsonObject couponJson = coupons.get(tbUserCouponVo.getCouponId());
tbUserCouponVo.setUseRestrictions(couponJson.get("useRestrictions").toString());
if(tbUserCouponVo.getType().equals(1)){
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())){
if (!pros.contains(tbUserCouponVo.getProId())) {
tbUserCouponVo.setUse(false);
}
}
@@ -129,14 +159,15 @@ public class TbShopCouponServiceImpl implements TbShopCouponService {
/**
* 使用券
*
* @param shopId
* @param orderId
* @param vipUserId
* @param param giveId 和 useNum 必传
* @param param giveId 和 useNum 必传
* @return
*/
@Override
public boolean use(Integer shopId,Integer orderId,Integer vipUserId,List<TbActivateOutRecord> param) {
public boolean use(Integer shopId, Integer orderId, Integer vipUserId, List<TbActivateOutRecord> param) {
for (TbActivateOutRecord outRecord : param) {
TbActivateInRecord inRecord = inRecordMapper.queryById(outRecord.getGiveId());
inRecord.setOverNum(inRecord.getOverNum() - outRecord.getUseNum());
@@ -155,14 +186,15 @@ public class TbShopCouponServiceImpl implements TbShopCouponService {
/**
* 退还券
* @param param giveId和 refNum 必传
*
* @param param giveId和 refNum 必传
* @return
*/
@Override
public boolean refund(List<TbActivateOutRecord> param) {
for (TbActivateOutRecord outRecord : param) {
outRecord.setUpdateTime(new Date());
outRecordMapper.updateRefNum(outRecord.getId(),outRecord.getRefNum());
outRecordMapper.updateRefNum(outRecord.getId(), outRecord.getRefNum());
TbActivateInRecord inRecord = inRecordMapper.queryById(outRecord.getGiveId());
inRecord.setOverNum(inRecord.getOverNum() + outRecord.getRefNum());
inRecordMapper.updateOverNum(inRecord.getId(), inRecord.getOverNum());