Pu Zhibing
2025-02-10 4c62b64cc158ed07f18ae7060dae71f9d1e3d9fd
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
package com.ruoyi.integration.barrierGate.server;
 
import com.ruoyi.account.api.feignClient.AppUserCarClient;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserCar;
import com.ruoyi.chargingPile.api.feignClient.ParkingLotClient;
import com.ruoyi.chargingPile.api.feignClient.ParkingRecordClient;
import com.ruoyi.chargingPile.api.model.TParkingLot;
import com.ruoyi.chargingPile.api.model.TParkingRecord;
import com.ruoyi.chargingPile.api.vo.GetParkingRecord;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.integration.barrierGate.model.CloudParkingOrder;
import com.ruoyi.integration.barrierGate.model.CloudParkingSpace;
import com.ruoyi.integration.barrierGate.model.OfflineParkingOrder;
import com.ruoyi.integration.barrierGate.model.OfflineParkingSpace;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.order.api.model.TChargingOrder;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
 
/**
 * @author zhibing.pu
 * @Date 2024/9/5 16:15
 */
@Service
public class ParkingOrderService {
    
    @Resource
    private ParkingRecordClient parkingRecordClient;
    
    @Resource
    private AppUserCarClient appUserCarClient;
    
    @Resource
    private ParkingLotClient parkingLotClient;
    
    @Resource
    private ChargingOrderClient chargingOrderClient;
    
    @Resource
    private AppUserClient appUserClient;
    
    
    
    
    
    /**
     * 云停车入场记录
     * @param order
     */
    public void cloudParkingInOrder(CloudParkingOrder order) {
        TParkingRecord parkingRecord = new TParkingRecord();
        parkingRecord.setLicensePlate(order.getPlate());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = "TC" + Double.valueOf(Math.random() * 1000).intValue() + sdf.format(new Date());
        parkingRecord.setCode(code);
        parkingRecord.setVehicleColor(order.getPlateColor());
        TParkingLot data = parkingLotClient.getParkingLotByAppKey(order.getAppkey()).getData();
        parkingRecord.setName(data.getName());
        parkingRecord.setParkingLotId(data.getId());
        LocalDateTime parse = LocalDateTime.parse(order.getEnterDateTime(), DateTimeFormatter.ISO_DATE_TIME);
        parse = parse.plusHours(8);
        parkingRecord.setInParkingTime(parse);
        parkingRecord.setStatus(1);
        parkingRecord.setCreateTime(LocalDateTime.now());
        parkingRecord.setSerialnumber(order.getId());
        parkingRecordClient.addParkingRecord(parkingRecord);
    }
    
    
    /**
     * 云停车出场记录
     * @param order
     */
    public void cloudParkingOutOrder(CloudParkingOrder order) {
        GetParkingRecord query = new GetParkingRecord();
        query.setLicensePlate(order.getPlate());
        query.setStatus(1);
        TParkingRecord parkingRecord = parkingRecordClient.getParkingRecord(query).getData();
        TParkingLot parkingLot = parkingLotClient.getParkingLotByAppKey(order.getAppkey()).getData();
        LocalDateTime parse = LocalDateTime.parse(order.getLeaveDateTime(), DateTimeFormatter.ISO_DATE_TIME);
        parse = parse.plusHours(8);
        parkingRecord.setOutParkingTime(parse);
        parkingRecord.setParkingDuration(Integer.valueOf(order.getLongTime()));
        parkingRecord.setOrderAmount(new BigDecimal(order.getReceivable()));
        parkingRecord.setPayment(new BigDecimal(order.getPayment()));
        parkingRecord.setPayType(order.getPayType());
        BigDecimal payment = new BigDecimal(order.getPayment());
        parkingRecord.setStatus(BigDecimal.ZERO.compareTo(payment) == 0 ? 2 : 3);
        parkingRecord.setOutParkingType(BigDecimal.ZERO.compareTo(payment) == 0 ? 2 : 1);
        parkingRecord.setTitle("【停车缴费】" + payment + "元");
        TAppUserCar appUserCar = appUserCarClient.getAppUserCarByLicensePlate(parkingRecord.getLicensePlate()).getData();
        TChargingOrder chargingOrder = null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(null != appUserCar){
            List<TChargingOrder> data = chargingOrderClient.getCarChargingOrder(appUserCar.getId(), parkingRecord.getInParkingTime().format(formatter), LocalDateTime.now().format(formatter)).getData();
            if(null != data && data.size() > 0){
                chargingOrder = data.get(0);
            }
        }else if(StringUtils.isNotEmpty(parkingRecord.getLicensePlate())){
            List<TChargingOrder> data = chargingOrderClient.getCarChargingOrderByPlateNum(parkingRecord.getLicensePlate(), parkingRecord.getInParkingTime().format(formatter), LocalDateTime.now().format(formatter)).getData();
            if(null != data && data.size() > 0){
                chargingOrder = data.get(0);
            }
        }
        
        
        if(null != chargingOrder){
            parkingRecord.setAppUserId(chargingOrder.getAppUserId());
            parkingRecord.setChargingOrderId(chargingOrder.getId());
            TAppUser appUser = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
            parkingRecord.setPhone(appUser.getPhone());
            parkingRecord.setFreeDuration(parkingLot.getChargeFreeDuration());
            long m = parkingRecord.getParkingDuration() - parkingRecord.getFreeDuration();
            parkingRecord.setTimeoutAmount(m > parkingLot.getChargeFreeDuration() ? new BigDecimal(m - parkingLot.getChargeFreeDuration()).multiply(parkingLot.getChargeRate()) : BigDecimal.ZERO);
        }else{
            parkingRecord.setFreeDuration(parkingLot.getNonChargeFreeDuration());
            long m = parkingRecord.getParkingDuration() - parkingRecord.getFreeDuration();
            parkingRecord.setTimeoutAmount(m > parkingLot.getNonChargeFreeDuration() ? new BigDecimal(m - parkingLot.getNonChargeFreeDuration()).multiply(parkingLot.getChargeRate()) : BigDecimal.ZERO);
        }
        parkingRecord.setFreeAmount(parkingRecord.getOrderAmount().subtract(parkingRecord.getPayment()));
        parkingRecordClient.updateParkingRecord(parkingRecord);
    }
    
    
    /**
     * 云停车实时车位
     * @param parkingSpace
     */
    public void cloudParkingSpace(CloudParkingSpace parkingSpace){
    
    }
    
    
    /**
     * 线下停车场入场记录
     * @param order
     */
    public void offlineParkingInOrder(OfflineParkingOrder order) {
        TParkingRecord parkingRecord = new TParkingRecord();
        parkingRecord.setLicensePlate(order.getPlatenumber());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = "TC" + Double.valueOf(Math.random() * 1000).intValue() + sdf.format(new Date());
        parkingRecord.setCode(code);
        parkingRecord.setVehicleColor(order.getPlatecolor());
        TParkingLot data = parkingLotClient.getParkingLotByAppKey(order.getAppkey()).getData();
        parkingRecord.setName(data.getName());
        parkingRecord.setParkingLotId(data.getId());
        LocalDateTime parse = LocalDateTime.parse(order.getEntertime(), DateTimeFormatter.ISO_DATE_TIME);
        parse = parse.plusHours(8);
        parkingRecord.setInParkingTime(parse);
        parkingRecord.setStatus(1);
        parkingRecord.setCreateTime(LocalDateTime.now());
        parkingRecord.setSerialnumber(order.getId());
        parkingRecordClient.addParkingRecord(parkingRecord);
    }
    
    
    /**
     * 线下停车场出场记录
     * @param order
     */
    public void offlineParkingOutOrder(OfflineParkingOrder order) {
        GetParkingRecord query = new GetParkingRecord();
        query.setLicensePlate(order.getPlatenumber());
        query.setStatus(1);
        TParkingRecord parkingRecord = parkingRecordClient.getParkingRecord(query).getData();
        TParkingLot parkingLot = parkingLotClient.getParkingLotByAppKey(order.getAppkey()).getData();
        parkingRecord.setOutParkingTime(LocalDateTime.now());
        parkingRecord.setParkingDuration(Integer.valueOf(order.getLongTime()));
        parkingRecord.setOrderAmount(new BigDecimal(order.getMoney()));
        parkingRecord.setPayment(new BigDecimal(order.getMoney()));
        BigDecimal payment = new BigDecimal(order.getMoney());
        parkingRecord.setStatus(BigDecimal.ZERO.compareTo(payment) == 0 ? 2 : 3);
        parkingRecord.setOutParkingType(BigDecimal.ZERO.compareTo(payment) == 0 ? 2 : 1);
        parkingRecord.setTitle("【停车缴费】" + payment + "元");
        TAppUserCar appUserCar = appUserCarClient.getAppUserCarByLicensePlate(parkingRecord.getLicensePlate()).getData();
        TChargingOrder chargingOrder = null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(null != appUserCar){
            List<TChargingOrder> data = chargingOrderClient.getCarChargingOrder(appUserCar.getId(), parkingRecord.getInParkingTime().format(formatter), LocalDateTime.now().format(formatter)).getData();
            if(null != data && data.size() > 0){
                chargingOrder = data.get(0);
            }
        }else if(StringUtils.isNotEmpty(parkingRecord.getLicensePlate())){
            List<TChargingOrder> data = chargingOrderClient.getCarChargingOrderByPlateNum(parkingRecord.getLicensePlate(), parkingRecord.getInParkingTime().format(formatter), LocalDateTime.now().format(formatter)).getData();
            if(null != data && data.size() > 0){
                chargingOrder = data.get(0);
            }
        }
        
        if(null != chargingOrder){
            parkingRecord.setAppUserId(chargingOrder.getAppUserId());
            parkingRecord.setChargingOrderId(chargingOrder.getId());
            TAppUser appUser = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
            parkingRecord.setPhone(appUser.getPhone());
            parkingRecord.setFreeDuration(parkingLot.getChargeFreeDuration());
            long m = parkingRecord.getParkingDuration() - parkingRecord.getFreeDuration();
            parkingRecord.setTimeoutAmount(m > parkingLot.getChargeFreeDuration() ? new BigDecimal(m - parkingLot.getChargeFreeDuration()).multiply(parkingLot.getChargeRate()) : BigDecimal.ZERO);
            
        }else{
            parkingRecord.setFreeDuration(parkingLot.getNonChargeFreeDuration());
            long m = parkingRecord.getParkingDuration() - parkingRecord.getFreeDuration();
            parkingRecord.setTimeoutAmount(m > parkingLot.getNonChargeFreeDuration() ? new BigDecimal(m - parkingLot.getNonChargeFreeDuration()).multiply(parkingLot.getChargeRate()) : BigDecimal.ZERO);
        }
        
        parkingRecord.setFreeAmount(parkingRecord.getOrderAmount().subtract(parkingRecord.getPayment()));
        parkingRecordClient.updateParkingRecord(parkingRecord);
    }
    
    
    /**
     * 线下停车场实时车位
     * @param parkingSpace
     */
    public void offlineParkingSpace(OfflineParkingSpace parkingSpace){
    
    }
}