From b6fdf185c7e8fb1f06da0e609e39aecaef6b66f5 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期日, 30 六月 2024 01:42:31 +0800
Subject: [PATCH] 微信强授权/后台管理

---
 src/main/java/com/taoke/autopay/service/impl/KeyOrderServiceImpl.java |  108 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 92 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/taoke/autopay/service/impl/KeyOrderServiceImpl.java b/src/main/java/com/taoke/autopay/service/impl/KeyOrderServiceImpl.java
index 2ccc418..182e135 100644
--- a/src/main/java/com/taoke/autopay/service/impl/KeyOrderServiceImpl.java
+++ b/src/main/java/com/taoke/autopay/service/impl/KeyOrderServiceImpl.java
@@ -1,18 +1,23 @@
 package com.taoke.autopay.service.impl;
 
 import com.taoke.autopay.dao.KeyOrderMapper;
+import com.taoke.autopay.dao.WxUserSettingsMapper;
 import com.taoke.autopay.entity.KeyOrder;
+import com.taoke.autopay.entity.OrderCountTypeEnum;
 import com.taoke.autopay.entity.OrderDistributeCountInfo;
+import com.taoke.autopay.entity.WxUserSettings;
 import com.taoke.autopay.exception.KeyOrderException;
+import com.taoke.autopay.exception.WxOrderCountException;
 import com.taoke.autopay.factory.OrderFactory;
 import com.taoke.autopay.service.KeyOrderService;
+import com.taoke.autopay.service.WxUserOrderCountService;
+import com.taoke.autopay.service.WxUserSettingService;
+import com.taoke.autopay.utils.TimeUtil;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * @author hxh
@@ -26,26 +31,41 @@
     @Resource
     private KeyOrderMapper keyOrderMapper;
 
+    @Resource
+    private WxUserSettingService wxUserSettingService;
+
+    @Resource
+    private WxUserOrderCountService wxUserOrderCountService;
+
 
     @Override
     public KeyOrder selectById(String id) {
         return keyOrderMapper.selectById(id);
     }
 
+    @Transactional(rollbackFor = Exception.class)
     @Override
-    public KeyOrder addKeyOrder(String key) throws KeyOrderException {
-        String id =   OrderFactory.createId(key);
-        KeyOrder order =   keyOrderMapper.selectById(id);
-        if(order!=null){
-           throw  new KeyOrderException("璇峰嬁閲嶅鎻愪氦鍙d护");
+    public KeyOrder addKeyOrder(String key, Long uid, String day) throws KeyOrderException, WxOrderCountException {
+        // 鍒ゆ柇鎻愪氦娆℃暟鏄惁杩囬噺
+        if (uid != null) {
+            WxUserSettings settings = wxUserSettingService.selectByUid(uid);
+            wxUserOrderCountService.addOrderCount(uid, OrderCountTypeEnum.SUBMIT_TOKEN_COUNT, day, 1, settings.getTotalOrderCountPerDay());
         }
-        order =new KeyOrder();
+        String id = OrderFactory.createId(key);
+        KeyOrder order = keyOrderMapper.selectById(id);
+        if (order != null) {
+            throw new KeyOrderException("璇峰嬁閲嶅鎻愪氦鍙d护");
+        }
+        order = new KeyOrder();
         order.setId(id);
         order.setKey(key);
+        order.setUid(uid);
         order.setState(KeyOrder.STATE_NOT_PROCESS);
         order.setStateDesc("灏氭湭澶勭悊");
         order.setCreateTime(new Date());
         keyOrderMapper.insertSelective(order);
+
+
         return order;
     }
 
@@ -67,12 +87,32 @@
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void setOrderInfo(String id,String orderNo, int orderState)  throws  KeyOrderException{
+    public void paySuccess(String id, String stateDesc,String day) throws WxOrderCountException {
+        KeyOrder old = keyOrderMapper.selectByPrimaryKeyForUpdate(id);
+        if(old==null){
+           return;
+        }
+        if(old.getState() == KeyOrder.STATE_PAY){
+            return;
+        }
+        if(old.getUid()!=null) {
+            wxUserOrderCountService.addOrderCount(old.getUid(),OrderCountTypeEnum.DY_ORDER_PAY,day,1,null);
+        }
+        KeyOrder orderUpdate = new KeyOrder();
+        orderUpdate.setId(id);
+        orderUpdate.setState(KeyOrder.STATE_PAY);
+        orderUpdate.setStateDesc(stateDesc);
+        update(orderUpdate);
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void setOrderInfo(String id, String orderNo, int orderState) throws KeyOrderException {
         KeyOrder old = keyOrderMapper.selectByPrimaryKeyForUpdate(id);
         if (old == null) {
-           throw new  KeyOrderException("鍙d护涓嶅瓨鍦�");
+            throw new KeyOrderException("鍙d护涓嶅瓨鍦�");
         }
-        KeyOrder keyOrder=new KeyOrder();
+        KeyOrder keyOrder = new KeyOrder();
         keyOrder.setId(id);
         // 榛樿1鎶栭煶
         keyOrder.setOrderType(1);
@@ -96,17 +136,53 @@
 
     @Override
     public Long getCanDistributeUid() {
-        List<OrderDistributeCountInfo>  list =   keyOrderMapper.listDistributeUids();
-        if(list==null||list.size()==0){
+        // 鏈�杩�1灏忔椂鏈夋椿璺�
+        List<OrderDistributeCountInfo> list = keyOrderMapper.listDistributeUids(new Date(System.currentTimeMillis() - 1000 * 60 * 60L));
+        if (list == null || list.size() == 0) {
             return null;
         }
-        Comparator<OrderDistributeCountInfo> cm = (OrderDistributeCountInfo o1, OrderDistributeCountInfo o2)-> o1.getCount()-o2.getCount();
+        // count灏忎簬2鐩存帴瑙嗕负0
+        for (OrderDistributeCountInfo info : list) {
+            if (info.getCount() < 2) {
+                info.setCount(0);
+            }
+        }
+
+        Comparator<OrderDistributeCountInfo> cm = new Comparator<OrderDistributeCountInfo>() {
+            @Override
+            public int compare(OrderDistributeCountInfo o1, OrderDistributeCountInfo o2) {
+                return o1.getCount() - o2.getCount();
+            }
+        };
         list.sort(cm);
+        if (list.get(0).getCount() == 0) {
+            // 澶勭悊澶у鏁拌澶囬兘娌℃湁鍒嗛厤鐨勬儏鍐�
+            // 灏嗕负0鐨勬暟鎹殢鏈哄垎閰�
+            List<OrderDistributeCountInfo> tempList = new ArrayList<>();
+            for (OrderDistributeCountInfo info : list) {
+                if (info.getCount() == 0) {
+                    tempList.add(info);
+                }
+            }
+            int index = new Random().nextInt(tempList.size());
+            if (index < 0) {
+                index = 0;
+            }
+            if (index >= tempList.size()) {
+                index = tempList.size() - 1;
+            }
+            return tempList.get(index).getUid();
+        }
         return list.get(0).getUid();
     }
 
     @Override
     public List<KeyOrder> listNotDistributed(int page, int pageSize) {
-        return keyOrderMapper.listNotDistributed((page-1)*pageSize,pageSize);
+        return keyOrderMapper.listNotDistributed((page - 1) * pageSize, pageSize);
+    }
+
+    @Override
+    public void deleteAll(Date maxCreateTime) {
+        keyOrderMapper.deleteAll(maxCreateTime);
     }
 }

--
Gitblit v1.8.0