dcs 3 ماه پیش
والد
کامیت
5b8ced3910

+ 80 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/controller/OrderInfoController.java

@@ -0,0 +1,80 @@
+package com.bosshand.virgo.api.workark.controller;
+
+
+import com.bosshand.virgo.api.workark.model.OrderInfo;
+import com.bosshand.virgo.api.workark.model.ProductCoupon;
+import com.bosshand.virgo.api.workark.service.OrderInfoService;
+import com.bosshand.virgo.api.workark.service.ProductCouponService;
+import com.bosshand.virgo.core.response.Response;
+import com.bosshand.virgo.core.utils.ContextUtils;
+import com.bosshand.virgo.core.utils.StringUtil;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RestController
+@RequestMapping({"workarkOrderInfo"})
+@Api(tags = {"订单管理"})
+public class OrderInfoController {
+
+    @Autowired
+    OrderInfoService orderInfoService;
+
+    @Autowired
+    ProductCouponService productCouponService;
+
+    @ApiOperation("创建订单")
+    @RequestMapping(value = "/create/{productId}/{productCouponIds}", method = RequestMethod.GET)
+    public Response createOrderByProductId(@PathVariable long productId, @PathVariable String productCouponIds) {
+        long userId = ContextUtils.getUserContext().getUserId();
+        List<ProductCoupon> productCouponList = new ArrayList<>();
+        if (!StringUtil.equals("-1", productCouponIds)) {
+            String[] ids = productCouponIds.split(",");
+            for (int i = 0; i < ids.length; i++) {
+                ProductCoupon productCoupon = productCouponService.getUserId(Long.parseLong(ids[i]), userId);
+                if (productCoupon == null) {
+                    return Response.fail(20000, "当前用户下没有这个优惠券!");
+                } else {
+                    productCouponList.add(productCoupon);
+                }
+            }
+        }
+        return Response.ok(orderInfoService.createOrderByProductId(productId, productCouponList));
+    }
+
+    @ApiOperation("计算价格")
+    @RequestMapping(value = "/calculatePrice/{productId}/{productCouponIds}", method = RequestMethod.GET)
+    public Response calculatePrice(@PathVariable long productId, @PathVariable String productCouponIds) {
+        long userId = ContextUtils.getUserContext().getUserId();
+        List<ProductCoupon> productCouponList = new ArrayList<>();
+        if (!StringUtil.equals("-1", productCouponIds)) {
+            String[] ids = productCouponIds.split(",");
+            for (int i = 0; i < ids.length; i++) {
+                ProductCoupon productCoupon = productCouponService.getUserId(Long.parseLong(ids[i]), userId);
+                if (productCoupon == null) {
+                    return Response.fail(20000, "当前用户下没有这个优惠券!");
+                } else {
+                    productCouponList.add(productCoupon);
+                }
+            }
+        }
+        return Response.ok(orderInfoService.calculatePrice(productId, productCouponList));
+    }
+
+    @ApiOperation("查询")
+    @RequestMapping(value = "", method = RequestMethod.POST)
+    public Response getList(@RequestBody OrderInfo orderInfo) {
+        return Response.ok(orderInfoService.getList(orderInfo));
+    }
+
+    @ApiOperation("详情")
+    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
+    public Response get(@PathVariable long id) {
+        return Response.ok(orderInfoService.get(id));
+    }
+
+}

+ 26 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/dao/OrderInfoDao.java

@@ -0,0 +1,26 @@
+package com.bosshand.virgo.api.workark.dao;
+
+import com.bosshand.virgo.api.workark.model.OrderInfo;
+import org.apache.ibatis.annotations.Mapper;
+
+import java.util.List;
+
+@Mapper
+public interface OrderInfoDao {
+
+    void delete(long id);
+
+    void save(OrderInfo orderInfo);
+
+    void update(OrderInfo orderInfo);
+
+    List<OrderInfo> getList(OrderInfo orderInfo);
+
+    OrderInfo get(long id);
+
+    OrderInfo getOrderNo(String orderNo);
+
+    OrderInfo getNoPayOrderByProductId(Long productId, Long userId, String orderStatus);
+
+    List<OrderInfo> selectList(OrderInfo orderInfo);
+}

+ 53 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/enums/OrderStatus.java

@@ -0,0 +1,53 @@
+package com.bosshand.virgo.api.workark.enums;
+
+public enum OrderStatus {
+    /**
+     * 未支付
+     */
+    NOTPAY("未支付"),
+
+
+    /**
+     * 支付成功
+     */
+    SUCCESS("支付成功"),
+
+    /**
+     * 已关闭
+     */
+    CLOSED("超时已关闭"),
+
+    /**
+     * 已取消
+     */
+    CANCEL("用户已取消"),
+
+    /**
+     * 退款中
+     */
+    REFUND_PROCESSING("退款中"),
+
+    /**
+     * 已退款
+     */
+    REFUND_SUCCESS("已退款"),
+
+    /**
+     * 退款异常
+     */
+    REFUND_ABNORMAL("退款异常");
+
+    /**
+     * 类型
+     */
+    private final String type;
+
+    OrderStatus(String type) {
+        this.type = type;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+}

+ 175 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/model/OrderInfo.java

@@ -0,0 +1,175 @@
+package com.bosshand.virgo.api.workark.model;
+
+import java.util.Date;
+
+/**
+ * 订单信息
+ */
+public class OrderInfo {
+
+    private Long id;
+
+    /**
+     * 付款方式(微信、支付宝)
+     */
+    private String paymentType;
+
+    /**
+     * 创建时间
+     */
+    private Date createTime;
+
+    /**
+     * 更新时间
+     */
+    private Date updateTime;
+
+    /**
+     * 订单标题
+     */
+    private String title;
+
+    /**
+     * 商户订单编号
+     */
+    private String orderNo;
+
+    /**
+     * 用户id
+     */
+    private Long userId;
+
+    /**
+     * 支付产品id
+     */
+    private Long productId;
+
+    /**
+     * 订单金额(分)
+     */
+    private Integer totalFee;
+
+    /**
+     * 订单二维码连接
+     */
+    private String codeUrl;
+
+    /**
+     * 订单状态
+     */
+    private String orderStatus;
+
+    /**
+     * 优惠券ids
+     */
+    private String productCouponIds;
+
+    /**
+     * 合同
+     */
+    private String contract;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getPaymentType() {
+        return paymentType;
+    }
+
+    public void setPaymentType(String paymentType) {
+        this.paymentType = paymentType;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public Date getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Date updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getOrderNo() {
+        return orderNo;
+    }
+
+    public void setOrderNo(String orderNo) {
+        this.orderNo = orderNo;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public Long getProductId() {
+        return productId;
+    }
+
+    public void setProductId(Long productId) {
+        this.productId = productId;
+    }
+
+    public Integer getTotalFee() {
+        return totalFee;
+    }
+
+    public void setTotalFee(Integer totalFee) {
+        this.totalFee = totalFee;
+    }
+
+    public String getCodeUrl() {
+        return codeUrl;
+    }
+
+    public void setCodeUrl(String codeUrl) {
+        this.codeUrl = codeUrl;
+    }
+
+    public String getOrderStatus() {
+        return orderStatus;
+    }
+
+    public void setOrderStatus(String orderStatus) {
+        this.orderStatus = orderStatus;
+    }
+
+    public String getProductCouponIds() {
+        return productCouponIds;
+    }
+
+    public void setProductCouponIds(String productCouponIds) {
+        this.productCouponIds = productCouponIds;
+    }
+
+    public String getContract() {
+        return contract;
+    }
+
+    public void setContract(String contract) {
+        this.contract = contract;
+    }
+}

+ 212 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/service/OrderInfoService.java

@@ -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;
+    }
+
+
+
+}

+ 46 - 0
virgo.api/src/main/java/com/bosshand/virgo/api/workark/util/OrderNoUtils.java

@@ -0,0 +1,46 @@
+package com.bosshand.virgo.api.workark.util;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Random;
+
+/**
+ * 订单号工具类
+ *
+ * @author qy
+ * @since 1.0
+ */
+public class OrderNoUtils {
+
+    /**
+     * 获取订单编号
+     * @return
+     */
+    public static String getOrderNo() {
+        return "ORDER_" + getNo();
+    }
+
+    /**
+     * 获取退款单编号
+     * @return
+     */
+    public static String getRefundNo() {
+        return "REFUND_" + getNo();
+    }
+
+    /**
+     * 获取编号
+     * @return
+     */
+    public static String getNo() {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
+        String newDate = sdf.format(new Date());
+        String result = "";
+        Random random = new Random();
+        for (int i = 0; i < 3; i++) {
+            result += random.nextInt(10);
+        }
+        return newDate + result;
+    }
+
+}

+ 69 - 0
virgo.api/src/main/resources/mapper/OrderInfoMapper.xml

@@ -0,0 +1,69 @@
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.bosshand.virgo.api.workark.dao.OrderInfoDao">
+
+    <resultMap type="com.bosshand.virgo.api.workark.model.OrderInfo" id="result">
+        <id column="id" property="id"/>
+        <result column="createTime" property="createTime"/>
+        <result column="updateTime" property="updateTime"/>
+        <result column="title" property="title"/>
+        <result column="orderNo" property="orderNo"/>
+        <result column="userId" property="userId"/>
+        <result column="productId" property="productId"/>
+        <result column="totalFee" property="totalFee"/>
+        <result column="codeUrl" property="codeUrl"/>
+        <result column="orderStatus" property="orderStatus"/>
+        <result column="paymentType" property="paymentType"/>
+        <result column="productCouponIds" property="productCouponIds"/>
+        <result column="contract" property="contract"/>
+    </resultMap>
+
+    <select id="get" resultMap="result">
+        select * from workark_orderInfo where id = #{id}
+    </select>
+
+    <select id="getOrderNo" resultMap="result">
+        select * from workark_orderInfo where orderNo = #{orderNo}
+    </select>
+
+    <select id="getNoPayOrderByProductId" resultMap="result">
+        select * from workark_orderInfo where productId = #{productId} AND userId =#{userId} AND orderStatus=#{orderStatus}
+    </select>
+
+    <select id="selectList" resultMap="result">
+        select * from workark_orderInfo where where orderStatus = #{orderStatus} and createTime &lt; #{createTime}
+    </select>
+
+    <select id="getList" resultMap="result">
+        select * from workark_orderInfo
+        <where>
+            <if test="userId != 0">
+                and userId = #{userId}
+            </if>
+        </where>
+    </select>
+
+    <insert id="save" useGeneratedKeys="true" keyProperty="id">
+        INSERT INTO workark_orderInfo (createTime, title, orderNo, userId, productId, totalFee, codeUrl, orderStatus, paymentType, productCouponIds) VALUES
+                            (now(), #{paymentType}, #{orderNo}, #{userId}, #{productId}, #{totalFee}, #{codeUrl}, #{orderStatus}, #{paymentType}, #{productCouponIds})
+    </insert>
+
+    <update id="update" parameterType="com.bosshand.virgo.api.workark.model.OrderInfo">
+        UPDATE workark_orderInfo
+        <trim prefix="set" suffixOverrides=",">
+            <if test="title!=null">name=#{name},</if>
+            <if test="codeUrl!=null">codeUrl=#{codeUrl},</if>
+            <if test="orderStatus!=null">orderStatus=#{orderStatus},</if>
+            <if test="paymentType!=null">paymentType=#{paymentType},</if>
+            <if test="updateTime==null">updateTime=now(),</if>
+        </trim>
+        WHERE id=#{id}
+    </update>
+
+    <delete id="delete">
+        DELETE FROM workark_orderInfo WHERE id = #{id}
+    </delete>
+
+</mapper>