Przeglądaj źródła

易乾不存在的订单5分钟关闭

周箭河 5 lat temu
rodzic
commit
377b6668cf

+ 24 - 11
src/main/java/com/ym/mec/collectfee/controller/YqPayController.java

@@ -17,6 +17,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
+import org.springframework.http.HttpStatus;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.transaction.annotation.Transactional;
@@ -27,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.io.InputStreamReader;
+import java.lang.reflect.Array;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
@@ -78,7 +80,8 @@ public class YqPayController extends BaseController {
         //1、判断已报名人数
         CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getCourseId());
         if (courseGroupInfo.getRegNum().compareTo(courseGroupInfo.getPlanNum()) >= 0) {
-            return failed("乐团人数暂时已满,请稍后再试");
+            Integer nums = orderService.getPayingOrderNums();
+            return failed(HttpStatus.FORBIDDEN, "当前排队人数" + nums + "人,请您耐心等待");
         }
 
         //课程组价格
@@ -383,9 +386,12 @@ public class YqPayController extends BaseController {
         List<Order> payingOrders = orderService.findPayingOrders();
 
         String merOrderNos = ""; //
+        ArrayList<String> orderNoList = new ArrayList<String>();
 
-        for (int i = 0; i < payingOrders.size(); i++) {
-            merOrderNos += payingOrders.get(i).getOrderNo() + ",";
+        for (Order payingOrder : payingOrders) {
+            String orderNo = payingOrder.getOrderNo();
+            orderNoList.add(orderNo);
+            merOrderNos += orderNo + ",";
         }
         if (merOrderNos.isEmpty()) {
             return;
@@ -402,14 +408,16 @@ public class YqPayController extends BaseController {
             String[] statusArr = {"0", "1", "7"};
             String responseParameters = queryRs.getResponseParameters();
             List<Map<String, String>> responseList = JSON.parseObject(responseParameters, List.class);
-            for (int i = 0; i < responseList.size(); i++) {
-                Map<String, String> rpMap = responseList.get(i);
+            for (Map<String, String> response : responseList) {
+                Map<String, String> rpMap = response;
                 if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
                     this.updateOrder(rpMap);
                 }
+                if (orderNoList.contains(rpMap.get("merOrderNo"))) {
+                    orderNoList.remove(rpMap.get("merOrderNo"));
+                }
             }
-
-
+            this.failOrders(orderNoList);
         }
     }
 
@@ -475,12 +483,17 @@ public class YqPayController extends BaseController {
     }
 
     @Transactional
-    @Scheduled(cron = "0/5 * * * * ?")
-    public void failOrders() throws Exception {
+    public void failOrders(ArrayList<String> orderNoList) throws Exception {
+        if (orderNoList.size() == 0) {
+            return;
+        }
         Calendar beforeTime = Calendar.getInstance();
-        beforeTime.add(Calendar.MINUTE, -30);// 5分钟之前的时间
+        beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
         Date beforeDate = beforeTime.getTime();
-        List<Order> payingOrders = orderService.findPayingOrdersOver30(beforeDate);
+        Map<String, Object> rqMap = new HashMap<>();
+        rqMap.put("orderNoList", orderNoList);
+        rqMap.put("beforeDate", beforeDate);
+        List<Order> payingOrders = orderService.findPayingOrdersOver(rqMap);
 
         for (Order order : payingOrders) {
             HashMap<String, Object> upMap = new HashMap<>();

+ 3 - 1
src/main/java/com/ym/mec/collectfee/dao/OrderDao.java

@@ -30,5 +30,7 @@ public interface OrderDao extends BaseDAO<Integer, Order> {
 
     int updateByIdAndStatus(Map upMap);
 
-    List<Order> findPayingOrdersOver30(Date beforeTime);
+    List<Order> findPayingOrdersOver(Map<String,Object> map);
+
+    Integer getPayingOrderNums();
 }

+ 8 - 3
src/main/java/com/ym/mec/collectfee/service/OrderService.java

@@ -20,13 +20,14 @@ public interface OrderService extends BaseService<Integer, Order> {
      * @return
      */
     List<Order> getOrderByUserId(Integer userId);
-    
+
     /**
      * 根据乐团编号,获取订单列表
+     *
      * @param userId
      * @return
      */
-    List<Order> getOrderByClassId(Integer classId,Integer type);
+    List<Order> getOrderByClassId(Integer classId, Integer type);
 
 
     /**
@@ -80,6 +81,7 @@ public interface OrderService extends BaseService<Integer, Order> {
 
     /**
      * 获取支付的参数
+     *
      * @param account
      * @param order
      * @param school
@@ -90,10 +92,13 @@ public interface OrderService extends BaseService<Integer, Order> {
 
     /**
      * 根据id和status状态更新订单
+     *
      * @param upMap
      * @return
      */
     int updateByIdAndStatus(Map upMap);
 
-    List<Order> findPayingOrdersOver30(Date beforeTime);
+    List<Order> findPayingOrdersOver(Map<String,Object> map);
+
+    Integer getPayingOrderNums();
 }

+ 7 - 2
src/main/java/com/ym/mec/collectfee/service/impl/OrderServiceImpl.java

@@ -198,8 +198,13 @@ public class OrderServiceImpl extends BaseServiceImpl<Integer, Order> implements
     }
 
     @Override
-    public List<Order> findPayingOrdersOver30(Date beforeTime) {
-        return orderDao.findPayingOrdersOver30(beforeTime);
+    public List<Order> findPayingOrdersOver(Map<String, Object> map) {
+        return orderDao.findPayingOrdersOver(map);
+    }
+
+    @Override
+    public Integer getPayingOrderNums() {
+        return orderDao.getPayingOrderNums();
     }
 
 

+ 174 - 162
src/main/resources/config/mybatis/OrderMapper.xml

@@ -5,201 +5,213 @@
 不要修改此文件。所有改动将在下次重新自动生成时丢失。
 -->
 <mapper namespace="com.ym.mec.collectfee.dao.OrderDao">
-	
-	<resultMap type="com.ym.mec.collectfee.entity.Order" id="Order">
-		<result column="id" property="id" />
-		<result column="oid" property="oid" />
-		<result column="branch_id" property="branchId" />
-		<result column="group_id" property="groupId" />
-		<result column="order_no" property="orderNo" />
-		<result column="user_id" property="userId" />
-		<result column="user_name" property="userName" />
-		<result column="amount" property="amount" />
-		<result column="type" property="type" />
-		<result column="pay" property="pay"/>
-		<result column="bank" property="bank" />
-		<result column="account" property="account" />
-		<result column="u_account" property="uAccount" />
-		<result column="pay_id" property="payId" />
-		<result column="pay_time" property="payTime" />
-		<result column="remark" property="remark" />
-		<result column="class_id" property="classId" />
-		<result column="status" property="status" />
-		<result column="create_time" property="createTime" />
-		<result column="po_name" property="poName" />
-		<result column="voicy_part" property="voicyPart" />
-		<result column="balance" property="balance" />
-		<result column="batch_num" property="batchNum" />
-		<result column="tui_fee" property="tuiFee" />
-		<result column="goods_fee" property="goodsFee" />
-		<result column="sd_name" property="sdName" />
-		<result column="sd_fee" property="sdFee" />
-	</resultMap>
 
-	<!-- 根据主键查询一条记录 -->
-	<select id="get" resultMap="Order" >
+    <resultMap type="com.ym.mec.collectfee.entity.Order" id="Order">
+        <result column="id" property="id"/>
+        <result column="oid" property="oid"/>
+        <result column="branch_id" property="branchId"/>
+        <result column="group_id" property="groupId"/>
+        <result column="order_no" property="orderNo"/>
+        <result column="user_id" property="userId"/>
+        <result column="user_name" property="userName"/>
+        <result column="amount" property="amount"/>
+        <result column="type" property="type"/>
+        <result column="pay" property="pay"/>
+        <result column="bank" property="bank"/>
+        <result column="account" property="account"/>
+        <result column="u_account" property="uAccount"/>
+        <result column="pay_id" property="payId"/>
+        <result column="pay_time" property="payTime"/>
+        <result column="remark" property="remark"/>
+        <result column="class_id" property="classId"/>
+        <result column="status" property="status"/>
+        <result column="create_time" property="createTime"/>
+        <result column="po_name" property="poName"/>
+        <result column="voicy_part" property="voicyPart"/>
+        <result column="balance" property="balance"/>
+        <result column="batch_num" property="batchNum"/>
+        <result column="tui_fee" property="tuiFee"/>
+        <result column="goods_fee" property="goodsFee"/>
+        <result column="sd_name" property="sdName"/>
+        <result column="sd_fee" property="sdFee"/>
+    </resultMap>
+
+    <!-- 根据主键查询一条记录 -->
+    <select id="get" resultMap="Order">
 		SELECT * FROM `order` WHERE id = #{id}
 	</select>
 
-	<!-- 全查询 -->
-	<select id="findAll" resultMap="Order">
+    <!-- 全查询 -->
+    <select id="findAll" resultMap="Order">
 		SELECT * FROM `order` ORDER BY id
 	</select>
 
-	<!-- 向数据库增加一条记录 -->
-	<insert id="insert" parameterType="com.ym.mec.collectfee.entity.Order" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
-		<!--
-		<selectKey resultClass="int" keyProperty="id" >
-		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
-		</selectKey>
-		-->
-		INSERT INTO `order` (id,oid,order_no,branch_id,group_id,user_id,user_name,amount,type,pay,bank,account,u_account,pay_id,pay_time,remark,class_id,status,create_time,po_name,voicy_part,balance,batch_num,tui_fee,goods_fee,sd_name,sd_fee) VALUES(#{id},#{oid},#{orderNo},#{branchId},#{groupId},#{userId},#{userName},#{amount},#{type},#{pay},#{bank},#{account},#{uAccount},#{payId},#{payTime},#{remark},#{classId},#{status},#{createTime},#{poName},#{voicyPart},#{balance},#{batchNum},#{tuiFee},#{goodsFee},#{sdName},#{sdFee})
-	</insert>
+    <!-- 向数据库增加一条记录 -->
+    <insert id="insert" parameterType="com.ym.mec.collectfee.entity.Order" useGeneratedKeys="true" keyColumn="id"
+            keyProperty="id">
+        <!--
+        <selectKey resultClass="int" keyProperty="id" >
+        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
+        </selectKey>
+        -->
+        INSERT INTO `order`
+        (id,oid,order_no,branch_id,group_id,user_id,user_name,amount,type,pay,bank,account,u_account,pay_id,pay_time,remark,class_id,status,create_time,po_name,voicy_part,balance,batch_num,tui_fee,goods_fee,sd_name,sd_fee)
+        VALUES(#{id},#{oid},#{orderNo},#{branchId},#{groupId},#{userId},#{userName},#{amount},#{type},#{pay},#{bank},#{account},#{uAccount},#{payId},#{payTime},#{remark},#{classId},#{status},#{createTime},#{poName},#{voicyPart},#{balance},#{batchNum},#{tuiFee},#{goodsFee},#{sdName},#{sdFee})
+    </insert>
 
-	<!-- 根据主键查询一条记录 -->
-	<update id="update" parameterType="com.ym.mec.collectfee.entity.Order">
-		UPDATE `order`
-		<set>
-			<if test="tuiFee != null">
-				tui_fee = #{tuiFee},
-			</if>
-			<if test="goodsFee != null">
-				goods_fee = #{goodsFee},
-			</if>
-			<if test="sdName != null">
-				sd_name = #{sdName},
-			</if>
-			<if test="sdFee != null">
-				sd_fee = #{sdFee},
-			</if>
-			<if test="amount != null">
-				amount = #{amount},
-			</if>
-			<if test="classId != null">
-				class_id = #{classId},
-			</if>
-			<if test="pay != null">
-				pay = #{pay},
-			</if>
-			<if test="uAccount != null">
-				u_account = #{uAccount},
-			</if>
-			<if test="remark != null">
-				remark = #{remark},
-			</if>
-			<if test="oid != null">
-				oid = #{oid},
-			</if>
-			<if test="batchNum != null">
-				batch_num = #{batchNum},
-			</if>
-			<if test="payTime != null">
-				pay_time = #{payTime},
-			</if>
-			<if test="bank != null">
-				bank = #{bank},
-			</if>
-			<if test="balance != null">
-				balance = #{balance},
-			</if>
-			<if test="userId != null">
-				user_id = #{userId},
-			</if>
-			<if test="voicyPart != null">
-				voicy_part = #{voicyPart},
-			</if>
-			<if test="poName != null">
-				po_name = #{poName},
-			</if>
-			<if test="account != null">
-				account = #{account},
-			</if>
-			<if test="payId != null">
-				pay_id = #{payId},
-			</if>
-			<if test="status != null">
-				status = #{status},
-			</if>
-		</set>
-		WHERE id = #{id}
-	</update>
+    <!-- 根据主键查询一条记录 -->
+    <update id="update" parameterType="com.ym.mec.collectfee.entity.Order">
+        UPDATE `order`
+        <set>
+            <if test="tuiFee != null">
+                tui_fee = #{tuiFee},
+            </if>
+            <if test="goodsFee != null">
+                goods_fee = #{goodsFee},
+            </if>
+            <if test="sdName != null">
+                sd_name = #{sdName},
+            </if>
+            <if test="sdFee != null">
+                sd_fee = #{sdFee},
+            </if>
+            <if test="amount != null">
+                amount = #{amount},
+            </if>
+            <if test="classId != null">
+                class_id = #{classId},
+            </if>
+            <if test="pay != null">
+                pay = #{pay},
+            </if>
+            <if test="uAccount != null">
+                u_account = #{uAccount},
+            </if>
+            <if test="remark != null">
+                remark = #{remark},
+            </if>
+            <if test="oid != null">
+                oid = #{oid},
+            </if>
+            <if test="batchNum != null">
+                batch_num = #{batchNum},
+            </if>
+            <if test="payTime != null">
+                pay_time = #{payTime},
+            </if>
+            <if test="bank != null">
+                bank = #{bank},
+            </if>
+            <if test="balance != null">
+                balance = #{balance},
+            </if>
+            <if test="userId != null">
+                user_id = #{userId},
+            </if>
+            <if test="voicyPart != null">
+                voicy_part = #{voicyPart},
+            </if>
+            <if test="poName != null">
+                po_name = #{poName},
+            </if>
+            <if test="account != null">
+                account = #{account},
+            </if>
+            <if test="payId != null">
+                pay_id = #{payId},
+            </if>
+            <if test="status != null">
+                status = #{status},
+            </if>
+        </set>
+        WHERE id = #{id}
+    </update>
 
-	<!-- 根据主键删除一条记录 -->
-	<delete id="delete" >
+    <!-- 根据主键删除一条记录 -->
+    <delete id="delete">
 		DELETE FROM `order` WHERE id = #{id}
 	</delete>
 
-	<!-- 分页查询 -->
-	<select id="queryPage" resultMap="Order" parameterType="map">
-		SELECT * FROM `order` ORDER BY id <include refid="global.limit"/>
-	</select>
+    <!-- 分页查询 -->
+    <select id="queryPage" resultMap="Order" parameterType="map">
+        SELECT * FROM `order` ORDER BY id
+        <include refid="global.limit"/>
+    </select>
 
-	<!-- 查询当前表的总记录数 -->
-	<select id="queryCount" resultType="int">
+    <!-- 查询当前表的总记录数 -->
+    <select id="queryCount" resultType="int">
 		SELECT COUNT(*) FROM `order`
 	</select>
 
-	<select id="getOrderByUserId" resultMap="Order">
+    <select id="getOrderByUserId" resultMap="Order">
 		SELECT * FROM `order` WHERE user_id = #{userId}
 	</select>
-	
-	<select id="getOrderByClassId" resultMap="Order">
-		SELECT * FROM `order` WHERE status=2
-		<if test="classId != null">
-				and class_id = #{classId}
-		</if>
-		<if test="type != null">
-				and type = #{type}
-		</if>
-	</select>
 
-	<select id="getOrderByBatch" resultMap="Order">
+    <select id="getOrderByClassId" resultMap="Order">
+        SELECT * FROM `order` WHERE status=2
+        <if test="classId != null">
+            and class_id = #{classId}
+        </if>
+        <if test="type != null">
+            and type = #{type}
+        </if>
+    </select>
+
+    <select id="getOrderByBatch" resultMap="Order">
 		SELECT * FROM `order` WHERE batch_num = #{batchNum}
 	</select>
-	<select id="countOrder" resultType="java.lang.Integer">
+    <select id="countOrder" resultType="java.lang.Integer">
 		SELECT COUNT(DISTINCT user_id) num FROM `order` WHERE po_name = #{poName} AND voicy_part = #{voicePort} AND status >= 1
 	</select>
 
-	<select id="getOrderByOrderNo" resultMap="Order">
+    <select id="getOrderByOrderNo" resultMap="Order">
 		SELECT * FROM `order` WHERE order_no = #{orderNo} AND status = 1
 	</select>
     <select id="findOrderByStatus" resultType="com.ym.mec.collectfee.entity.Order">
 		SELECT * FROM `order` WHERE user_id = #{userId} AND status = #{status}
 	</select>
-	<!-- 获取支付中和成功的订单数-->
-	<select id="getPayOrderNums" resultType="java.lang.Integer">
+    <!-- 获取支付中和成功的订单数-->
+    <select id="getPayOrderNums" resultType="java.lang.Integer">
 		SELECT COUNT(*) FROM `order` WHERE status >= 1
 	</select>
-	<!-- 获取支付中的订单-->
-	<select id="findPayingOrders" resultMap="Order">
+    <!-- 获取支付中的订单-->
+    <select id="findPayingOrders" resultMap="Order">
 		SELECT * FROM `order` WHERE status = 1
 	</select>
+    <!-- 获取支付中和成功的订单数-->
+    <select id="getPayingOrderNums" resultType="java.lang.Integer">
+		SELECT COUNT(*) FROM `order` WHERE status = 1
+	</select>
 
-	<!-- 根据id和status更新订单 -->
-	<update id="updateByIdAndStatus" parameterType="map">
-		UPDATE `order`
-		<set>
-			<if test="pay != null">
-				pay = #{pay},
-			</if>
-			<if test="payTime != null">
-				pay_time = #{payTime},
-			</if>
-			<if test="bank != null">
-				bank = #{bank},
-			</if>
-			<if test="payId != null">
-				pay_id = #{payId},
-			</if>
-			<if test="status != null">
-				status = #{status}
-			</if>
-		</set>
-		WHERE id = #{id} AND `status` =  #{oldStatus}
-	</update>
+    <!-- 根据id和status更新订单 -->
+    <update id="updateByIdAndStatus" parameterType="map">
+        UPDATE `order`
+        <set>
+            <if test="pay != null">
+                pay = #{pay},
+            </if>
+            <if test="payTime != null">
+                pay_time = #{payTime},
+            </if>
+            <if test="bank != null">
+                bank = #{bank},
+            </if>
+            <if test="payId != null">
+                pay_id = #{payId},
+            </if>
+            <if test="status != null">
+                status = #{status}
+            </if>
+        </set>
+        WHERE id = #{id} AND `status` = #{oldStatus}
+    </update>
 
-	<!-- 查询支付中超过30分钟的订单 -->
-	<select id="findPayingOrdersOver30" resultMap="Order" parameterType="date">
-		<![CDATA[ SELECT * FROM `order` WHERE `status`=1 AND `create_time` <= ]]> #{berferTime}
-	</select>
+    <!-- 查询支付中超过30分钟的订单 -->
+    <select id="findPayingOrdersOver" resultMap="Order" parameterType="map">
+        SELECT * FROM `order` WHERE `order_no` IN
+        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
+            #{orderNo}
+        </foreach>
+        <![CDATA[ AND  `status`=1 AND `create_time` <= ]]> #{berferTime}
+    </select>
 </mapper>