|
|
@@ -0,0 +1,321 @@
|
|
|
+package com.fs.his.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+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.company.domain.CompanyMiniapp;
|
|
|
+import com.fs.company.service.ICompanyMiniappService;
|
|
|
+import com.fs.course.service.IFsCourseLinkService;
|
|
|
+import com.fs.his.domain.*;
|
|
|
+import com.fs.his.dto.FsPriceDTO;
|
|
|
+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.service.IFsPreOrderService;
|
|
|
+import com.fs.system.service.ISysConfigService;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 预制单Service业务层处理
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class FsPreOrderServiceImpl implements IFsPreOrderService {
|
|
|
+ private static final int TYPE_PACKAGE = 1;
|
|
|
+ private static final int TYPE_INQUIRY = 2;
|
|
|
+
|
|
|
+ private final FsPreOrderMapper fsPreOrderMapper;
|
|
|
+ private final IFsCourseLinkService fsCourseLinkService;
|
|
|
+ private final FsPatientMapper fsPatientMapper;
|
|
|
+ private final FsPackageMapper fsPackageMapper;
|
|
|
+ private final FsUserAddressMapper fsUserAddressMapper;
|
|
|
+ private final FsCouponMapper fsCouponMapper;
|
|
|
+ private final FsUserCouponMapper fsUserCouponMapper;
|
|
|
+ private final FsDoctorMapper fsDoctorMapper;
|
|
|
+ private final ISysConfigService configService;
|
|
|
+ private final ICompanyMiniappService companyMiniappService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R createPackagePreOrderLink(FsPackageOrderAddParam param, Long companyId, Long companyUserId) {
|
|
|
+ if (param == null) {
|
|
|
+ return R.error("参数不能为空");
|
|
|
+ }
|
|
|
+ FsPatient patient = fsPatientMapper.selectFsPatientByPatientId(param.getPatientId());
|
|
|
+ if (patient == null) {
|
|
|
+ return R.error("请提交患者信息");
|
|
|
+ }
|
|
|
+ FsPackage fsPackage = fsPackageMapper.selectFsPackageByPackageId(param.getPackageId());
|
|
|
+ if (fsPackage == null || Integer.valueOf(0).equals(fsPackage.getStatus())) {
|
|
|
+ return R.error("套餐已下架");
|
|
|
+ }
|
|
|
+ Integer num = fsPackage.getNum();
|
|
|
+ if (num != null && num != 0) {
|
|
|
+ int count = fsPackageMapper.selectFsPackageListByUser(param.getUserId(), fsPackage.getPackageId());
|
|
|
+ if (count >= num) {
|
|
|
+ return R.error("超过限购次数");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FsUserAddress address = fsUserAddressMapper.selectFsUserAddressByAddressId(param.getAddressId());
|
|
|
+ if (address == null) {
|
|
|
+ return R.error("地址错误");
|
|
|
+ }
|
|
|
+ //支付金额
|
|
|
+ BigDecimal totalPrice =param.getPayMoney();
|
|
|
+ BigDecimal payableAmount = param.getPayableAmount() != null ? param.getPayableAmount(): fsPackage.getTotalPrice();
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FsPreOrder preOrder = buildPackagePreOrder(param, companyId, companyUserId, totalPrice, payableAmount);
|
|
|
+ return createPreOrderLink(preOrder, "/pages_index/packageDetails.html");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R createInquiryPreOrderLink(FsInquiryOrderCreateParam param, Long companyId, Long companyUserId) {
|
|
|
+ if (param == null) {
|
|
|
+ return R.error("参数不能为空");
|
|
|
+ }
|
|
|
+ FsPatient patient = fsPatientMapper.selectFsPatientByPatientId(param.getPatientId());
|
|
|
+ if (patient == null) {
|
|
|
+ return R.error("请提交患者信息");
|
|
|
+ }
|
|
|
+ BigDecimal payableAmount = BigDecimal.ZERO;
|
|
|
+ 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) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FsPreOrder preOrder = buildInquiryPreOrder(param, companyId, companyUserId, payableAmount);
|
|
|
+ return createPreOrderLink(preOrder, "/pages_order/inquiryForm1.html");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R getPreOrder(Long preOrderId) {
|
|
|
+ if (preOrderId == null) {
|
|
|
+ return R.error("预制单ID不能为空");
|
|
|
+ }
|
|
|
+ FsPreOrder preOrder = fsPreOrderMapper.selectFsPreOrderById(preOrderId);
|
|
|
+ if (preOrder == null) {
|
|
|
+ return R.error("预制单不存在");
|
|
|
+ }
|
|
|
+ return R.ok().put("data", preOrder);
|
|
|
+ }
|
|
|
+
|
|
|
+ private R createPreOrderLink(FsPreOrder preOrder, String pagePath){
|
|
|
+ if (preOrder.getCompanyId() == null || preOrder.getCompanyUserId() == null) {
|
|
|
+ return R.error("公司或销售不能为空");
|
|
|
+ }
|
|
|
+ preOrder.setStatus(0);
|
|
|
+ preOrder.setCreateTime(new Date());
|
|
|
+ fsPreOrderMapper.insertFsPreOrder(preOrder);
|
|
|
+
|
|
|
+ String rawLinkStr;
|
|
|
+ String appId = null;
|
|
|
+ if (Integer.valueOf(TYPE_INQUIRY).equals(preOrder.getPreOrderType())) {
|
|
|
+ rawLinkStr = pagePath + "?preOrderId=" + preOrder.getPreOrderId() + "&isNew=true";
|
|
|
+ appId = getCompanyFirstAppId(preOrder.getCompanyId());
|
|
|
+ } else {
|
|
|
+ rawLinkStr = pagePath + "?companyId=" + preOrder.getCompanyId()
|
|
|
+ + "&companyUserId=" + preOrder.getCompanyUserId()
|
|
|
+ + "&preOrderId=" + preOrder.getPreOrderId()
|
|
|
+ + "&choose=0"
|
|
|
+ + "&isNew=true";
|
|
|
+ }
|
|
|
+ String linkStr;
|
|
|
+ try {
|
|
|
+ linkStr = URLEncoder.encode(rawLinkStr, StandardCharsets.UTF_8.toString());
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException("链接编码失败", e);
|
|
|
+ }
|
|
|
+ String shareLink = fsCourseLinkService.getGotoWxAppLinkForPreOrder(linkStr, appId);
|
|
|
+
|
|
|
+ FsPreOrder update = new FsPreOrder();
|
|
|
+ update.setPreOrderId(preOrder.getPreOrderId());
|
|
|
+ update.setLinkStr(rawLinkStr);
|
|
|
+ update.setShareLink(shareLink);
|
|
|
+ update.setUpdateTime(new Date());
|
|
|
+ fsPreOrderMapper.updateFsPreOrder(update);
|
|
|
+
|
|
|
+ return R.ok()
|
|
|
+ .put("preOrderId", preOrder.getPreOrderId())
|
|
|
+ .put("linkStr", linkStr)
|
|
|
+ .put("shareLink", shareLink);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getCompanyFirstAppId(Long companyId) {
|
|
|
+ if (companyId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<CompanyMiniapp> miniappList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().eq("company_id", companyId));
|
|
|
+ if (miniappList == null || miniappList.isEmpty() || miniappList.get(0) == null || miniappList.get(0).getAppId() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String[] appIds = miniappList.get(0).getAppId().split(",");
|
|
|
+ return appIds.length > 0 ? appIds[0].trim() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FsPreOrder buildPackagePreOrder(FsPackageOrderAddParam param, Long companyId, Long companyUserId, BigDecimal totalPrice, BigDecimal payableAmount) {
|
|
|
+ FsPreOrder preOrder = new FsPreOrder();
|
|
|
+ preOrder.setPreOrderType(TYPE_PACKAGE);
|
|
|
+ preOrder.setCompanyId(companyId);
|
|
|
+ preOrder.setCompanyUserId(companyUserId);
|
|
|
+ preOrder.setUserId(param.getUserId());
|
|
|
+ preOrder.setPatientId(param.getPatientId());
|
|
|
+ preOrder.setPackageId(param.getPackageId());
|
|
|
+ preOrder.setCouponId(param.getCouponId());
|
|
|
+ preOrder.setCompanyUserRemark(param.getOrderRemark());
|
|
|
+ preOrder.setPayType(param.getPayType());
|
|
|
+ preOrder.setPayMoney(totalPrice);
|
|
|
+ preOrder.setPayableAmount(payableAmount);
|
|
|
+ preOrder.setAddressId(param.getAddressId());
|
|
|
+ preOrder.setProductIds(param.getProductIds() == null ? null : JSON.toJSONString(param.getProductIds()));
|
|
|
+ return preOrder;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FsPreOrder buildInquiryPreOrder(FsInquiryOrderCreateParam param, Long companyId, Long companyUserId, 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());
|
|
|
+ preOrder.setCompanyUserRemark(param.getCompanyUserRemark());
|
|
|
+ preOrder.setDuration(param.getDuration());
|
|
|
+ preOrder.setIsVisit(param.getIsVisit());
|
|
|
+ preOrder.setInquiryType(param.getInquiryType());
|
|
|
+ preOrder.setInquirySubType(param.getInquirySubType());
|
|
|
+ preOrder.setInquiryOrderType(param.getOrderType());
|
|
|
+ preOrder.setDoctorId(param.getDoctorId());
|
|
|
+ preOrder.setReportImages(param.getReportImages());
|
|
|
+ preOrder.setTongueImages(param.getTongueImages());
|
|
|
+ preOrder.setFaceImages(param.getFaceImages());
|
|
|
+ preOrder.setHandImages(param.getHandImages());
|
|
|
+ preOrder.setHeight(param.getHeight());
|
|
|
+ preOrder.setWeight(param.getWeight());
|
|
|
+ preOrder.setMobile(param.getMobile());
|
|
|
+ preOrder.setMedication(param.getMedication());
|
|
|
+ preOrder.setUsage(param.getUsage());
|
|
|
+ preOrder.setSource(param.getSource());
|
|
|
+ preOrder.setPayableAmount(payableAmount);
|
|
|
+ return preOrder;
|
|
|
+ }
|
|
|
+
|
|
|
+ private R checkPackageCoupon(Long couponId, Long userId, FsPackage fsPackage) {
|
|
|
+ FsCoupon coupon = fsCouponMapper.selectFsCouponByCouponId(couponId);
|
|
|
+ if (coupon == null) {
|
|
|
+ return R.error("优惠券配置不存在");
|
|
|
+ }
|
|
|
+ if (!Integer.valueOf(5).equals(coupon.getCouponType())) {
|
|
|
+ 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 (!Integer.valueOf(0).equals(userCoupon.getStatus())) {
|
|
|
+ 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);
|
|
|
+ if (userCoupon == null) {
|
|
|
+ return R.error("优惠券不存在");
|
|
|
+ }
|
|
|
+ if (!Integer.valueOf(0).equals(userCoupon.getStatus())) {
|
|
|
+ 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(2).equals(coupon.getCouponType()) || Integer.valueOf(5).equals(coupon.getCouponType())) {
|
|
|
+ return R.error("此优惠券不可用于问诊订单");
|
|
|
+ }
|
|
|
+ if (Integer.valueOf(1).equals(coupon.getCouponType()) && coupon.getMinPrice() != null && coupon.getMinPrice().compareTo(money) == 1) {
|
|
|
+ return R.error("此优惠券不可用");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal getInquiryPrice(Long doctorId, Integer orderType) {
|
|
|
+ String json = configService.selectConfigByKey("his.inquiryConfig");
|
|
|
+ InquiryConfigDTO configDTO = JSONUtil.toBean(json, InquiryConfigDTO.class);
|
|
|
+
|
|
|
+ if (doctorId != null) {
|
|
|
+ FsDoctor doctor = fsDoctorMapper.selectFsDoctorByDoctorId(doctorId);
|
|
|
+ if (doctor == null || Integer.valueOf(0).equals(doctor.getStatus()) || !Integer.valueOf(1).equals(doctor.getWorkStatus())) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ if (doctor.getPriceJson() != null) {
|
|
|
+ List<FsPriceDTO> priceDTOList = JSONUtil.parseArray(doctor.getPriceJson()).toList(FsPriceDTO.class);
|
|
|
+ List<FsPriceDTO> doctorPrice = priceDTOList.stream().filter(x -> x.getType().equals(orderType)).collect(Collectors.toList());
|
|
|
+ if (doctorPrice.size() == 1 && doctorPrice.get(0).getPrice() != null) {
|
|
|
+ return doctorPrice.get(0).getPrice();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (configDTO != null && configDTO.getPrices() != null) {
|
|
|
+ List<FsPriceDTO> price = configDTO.getPrices().stream().filter(x -> x.getType().equals(orderType)).collect(Collectors.toList());
|
|
|
+ if (price.size() == 1 && price.get(0).getPrice() != null) {
|
|
|
+ return price.get(0).getPrice();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+}
|