Helius
2021-03-10 087cd794187727bf9a34fcef3027cd56f6dec436
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package com.matrix.system.shopXcx.api.action;
 
import com.matrix.biz.bean.BizUser;
import com.matrix.component.redis.RedisUserLoginUtils;
import com.matrix.component.tools.WxUtils;
import com.matrix.component.wechat.externalInterface.protocol.paramProtocol.BrandWCPayRequestData;
import com.matrix.component.wechat.externalInterface.weixinUtil.WeixinServiceUtil;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.hive.bean.ShoppingGoods;
import com.matrix.system.hive.dao.ShoppingGoodsDao;
import com.matrix.system.shopXcx.api.pojo.AddShopOrderPOJO;
import com.matrix.system.shopXcx.api.pojo.CreateOrderDto;
import com.matrix.system.shopXcx.api.pojo.OrderInfoQueryPOJO;
import com.matrix.system.shopXcx.api.service.ShoppingCartService;
import com.matrix.system.shopXcx.api.service.WXShopOrderService;
import com.matrix.system.shopXcx.api.service.WxShopCouponService;
import com.matrix.system.shopXcx.api.tools.WxShopCouponUtil;
import com.matrix.system.shopXcx.api.tools.WxShopOrderUtil;
import com.matrix.system.shopXcx.api.vo.CouponReceiveInfoVO;
import com.matrix.system.shopXcx.api.vo.ShopCartVo;
import com.matrix.system.shopXcx.bean.*;
import com.matrix.system.shopXcx.dao.*;
import com.matrix.system.shopXcx.dto.DiscountExplain;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
 
/**
 * @author jyy
 * @description 订单控制器
 * @date 2019-06-10 10:58
 */
@Controller
@RequestMapping(value = "wxapi/shopOrder")
public class WXShopOrderAction {
 
    @Autowired
    private WXShopOrderService shopOrderService;
 
    @Autowired
    WxShopCouponService wxShopCouponService;
 
    @Autowired
    private ShopReceiveAddressDao shopReceiveAddressDao;
 
    @Autowired
    ShoppingCartService shoppingCartService;
 
    @Autowired
    private ShopCouponDao shopCouponDao;
    @Autowired
    private WxShopCouponUtil wxShopCouponUtil;
 
    @Autowired
    private RedisUserLoginUtils redisUserLoginUtils;
 
    @Autowired
    ShopCouponRecordDao shopCouponRecordDao;
 
    @Autowired
    ShopSkuDao shopSkuDao;
 
    @Autowired
    ShopProductDao shopProductDao;
    @Autowired
    private WxShopOrderUtil wxShopOrderUtil;
 
    @Autowired
    private ShopOrderDetailsDao shopOrderDetailsDao;
 
    @Autowired
    ShopOrderDao shopOrderDao;
 
    /**
     * 计算购物车订单价格
     *
     * @param addShopOrderPOJO
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/calculationCartOrder")
    public
    @ResponseBody
    AjaxResult calculationCartOrder(@RequestBody AddShopOrderPOJO addShopOrderPOJO) throws Exception {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class); //计算购物车中物品的优惠信息
 
        List<ShopCartVo> cartList = shoppingCartService.findUserCartList(addShopOrderPOJO.getShopId());
 
 
        //取出购物车中选中的产品
        List<ShopShoppingCart> buyCart = new ArrayList<>();
 
        //取出购物车中没有参加店铺活动的产品,和参加了店铺活动但是活动没有达到标准的产品,用来计算是否可以使用优惠券
        List<ShopShoppingCart> notCouponuyCart = new ArrayList<>();
        for (ShopCartVo shopCartVo : cartList) {
            for (ShopShoppingCart shopShoppingCart : shopCartVo.getCartList()) {
                if (shopShoppingCart.getIsSelected() == 1) {
                    buyCart.add(shopShoppingCart);
                    if (shopCartVo.getCoupon() == null || !shopCartVo.isSatisfactionCoupon()) {
                        notCouponuyCart.add(shopShoppingCart);
                    }
                }
            }
        }
        //获取当前购物车产品可用的优惠券
        List<CouponReceiveInfoVO> usefulCoupon = wxShopCouponService.getCartVoCouponList(bizUser.getCompanyId(),notCouponuyCart);
 
        //获取用户使用的优惠券
        CouponReceiveInfoVO shopCoupon = new CouponReceiveInfoVO();
        if (addShopOrderPOJO.getCouponReceiveId() == null) {
            //如果用户没有选择优惠券则默认选优惠金额最高的
            //计算使用优惠券后的价格
            if (CollectionUtils.isNotEmpty(usefulCoupon)) {
                //根据优惠券ID用户ID查询优惠券信息
                shopCoupon = usefulCoupon.get(0);
            }
        } else if (addShopOrderPOJO.getCouponReceiveId() != 1) {
            //用户选择了优惠券
            shopCoupon = shopCouponRecordDao.selectMyCouponByReceiveId(addShopOrderPOJO.getCouponReceiveId());
        } else {
            //用户不使用优惠券
        }
 
 
        //计算收货地址:
 
        ShopReceiveAddress shopReceiveAddress = null;
        if (addShopOrderPOJO.getReceiveAddrId() == null) {
            //如果没有传收货地址则默认收货地址
            shopReceiveAddress = shopReceiveAddressDao.selectDefaultAddress(bizUser.getOpenId());
            if (shopReceiveAddress != null) {
                addShopOrderPOJO.setReceiveAddrId(shopReceiveAddress.getAddrId());
            }
        } else {
            shopReceiveAddress = shopReceiveAddressDao.selectById(addShopOrderPOJO.getReceiveAddrId());
            addShopOrderPOJO.setReceiveAddrId(shopReceiveAddress.getAddrId());
 
        }
 
 
        //得到订单计价说明
        DiscountExplain discountExplain = shopOrderService.buildDiscountExplain(cartList, shopCoupon.getReceiveId(), bizUser.getCompanyId());
 
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, buyCart, buyCart.size());
        //结算金额信息
        result.putInMap("discountExplain", discountExplain);
 
 
        //用户优惠券
        result.putInMap("usefulCoupon", usefulCoupon);
        //用户选中的优惠券
        result.putInMap("shopCoupon", shopCoupon);
 
 
        //默认地址
        String deliverySelect = "立即配送";
        if (shopReceiveAddress != null) {
            if (StringUtils.isNotBlank(shopReceiveAddress.getSchool())) {
                deliverySelect = "立即配送";
            } else {
                deliverySelect = "物流配送";
            }
        }
        result.putInMap("defaultAddress", shopReceiveAddress);
        //配送方式
        result.putInMap("deliverySelect", deliverySelect);
 
 
        result.setRows(buyCart);
        return result;
    }
 
 
    @Autowired
    ShoppingGoodsDao shoppingGoodsDao;
 
    /**
     * 新增订单
     *
     * @param addShopOrderPOJO
     * @return
     */
    @PostMapping(value = "/addShopOrder")
    public
    @ResponseBody
    AjaxResult addShopOrder(@RequestBody AddShopOrderPOJO addShopOrderPOJO) throws Exception {
        return shopOrderService.createShopOrder(addShopOrderPOJO);
    }
 
 
    /**
     * 新增订单
     * 本方法抛弃,不再使用,本方法是单单个产品购买
     * 在多产品的时候不适合
     * @return
     */
    @Deprecated
    @PostMapping(value = "/addShopOrder2")
    public
    @ResponseBody
    AjaxResult addShopOrder2(@RequestBody CreateOrderDto createOrderDto) throws Exception {
 
 
        //校验收货地址
        ShopReceiveAddress shopReceiveAddress = shopReceiveAddressDao.selectById(createOrderDto.getReceiveAddrId());
        if (shopReceiveAddress == null) {
            return new AjaxResult(AjaxResult.STATUS_FAIL, "请选择正确的收获地址");
        }
 
 
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        ShopSku shopSku = shopSkuDao.selectById(createOrderDto.getSkuId());
        ShopProduct shopProduct = shopProductDao.selectById(createOrderDto.getProductId());
        String stokCode = shopSku.getStockCode();
 
        ShopOrder shopOrder = new ShopOrder();
        //查询绑定的产品是否为家居产品
        if (StringUtils.isNotBlank(stokCode)) {
            ShoppingGoods shoppingGoods = shoppingGoodsDao.selectById(Long.parseLong(stokCode));
            if (shoppingGoods != null) {
                //根据购买的产品设置订单类型
                if (Dictionary.SHOPPING_GOODS_TYPE_JJCP.equals(shoppingGoods.getGoodType())) {
                    shopOrder.setOrderType(ShopOrder.ORDER_TYPE_GOODS);
                } else {
                    shopOrder.setOrderType(ShopOrder.ORDER_TYPE_SERVICE);
                }
            } else {
                shopOrder.setOrderType(ShopOrder.ORDER_TYPE_GOODS);
            }
        }
 
        List<ShopOrderDetails> details = new ArrayList<>();
        Integer cartCount = 0;
        ShopOrderDetails shopOrderDetails = new ShopOrderDetails();
        shopOrderDetails.setCreateBy(AppConstance.SYSTEM_USER);
        shopOrderDetails.setUpdateBy(AppConstance.SYSTEM_USER);
        shopOrderDetails.setpId(createOrderDto.getProductId());
        shopOrderDetails.setsId(createOrderDto.getSkuId());
        shopOrderDetails.setPrice(shopSku.getPrice());
        shopOrderDetails.setCount(createOrderDto.getNumber());
        shopOrderDetails.setsTitle(shopProduct.getTitle() + shopSku.getName());
        shopOrderDetails.setDiscountAmount(BigDecimal.ZERO);
        shopOrderDetails.setTotalPrice(shopOrderDetails.getPrice().multiply(BigDecimal.valueOf(shopOrderDetails.getCount())));
        details.add(shopOrderDetails);
        cartCount += shopOrderDetails.getCount();
 
 
        //TODO 更新库存和销量[考虑和erp库存联动]
        wxShopOrderUtil.updateProductAndSkuInfo(details);
 
        //用户openId
        shopOrder.setUserId(bizUser.getOpenId());
        shopOrder.setCreateBy("微信小程序生成");
        shopOrder.setUpdateBy("微信小程序生成");
        shopOrder.setCommodityPrice(shopOrderDetails.getTotalPrice());
        shopOrder.setPurchaseQuantity(cartCount);
        //购买方式
        shopOrder.setShippingMethod("物流配送");
        //生成订单号
        shopOrder.setOrderNo(WxUtils.getOrderNum());
        //设置支付状态
        shopOrder.setPayStatus(ShopOrder.ORDER_STATUS_WAIT_PAY);
        //设置订单状态
        shopOrder.setOrderStatus(ShopOrder.ORDER_STATUS_WAIT_SEND);
 
        //设置退款费用(注:下次优化从数据字典表获取)
        shopOrder.setRefundCharge(BigDecimal.ZERO);
        //客户电话
        shopOrder.setUserTel(shopReceiveAddress.getAddrTelephone());
        //客户姓名
        shopOrder.setUserName(shopReceiveAddress.getAddrLiaisonman());
        //备注
        shopOrder.setRemarks(createOrderDto.getRemarks());
        //删除标识
        shopOrder.setDelFlag(AppConstance.DATA_USEABLE);
        //微信订单号
        shopOrder.setWxOrderNo("");
        //微信支付结果
        shopOrder.setPayResult(ShopOrder.ORDER_WX_STATUS_WAIT_PAY);
        //如果没有提交购买方式或配送方式不是物流
        //获取邮费
        shopOrder.setOrderMoney(shopOrderDetails.getTotalPrice());
        shopOrder.setStoreId(createOrderDto.getShopId());
 
        shopOrderDao.insert(shopOrder);
 
        wxShopOrderUtil.insertSendPackageInfo(shopOrder, "物流配送", shopReceiveAddress);
 
        //给订单详情表添加订单ID
        shopOrderDetails.setOrderId(shopOrder.getId());
 
        shopOrderDetailsDao.insert(shopOrderDetails);
 
 
        BrandWCPayRequestData payData = startPayment(shopOrder);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, Arrays.asList(payData));
        result.putInMap("orderId", shopOrder.getId());
 
        return result;
 
 
    }
 
    @Value("${wx_pay_debug_onoff}")
    private boolean isDebug;
    @Autowired
    private WeixinServiceUtil weixinServiceUtil;
 
    /**
     * 开始付款
     *
     * @param shopOrder
     * @return
     * @throws Exception
     */
    public BrandWCPayRequestData startPayment(ShopOrder shopOrder) throws Exception {
        BigDecimal unit = new BigDecimal("100");
        BigDecimal money = new BigDecimal(shopOrder.getOrderMoney().toString());
        BrandWCPayRequestData payData;
        String productNames = wxShopOrderUtil.getProductNames(shopOrder.getUserId(), shopOrder.getId());
        if (isDebug) {
            payData = weixinServiceUtil.createOrder("[测试]" + productNames, shopOrder.getOrderNo(),
                    1, shopOrder.getUserId(), String.valueOf(shopOrder.getId()));
        } else {
            payData = weixinServiceUtil.createOrder(productNames, shopOrder.getOrderNo(),
                    unit.multiply(money).intValue(), shopOrder.getUserId(), String.valueOf(shopOrder.getId()));
        }
        ShopOrder updateParam = new ShopOrder();
        updateParam.setId(shopOrder.getId());
        updateParam.setWxOrderNo(payData.getPrepay_id());
        shopOrderDao.updateByModel(updateParam);
        return payData;
    }
 
 
    /**
     * 订单结算
     *
     * @param orderId 订单ID
     * @return
     */
    @GetMapping(value = "orderSettlement/{orderId}")
    public
    @ResponseBody
    AjaxResult orderSettlement(@PathVariable("orderId") int orderId) {
        try {
            return shopOrderService.orderSettlement(orderId);
        } catch (Exception e) {
            throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
        }
    }
 
    /**
     * 查询订单支付结果
     *
     * @param orderId
     * @return
     * @throws InterruptedException
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年9月8日
     */
    @GetMapping(value = "/findOrderPayStatus/{orderId}")
    public
    @ResponseBody
    AjaxResult findOrderPayStatus(@PathVariable Integer orderId) throws InterruptedException {
        AjaxResult ajaxResult = shopOrderService.findOrderPayStatus(orderId);
        return ajaxResult;
    }
 
    /**
     * 获取我的订单信息
     *
     * @return
     */
    @PostMapping(value = "getMyOrderInfo")
    @ResponseBody
    public AjaxResult getMyOrderInfo(@RequestBody OrderInfoQueryPOJO orderInfoQueryPOJO) {
        AjaxResult ajaxResult = shopOrderService.getMyOrderInfo(orderInfoQueryPOJO);
        return ajaxResult;
    }
 
    /**
     * 通过订单ID获取订单信息
     *
     * @return
     */
    @GetMapping(value = "getOrderInfoById/{orderId}")
    @ResponseBody
    public AjaxResult getOrderInfoById(@PathVariable Integer orderId) {
        AjaxResult ajaxResult = shopOrderService.getOrderInfoById(orderId);
        return ajaxResult;
    }
 
    /**
     * 取消订单
     *
     * @param
     * @return
     */
    @GetMapping(value = "cancelOrderWhenWaitPay/{orderId}")
    @ResponseBody
    public AjaxResult cancelOrderWhenWaitPay(@PathVariable("orderId") Integer orderId) {
        return shopOrderService.cancelOrderWhenWaitPay(orderId);
    }
 
    /**
     * 删除订单
     *
     * @param
     * @return
     */
    @GetMapping(value = "delOrder/{orderId}")
    @ResponseBody
    public AjaxResult delOrderById(@PathVariable("orderId") Integer orderId) {
        return shopOrderService.delOrderById(orderId);
    }
 
    /**
     * 确认收货
     *
     * @param
     * @return
     */
    @GetMapping(value = "confirmPackageById/{orderId}")
    @ResponseBody
    public AjaxResult confirmPackageById(@PathVariable("orderId") Integer orderId) {
        return shopOrderService.confirmPackageById(orderId);
    }
 
 
    @GetMapping(value = "/getOrderStatusCount")
    @ResponseBody
    public AjaxResult getOrderStatusCount() {
        BizUser bizUser = redisUserLoginUtils.getLoginUser(BizUser.class);
        AjaxResult result = AjaxResult.buildSuccessInstance(
                shopOrderDao.selectOrderStatusCount(bizUser.getOpenId()));
        return result;
    }
 
}