From 0d5bade502337cab3fc2f96cf2d6891ded35bb77 Mon Sep 17 00:00:00 2001
From: zhibing.pu <393733352@qq.com>
Date: 星期一, 15 四月 2024 20:50:29 +0800
Subject: [PATCH] 新增加功能

---
 UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java |  140 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 133 insertions(+), 7 deletions(-)

diff --git a/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java b/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java
index cd0ef18..ada1b3e 100644
--- a/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java
+++ b/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java
@@ -1,6 +1,7 @@
 package com.stylefeng.guns.modular.smallLogistics.server.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
@@ -155,6 +156,12 @@
     
     @Autowired
     private RestTemplate internalRestTemplate;
+    
+    @Autowired
+    private IRedEnvelopePaymentSettingsService redEnvelopePaymentSettingsService;
+    
+    @Autowired
+    private IUserRedPacketRecordService userRedPacketRecordService;
 
 
 
@@ -536,7 +543,7 @@
      * @throws Exception
      */
     @Override
-    public ResultUtil payLogisticsOrder(Integer payType, Integer bankCardId, Integer orderId,Integer couponId, Integer type, Integer language) throws Exception {
+    public ResultUtil payLogisticsOrder(Integer payType, Integer bankCardId, Integer orderId,Integer couponId, Integer redDeduction, Integer type, Integer language) throws Exception {
         OrderLogistics orderLogistics = this.selectById(orderId);
         if(orderLogistics.getState() != 7){
             return ResultUtil.error(language == 1 ? "订单已完成支付,不允许重复支付" : language == 2 ? "The order has been paid, recurring payments is not allowed." : "La commande a été payée, les paiements récurrents ne sont pas autorisés.", "");
@@ -548,11 +555,18 @@
         }
         UserInfo userInfo = userInfoService.selectById(uid);
         ResultUtil resultUtil = ResultUtil.success("");
-        orderLogistics.setCouponMoney(0D);//初始化历史数据
-        orderLogistics.setCouponId(null);
+        //折扣
+        if(null != orderLogistics.getActivityId()){
+            orderMoney = orderMoney - orderLogistics.getDiscountMoney();
+        }
+        
         //计算优惠券
         UserCouponRecord userCouponRecord = null;
         if(null != couponId){
+            //TODO 待翻译
+            if(null != redDeduction && 1 == redDeduction){
+                return ResultUtil.error(language == 1 ? "优惠券和红包不能同时使用" : language == 2 ? "" : "", "");
+            }
             userCouponRecord = userCouponRecordService.selectById(couponId);
             if(userCouponRecord.getCompanyId() != orderLogistics.getCompanyId()){
                 return ResultUtil.error(language == 1 ? "优惠券不能用于此订单" : language == 2 ? "Coupon cannot be used for this order." : "Le coupon ne peut pas être utilisé pour cette commande.", "");
@@ -573,10 +587,64 @@
             orderLogistics.setCouponMoney(userCouponRecord.getMoney());
             orderLogistics.setCouponId(couponId);
         }
-        //折扣
-        if(null != orderLogistics.getActivityId()){
-            orderMoney = orderMoney - orderLogistics.getDiscountMoney();
+    
+        //计算红包
+        if(null != redDeduction && 1 == redDeduction){
+            RedEnvelopePaymentSettings redEnvelopePaymentSettings = redEnvelopePaymentSettingsService.getRedEnvelopePaymentSettings();
+            if(null != redEnvelopePaymentSettings){
+                Double total = userRedPacketRecordService.queryRemainingAmount(uid);
+                List<UserRedPacketRecord> userRedPacketRecords = userRedPacketRecordService.selectList(new EntityWrapper<UserRedPacketRecord>().eq("userId", uid)
+                        .eq("state", 1).eq("companyId", orderLogistics.getCompanyId()).gt("remainingAmount", 0).orderBy("insertTime", false));
+                if(total.compareTo(orderMoney) >= 0){
+                    BigDecimal deductionRatio = redEnvelopePaymentSettings.getDeductionRatio();
+                    BigDecimal multiply = new BigDecimal(orderMoney).multiply(deductionRatio.divide(new BigDecimal(100)));
+                    orderLogistics.setRedPacketMoney(multiply.doubleValue());
+                    orderMoney = orderMoney - multiply.doubleValue();
+                
+                    //获取红包id
+                    JSONArray jsonArray = new JSONArray();
+                    for (UserRedPacketRecord userRedPacketRecord : userRedPacketRecords) {
+                        JSONObject jsonObject = new JSONObject();
+                        jsonObject.put("id", userRedPacketRecord.getId());
+                        BigDecimal remainingAmount = new BigDecimal(userRedPacketRecord.getRemainingAmount());
+                        if(multiply.compareTo(remainingAmount) >= 0){
+                            multiply = multiply.subtract(remainingAmount);
+                            jsonObject.put("money", remainingAmount);
+                            jsonArray.add(jsonObject);
+                        }else{
+                            userRedPacketRecord.setRemainingAmount(remainingAmount.subtract(multiply).doubleValue());
+                            jsonObject.put("money", multiply);
+                            jsonArray.add(jsonObject);
+                            break;
+                        }
+                    }
+                    orderLogistics.setRedPacketId(jsonArray.toJSONString());
+                }else{
+                    orderLogistics.setRedPacketMoney(total);
+                    orderMoney = orderMoney - total;
+                    //获取红包id
+                    JSONArray jsonArray = new JSONArray();
+                    BigDecimal multiply = new BigDecimal(total);
+                    for (UserRedPacketRecord userRedPacketRecord : userRedPacketRecords) {
+                        JSONObject jsonObject = new JSONObject();
+                        jsonObject.put("id", userRedPacketRecord.getId());
+                        BigDecimal remainingAmount = new BigDecimal(userRedPacketRecord.getRemainingAmount());
+                        if(multiply.compareTo(remainingAmount) >= 0){
+                            multiply = multiply.subtract(remainingAmount);
+                            jsonObject.put("money", remainingAmount);
+                            jsonArray.add(jsonObject);
+                        }else{
+                            userRedPacketRecord.setRemainingAmount(remainingAmount.subtract(multiply).doubleValue());
+                            jsonObject.put("money", multiply);
+                            jsonArray.add(jsonObject);
+                            break;
+                        }
+                    }
+                    orderLogistics.setRedPacketId(jsonArray.toJSONString());
+                }
+            }
         }
+        
         orderMoney=new BigDecimal(orderMoney).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
 
         if(payType == 1) {//手机支付
@@ -647,6 +715,21 @@
                 userCouponRecord.setEndTime(new Date());
                 userCouponRecordService.updateById(userCouponRecord);
             }
+            if(null != orderLogistics.getRedPacketId()){
+                JSONArray jsonArray = JSON.parseArray(orderLogistics.getRedPacketId());
+                for (int i = 0; i < jsonArray.size(); i++) {
+                    JSONObject jsonObject = jsonArray.getJSONObject(i);
+                    Integer id1 = jsonObject.getInteger("id");
+                    Double money = jsonObject.getDouble("money");
+                    UserRedPacketRecord userRedPacketRecord = userRedPacketRecordService.selectById(id1);
+                    userRedPacketRecord.setRemainingAmount(userRedPacketRecord.getRemainingAmount() - money);
+                    if(0 == userRedPacketRecord.getRemainingAmount()){
+                        userRedPacketRecord.setState(2);
+                        userRedPacketRecord.setEndTime(new Date());
+                    }
+                    userRedPacketRecordService.updateById(userRedPacketRecord);
+                }
+            }
 
             //添加交易明细
             transactionDetailsService.saveData(uid, "包裹下单支付", orderMoney, 2, 1, 1, 4, orderId);
@@ -669,7 +752,7 @@
                     "Vous avez payé la commande de livraison avec succès, merci d’utiliser I-GO", uid, 1);
             this.pushOrder(orderLogistics);//推单
         }
-
+        Driver driver = driverService.selectById(orderLogistics.getDriverId());
         if(payType == 4){//现金支付
             SysIntegral query1 = sysIntegralMapper.query(orderLogistics.getCompanyId());
             userInfo.setIntegral(userInfo.getIntegral() + (orderMoney.intValue() * query1.getIntegral()));//积分
@@ -679,6 +762,34 @@
                 userCouponRecord.setState(2);
                 userCouponRecord.setEndTime(new Date());
                 userCouponRecordService.updateById(userCouponRecord);
+                //添加优惠收入补贴
+                Double money = userCouponRecord.getMoney();
+                incomeService.saveData(2, orderLogistics.getDriverId(), 5, orderLogistics.getId(), 4, money);
+                driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driverService.updateById(driver);
+            }
+            if(null != orderLogistics.getRedPacketId()){
+                JSONArray jsonArray = JSON.parseArray(orderLogistics.getRedPacketId());
+                for (int i = 0; i < jsonArray.size(); i++) {
+                    JSONObject jsonObject = jsonArray.getJSONObject(i);
+                    Integer id1 = jsonObject.getInteger("id");
+                    Double money = jsonObject.getDouble("money");
+                    UserRedPacketRecord userRedPacketRecord = userRedPacketRecordService.selectById(id1);
+                    userRedPacketRecord.setRemainingAmount(userRedPacketRecord.getRemainingAmount() - money);
+                    if(0 == userRedPacketRecord.getRemainingAmount()){
+                        userRedPacketRecord.setState(2);
+                        userRedPacketRecord.setEndTime(new Date());
+                    }
+                    userRedPacketRecordService.updateById(userRedPacketRecord);
+                }
+                Double money = orderLogistics.getRedPacketMoney();
+                incomeService.saveData(2, orderLogistics.getDriverId(), 7, orderLogistics.getId(), 4, money);
+                driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driverService.updateById(driver);
             }
 
             //添加交易明细
@@ -918,6 +1029,21 @@
                 userCouponRecord.setEndTime(new Date());
                 userCouponRecordService.updateById(userCouponRecord);
             }
+            if(null != orderLogistics.getRedPacketId()){
+                JSONArray jsonArray = JSON.parseArray(orderLogistics.getRedPacketId());
+                for (int i = 0; i < jsonArray.size(); i++) {
+                    JSONObject jsonObject = jsonArray.getJSONObject(i);
+                    Integer id1 = jsonObject.getInteger("id");
+                    Double money = jsonObject.getDouble("money");
+                    UserRedPacketRecord userRedPacketRecord = userRedPacketRecordService.selectById(id1);
+                    userRedPacketRecord.setRemainingAmount(userRedPacketRecord.getRemainingAmount() - money);
+                    if(0 == userRedPacketRecord.getRemainingAmount()){
+                        userRedPacketRecord.setState(2);
+                        userRedPacketRecord.setEndTime(new Date());
+                    }
+                    userRedPacketRecordService.updateById(userRedPacketRecord);
+                }
+            }
 
             query.setState(2);
             query.setCode(order_id);

--
Gitblit v1.7.1