From d85000e8f03740bd68523a36e108065a8168bd6a Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期三, 19 七月 2023 09:09:24 +0800
Subject: [PATCH] 更新预约场地部分接口

---
 cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java |  329 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 314 insertions(+), 15 deletions(-)

diff --git a/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java b/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
index 9b6a244..6a309f0 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/service/impl/SiteServiceImpl.java
@@ -1,26 +1,27 @@
 package com.dsh.other.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.dsh.other.entity.Site;
-import com.dsh.other.entity.SiteBooking;
-import com.dsh.other.entity.SiteType;
-import com.dsh.other.entity.Store;
+import com.dsh.other.entity.*;
+import com.dsh.other.feignclient.account.AppUserClient;
+import com.dsh.other.feignclient.account.model.AppUser;
+import com.dsh.other.feignclient.activity.CouponClient;
+import com.dsh.other.feignclient.activity.UserCouponClient;
+import com.dsh.other.feignclient.activity.model.Coupon;
+import com.dsh.other.feignclient.activity.model.UserCoupon;
 import com.dsh.other.mapper.SiteMapper;
-import com.dsh.other.model.QuerySiteInfoVo;
-import com.dsh.other.model.QuerySiteList;
-import com.dsh.other.model.QuerySiteListVo;
-import com.dsh.other.model.QuerySiteTimes;
-import com.dsh.other.service.ISiteBookingService;
-import com.dsh.other.service.ISiteService;
-import com.dsh.other.service.ISiteTypeService;
-import com.dsh.other.service.StoreService;
-import com.dsh.other.util.GeodesyUtil;
-import com.dsh.other.util.ToolUtil;
+import com.dsh.other.model.*;
+import com.dsh.other.service.*;
+import com.dsh.other.util.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
@@ -39,6 +40,21 @@
 
     @Autowired
     private ISiteBookingService siteBookingService;
+
+    @Autowired
+    private ISiteLockService siteLockService;
+
+    @Resource
+    private AppUserClient appUserClient;
+
+    @Resource
+    private UserCouponClient userCouponClient;
+
+    @Resource
+    private CouponClient couponClient;
+
+    @Autowired
+    private PayMoneyUtil payMoneyUtil;
 
 
 
@@ -73,7 +89,7 @@
      * @throws Exception
      */
     @Override
-    public QuerySiteInfoVo querySiteInfo(Integer id) throws Exception {
+    public QuerySiteInfoVo querySiteInfo(Integer id, String lon, String lat) throws Exception {
         Site site = this.getById(id);
         SiteType siteType = siteTypeService.getById(site.getSiteTypeId());
         Store store = storeService.getById(site.getStoreId());
@@ -82,10 +98,17 @@
         querySiteInfoVo.setName(site.getName());
         querySiteInfoVo.setSiteTypeName(siteType.getName());
         querySiteInfoVo.setStoreName(store.getName());
+        querySiteInfoVo.setStoreCoverDrawing(store.getCoverDrawing());
         querySiteInfoVo.setStoreAddress(store.getAddress());
         querySiteInfoVo.setStoreLon(store.getLon());
         querySiteInfoVo.setStoreLat(store.getLat());
         querySiteInfoVo.setStorePhone(store.getPhone());
+        querySiteInfoVo.setDistance(0D);
+        if(ToolUtil.isNotEmpty(lon) && ToolUtil.isNotEmpty(lat)){
+            Map<String, Double> distance = GeodesyUtil.getDistance(lon + "," + lat, store.getLon() + "," + store.getLat());
+            double wgs84 = new BigDecimal(distance.get("WGS84")).divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+            querySiteInfoVo.setDistance(wgs84);
+        }
         querySiteInfoVo.setCashPrice(site.getCashPrice());
         querySiteInfoVo.setPlayPaiCoin(site.getPlayPaiCoin());
         return querySiteInfoVo;
@@ -130,6 +153,11 @@
             if(null != siteBooking){
                 querySiteTimes.setSelectable(0);
             }
+            int count = siteLockService.count(new QueryWrapper<SiteLock>().eq("siteId", id).last(" and DATE_FORMAT(startTime, '%Y-%m-%d %H:%i') <= '" + day + " " + start + "' and DATE_FORMAT(endTime, '%Y-%m-%d %H:%i') >= '" + day + " " + end + "'"));
+            if(count > 0){
+                querySiteTimes.setSelectable(0);
+            }
+
             list.add(querySiteTimes);
 
             if(e_hour == hour && minute == e_minute){
@@ -138,4 +166,275 @@
         }
         return list;
     }
+
+
+    /**
+     * 预约场地
+     * @param reservationSite
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public ResultUtil reservationSite(Integer uid, ReservationSite reservationSite) throws Exception {
+        Site site = this.getById(reservationSite.getId());
+        AppUser appUser = appUserClient.queryAppUser(uid);
+        String[] split = reservationSite.getTimes().split(";");
+        if(reservationSite.getPayType() == 3){
+            Integer playPaiCoin = site.getPlayPaiCoin() * split.length;
+            if(appUser.getPlayPaiCoins().compareTo(playPaiCoin) < 0){
+                return ResultUtil.error("玩湃币不足");
+            }
+        }
+
+        for (String s : split) {
+            String day = s.split(" ")[0];
+            String time = s.split(" ")[1];
+            List<QuerySiteTimes> querySiteTimes = querySiteTimes(reservationSite.getId(), day);
+            for (QuerySiteTimes querySiteTime : querySiteTimes) {
+                if(querySiteTime.getTime().equals(time) && querySiteTime.getSelectable() == 0){
+                    return ResultUtil.error("【" + s + "】时间段已被使用");
+                }
+            }
+        }
+
+
+        String s_time = split[0];
+        String e_time = split[split.length - 1];
+        s_time = s_time.substring(s_time.lastIndexOf("-"));
+        String[] s1 = e_time.split(" ");
+        e_time = s1[0] + " " + s1[1].split("-")[1];
+        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
+        Double payMoney = 0D;
+        if(reservationSite.getPayType() == 3){
+            payMoney = new BigDecimal(site.getCashPrice()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+        }else{
+            payMoney = new BigDecimal(site.getCashPrice()).multiply(new BigDecimal(split.length)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+        }
+
+        //校验优惠券
+        Long couponId = reservationSite.getCouponId();
+        if(null != couponId && reservationSite.getPayType() != 3){
+            UserCoupon userCoupon = userCouponClient.queryUserCouponById(couponId);
+            if(userCoupon.getStatus() == 2){
+                return ResultUtil.error("优惠券已被核销");
+            }
+            Coupon coupon = couponClient.queryCouponById(userCoupon.getCouponId());
+            long time = coupon.getEndTime().getTime();
+            if(System.currentTimeMillis() >= time){
+                return ResultUtil.error("优惠券已过期");
+            }
+            if(coupon.getType() == 1){//满减
+                JSONObject jsonObject = JSON.parseObject(coupon.getContent());
+                Double num1 = jsonObject.getDouble("num1");
+                Double num2 = jsonObject.getDouble("num2");
+                if(payMoney.compareTo(num1) <= 0){
+                    return ResultUtil.error("该优惠券无法使用");
+                }
+
+                payMoney = new BigDecimal(payMoney).subtract(new BigDecimal(num2)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+            }
+            if(coupon.getType() == 2){//代金券
+                JSONObject jsonObject = JSON.parseObject(coupon.getContent());
+                Double num1 = jsonObject.getDouble("num1");
+                if(payMoney.compareTo(num1) <= 0){
+                    return ResultUtil.error("该优惠券无法使用");
+                }
+                payMoney = new BigDecimal(payMoney).subtract(new BigDecimal(num1)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+            }
+            userCoupon.setStatus(2);
+            userCouponClient.updateUserCoupon(userCoupon);
+        }
+
+
+        SiteBooking siteBooking = new SiteBooking();
+        siteBooking.setOrderNo(sdf.format(new Date()) + UUIDUtil.getNumberRandom(3));
+        siteBooking.setProvince(site.getProvince());
+        siteBooking.setProvinceCode(site.getProvinceCode());
+        siteBooking.setCity(site.getCity());
+        siteBooking.setCityCode(site.getCityCode());
+        siteBooking.setStoreId(site.getStoreId());
+        siteBooking.setSiteId(reservationSite.getId());
+        siteBooking.setAppUserId(uid);
+        siteBooking.setStartTime(sdf1.parse(s_time));
+        siteBooking.setEndTime(sdf1.parse(e_time));
+        siteBooking.setBooker(reservationSite.getBooker());
+        siteBooking.setPhone(reservationSite.getPhone());
+        siteBooking.setPayType(reservationSite.getPayType());
+        siteBooking.setPayMoney(payMoney);
+        siteBooking.setUserCouponId(reservationSite.getCouponId());
+        siteBooking.setStatus(0);
+        siteBooking.setState(1);
+        siteBooking.setInsertTime(new Date());
+        siteBookingService.save(siteBooking);
+
+        if(reservationSite.getPayType() == 1){//微信支付
+            return weChatPaymentSite(uid, payMoney, siteBooking);
+        }
+        if(reservationSite.getPayType() == 2){//支付宝支付
+            return aliPaymentSite(payMoney, siteBooking);
+        }
+        if(reservationSite.getPayType() == 3){//玩湃币支付
+            return playPaiCoinPaymentSite(appUser, payMoney, siteBooking);
+        }
+        return ResultUtil.success();
+    }
+
+
+    /**
+     * 课程微信支付
+     * @param uid
+     * @param paymentPrice
+     * @return
+     * @throws Exception
+     */
+    public ResultUtil weChatPaymentSite(Integer uid, Double paymentPrice, SiteBooking siteBooking) throws Exception{
+        String code = siteBooking.getOrderNo();
+        Integer id = siteBooking.getId();
+        ResultUtil weixinpay = payMoneyUtil.weixinpay("预约场地", "", code, paymentPrice.toString(), "/base/site/weChatPaymentSiteCallback", "APP", "");
+        if(weixinpay.getCode() == 200){
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        int num = 1;
+                        int wait = 0;
+                        while (num <= 10){
+                            int min = 5000;
+                            wait += (min * num);
+                            Thread.sleep(wait);
+                            SiteBooking siteBooking = siteBookingService.getById(id);
+                            if(siteBooking.getStatus() != 0){
+                                break;
+                            }
+                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(siteBooking.getOrderNo(), "");
+                            if(resultUtil.getCode() == 200 && siteBooking.getStatus() == 0){
+                                /**
+                                 * SUCCESS—支付成功,
+                                 * REFUND—转入退款,
+                                 * NOTPAY—未支付,
+                                 * CLOSED—已关闭,
+                                 * REVOKED—已撤销(刷卡支付),
+                                 * USERPAYING--用户支付中,
+                                 * PAYERROR--支付失败(其他原因,如银行返回失败)
+                                 */
+                                Map<String, String> data1 = resultUtil.getData();
+                                String s = data1.get("trade_state");
+                                String transaction_id = data1.get("transaction_id");
+                                if("REFUND".equals(s) || "NOTPAY".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10){
+                                    siteBooking.setState(3);
+                                    siteBookingService.updateById(siteBooking);
+                                    if(null != siteBooking.getUserCouponId()){
+                                        UserCoupon userCoupon = userCouponClient.queryUserCouponById(siteBooking.getUserCouponId());
+                                        userCoupon.setStatus(1);
+                                        userCouponClient.updateUserCoupon(userCoupon);
+                                    }
+                                    break;
+                                }
+                                if("SUCCESS".equals(s)){
+                                    siteBooking.setPayTime(new Date());
+                                    siteBooking.setStatus(1);
+                                    siteBooking.setPayOrderNo(transaction_id);
+                                    siteBookingService.updateById(siteBooking);
+                                    break;
+                                }
+                                if("USERPAYING".equals(s)){
+                                    num++;
+                                }
+                            }
+                        }
+                    }catch (Exception e){
+                        e.printStackTrace();
+                    }
+                }
+            }).start();
+        }
+        return weixinpay;
+    }
+
+
+    /**
+     * 课程支付宝支付
+     * @param paymentPrice
+     * @return
+     * @throws Exception
+     */
+    public ResultUtil aliPaymentSite(Double paymentPrice, SiteBooking siteBooking) throws Exception{
+        String code = siteBooking.getOrderNo();
+        Integer id = siteBooking.getId();
+        ResultUtil alipay = payMoneyUtil.alipay("预约场地", "预约场地", "", code, paymentPrice.toString(), "/base/site/aliPaymentSiteCallback");
+        if(alipay.getCode() == 200){
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        int num = 1;
+                        int wait = 0;
+                        while (num <= 10){
+                            int min = 5000;
+                            wait += (min * num);
+                            Thread.sleep(wait);
+                            SiteBooking siteBooking = siteBookingService.getById(id);
+                            if(siteBooking.getStatus() != 0){
+                                break;
+                            }
+                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryALIOrder(code);
+                            if(resultUtil.getCode() == 200 && siteBooking.getStatus() == 0){
+                                /**
+                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
+                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
+                                 * TRADE_SUCCESS(交易支付成功)、
+                                 * TRADE_FINISHED(交易结束,不可退款)
+                                 */
+                                Map<String, String> data1 = resultUtil.getData();
+                                String s = data1.get("tradeStatus");
+                                String tradeNo = data1.get("tradeNo");
+                                if("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10){
+                                    siteBooking.setState(3);
+                                    siteBookingService.updateById(siteBooking);
+                                    if(null != siteBooking.getUserCouponId()){
+                                        UserCoupon userCoupon = userCouponClient.queryUserCouponById(siteBooking.getUserCouponId());
+                                        userCoupon.setStatus(1);
+                                        userCouponClient.updateUserCoupon(userCoupon);
+                                    }
+                                    break;
+                                }
+                                if("TRADE_SUCCESS".equals(s)){
+                                    siteBooking.setPayTime(new Date());
+                                    siteBooking.setStatus(1);
+                                    siteBooking.setPayOrderNo(tradeNo);
+                                    siteBookingService.updateById(siteBooking);
+                                    break;
+                                }
+                                if("WAIT_BUYER_PAY".equals(s)){
+                                    num++;
+                                }
+                            }
+                        }
+                    }catch (Exception e){
+                        e.printStackTrace();
+                    }
+                }
+            }).start();
+        }
+        return alipay;
+    }
+
+
+    /**
+     * 玩湃币支付课程
+     * @param appUser
+     * @param paymentPrice
+     * @return
+     * @throws Exception
+     */
+    public ResultUtil playPaiCoinPaymentSite(AppUser appUser, Double paymentPrice, SiteBooking siteBooking) throws Exception{
+        Integer playPaiCoins = appUser.getPlayPaiCoins();
+        appUser.setPlayPaiCoins(playPaiCoins - paymentPrice.intValue());
+        appUserClient.updateAppUser(appUser);
+        siteBooking.setPayTime(new Date());
+        siteBooking.setStatus(1);
+        siteBookingService.updateById(siteBooking);
+        return ResultUtil.success();
+    }
 }

--
Gitblit v1.7.1