From a075cdd2f76fae321ecf95dce363141ade30f0f1 Mon Sep 17 00:00:00 2001
From: zhibing.pu <393733352@qq.com>
Date: 星期五, 12 四月 2024 22:37:58 +0800
Subject: [PATCH] 新增加功能

---
 UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/smallLogistics/server/impl/OrderLogisticsServiceImpl.java |  633 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 468 insertions(+), 165 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 b5d2a2a..cd0ef18 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
@@ -6,6 +6,7 @@
 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
 import com.stylefeng.guns.core.util.ToolUtil;
 import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
+import com.stylefeng.guns.modular.crossCity.server.impl.OrderCrossCityServiceImpl;
 import com.stylefeng.guns.modular.smallLogistics.dao.OrderLogisticsMapper;
 import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
 import com.stylefeng.guns.modular.smallLogistics.model.OrderLogisticsSpread;
@@ -25,6 +26,7 @@
 import com.stylefeng.guns.modular.system.util.Tingg.model.Results;
 import com.stylefeng.guns.modular.system.util.itextpdf.HtmlToPdfUtils;
 import com.stylefeng.guns.modular.system.warpper.BaseWarpper;
+import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
 import com.stylefeng.guns.modular.taxi.model.PaymentRecord;
 import com.stylefeng.guns.modular.taxi.service.IPaymentRecordService;
 import com.stylefeng.guns.modular.taxi.service.ITransactionDetailsService;
@@ -35,7 +37,13 @@
 import org.jsoup.select.Elements;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
 import java.io.File;
@@ -141,6 +149,12 @@
 
     @Autowired
     private IPhoneService phoneService;
+    
+    @Autowired
+    private ICancleOrderService cancleOrderService;
+    
+    @Autowired
+    private RestTemplate internalRestTemplate;
 
 
 
@@ -181,12 +195,12 @@
         String[] split = endLonLat.split(",");
         Double lng = Double.valueOf(split[0]);
         Double lat = Double.valueOf(split[1]);
-        ResultUtil<Map<String, Double>> price = this.getPrice1(type, startLonLat.split(",")[0], startLonLat.split(",")[1], lng.toString(), lat.toString(), language);
+        ResultUtil<Map<String, Object>> price = this.getPrice1(type, startLonLat.split(",")[0], startLonLat.split(",")[1], lng.toString(), lat.toString(), language);
         if(price.getCode() != 200){
             return price;
         }
         Integer i = this.selectCount(new EntityWrapper<OrderLogistics>().eq("userId", uid).eq("isDelete", 1));
-        Map<String, Double> map = price.getData();
+        Map<String, Object> map = price.getData();
         map.put("first", i.doubleValue());
         return price;
     }
@@ -204,11 +218,21 @@
         String[] split = endLonLat.split(",");
         Double lng = Double.valueOf(split[0]);
         Double lat = Double.valueOf(split[1]);
-        ResultUtil<Map<String, Double>> price = this.getPrice1(type, startLonLat.split(",")[0], startLonLat.split(",")[1], lng.toString(), lat.toString(), language);
+        ResultUtil<Map<String, Object>> price = this.getPrice1(type, startLonLat.split(",")[0], startLonLat.split(",")[1], lng.toString(), lat.toString(), language);
         if(price.getCode() == 200 && type == 5){
-            Map<String, Double> data = price.getData();
-            data.put("ordinary", data.get("ordinary") * number);
-            data.put("precious", data.get("precious") * number);
+            Map<String, Object> data = price.getData();
+            Double ordinary = Double.valueOf(data.get("ordinary").toString());
+            Double precious = Double.valueOf(data.get("precious").toString());
+            data.put("ordinary", ordinary * number);
+            data.put("precious", precious * number);
+        }
+        if(price.getCode() == 200){
+            Map<String, Object> data = price.getData();
+            Double ordinary = Double.valueOf(data.get("ordinary").toString());
+            Double precious = Double.valueOf(data.get("precious").toString());
+            Double discountMoney = Double.valueOf(data.get("discountMoney").toString());
+            data.put("ordinary", ordinary - discountMoney);
+            data.put("precious", precious - discountMoney);
         }
         return price;
     }
@@ -280,17 +304,31 @@
         orderLogistics.setMileage(0D);
         orderLogistics.setIsReassign(1);
         orderLogistics.setReassignNotice(0);
-        ResultUtil<Map<String, Double>> price = this.getPrice1(type, String.valueOf(startLon), String.valueOf(startLat), lng.toString(), lat.toString(), language);
+        ResultUtil<Map<String, Object>> price = this.getPrice1(type, String.valueOf(startLon), String.valueOf(startLat), lng.toString(), lat.toString(), language);
         if(price.getCode() != 200){
             return price;
         }
-        Map<String, Double> data = price.getData();
-        orderLogistics.setOrderMoney((cargoType == 1 ? data.get("ordinary") : data.get("precious")) + tipMoney);
-        orderLogistics.setTravelMoney(cargoType == 1 ? data.get("ordinary") : data.get("precious"));
-        if(type == 5){
-            orderLogistics.setOrderMoney(((cargoType == 1 ? data.get("ordinary") : data.get("precious")) * cargoNumber) + tipMoney);
-            orderLogistics.setTravelMoney((cargoType == 1 ? data.get("ordinary") : data.get("precious")) * cargoNumber);
+        Map<String, Object> data = price.getData();
+        Double ordinary = Double.valueOf(data.get("ordinary").toString());
+        Double precious = Double.valueOf(data.get("precious").toString());
+        Double discount = null;
+        if(null != data.get("discount")){
+            discount = Double.valueOf(data.get("discount").toString());
         }
+        Double discountMoney = Double.valueOf(data.get("discountMoney").toString());
+        Integer activityId = null;
+        if(null != data.get("activityId")){
+            activityId = Integer.valueOf(data.get("activityId").toString());
+        }
+        orderLogistics.setOrderMoney((cargoType == 1 ? ordinary : precious) + tipMoney);
+        orderLogistics.setTravelMoney((cargoType == 1 ? ordinary : precious));
+        if(type == 5){
+            orderLogistics.setOrderMoney(((cargoType == 1 ? ordinary : precious) * cargoNumber) + tipMoney);
+            orderLogistics.setTravelMoney((cargoType == 1 ? ordinary : precious) * cargoNumber);
+        }
+        orderLogistics.setDiscount(discount);
+        orderLogistics.setDiscountMoney(discountMoney);
+        orderLogistics.setActivityId(activityId);
         orderLogistics.setTipMoney(tipMoney);
         orderLogistics.setState(7);//待支付
         orderLogistics.setInsertTime(new Date());
@@ -360,7 +398,7 @@
 
 
 
-    public ResultUtil<Map<String, Double>> getPrice1(Integer type, String startLon, String startLat, String endLon, String endLat, Integer language) throws Exception{
+    public ResultUtil<Map<String, Object>> getPrice1(Integer type, String startLon, String startLat, String endLon, String endLat, Integer language) throws Exception{
         Company query = companyCityService.query1(startLon, startLat);
         if(null == query){
             return ResultUtil.error(language == 1 ? "预约取货点暂无企业服务" : language == 2 ?
@@ -368,6 +406,9 @@
         }
         Double price1 = 0D;
         Double price2 = 0D;
+        Double discount = null;
+        Double discountMoney = 0D;
+        Integer activityId = null;
         if(type == 4){//同城
             Map<String, Object> query1 = systemPriceMapper.query(query.getId(), type, null);
             DistancematrixVo distancematrix = GoogleMapUtil.getDistancematrix(Double.valueOf(startLat), Double.valueOf(startLon), Double.valueOf(endLat), Double.valueOf(endLon));
@@ -403,9 +444,23 @@
             price1 = jsonObject.getDouble("num1");
             price2 = jsonObject.getDouble("num2");
         }
-        Map<String, Double> map = new HashMap<>();
+
+        //计算折扣
+        UserActivityDiscount1 query2 = userActivityDiscount1Mapper.query(query.getId());
+        if(null != query2){
+            activityId = query2.getId();
+            discount = query2.getLogistics();
+            if(null != discount){
+                discountMoney = new BigDecimal(price1).multiply(new BigDecimal(discount / 10)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+            }
+        }
+
+        Map<String, Object> map = new HashMap<>();
         map.put("ordinary", price1);//普通
         map.put("precious", price2);//贵重
+        map.put("discount", discount);//折扣
+        map.put("discountMoney", discountMoney);//折扣金额
+        map.put("activityId", activityId);//折扣活动id
         return ResultUtil.success(map);
     }
 
@@ -508,7 +563,7 @@
             if(userCouponRecord.getState() == 3){
                 return ResultUtil.error(language == 1 ? "优惠券已过期" : language == 2 ? "Coupon is expired." : "Le coupon a expiré.", "");
             }
-            if(userCouponRecord.getCouponUseType() != 0 && userCouponRecord.getCouponUseType() != 1){
+            if(userCouponRecord.getCouponUseType() != 0 && userCouponRecord.getCouponUseType() != 4){
                 return ResultUtil.error(language == 1 ? "优惠券不能用于此订单" : language == 2 ? "Coupon cannot be used for this order." : "Le coupon ne peut pas être utilisé pour cette commande.", "");
             }
             if(userCouponRecord.getCouponType() == 2 && orderMoney.compareTo(userCouponRecord.getFullMoney()) < 0){
@@ -517,6 +572,10 @@
             orderMoney = orderMoney - userCouponRecord.getMoney();
             orderLogistics.setCouponMoney(userCouponRecord.getMoney());
             orderLogistics.setCouponId(couponId);
+        }
+        //折扣
+        if(null != orderLogistics.getActivityId()){
+            orderMoney = orderMoney - orderLogistics.getDiscountMoney();
         }
         orderMoney=new BigDecimal(orderMoney).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
 
@@ -582,6 +641,13 @@
             SysIntegral query1 = sysIntegralMapper.query(orderLogistics.getCompanyId());
             userInfo.setIntegral(userInfo.getIntegral() + (orderMoney.intValue() * query1.getIntegral()));//积分
 
+            //处理优惠券
+            if(null != userCouponRecord){
+                userCouponRecord.setState(2);
+                userCouponRecord.setEndTime(new Date());
+                userCouponRecordService.updateById(userCouponRecord);
+            }
+
             //添加交易明细
             transactionDetailsService.saveData(uid, "包裹下单支付", orderMoney, 2, 1, 1, 4, orderId);
             userInfoService.updateById(userInfo);
@@ -607,6 +673,13 @@
         if(payType == 4){//现金支付
             SysIntegral query1 = sysIntegralMapper.query(orderLogistics.getCompanyId());
             userInfo.setIntegral(userInfo.getIntegral() + (orderMoney.intValue() * query1.getIntegral()));//积分
+
+            //处理优惠券和红包
+            if(null != userCouponRecord){
+                userCouponRecord.setState(2);
+                userCouponRecord.setEndTime(new Date());
+                userCouponRecordService.updateById(userCouponRecord);
+            }
 
             //添加交易明细
             transactionDetailsService.saveData(uid, "包裹下单支付", orderMoney, 2, 1, 1, 4, orderId);
@@ -774,30 +847,31 @@
             orderLogisticsSpreadService.updateById(orderLogisticsSpread);
 
             //添加已收入明细
-            Company company = companyService.selectById(orderLogistics.getCompanyId());
-            Double speMoney =  orderLogistics.getType() == 4 ? company.getSameLogisticsMoney() : company.getCrossLogisticsMoney();
-            BigDecimal d = null;
-            BigDecimal c = null;
-            if(company.getIsSpeFixedOrProportional() == 2){//固定
-                d = new BigDecimal(speMoney);
-                c = new BigDecimal(orderMoney).subtract(d);
-            }
-            if(company.getIsSpeFixedOrProportional() == 1){//比例
-                d = new BigDecimal(orderMoney).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100), new MathContext(2, RoundingMode.HALF_EVEN)));
-                c = new BigDecimal(orderMoney).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
-            }
-
-            Income income = incomeService.selectOne(new EntityWrapper<Income>().eq("userType", 1).eq("objectId", orderLogistics.getCompanyId()).eq("type", 2).eq("incomeId", orderLogistics.getId()).eq("orderType", orderLogistics.getType()));
-            income.setMoney(income.getMoney() + d.doubleValue());
-            incomeService.updateById(income);
-            income = incomeService.selectOne(new EntityWrapper<Income>().eq("userType", 2).eq("objectId", orderLogistics.getCompanyId()).eq("type", 2).eq("incomeId", orderLogistics.getId()).eq("orderType", orderLogistics.getType()));
-            income.setMoney(income.getMoney() + c.doubleValue());
-            incomeService.updateById(income);
-            Driver driver = driverService.selectById(orderLogistics.getDriverId());
-            driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-            driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-            driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
-            driverService.updateById(driver);
+//            Company company = companyService.selectById(orderLogistics.getCompanyId());
+//            Double speMoney =  orderLogistics.getType() == 4 ? company.getSameLogisticsMoney() : company.getCrossLogisticsMoney();
+//            BigDecimal d = BigDecimal.ZERO;
+//            BigDecimal c = BigDecimal.ZERO;
+//            if(company.getIsSpeFixedOrProportional() == 2){//固定
+//                d = new BigDecimal(speMoney);
+//                c = new BigDecimal(orderMoney).subtract(d);
+//            }
+//            if(company.getIsSpeFixedOrProportional() == 1){//比例
+//                d = new BigDecimal(orderMoney).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100), new MathContext(2, RoundingMode.HALF_EVEN)));
+//                c = new BigDecimal(orderMoney).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
+//            }
+//
+//            Income income = incomeService.selectOne(new EntityWrapper<Income>().eq("userType", 1).eq("objectId", orderLogistics.getCompanyId()).eq("type", 2).eq("incomeId", orderLogistics.getId()).eq("orderType", orderLogistics.getType()));
+//            income.setMoney(income.getMoney() + d.doubleValue());
+//            incomeService.updateById(income);
+//            income = incomeService.selectOne(new EntityWrapper<Income>().eq("userType", 2).eq("objectId", orderLogistics.getCompanyId()).eq("type", 2).eq("incomeId", orderLogistics.getId()).eq("orderType", orderLogistics.getType()));
+//            System.out.println("小件物流补差价income======"+income);
+//            income.setMoney(income.getMoney() + c.doubleValue());
+//            incomeService.updateById(income);
+//            Driver driver = driverService.selectById(orderLogistics.getDriverId());
+//            driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+//            driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+//            driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+//            driverService.updateById(driver);
 
             // TODO: 2020/5/24 这里需要给司机和用户推送订单状态
             new Thread(new Runnable() {
@@ -836,6 +910,14 @@
             SysIntegral query1 = sysIntegralMapper.query(orderLogistics.getCompanyId());
             userInfo.setIntegral(userInfo.getIntegral() + (query.getAmount().intValue() * query1.getIntegral()));//积分
             userInfoService.updateById(userInfo);
+
+            //处理优惠券和红包
+            if(null != orderLogistics.getCouponId()){
+                UserCouponRecord userCouponRecord = userCouponRecordService.selectById(orderLogistics.getCouponId());
+                userCouponRecord.setState(2);
+                userCouponRecord.setEndTime(new Date());
+                userCouponRecordService.updateById(userCouponRecord);
+            }
 
             query.setState(2);
             query.setCode(order_id);
@@ -977,6 +1059,11 @@
             }else{
                 map.put("receipt", "");
             }
+            if(state == 7){
+                Double discountMoney = Double.valueOf(map.get("discountMoney").toString());
+                Double orderMoney = Double.valueOf(map.get("orderMoney").toString());
+                map.put("orderMoney", orderMoney - discountMoney);
+            }
         }
         return maps;
     }
@@ -1018,7 +1105,7 @@
 
 
     @Override
-    public ResultUtil addCancle(Integer id, String reason, String remark, Integer uid, Integer language) throws Exception {
+    public ResultUtil addCancle(Integer id, String reason, String remark, Integer uid, Double lon, Double lat, String address, Integer language) throws Exception {
         if(ToolUtil.isNotEmpty(remark)){
             if(ToolUtil.isNotEmpty(remark)){
                 List<SensitiveWords> sensitiveWords = sensitiveWordsMapper.selectList(null);
@@ -1034,133 +1121,57 @@
         if(null == orderLogistics){
             return ResultUtil.error(language == 1 ? "取消订单失败,订单信息有误" : language == 2 ? "Failed to cancel order, order-information error." : "Échec de l’annulation de la commande, erreur d’information de commande.");
         }
-        if(orderLogistics.getState() > 5 && orderLogistics.getState() != 7 && orderLogistics.getState() != 11){
+        if(orderLogistics.getState() > 5 && orderLogistics.getState() != 11){
             return ResultUtil.error(language == 1 ? "取消订单失败,不合法的操作" : language == 2 ? "Failed to cancel order, illegal operation." : "Échec de l’annulation de la commande, opération illégale.");
         }
-
-        if(orderLogistics.getState() == 7){
-            OrderCancel orderCancel = new OrderCancel();
-            orderCancel.setOrderId(id);
-            orderCancel.setOrderType(orderLogistics.getType());
-            orderCancel.setReason(reason);
-            orderCancel.setRemark(remark);
-            orderCancel.setState(2);
-            orderCancel.setInsertTime(new Date());
-            orderCancel.setUserType(1);
-            orderCancelService.insert(orderCancel);
+    
+        if(null == orderLogistics.getDriverId()){//没有接单的情况
+            integer = orderCancelService.saveData(id, 4, language == 1 ? "无司机接单" : language == 2 ? "No driver to take orders" : "Pas de chauffeur pour prendre les commandes", language == 1 ? "无司机接单" : language == 2 ? "No driver to take orders" : "Pas de chauffeur pour prendre les commandes", null, null, 2, 1, uid);
             orderLogistics.setState(10);
             this.updateById(orderLogistics);
-            return ResultUtil.success();
-        }
-
-
-        //回退支付金额
-        if(null != orderLogistics.getPayType() && orderLogistics.getPayType() == 1) {//微信
-            OrderCancel orderCancel = new OrderCancel();
-            orderCancel.setOrderId(id);
-            orderCancel.setOrderType(orderLogistics.getType());
-            orderCancel.setReason(reason);
-            orderCancel.setRemark(remark);
-            orderCancel.setState(1);
-            orderCancel.setInsertTime(new Date());
-            orderCancel.setUserType(1);
-            orderCancelService.insert(orderCancel);
-            orderLogistics.setState(10);
-            this.updateById(orderLogistics);
-            //调用回退接口
-            PaymentRecord query = paymentRecordService.query(1, orderLogistics.getUserId(), 1, orderLogistics.getId(), orderLogistics.getType(), 1, 2);
-            if(null == query){
-                return ResultUtil.error("支付数据异常,无法完成退款");
+        }else {
+            BaseWarpper data = queryCancleAmount(id, language).getData();
+            Double amount = data.getAmount();
+            if(0 == amount){
+                integer = orderCancelService.saveData(id, 4, reason, remark, null, null, 2, 1, uid);
+                if(5 == orderLogistics.getState()){
+                    orderLogistics.setState(6);
+                    orderLogistics.setGetoffLon(lon);
+                    orderLogistics.setGetoffLat(lat);
+                    orderLogistics.setGetoffAddress(address);
+                    orderLogistics.setGetoffTime(new Date());
+                    orderLogistics.setEndServiceTime(new Date());
+                }else{
+                    orderLogistics.setState(10);
+                }
+                this.updateById(orderLogistics);
+            }else{
+                if(5 == orderLogistics.getState()){
+                    orderLogistics.setGetoffLon(lon);
+                    orderLogistics.setGetoffLat(lat);
+                    orderLogistics.setGetoffAddress(address);
+                    orderLogistics.setGetoffTime(new Date());
+                    orderLogistics.setEndServiceTime(new Date());
+                }
+                orderLogistics.setOldState(orderLogistics.getState());
+                orderLogistics.setState(12);
+                this.updateById(orderLogistics);
+                integer = orderCancelService.saveData(id, 4, reason, remark, null, amount, 1, 1, uid);
             }
-            UserInfo userInfo = userInfoService.selectById(orderLogistics.getUserId());
-            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
-            String payerTransactionID = sdf.format(new Date()) + orderCancel.getId();
-            PayoutResponse payoutResponse = tinggPayoutUtil.sendPayout(userInfo.getPhoneOperator(), "+" + userInfo.getPhone(), payerTransactionID, query.getAmount().doubleValue(), callbackPath + "/base/orderCancelRefundCallback", "取消订单退款");
-            AuthStatus authStatus = payoutResponse.getAuthStatus();
-            Results results = payoutResponse.getResults().get(0);
-            String statusCode = results.getStatusCode();
-            if(!"131".equals(authStatus.getAuthStatusCode())){
-                return ResultUtil.error(authStatus.getAuthStatusDescription());
-            }
-            if(!"139".equals(statusCode)){
-                return ResultUtil.error(results.getStatusDescription());
-            }
-        }
-        if(null != orderLogistics.getPayType() && orderLogistics.getPayType() == 2){//支付宝
-            OrderCancel orderCancel = new OrderCancel();
-            orderCancel.setOrderId(id);
-            orderCancel.setOrderType(orderLogistics.getType());
-            orderCancel.setReason(reason);
-            orderCancel.setRemark(remark);
-            orderCancel.setState(1);
-            orderCancel.setInsertTime(new Date());
-            orderCancel.setUserType(1);
-            orderCancelService.insert(orderCancel);
-            orderLogistics.setState(10);
-            this.updateById(orderLogistics);
-            integer = orderCancel.getId();
-            //调用回退接口
-            PaymentRecord query = paymentRecordService.query(1, orderLogistics.getUserId(), 1, orderLogistics.getId(), orderLogistics.getType(), 2, 2);
-            if(null == query){
-                return ResultUtil.error("支付数据异常,无法完成退款");
-            }
-            UserInfo userInfo = userInfoService.selectById(orderLogistics.getUserId());
-            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
-            String payerTransactionID = sdf.format(new Date()) + orderCancel.getId();
-            PayoutResponse payoutResponse = tinggPayoutUtil.sendPayout(userInfo.getPhoneOperator(), "+" + userInfo.getPhone(), payerTransactionID, query.getAmount().doubleValue(), callbackPath + "/base/orderCancelRefundCallback", "取消订单退款");
-            AuthStatus authStatus = payoutResponse.getAuthStatus();
-            Results results = payoutResponse.getResults().get(0);
-            String statusCode = results.getStatusCode();
-            if(!"131".equals(authStatus.getAuthStatusCode())){
-                return ResultUtil.error(authStatus.getAuthStatusDescription());
-            }
-            if(!"139".equals(statusCode)){
-                return ResultUtil.error(results.getStatusDescription());
-            }
-        }
-        if(null != orderLogistics.getPayType() && orderLogistics.getPayType() == 3){//余额
-            UserInfo userInfo = userInfoService.selectById(orderLogistics.getUserId());
-            userInfo.setBalance(userInfo.getBalance() + orderLogistics.getPayMoney());
-
-            OrderCancel orderCancel = new OrderCancel();
-            orderCancel.setOrderId(id);
-            orderCancel.setOrderType(orderLogistics.getType());
-            orderCancel.setReason(reason);
-            orderCancel.setRemark(remark);
-            orderCancel.setState(2);
-            orderCancel.setInsertTime(new Date());
-            orderCancel.setUserType(1);
-            orderCancelService.insert(orderCancel);
-            userInfoService.updateById(userInfo);
-            orderLogistics.setState(10);
-            this.updateById(orderLogistics);
-            integer = orderCancel.getId();
-
-            //添加交易明细
-            transactionDetailsService.saveData(orderLogistics.getUserId(), "包裹取消退款", orderLogistics.getPayMoney(), 1, 1, 1, 4, orderLogistics.getId());
-        }
-
-
-        if(null != orderLogistics.getPayType() && orderLogistics.getPayType() == 4){//现金
-//            UserInfo userInfo = userInfoService.selectById(orderLogistics.getUserId());
-//            userInfo.setBalance(userInfo.getBalance() + orderLogistics.getPayMoney());
-//            userInfoService.updateById(userInfo);
-            OrderCancel orderCancel = new OrderCancel();
-            orderCancel.setOrderId(id);
-            orderCancel.setOrderType(orderLogistics.getType());
-            orderCancel.setReason(reason);
-            orderCancel.setRemark(remark);
-            orderCancel.setState(2);
-            orderCancel.setInsertTime(new Date());
-            orderCancel.setUserType(1);
-            orderCancelService.insert(orderCancel);
-
-            orderLogistics.setState(10);
-            this.updateById(orderLogistics);
-            integer = orderCancel.getId();
-
-            //添加交易明细
-            transactionDetailsService.saveData(orderLogistics.getUserId(), "包裹取消退款", orderLogistics.getPayMoney(), 1, 1, 1, 4, orderLogistics.getId());
+            this.deleteTask(id);//删除定时任务
+        
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), 4, orderLogistics.getState(), 0);
+                    System.err.println("推送取消操作---------------------");
+                }
+            }).start();
+        
+            //修改司机为空闲
+            Driver driver = driverService.selectById(orderLogistics.getDriverId());
+            driver.setState(2);
+            driverService.updateById(driver);
         }
 
         if(null != orderLogistics.getDriverId()){
@@ -1288,9 +1299,301 @@
     @Override
     public List<Map<String, Object>> queryCoupon(Integer language, Integer orderId, Integer uid, Integer pageNum, Integer size) throws Exception {
         OrderLogistics orderLogistics = this.selectById(orderId);
-        List<Map<String, Object>> list = userCouponRecordService.queryCoupon(language, uid, orderLogistics.getCompanyId(), 1, 4, orderLogistics.getOrderMoney(), pageNum, size);
-        List<Map<String, Object>> list1 = userCouponRecordService.queryCoupon(language, uid, orderLogistics.getCompanyId(), 1, 0, orderLogistics.getOrderMoney(), pageNum, size);
+        List<Map<String, Object>> list = userCouponRecordService.queryCoupon(language, uid, orderLogistics.getCompanyId(), 1, 4, orderLogistics.getOrderMoney() -  orderLogistics.getDiscountMoney(), pageNum, size);
+        List<Map<String, Object>> list1 = userCouponRecordService.queryCoupon(language, uid, orderLogistics.getCompanyId(), 1, 0, orderLogistics.getOrderMoney() -  orderLogistics.getDiscountMoney(), pageNum, size);
         list.addAll(list1);
         return list;
     }
+    
+    
+    
+    /**
+     * 获取取消需要支付的金额
+     * @param id
+     * @param language
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public ResultUtil<BaseWarpper> queryCancleAmount(Integer id, Integer language) throws Exception {
+        OrderLogistics orderLogistics = this.selectById(id);
+        if(null == orderLogistics){
+            return ResultUtil.error(language == 1 ? "获取数据失败,订单信息有误" : language == 2 ? "Failed to obtain data, order-information error." : "Impossible d’obtenir les données, erreur d’information de commande. ");
+        }
+        BaseWarpper baseWarpper = new BaseWarpper();
+        double amount = 0;
+        CancleOrder query = cancleOrderService.query(orderLogistics.getCompanyId());
+        if(null == query || null == orderLogistics.getDriverId()){
+            baseWarpper.setAmount(amount);
+            return ResultUtil.success(baseWarpper);
+        }
+        JSONObject object = JSON.parseObject(query.getContent());
+        long t = object.getInteger("driverTimeout") * 60000L;
+        //司机超时未到达起点,免费取消
+        if(orderLogistics.getState() < 3 && orderLogistics.getSnatchOrderTime().getTime() + t < System.currentTimeMillis()){
+            baseWarpper.setAmount(amount);
+            return ResultUtil.success(baseWarpper);
+        }
+        
+        JSONObject order = object.getJSONObject("order");
+        long m = order.getInteger("orderAcceptanceTime") * 60000L;
+        //超过免费时间,计算费用
+        if(orderLogistics.getSnatchOrderTime().getTime() + m < System.currentTimeMillis()){
+            BigDecimal money = order.getBigDecimal("money");
+            if(null != orderLogistics.getSetOutTime()){
+                //里程费
+                BigDecimal mileageFee = order.getBigDecimal("mileageFee").multiply(new BigDecimal(orderLogistics.getMileage() / 1000));
+                //时长费
+                int s = Double.valueOf((System.currentTimeMillis() - orderLogistics.getSetOutTime().getTime()) / 60000).intValue();
+                BigDecimal durationFee = order.getBigDecimal("durationFee").multiply(new BigDecimal(s));
+                money = money.add(durationFee).add(mileageFee);
+            }
+            amount = money.setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+        }
+        baseWarpper.setAmount(amount);
+        return ResultUtil.success(baseWarpper);
+    }
+    
+    
+    /**
+     * 删除定时任务
+     * @param orderId
+     */
+    @Override
+    public void deleteTask(Integer orderId){
+        //发送验证码短信
+        HttpHeaders headers = new HttpHeaders();
+        // 以表单的方式提交
+        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+        //将请求头部和参数合成一个请求
+        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
+        params.add("orderId", orderId + "");
+        params.add("orderType", "4");
+        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
+        String s = internalRestTemplate.postForObject("http://driver-server/base/netty/deleteTask", requestEntity, String.class);
+        JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);
+        if(jsonObject.getIntValue("code") != 200){
+            System.err.println("调用driver-server出错了");
+        }
+    }
+    
+    
+    
+    
+    /**
+     * 取消订单操作
+     * @param id
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public ResultUtil cancleOrderPrivateCar(Integer id, Integer payType, Integer bankCardId, Integer cancleId, Integer type, Integer language) throws Exception {
+        OrderLogistics orderLogistics = this.selectById(id);
+        Integer uid = orderLogistics.getUserId();
+        UserInfo userInfo = userInfoService.selectById(uid);
+        ResultUtil resultUtil = ResultUtil.success("");
+        
+        if(null == orderLogistics){
+            return ResultUtil.error(language == 1 ? "取消订单失败,订单信息有误" : language == 2 ? "Failed to cancel order, order-information error." : "Échec de l’annulation de la commande, erreur d’information de commande.", "");
+        }
+        if(orderLogistics.getState() != 13){
+            return ResultUtil.error(language == 1 ? "取消订单失败,不合法的操作" : language == 2 ? "Failed to cancel order, illegal operation." : "Échec de l’annulation de la commande, opération illégale.", "");
+        }
+        OrderCancel orderCancel = null;
+        if(null == cancleId || cancleId==0){
+            orderCancel = orderCancelService.query(id, 4, null, null, 1);
+        }else{
+            orderCancel = orderCancelService.selectById(cancleId);
+        }
+        
+        Double amount = queryCancleAmount(id, language).getData().getAmount();
+        if(0 < amount){
+            if(payType == 1){//手机支付
+                orderCancel.setPayType(1);
+                orderCancelService.updateById(orderCancel);
+                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
+                String merchantTransactionId = sdf.format(new Date()) + "4" + language + id;
+                CheckoutRequest checkoutRequest = new CheckoutRequest();
+                checkoutRequest.setMsisdn(Long.valueOf(userInfo.getPhone()));
+                checkoutRequest.setCustomerEmail(userInfo.getEmail());
+                checkoutRequest.setAccountNumber(userInfo.getPhone());
+                checkoutRequest.setCustomerFirstName(userInfo.getFirstName());
+                checkoutRequest.setCustomerLastName(userInfo.getLastName());
+                checkoutRequest.setRequestAmount(amount);
+                checkoutRequest.setMerchantTransactionId(merchantTransactionId);
+                checkoutRequest.setRequestDescription("Cancel a trip");
+                checkoutRequest.setCallbackUrl(callbackPath + "/base/wxCancelOrderTaxi");
+                checkoutRequest.setPendingRedirectUrl("");
+                checkoutRequest.setSuccessRedirectUrl("http://182.160.16.251:81/payMoney/pages/success.html");
+                checkoutRequest.setFailRedirectUrl("http://182.160.16.251:81/payMoney/pages/fail.html");
+                resultUtil = TinggPayUtil.checkoutRequest(checkoutRequest);
+                if(resultUtil.getCode()==200){
+                    paymentRecordService.saveData(1, null, null, id, 4, 1, amount, null, 1);//添加预支付数据
+                }
+                
+            }
+            if(payType == 2){//银行卡支付
+                orderCancel.setPayType(2);
+                orderCancelService.updateById(orderCancel);
+                BankCard bankCard = bankCardService.selectById(bankCardId);
+                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
+                String merchantTransactionId = sdf.format(new Date()) + "4" + id;
+                CheckoutRequest checkoutRequest = new CheckoutRequest();
+                checkoutRequest.setMsisdn(Long.valueOf(bankCard.getCode()));
+                checkoutRequest.setCustomerEmail(userInfo.getEmail());
+                checkoutRequest.setAccountNumber(bankCard.getCode());
+                checkoutRequest.setCustomerFirstName(bankCard.getFirstName());
+                checkoutRequest.setCustomerLastName(bankCard.getLastName());
+                checkoutRequest.setRequestAmount(amount);
+                checkoutRequest.setMerchantTransactionId(merchantTransactionId);
+                checkoutRequest.setRequestDescription("Cancel a trip");
+                checkoutRequest.setCallbackUrl(callbackPath + "/base/wxCancelOrderTaxi");
+                checkoutRequest.setPendingRedirectUrl("");
+                checkoutRequest.setSuccessRedirectUrl("http://182.160.16.251:81/payMoney/pages/success.html");
+                checkoutRequest.setFailRedirectUrl("http://182.160.16.251:81/payMoney/pages/fail.html");
+                resultUtil = TinggPayUtil.checkoutRequest(checkoutRequest);
+                if(resultUtil.getCode()==200){
+                    paymentRecordService.saveData(1, null, null, id, 4, 2, amount, null, 1);//添加预支付数据
+                }
+                
+            }
+            if(payType == 3){//余额支付
+                if(userInfo.getBalance() != null && userInfo.getBalance() < amount){
+                    return ResultUtil.error((language == 1 ? "账户余额不足" : language == 2 ? "Insufficient balance" : "Solde insuffisant"), "");
+                }
+                
+                userInfo.setBalance(new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(amount)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                //添加交易明细
+                transactionDetailsService.saveData(uid, "取消订单", amount, 2, 1, 1, 4, id);
+                userInfoService.updateById(userInfo);
+                
+                //解除小号绑定
+                if(orderLogistics.getBindId() != null){
+                    ChinaMobileUtil.midAxbUnBindSend(orderLogistics.getBindId(),orderLogistics.getTelX());
+                }
+    
+                if(5 == orderLogistics.getOldState()){
+                    orderLogistics.setState(6);
+                }else{
+                    orderLogistics.setState(10);
+                }
+                orderLogistics.setTelX("");
+                orderLogistics.setBindId("");
+                this.updateById(orderLogistics);
+                
+                orderCancel.setState(2);
+                orderCancel.setPayType(3);
+                orderCancelService.updateById(orderCancel);
+
+                if(null != orderLogistics.getDriverId()){
+                    BigDecimal c =new BigDecimal(amount);//司机收入
+                    incomeService.saveData(2, orderLogistics.getDriverId(), 3, orderLogistics.getId(), 4, c.doubleValue());
+                    Driver driver = driverService.selectById(orderLogistics.getDriverId());
+                    driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                    driverService.updateById(driver);
+    
+                    this.deleteTask(id);//删除定时任务
+                    pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), 4, orderLogistics.getState(), 0);
+                }
+                pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), 4, orderLogistics.getState(), 0);
+    
+    
+                //添加消息
+                systemNoticeService.addSystemNotice(1, language == 1 ? "您已使用余额成功支付取消订单费用,谢谢使用!" : language == 2
+                        ? "You've paid for cancellation fee with the balance successfully, thank you for using I-GO"
+                        : "Vous avez utilisé avec succès votre solde pour payer les frais d’annulation, merci d’utiliser I-GO", orderLogistics.getUserId(), 1);
+            }
+        }
+        return resultUtil;
+    }
+    
+    
+    
+    /**
+     * 取消订单支付后的处理
+     * @param id            订单=id
+     * @param order_id      工行支付单号
+     * @throws Exception
+     */
+    @Override
+    public void payCancelOrderPrivateCar(Integer id, String order_id, Integer language) throws Exception {
+        OrderLogistics orderLogistics = this.selectById(id);
+        PaymentRecord query = paymentRecordService.query(1, null, null, Integer.valueOf(id), 4, null, 1);
+        if(null != query){
+            //添加交易明细
+            transactionDetailsService.saveData(orderLogistics.getUserId(), "取消订单", query.getAmount(), 2, 1, 1, 4, query.getOrderId());
+            
+            //解除小号绑定
+            if(orderLogistics.getBindId() != null){
+                ChinaMobileUtil.midAxbUnBindSend(orderLogistics.getBindId(),orderLogistics.getTelX());
+            }
+            if(5 == orderLogistics.getOldState()){
+                orderLogistics.setState(6);
+            }else{
+                orderLogistics.setState(10);
+            }
+            orderLogistics.setBindId("");
+            orderLogistics.setTelX("");
+            this.updateById(orderLogistics);
+            
+            query.setState(2);
+            query.setCode(order_id);
+            paymentRecordService.updateById(query);
+            
+            OrderCancel query1 = orderCancelService.query(query.getOrderId(), query.getOrderType(), query.getAmount(), query.getPayType(), 1);
+            if (null != query1){
+                query1.setState(2);
+                orderCancelService.updateById(query1);
+            }
+            
+            
+            if(null != orderLogistics.getDriverId()){
+                BigDecimal c =new BigDecimal(query.getAmount());//司机收入
+                incomeService.saveData(2, orderLogistics.getDriverId(), 3, orderLogistics.getId(), 4, c.doubleValue());
+                Driver driver = driverService.selectById(orderLogistics.getDriverId());
+                driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
+                driverService.updateById(driver);
+                this.deleteTask(orderLogistics.getId());//删除定时任务
+                
+                pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), 4, orderLogistics.getState(), 0);
+            }
+            pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), 4, orderLogistics.getState(), 0);
+            
+            //添加消息
+            systemNoticeService.addSystemNotice(1, language == 1 ? "您已使用余额成功支付取消订单费用,谢谢使用!" : language == 2
+                    ? "You've paid for cancellation fee with the balance successfully, thank you for using I-GO"
+                    : "Vous avez utilisé avec succès votre solde pour payer les frais d’annulation, merci d’utiliser I-GO", orderLogistics.getUserId(), 1);
+        }else{
+            System.err.println("预支付数据异常(orderId = "  + id + ")");
+        }
+    }
+    
+    
+    /**
+     * 修改目的地
+     * @param orderId
+     * @param endLon
+     * @param endLat
+     * @param endAddress
+     */
+    @Override
+    public void updateEndAddress(Integer orderId, String endLon, String endLat, String endAddress) {
+        OrderLogistics orderLogistics = this.selectById(orderId);
+        if(null != orderLogistics.getDriverId()){
+            orderLogistics.setDestination(endAddress);
+            orderLogistics.setDestinationLat(endLat);
+            orderLogistics.setDestinationLon(endLon);
+            this.updateById(orderLogistics);
+            pushUtil.pushModifyAddress(2, orderLogistics.getDriverId(), orderId, 1, 1);
+        }else{
+            orderLogistics.setEndAddress(endAddress);
+            orderLogistics.setEndLat(Double.valueOf(endLat));
+            orderLogistics.setEndLon(Double.valueOf(endLon));
+            this.updateById(orderLogistics);
+        }
+    }
 }

--
Gitblit v1.7.1