|
|
@@ -5,6 +5,7 @@ import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
+import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.company.domain.CompanyMiniapp;
|
|
|
import com.fs.company.service.ICompanyMiniappService;
|
|
|
import com.fs.course.service.IFsCourseLinkService;
|
|
|
@@ -14,9 +15,11 @@ import com.fs.his.dto.InquiryConfigDTO;
|
|
|
import com.fs.his.mapper.*;
|
|
|
import com.fs.his.param.FsInquiryOrderCreateParam;
|
|
|
import com.fs.his.param.FsPackageOrderAddParam;
|
|
|
+import com.fs.his.param.FsPreOrderComputeParam;
|
|
|
import com.fs.his.service.IFsPreOrderService;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -26,6 +29,7 @@ import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -48,6 +52,9 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
private final ISysConfigService configService;
|
|
|
private final ICompanyMiniappService companyMiniappService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ RedisCache redisCache;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public R createPackagePreOrderLink(FsPackageOrderAddParam param, Long companyId, Long companyUserId) {
|
|
|
@@ -74,20 +81,14 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
return R.error("地址错误");
|
|
|
}
|
|
|
//支付金额
|
|
|
- BigDecimal totalPrice =param.getPayMoney();
|
|
|
- BigDecimal payableAmount = param.getPayableAmount() != null ? param.getPayableAmount(): fsPackage.getTotalPrice();
|
|
|
+ BigDecimal payMoney = param.getPayMoney();
|
|
|
+ BigDecimal payableAmount = param.getPayableAmount() != null ? param.getPayableAmount() : fsPackage.getTotalPrice();
|
|
|
+ FsCoupon coupon = null;
|
|
|
if (param.getCouponId() != null) {
|
|
|
- R couponCheck = checkPackageCoupon(param.getCouponId(), param.getUserId(), fsPackage);
|
|
|
- if (couponCheck != null) {
|
|
|
- return couponCheck;
|
|
|
- }
|
|
|
- FsCoupon coupon = fsCouponMapper.selectFsCouponByCouponId(param.getCouponId());
|
|
|
- payableAmount = payableAmount.subtract(coupon.getPrice() == null ? BigDecimal.ZERO : coupon.getPrice());
|
|
|
- if (payableAmount.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
- payableAmount = BigDecimal.ZERO;
|
|
|
- }
|
|
|
+ coupon = fsCouponMapper.selectFsCouponByCouponId(param.getCouponId());
|
|
|
}
|
|
|
- FsPreOrder preOrder = buildPackagePreOrder(param, companyId, companyUserId, totalPrice, payableAmount);
|
|
|
+ FsPreOrder preOrder = buildPackagePreOrder(param, companyId, companyUserId, payMoney, payableAmount);
|
|
|
+ setPreOrderCouponInfo(preOrder, coupon, fsPackage.getTotalPrice(), payableAmount);
|
|
|
return createPreOrderLink(preOrder, "/pages_index/packageDetails.html");
|
|
|
}
|
|
|
|
|
|
@@ -101,35 +102,36 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
if (patient == null) {
|
|
|
return R.error("请提交患者信息");
|
|
|
}
|
|
|
- BigDecimal payableAmount = BigDecimal.ZERO;
|
|
|
+ BigDecimal payMoney = param.getPayMoney();
|
|
|
+ BigDecimal payableAmount = param.getPayableAmount() != null ? param.getPayableAmount() : getInquiryPrice(param.getDoctorId(), param.getOrderType());
|
|
|
+ FsCoupon coupon = null;
|
|
|
if (param.getDoctorId() != null) {
|
|
|
FsDoctor doctor = fsDoctorMapper.selectFsDoctorByDoctorId(param.getDoctorId());
|
|
|
if (doctor == null) {
|
|
|
return R.error("医生不存在");
|
|
|
}
|
|
|
- payableAmount = getInquiryPrice(param.getDoctorId(), param.getOrderType());
|
|
|
}
|
|
|
if (param.getCouponId() != null) {
|
|
|
- R couponCheck = checkInquiryCoupon(param.getCouponId(), param.getUserId(), payableAmount, companyId);
|
|
|
- if (couponCheck != null) {
|
|
|
- return couponCheck;
|
|
|
- }
|
|
|
FsUserCoupon userCoupon = fsUserCouponMapper.selectFsUserCouponById(param.getCouponId());
|
|
|
+ if (userCoupon == null) {
|
|
|
+ userCoupon = fsUserCouponMapper.selectByCouponIdAndUserId(param.getCouponId(), param.getUserId());
|
|
|
+ }
|
|
|
if (userCoupon != null) {
|
|
|
- FsCoupon coupon = fsCouponMapper.selectFsCouponByCouponId(userCoupon.getCouponId());
|
|
|
- if (coupon != null) {
|
|
|
- BigDecimal couponPrice = coupon.getPrice() == null ? BigDecimal.ZERO : coupon.getPrice();
|
|
|
- payableAmount = payableAmount.subtract(couponPrice);
|
|
|
- if (payableAmount.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
- payableAmount = BigDecimal.ZERO;
|
|
|
- }
|
|
|
- }
|
|
|
+ coupon = fsCouponMapper.selectFsCouponByCouponId(userCoupon.getCouponId());
|
|
|
}
|
|
|
}
|
|
|
- FsPreOrder preOrder = buildInquiryPreOrder(param, companyId, companyUserId, payableAmount);
|
|
|
+ FsPreOrder preOrder = buildInquiryPreOrder(param, companyId, companyUserId, payMoney, payableAmount);
|
|
|
+ setPreOrderCouponInfo(preOrder, coupon, getInquiryPrice(param.getDoctorId(), param.getOrderType()), payableAmount);
|
|
|
+ setPreOrderkey(preOrder);
|
|
|
return createPreOrderLink(preOrder, "/pages_order/inquiryForm1.html");
|
|
|
}
|
|
|
|
|
|
+ private void setPreOrderkey(FsPreOrder preOrder){
|
|
|
+ String uuid = IdUtil.randomUUID();
|
|
|
+ preOrder.setOrderKey(uuid);
|
|
|
+ redisCache.setCacheObject("orderKey:"+uuid,uuid,200, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public R getPreOrder(Long preOrderId) {
|
|
|
if (preOrderId == null) {
|
|
|
@@ -139,7 +141,44 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
if (preOrder == null) {
|
|
|
return R.error("预制单不存在");
|
|
|
}
|
|
|
- return R.ok().put("data", preOrder);
|
|
|
+ Long userCouponId = getUserCouponId(preOrder.getCouponId(), preOrder.getUserId());
|
|
|
+ return R.ok()
|
|
|
+ .put("data", preOrder)
|
|
|
+ .put("userCouponId", userCouponId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R computePreOrderAmount(FsPreOrderComputeParam param) {
|
|
|
+ if (param == null) {
|
|
|
+ return R.error("参数不能为空");
|
|
|
+ }
|
|
|
+ BigDecimal payMoney = param.getPayMoney() == null ? BigDecimal.ZERO : param.getPayMoney();
|
|
|
+ BigDecimal originalPayableAmount = param.getPayableAmount() == null ? payMoney : param.getPayableAmount();
|
|
|
+ BigDecimal payableAmount = originalPayableAmount;
|
|
|
+ BigDecimal discountMoney = BigDecimal.ZERO;
|
|
|
+ String couponName = null;
|
|
|
+ FsCoupon coupon = getPreOrderCoupon(param);
|
|
|
+ if (param.getCouponId() != null && coupon == null) {
|
|
|
+ return R.error("优惠券不存在");
|
|
|
+ }
|
|
|
+ if (coupon != null) {
|
|
|
+ R couponCheck = checkPreOrderCoupon(param, coupon, originalPayableAmount);
|
|
|
+ if (couponCheck != null) {
|
|
|
+ return couponCheck;
|
|
|
+ }
|
|
|
+ couponName = coupon.getTitle();
|
|
|
+ discountMoney = calculateDiscountMoney(param, coupon, originalPayableAmount);
|
|
|
+ payableAmount = originalPayableAmount.subtract(discountMoney);
|
|
|
+ if (payableAmount.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ payableAmount = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.ok()
|
|
|
+ .put("payMoney", payMoney)
|
|
|
+ .put("payableAmount", payableAmount)
|
|
|
+ .put("discountMoney", discountMoney)
|
|
|
+ .put("couponName", couponName)
|
|
|
+ .put("couponId", param.getCouponId());
|
|
|
}
|
|
|
|
|
|
private R createPreOrderLink(FsPreOrder preOrder, String pagePath){
|
|
|
@@ -182,7 +221,19 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
.put("linkStr", linkStr)
|
|
|
.put("shareLink", shareLink);
|
|
|
}
|
|
|
-
|
|
|
+ private Long getUserCouponId(Long couponId, Long userId) {
|
|
|
+ if (couponId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ FsUserCoupon userCoupon = null;
|
|
|
+ if (userId != null) {
|
|
|
+ userCoupon = fsUserCouponMapper.selectByCouponIdAndUserId(couponId, userId);
|
|
|
+ }
|
|
|
+ if (userCoupon == null) {
|
|
|
+ userCoupon = fsUserCouponMapper.selectFsUserCouponById(couponId);
|
|
|
+ }
|
|
|
+ return userCoupon == null ? null : userCoupon.getId();
|
|
|
+ }
|
|
|
private String getCompanyFirstAppId(Long companyId) {
|
|
|
if (companyId == null) {
|
|
|
return null;
|
|
|
@@ -213,13 +264,12 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
return preOrder;
|
|
|
}
|
|
|
|
|
|
- private FsPreOrder buildInquiryPreOrder(FsInquiryOrderCreateParam param, Long companyId, Long companyUserId, BigDecimal payableAmount) {
|
|
|
+ private FsPreOrder buildInquiryPreOrder(FsInquiryOrderCreateParam param, Long companyId, Long companyUserId, BigDecimal payMoney, BigDecimal payableAmount) {
|
|
|
FsPreOrder preOrder = new FsPreOrder();
|
|
|
preOrder.setPreOrderType(TYPE_INQUIRY);
|
|
|
preOrder.setCompanyId(companyId);
|
|
|
preOrder.setCompanyUserId(companyUserId);
|
|
|
preOrder.setUserId(param.getUserId());
|
|
|
- preOrder.setOrderKey(param.getOrderKey() == null ? IdUtil.randomUUID() : param.getOrderKey());
|
|
|
preOrder.setPatientId(param.getPatientId());
|
|
|
preOrder.setCouponId(param.getCouponId());
|
|
|
preOrder.setTitle(param.getTitle());
|
|
|
@@ -240,58 +290,112 @@ public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
preOrder.setMedication(param.getMedication());
|
|
|
preOrder.setUsage(param.getUsage());
|
|
|
preOrder.setSource(param.getSource());
|
|
|
+ preOrder.setPayMoney(payMoney);
|
|
|
preOrder.setPayableAmount(payableAmount);
|
|
|
return preOrder;
|
|
|
}
|
|
|
|
|
|
- private R checkPackageCoupon(Long couponId, Long userId, FsPackage fsPackage) {
|
|
|
- FsCoupon coupon = fsCouponMapper.selectFsCouponByCouponId(couponId);
|
|
|
+ private void setPreOrderCouponInfo(FsPreOrder preOrder, FsCoupon coupon, BigDecimal payMoney, BigDecimal payableAmount) {
|
|
|
if (coupon == null) {
|
|
|
- return R.error("优惠券配置不存在");
|
|
|
+ preOrder.setDiscountMoney(BigDecimal.ZERO);
|
|
|
+ return;
|
|
|
}
|
|
|
- if (!Integer.valueOf(5).equals(coupon.getCouponType())) {
|
|
|
- return R.error("优惠券类型不正确,仅支持套餐优惠券");
|
|
|
+ preOrder.setCouponName(coupon.getTitle());
|
|
|
+ BigDecimal baseAmount = payMoney == null ? BigDecimal.ZERO : payMoney;
|
|
|
+ BigDecimal discountMoney = baseAmount.subtract(payableAmount == null ? BigDecimal.ZERO : payableAmount);
|
|
|
+ if (discountMoney.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ discountMoney = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ preOrder.setDiscountMoney(discountMoney);
|
|
|
+ }
|
|
|
+
|
|
|
+ private FsCoupon getPreOrderCoupon(FsPreOrderComputeParam param) {
|
|
|
+ if (param.getCouponId() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (Integer.valueOf(TYPE_INQUIRY).equals(param.getPreOrderType())) {
|
|
|
+ FsUserCoupon userCoupon = fsUserCouponMapper.selectFsUserCouponById(param.getCouponId());
|
|
|
+ if (userCoupon == null) {
|
|
|
+ userCoupon = fsUserCouponMapper.selectByCouponIdAndUserId(param.getCouponId(), param.getUserId());
|
|
|
+ }
|
|
|
+ return userCoupon == null ? null : fsCouponMapper.selectFsCouponByCouponId(userCoupon.getCouponId());
|
|
|
+ }
|
|
|
+ return fsCouponMapper.selectFsCouponByCouponId(param.getCouponId());
|
|
|
+ }
|
|
|
+
|
|
|
+ private R checkPreOrderCoupon(FsPreOrderComputeParam param, FsCoupon coupon, BigDecimal amount) {
|
|
|
+ R commonCheck = checkCouponConfig(coupon, amount);
|
|
|
+ if (commonCheck != null) {
|
|
|
+ return commonCheck;
|
|
|
+ }
|
|
|
+ if (Integer.valueOf(TYPE_INQUIRY).equals(param.getPreOrderType())) {
|
|
|
+ FsUserCoupon userCoupon = fsUserCouponMapper.selectFsUserCouponById(param.getCouponId());
|
|
|
+ if (userCoupon == null) {
|
|
|
+ userCoupon = fsUserCouponMapper.selectByCouponIdAndUserId(param.getCouponId(), param.getUserId());
|
|
|
+ }
|
|
|
+ return checkUserCoupon(userCoupon, param.getUserId(), coupon);
|
|
|
+ }
|
|
|
+ FsUserCoupon userCoupon = null;
|
|
|
+ if (param.getCouponId() != null && param.getUserId() != null) {
|
|
|
+ userCoupon = fsUserCouponMapper.selectByCouponIdAndUserId(param.getCouponId(), param.getUserId());
|
|
|
+ }
|
|
|
+ return checkUserCoupon(userCoupon, param.getUserId(), coupon);
|
|
|
+ }
|
|
|
+
|
|
|
+ private R checkCouponConfig(FsCoupon coupon, BigDecimal amount) {
|
|
|
+ if (!Integer.valueOf(1).equals(coupon.getStatus())) {
|
|
|
+ return R.error("优惠券已停用");
|
|
|
}
|
|
|
if (coupon.getLimitTime() != null && coupon.getLimitTime().before(new Date())) {
|
|
|
return R.error("优惠券已过期");
|
|
|
}
|
|
|
- FsUserCoupon userCoupon = fsUserCouponMapper.selectByCouponIdAndUserId(couponId, userId);
|
|
|
- if (userCoupon == null) {
|
|
|
- return R.error("该用户的优惠券不存在");
|
|
|
+ if (coupon.getRemainNumber() != null && coupon.getRemainNumber() <= 0) {
|
|
|
+ return R.error("优惠券剩余数量不足");
|
|
|
}
|
|
|
- if (!Integer.valueOf(0).equals(userCoupon.getStatus())) {
|
|
|
- return R.error("优惠券已使用或已过期");
|
|
|
+ if (coupon.getNumber() != null && coupon.getNumber() <= 0) {
|
|
|
+ return R.error("优惠券数量不足");
|
|
|
+ }
|
|
|
+ BigDecimal baseAmount = amount == null ? BigDecimal.ZERO : amount;
|
|
|
+ if (coupon.getMinPrice() != null && coupon.getMinPrice().compareTo(baseAmount) > 0) {
|
|
|
+ return R.error("未达到优惠券最低消费金额");
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private R checkInquiryCoupon(Long userCouponId, Long userId, BigDecimal money, Long companyId) {
|
|
|
- if (companyId != null && companyId > 0) {
|
|
|
- return R.error("此订单不能使用优惠券");
|
|
|
- }
|
|
|
- FsUserCoupon userCoupon = fsUserCouponMapper.selectFsUserCouponById(userCouponId);
|
|
|
+ private R checkUserCoupon(FsUserCoupon userCoupon, Long userId, FsCoupon coupon) {
|
|
|
if (userCoupon == null) {
|
|
|
- return R.error("优惠券不存在");
|
|
|
- }
|
|
|
- if (!Integer.valueOf(0).equals(userCoupon.getStatus())) {
|
|
|
- return R.error("优惠券已过期或已使用");
|
|
|
+ return R.error("该用户的优惠券不存在");
|
|
|
}
|
|
|
if (userId != null && userCoupon.getUserId() != null && !userId.equals(userCoupon.getUserId())) {
|
|
|
return R.error("该用户的优惠券不存在");
|
|
|
}
|
|
|
- FsCoupon coupon = fsCouponMapper.selectFsCouponByCouponId(userCoupon.getCouponId());
|
|
|
- if (coupon == null) {
|
|
|
- return R.error("优惠券不存在");
|
|
|
+ if (!Integer.valueOf(0).equals(userCoupon.getStatus())) {
|
|
|
+ return R.error("优惠券已使用或已过期");
|
|
|
}
|
|
|
- if (Integer.valueOf(2).equals(coupon.getCouponType()) || Integer.valueOf(5).equals(coupon.getCouponType())) {
|
|
|
- return R.error("此优惠券不可用于问诊订单");
|
|
|
+ if (userCoupon.getLimitTime() != null && userCoupon.getLimitTime().before(new Date())) {
|
|
|
+ return R.error("优惠券已过期");
|
|
|
}
|
|
|
- if (Integer.valueOf(1).equals(coupon.getCouponType()) && coupon.getMinPrice() != null && coupon.getMinPrice().compareTo(money) == 1) {
|
|
|
- return R.error("此优惠券不可用");
|
|
|
+ if (coupon.getCouponId() != null && userCoupon.getCouponId() != null && !coupon.getCouponId().equals(userCoupon.getCouponId())) {
|
|
|
+ return R.error("优惠券不存在");
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private BigDecimal calculateDiscountMoney(FsPreOrderComputeParam param, FsCoupon coupon, BigDecimal amount) {
|
|
|
+ BigDecimal baseAmount = amount == null ? BigDecimal.ZERO : amount;
|
|
|
+ if (Integer.valueOf(TYPE_INQUIRY).equals(param.getPreOrderType())) {
|
|
|
+ return baseAmount;
|
|
|
+ }
|
|
|
+ if (Integer.valueOf(2).equals(coupon.getCouponType()) || Integer.valueOf(3).equals(coupon.getCouponType())) {
|
|
|
+ return baseAmount;
|
|
|
+ }
|
|
|
+ if (coupon.getMinPrice() != null && coupon.getMinPrice().compareTo(baseAmount) > 0) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ BigDecimal couponPrice = coupon.getPrice() == null ? BigDecimal.ZERO : coupon.getPrice();
|
|
|
+ return couponPrice.compareTo(baseAmount) > 0 ? baseAmount : couponPrice;
|
|
|
+ }
|
|
|
+
|
|
|
private BigDecimal getInquiryPrice(Long doctorId, Integer orderType) {
|
|
|
String json = configService.selectConfigByKey("his.inquiryConfig");
|
|
|
InquiryConfigDTO configDTO = JSONUtil.toBean(json, InquiryConfigDTO.class);
|