luodangjia
2024-10-18 06ea0fe23faf2238d9d1141826ecb404367aecec
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
package com.ruoyi.integration.barrierGate.server;
 
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.integration.barrierGate.model.GetCouponReq;
import com.ruoyi.integration.barrierGate.model.GetCouponResp;
import com.ruoyi.integration.barrierGate.model.Parameter;
import com.ruoyi.integration.barrierGate.model.UsedCoupon;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.vo.GetChargingOrderByLicensePlate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
 
/**
 * @author zhibing.pu
 * @Date 2024/9/5 11:11
 */
@Service
public class CouponService {
    
    @Resource
    private ParkingLotClient parkingLotClient;
    
    @Resource
    private ParkingRecordClient parkingRecordClient;
    
    @Resource
    private ChargingOrderClient chargingOrderClient;
    
    
    
    
    
    /**
     * 获取优惠券
     * @param req
     * @return
     */
    public GetCouponResp getCoupon(GetCouponReq req){
        //根据车牌查询入场时间,后再根据入场时间和车牌查询是否有充电订单
        String carNumber = req.getCarNumber();
        String appKey = req.getAppKey();
        TParkingLot parkingLot = parkingLotClient.getParkingLotByAppKey(appKey).getData();
        if(null == parkingLot){
            return null;
        }
        GetParkingRecord query = new GetParkingRecord();
        query.setStatus(1);
        query.setLicensePlate(carNumber);
        TParkingRecord data = parkingRecordClient.getParkingRecord(query).getData();
        if(null == data){
            return null;
        }
        //入场时间
        LocalDateTime inParkingTime = data.getInParkingTime();
        //开始查询充电订单
        GetChargingOrderByLicensePlate getChargingOrderByLicensePlate = new GetChargingOrderByLicensePlate();
        getChargingOrderByLicensePlate.setLicensePlate(carNumber);
        getChargingOrderByLicensePlate.setStartTime(inParkingTime);
        TChargingOrder tChargingOrder = chargingOrderClient.getChargingOrderByLicensePlate(getChargingOrderByLicensePlate).getData();
        
        GetCouponResp resp = new GetCouponResp();
        resp.setRecordId(data.getId());
        resp.setCarNumber(carNumber);
        resp.setSysOrgId("MXCD7QjHapTJGZ8TZZz3");
        resp.setPosName("明星电力");
        resp.setLssuer("admin");
        resp.setType(4);
        Parameter parameter = new Parameter();
        parameter.setFreeFlag(false);
        if(null == tChargingOrder){
            //按照非充电进行优惠计算
            resp.setCouponName("普通停车时长优惠");
            parameter.setTime(parkingLot.getNonChargeFreeDuration());
            data.setFreeDuration(parkingLot.getNonChargeFreeDuration());
            parkingRecordClient.updateParkingRecord(data);
        }else{
            resp.setCouponName("充电停车时长优惠");
            parameter.setTime(parkingLot.getChargeFreeDuration());
            data.setFreeDuration(parkingLot.getNonChargeFreeDuration());
            data.setChargingOrderId(tChargingOrder.getId());
            data.setAppUserId(tChargingOrder.getAppUserId());
            parkingRecordClient.updateParkingRecord(data);
        }
        resp.setParameter(parameter);
        return resp;
    }
    
    
    /**
     * 使用优惠券
     * @param req
     */
    public void usedCoupon(UsedCoupon req){
        TParkingRecord data = parkingRecordClient.getParkingRecordById(Long.valueOf(req.getRecordId())).getData();
        data.setSerialnumber(req.getSerialnumber());
        data.setFreeAmount(new BigDecimal(req.getActualFee()));
        data.setOrderAmount(new BigDecimal(req.getFee()));
        BigDecimal subtract = data.getOrderAmount().subtract(data.getFreeAmount());
        if(BigDecimal.ZERO.compareTo(subtract) < 0){
            data.setTimeoutAmount(subtract);
            data.setOutParkingType(1);
            data.setStatus(3);
        }else{
            data.setTimeoutAmount(BigDecimal.ZERO);
            data.setOutParkingType(2);
            data.setStatus(2);
        }
        parkingRecordClient.updateParkingRecord(data);
    }
}