傅行帆 пре 5 година
родитељ
комит
62dbcecdbd

+ 30
- 0
src/main/java/com/huiju/estateagents/common/CommConstant.java Прегледај датотеку

@@ -641,4 +641,34 @@ public class CommConstant {
641 641
      * 未锁定
642 642
      */
643 643
     public static final String HOUSE_LOCKING_STATUS_UNLOCKED = "unlocked";
644
+
645
+    /**
646
+     * 房源订单
647
+     */
648
+    public static final String ORDER_TARGET_TYPE_HOUSE = "house";
649
+
650
+    /**
651
+     * 自动锁房
652
+     */
653
+    public static final String HOUSE_LOCKING_TYPE_AUTO = "auto";
654
+
655
+    /**
656
+     * 正在缴费
657
+     */
658
+    public static final String PAY_STATUS_PAYING = "paying";
659
+
660
+    /**
661
+     * 未缴费
662
+     */
663
+    public static final String PAY_STATUS_UNPAID = "unpaid";
664
+
665
+    /**
666
+     * 已缴费
667
+     */
668
+    public static final String PAY_STATUS_PAID = "paid";
669
+
670
+    /**
671
+     * 已退费
672
+     */
673
+    public static final String PAY_STATUS_REFUNDED = "refunded";
644 674
 }

+ 7
- 5
src/main/java/com/huiju/estateagents/controller/WxPayController.java Прегледај датотеку

@@ -1,6 +1,5 @@
1 1
 package com.huiju.estateagents.controller;
2 2
 
3
-import com.google.common.collect.Maps;
4 3
 import com.huiju.estateagents.base.BaseController;
5 4
 import com.huiju.estateagents.base.ResponseBean;
6 5
 import com.huiju.estateagents.common.wxpay.WXPayUtil;
@@ -11,10 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
11 10
 import org.springframework.web.bind.annotation.*;
12 11
 
13 12
 import javax.servlet.http.HttpServletRequest;
14
-import javax.servlet.http.HttpServletResponse;
15
-import javax.servlet.http.HttpSession;
16 13
 import java.io.ByteArrayOutputStream;
17
-import java.io.IOException;
18 14
 import java.io.InputStream;
19 15
 import java.util.HashMap;
20 16
 import java.util.Map;
@@ -27,6 +23,12 @@ public class WxPayController extends BaseController {
27 23
     @Autowired
28 24
     private IWxPayService wxPayService;
29 25
 
26
+    /**
27
+     * 微信统一下单接口
28
+     * @param taOrder
29
+     * @param request
30
+     * @return
31
+     */
30 32
     @RequestMapping(value = "/pay/unifiedOrder",method = RequestMethod.POST)
31 33
     public ResponseBean wxUnifiedOrder(@RequestBody TaOrder taOrder,
32 34
                                        HttpServletRequest request){
@@ -36,7 +38,7 @@ public class WxPayController extends BaseController {
36 38
             taOrder.setPersonId(getPersonId(request));
37 39
             responseBean = wxPayService.payUnifiedOrder(taOrder,request.getRemoteAddr());
38 40
         } catch (Exception e) {
39
-            e.printStackTrace();
41
+            log.error("微信统一下单失败: {}" , e.getMessage());
40 42
             responseBean.addError(e.getMessage());
41 43
             return responseBean;
42 44
         }

+ 102
- 17
src/main/java/com/huiju/estateagents/service/impl/WxPayServiceImpl.java Прегледај датотеку

@@ -8,21 +8,24 @@ import com.huiju.estateagents.common.wxpay.WXPayConstants;
8 8
 import com.huiju.estateagents.common.wxpay.WXPayUtil;
9 9
 import com.huiju.estateagents.common.wxpay.WxConfig;
10 10
 import com.huiju.estateagents.entity.*;
11
-import com.huiju.estateagents.mapper.TaRaiseMapper;
12
-import com.huiju.estateagents.mapper.TaRaiseRecordMapper;
13
-import com.huiju.estateagents.mapper.TaWxPayConfigMapper;
11
+import com.huiju.estateagents.mapper.*;
14 12
 import com.huiju.estateagents.service.IWxPayService;
15 13
 import lombok.extern.slf4j.Slf4j;
16 14
 import org.springframework.beans.factory.annotation.Autowired;
17 15
 import org.springframework.beans.factory.annotation.Value;
18 16
 import org.springframework.stereotype.Service;
17
+import org.springframework.transaction.annotation.Transactional;
19 18
 
19
+import javax.xml.ws.soap.Addressing;
20 20
 import java.time.Instant;
21
+import java.time.LocalDateTime;
21 22
 import java.util.HashMap;
23
+import java.util.List;
22 24
 import java.util.Map;
23 25
 
24 26
 @Slf4j
25 27
 @Service
28
+@Transactional(rollbackFor = Exception.class)
26 29
 public class WxPayServiceImpl implements IWxPayService {
27 30
 
28 31
     @Value("${pay-notify}")
@@ -37,6 +40,15 @@ public class WxPayServiceImpl implements IWxPayService {
37 40
     @Autowired
38 41
     private TaRaiseMapper taRaiseMapper;
39 42
 
43
+    @Autowired
44
+    private TaRaiseHouseMapper taRaiseHouseMapper;
45
+    
46
+    @Autowired
47
+    private TaHousingResourcesMapper taHousingResourcesMapper;
48
+
49
+    @Autowired
50
+    private TaOrderMapper taOrderMapper;
51
+
40 52
     /**
41 53
      * 微信支付-统一下单
42 54
      *
@@ -50,18 +62,20 @@ public class WxPayServiceImpl implements IWxPayService {
50 62
         WxConfig config = getWxConfig(taOrder.getOrgId());
51 63
         WXPay wxpay = new WXPay(config);
52 64
 
65
+        //获取支付的钱数并且判断是自动锁房还是手动锁房,自动锁房需要锁定房源,手动锁房跳过
53 66
         String payPrice = getPayPrice(taOrder);
54 67
         if (null == payPrice){
55
-            throw new Exception("此认筹单异常请联系置业顾问!");
68
+            throw new Exception("此认筹单异常请联系置业顾问!");
56 69
         }
57 70
 
58 71
         //生成商户订单
59
-        String tradeNo = "这边的规则要重新定义";
72
+        String prefix = taOrder.getTargetType().equals(CommConstant.ORDER_TARGET_TYPE_HOUSE) ? "H" : "D";
73
+        String tradeNo = prefix + System.currentTimeMillis() + taOrder.getTargetId();
60 74
         taOrder.setTradeNo(tradeNo);
61 75
 
62 76
         //下单
63 77
         Map<String, String> data = new HashMap<String, String>();
64
-        data.put("body", taOrder.getTargetType().equals("house") ? "房源认筹" : "微信支付");
78
+        data.put("body", taOrder.getTargetType().equals(CommConstant.ORDER_TARGET_TYPE_HOUSE) ? "房源认筹" : "微信支付");
65 79
         //商品号唯一
66 80
         data.put("out_trade_no", tradeNo);
67 81
         data.put("device_info", "");
@@ -106,7 +120,7 @@ public class WxPayServiceImpl implements IWxPayService {
106 120
             //处理业务逻辑 修改订单状态等
107 121
             resultMap.put("pay_type","0");
108 122
             String state = "";
109
-            if (type.equals("house")) {
123
+            if (type.equals(CommConstant.ORDER_TARGET_TYPE_HOUSE)) {
110 124
                 //state = wxPayService.wxCarNotify(resultMap);
111 125
             }
112 126
 
@@ -121,29 +135,88 @@ public class WxPayServiceImpl implements IWxPayService {
121 135
     }
122 136
 
123 137
     /**
124
-     * 获取支付的钱数
138
+     * 获取支付的钱数并且判断是自动锁房还是手动锁房,自动锁房需要锁定房源,手动锁房跳过
125 139
      * @param taOrder
126 140
      * @return
127 141
      */
128
-    private String getPayPrice(TaOrder taOrder) {
142
+    private String getPayPrice(TaOrder taOrder) throws Exception {
143
+        log.info("正在获取认类型为{}认筹筹单为{},的认筹金额",taOrder.getTargetType(),taOrder.getTargetId());
129 144
         //根据targetType取不同的表的数据
130
-        if (taOrder.getTargetType().equals("house")){
145
+        if (taOrder.getTargetType().equals(CommConstant.ORDER_TARGET_TYPE_HOUSE)){
131 146
             TaRaiseRecord taRaiseRecord = taRaiseRecordMapper.selectById(taOrder.getTargetId());
132 147
             if (null == taRaiseRecord){
133
-                return null;
134
-            }
135
-            if (taRaiseRecord.getHouseLockingStatus().equals(CommConstant.HOUSE_LOCKING_STATUS_UNLOCKED)){
136
-                return null;
148
+                throw new Exception("此认筹单不存在!请联系置业顾问!");
137 149
             }
150
+
151
+            //获取认筹信息
138 152
             TaRaise taRaise = taRaiseMapper.selectById(taRaiseRecord.getRaiseId());
139 153
             if (null == taRaise){
140
-                return null;
154
+                throw new Exception("此认筹不存在!请联系置业顾问!");
155
+            }
156
+            if (null == taRaise.getRaisePrice()){
157
+                throw new Exception("此认筹金额存在异常!请联系置业顾问!");
158
+            }
159
+            //判断是自动锁房状态还是手动锁房状态,手动锁房跳过,自动锁房需要锁房
160
+            if (taRaise.getHouseLockingType().equals(CommConstant.HOUSE_LOCKING_TYPE_AUTO)){
161
+                //校验并自动锁房
162
+                autoLockingHouse(taOrder,taRaiseRecord,taRaise);
141 163
             }
164
+
165
+            //插入实付金额
166
+            taOrder.setTotalFee(taRaise.getRaisePrice());
142 167
             return String.valueOf(taRaise.getRaisePrice());
143 168
         }
144 169
         return null;
145 170
     }
146 171
 
172
+    /**
173
+     * 校验这个人的合法性,并自动锁房
174
+     * @param taOrder
175
+     * @param taRaiseRecord
176
+     * @param taRaise
177
+     */
178
+    private void autoLockingHouse(TaOrder taOrder, TaRaiseRecord taRaiseRecord, TaRaise taRaise) throws Exception {
179
+        //如果是自动锁房,客户只能锁定一个房源
180
+        QueryWrapper<TaRaiseHouse> taRaiseHouseQueryWrapper = new QueryWrapper<>();
181
+        taRaiseHouseQueryWrapper.eq("org_id",taOrder.getOrgId());
182
+        taRaiseHouseQueryWrapper.eq("raise_record_id",taRaiseRecord.getRaiseRecordId());
183
+        taRaiseHouseQueryWrapper.eq("person_id",taOrder.getPersonId());
184
+        List<TaRaiseHouse> taRaiseHouseList = taRaiseHouseMapper.selectList(taRaiseHouseQueryWrapper);
185
+        if (taRaiseHouseList.size() != 1){
186
+            throw new Exception("自动锁房只能认筹一个房源。");
187
+        }
188
+        //判断此房源有没有被锁定
189
+        TaHousingResources taHousingResources = getHousingResourcesById(taRaiseHouseList.get(0).getHouseId());
190
+        if (taHousingResources.getHouseLockingStatus().equals(CommConstant.HOUSE_LOCKING_STATUS_LOCKED)){
191
+            throw new Exception("改房源已被锁定暂时无法认筹。");
192
+        }
193
+        //锁定此房源并且更改认筹单的锁定状态
194
+        changeLockingStatus(taRaiseRecord,taRaiseHouseList.get(0),taHousingResources);
195
+    }
196
+
197
+    /**
198
+     * 锁定此房源并且更改认筹单的锁定状态
199
+     * @param taRaiseRecord
200
+     * @param taRaiseHouse
201
+     * @param taHousingResources
202
+     */
203
+    private synchronized void changeLockingStatus(TaRaiseRecord taRaiseRecord, TaRaiseHouse taRaiseHouse, TaHousingResources taHousingResources) {
204
+        log.info("正在为客户personId为{}选中的房源id为{}自动锁房",taRaiseHouse.getPersonId(),taRaiseHouse.getHouseId());
205
+        //更改此房源为锁定状态
206
+        taHousingResources.setHouseLockingStatus(CommConstant.HOUSE_LOCKING_STATUS_LOCKED);
207
+        taHousingResourcesMapper.updateById(taHousingResources);
208
+        //更改此认筹旦和房源关系为锁定状态
209
+        taRaiseHouse.setHouseLockingStatus(CommConstant.HOUSE_LOCKING_STATUS_LOCKED);
210
+        taRaiseHouseMapper.updateById(taRaiseHouse);
211
+        //更改认筹单为锁定状态
212
+        taRaiseRecord.setHouseLockingStatus(CommConstant.HOUSE_LOCKING_STATUS_LOCKED);
213
+        taRaiseRecordMapper.updateById(taRaiseRecord);
214
+    }
215
+
216
+    private synchronized TaHousingResources getHousingResourcesById(Integer houseId) {
217
+        return taHousingResourcesMapper.selectById(houseId);
218
+    }
219
+
147 220
     /**
148 221
      * 获取每个小程序独立的微信账户
149 222
      * @param orgId
@@ -157,6 +230,7 @@ public class WxPayServiceImpl implements IWxPayService {
157 230
         if (null == taWxPayConfig){
158 231
             throw new Exception("请先完善支付信息");
159 232
         }
233
+        log.info("正在配置orgId为{}的微信支付参数,商户号为{}",orgId,taWxPayConfig.getMchId());
160 234
         //把库里的值赋值给微信
161 235
         WxConfig config = new WxConfig();
162 236
         config.setAppid(taWxPayConfig.getAppid());
@@ -167,10 +241,21 @@ public class WxPayServiceImpl implements IWxPayService {
167 241
     }
168 242
 
169 243
     /**
170
-     * 更改订单状态为正在支付
244
+     * 更改认筹单状态 为正在支付,并插入订单表
171 245
      * @param taOrder
172 246
      */
173
-    private void updateOrderStatus(TaOrder taOrder) {
247
+    private synchronized void updateOrderStatus(TaOrder taOrder) {
248
+        //生成订单表
249
+        taOrder.setCreateDate(LocalDateTime.now());
250
+        taOrder.setPayStatus(CommConstant.PAY_STATUS_PAYING);
251
+        taOrderMapper.insert(taOrder);
252
+
253
+        log.info("正在生成订单,订单id为{}",taOrder.getOrgId());
174 254
 
255
+        //更改支付状态并插入订单id
256
+        TaRaiseRecord taRaiseRecord = taRaiseRecordMapper.selectById(taOrder.getTargetId());
257
+        taRaiseRecord.setPayStatus(CommConstant.PAY_STATUS_PAYING);
258
+        taRaiseRecord.setOrderId(taOrder.getOrderId());
259
+        taRaiseRecordMapper.updateById(taRaiseRecord);
175 260
     }
176 261
 }

+ 2
- 2
src/test/java/com/huiju/estateagents/WxPayTest.java Прегледај датотеку

@@ -11,8 +11,8 @@ import org.springframework.transaction.annotation.Transactional;
11 11
 @Transactional
12 12
 public class WxPayTest {
13 13
 
14
-    @Test
14
+//    @Test
15 15
     public void getLocationCity() {
16
-
16
+        System.out.println(System.currentTimeMillis());
17 17
     }
18 18
 }