|
@@ -0,0 +1,212 @@
|
|
|
+package com.bosshand.virgo.api.workark.service;
|
|
|
+
|
|
|
+import com.bosshand.virgo.api.workark.dao.OrderInfoDao;
|
|
|
+import com.bosshand.virgo.api.workark.dao.ProductCouponDao;
|
|
|
+import com.bosshand.virgo.api.workark.dao.ProductDao;
|
|
|
+import com.bosshand.virgo.api.workark.enums.OrderStatus;
|
|
|
+import com.bosshand.virgo.api.workark.model.OrderInfo;
|
|
|
+import com.bosshand.virgo.api.workark.model.Product;
|
|
|
+import com.bosshand.virgo.api.workark.model.ProductCoupon;
|
|
|
+import com.bosshand.virgo.api.workark.util.OrderNoUtils;
|
|
|
+import com.bosshand.virgo.core.utils.ContextUtils;
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class OrderInfoService {
|
|
|
+
|
|
|
+ private final static Log log = LogFactory.getLog(OrderInfoService.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrderInfoDao orderInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProductDao productDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProductCouponDao productCouponDao;
|
|
|
+
|
|
|
+ public void delete(long id) {
|
|
|
+ orderInfoDao.delete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void update(OrderInfo orderInfo) {
|
|
|
+ orderInfoDao.update(orderInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<OrderInfo> getList(OrderInfo orderInfo){
|
|
|
+ return orderInfoDao.getList(orderInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public OrderInfo get(long id){
|
|
|
+ return orderInfoDao.get(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public OrderInfo createOrderByProductId(Long productId, List<ProductCoupon> productCouponList) {
|
|
|
+
|
|
|
+ long userId = ContextUtils.getUserContext().getUserId();
|
|
|
+
|
|
|
+ //查找已存在但未支付的订单
|
|
|
+ OrderInfo orderInfo = orderInfoDao.getNoPayOrderByProductId(productId, userId, OrderStatus.NOTPAY.getType());
|
|
|
+ if(orderInfo != null){
|
|
|
+ return orderInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取商品信息
|
|
|
+ Product product = productDao.get(productId);
|
|
|
+
|
|
|
+ String productCouponIds = null;
|
|
|
+
|
|
|
+ if (productCouponList.size() > 0) {
|
|
|
+ List<String> ids = new ArrayList();
|
|
|
+ productCouponList.forEach(ls -> ids.add(String.valueOf(ls.getId())));
|
|
|
+ productCouponIds = String.join(",", ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算价格
|
|
|
+ Map<Long, BigDecimal> longBigDecimalMap = calculatePrice(productId, productCouponList);
|
|
|
+ BigDecimal price = longBigDecimalMap.get(-1L);
|
|
|
+
|
|
|
+ //生成订单
|
|
|
+ orderInfo = new OrderInfo();
|
|
|
+ orderInfo.setTitle(product.getName());
|
|
|
+ orderInfo.setOrderNo(OrderNoUtils.getOrderNo()); //订单号
|
|
|
+ orderInfo.setProductId(productId);
|
|
|
+ orderInfo.setProductCouponIds(productCouponIds);
|
|
|
+
|
|
|
+ // 元 转换 分
|
|
|
+ BigDecimal multiplier = new BigDecimal("100");
|
|
|
+
|
|
|
+ // 使用multiply方法乘以100
|
|
|
+ BigDecimal result = price.multiply(multiplier);
|
|
|
+
|
|
|
+ orderInfo.setTotalFee(result.intValue()); //分
|
|
|
+ orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
|
|
|
+ orderInfo.setUserId(userId);
|
|
|
+ orderInfoDao.save(orderInfo);
|
|
|
+ return orderInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OrderInfo getOrderNo(String orderNo) {
|
|
|
+ return orderInfoDao.getOrderNo(orderNo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存储订单二维码
|
|
|
+ * @param orderNo
|
|
|
+ * @param codeUrl
|
|
|
+ */
|
|
|
+ public void saveCodeUrl(String orderNo, String codeUrl) {
|
|
|
+ OrderInfo orderInfo = orderInfoDao.getOrderNo(orderNo);
|
|
|
+ orderInfo.setCodeUrl(codeUrl);
|
|
|
+ orderInfoDao.update(orderInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据订单号获取订单状态
|
|
|
+ * @param orderNo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getOrderStatus(String orderNo) {
|
|
|
+ OrderInfo orderInfo = orderInfoDao.getOrderNo(orderNo);
|
|
|
+ if(orderInfo == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return orderInfo.getOrderStatus();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据订单号更新订单状态
|
|
|
+ * @param orderNo
|
|
|
+ * @param orderStatus
|
|
|
+ */
|
|
|
+ public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
|
|
|
+
|
|
|
+ log.info("更新订单状态 ===>"+ orderStatus.getType());
|
|
|
+
|
|
|
+ OrderInfo orderInfo = orderInfoDao.getOrderNo(orderNo);
|
|
|
+ orderInfo.setOrderStatus(orderStatus.getType());
|
|
|
+
|
|
|
+ orderInfoDao.update(orderInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询创建超过minutes分钟并且未支付的订单
|
|
|
+ * @param minutes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<OrderInfo> getNoPayOrderByDuration(int minutes) {
|
|
|
+
|
|
|
+ Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
|
|
|
+
|
|
|
+ ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
|
|
|
+ Date date = Date.from(zonedDateTime.toInstant());
|
|
|
+
|
|
|
+ OrderInfo orderInfo = new OrderInfo();
|
|
|
+ orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
|
|
|
+ orderInfo.setCreateTime(date);
|
|
|
+
|
|
|
+ List<OrderInfo> orderInfoList = orderInfoDao.selectList(orderInfo);
|
|
|
+
|
|
|
+ return orderInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<Long, BigDecimal> calculatePrice(long productId, List<ProductCoupon> productCouponList) {
|
|
|
+
|
|
|
+ Map<Long, BigDecimal> map = new HashMap<>();
|
|
|
+
|
|
|
+ //获取商品信息
|
|
|
+ Product product = productDao.get(productId);
|
|
|
+ BigDecimal price = product.getPrice();
|
|
|
+
|
|
|
+ // 优惠券
|
|
|
+ if (productCouponList.size() > 0) {
|
|
|
+ for (ProductCoupon productCoupon : productCouponList) {
|
|
|
+ if (productCoupon.getType() == 1) {
|
|
|
+ // 门槛
|
|
|
+ BigDecimal threshold = productCoupon.getThreshold();
|
|
|
+ if (price.compareTo(threshold) > 0) {
|
|
|
+ //优惠金额
|
|
|
+ BigDecimal couponAmount = productCoupon.getCouponAmount();
|
|
|
+ price = price.subtract(couponAmount);
|
|
|
+ map.put(productCoupon.getId(), couponAmount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (productCoupon.getType() == 2) {
|
|
|
+ // 打折
|
|
|
+ BigDecimal discount = productCoupon.getDiscount();
|
|
|
+ // 门槛
|
|
|
+ BigDecimal threshold = productCoupon.getThreshold();
|
|
|
+ // 最高优惠
|
|
|
+ BigDecimal mostConstraint = productCoupon.getMostConstraint();
|
|
|
+ if (price.compareTo(threshold) > 0) {
|
|
|
+ // 四舍五入 保留两位小数
|
|
|
+ BigDecimal multiply = price.multiply(discount).setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ // 大于1000,按1000算
|
|
|
+ if (multiply.compareTo(mostConstraint) > 0) {
|
|
|
+ price = price.subtract(mostConstraint);
|
|
|
+ map.put(productCoupon.getId(), mostConstraint);
|
|
|
+ } else {
|
|
|
+ price = price.subtract(multiply);
|
|
|
+ map.put(productCoupon.getId(), multiply);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ map.put(-1L, price);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|