yanghb
2023-04-10 b2f678ad387ca24e05a11100ea4583f0f2f730f0
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.ServerCarModelMapper;
import com.stylefeng.guns.modular.system.dao.SystemPriceMapper;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.OpenCity;
import com.stylefeng.guns.modular.system.model.OpenCityBusiness;
import com.stylefeng.guns.modular.system.model.ServerCarModel;
import com.stylefeng.guns.modular.system.service.ICompanyCityService;
import com.stylefeng.guns.modular.system.service.IOpenCityBusinessService;
import com.stylefeng.guns.modular.system.service.IOpenCityService;
import com.stylefeng.guns.modular.system.service.IServerCarModelService;
import com.stylefeng.guns.modular.system.util.GDMapElectricFenceUtil;
import com.stylefeng.guns.modular.system.util.GDMapGeocodingUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.warpper.ServerCarModelWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
 
@Service
public class ServerCarModelServiceImpl extends ServiceImpl<ServerCarModelMapper, ServerCarModel> implements IServerCarModelService {
 
    @Resource
    private ServerCarModelMapper serverCarModelMapper;
 
    @Resource
    private SystemPriceMapper systemPriceMapper;
 
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    @Autowired
    private ICompanyCityService companyCityService;
 
    @Autowired
    private IOpenCityService openCityService;
 
    @Autowired
    private IOpenCityBusinessService openCityBusinessService;
 
 
 
 
 
 
    /**
     * 根据起点和终点获取车型
     * @param startLonLat
     * @param endLonLat
     * @param type          业务类型
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil<List<ServerCarModelWarpper>> queryServerCarModel(String startLonLat, String endLonLat, Integer type) throws Exception {
        //查找与起点匹配的企业(经营范围)
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(startLonLat.split(",")[0], startLonLat.split(",")[1]);
        Company query = companyCityService.query(geocode.get("districtCode"));
        if(null == query){
            return ResultUtil.error("起点暂无企业提供服务", new ArrayList<>());
        }
        if(type == 1 && query.getIsSpe() == 2){
            return ResultUtil.success(new ArrayList<>());
        }
        OpenCity openCity = openCityService.openCity1(geocode.get("districtCode"));
        if(null != openCity){
            OpenCityBusiness openCityBusiness = openCityBusinessService.selectOne(new EntityWrapper<OpenCityBusiness>().eq("businessType", 1).eq("openCityId", openCity.getId()).ne("flag", 3));
            if(null == openCityBusiness){
                return ResultUtil.success(new ArrayList<>());
            }
        }
 
        Map<String, String> distance = gdMapElectricFenceUtil.getDistance(startLonLat, endLonLat, 1);
        if(null == distance){
            return ResultUtil.error("获取预估距离出错", new ArrayList<>());
        }
        String distance1 = distance.get("distance");//距离(米)
        double distance1_ = Double.valueOf(distance1).doubleValue();
        String duration = distance.get("duration");//时间(秒)
        long duration_ = Long.valueOf(duration).longValue();
 
        List<ServerCarModelWarpper> price = this.getPrice(query.getId(), distance1_, duration_, 0, 1);
//        if(price.size() == 0){
//            return ResultUtil.error("未获取到可服务的车型", new ArrayList<>());
//        }
        return ResultUtil.success(price);
    }
 
 
    /**
     * 获取业务类型对应的所有服务车型
     * @param type
     * @return
     */
    @Override
    public List<Map<String, Object>> queryServerCarModels(Integer type) {
        return serverCarModelMapper.queryServerCarModel(type);
    }
 
 
    /**
     * 计算价格
     * @param companyId
     * @param distance
     * @param duration
     * @param wait
     * @param type
     * @return
     * @throws Exception
     */
    public List<ServerCarModelWarpper> getPrice(Integer companyId, double distance, long duration, long wait, Integer type) throws Exception {
        List<Map<String, Object>> list = serverCarModelMapper.queryServerCarModel(type);
        List<ServerCarModelWarpper> data = new ArrayList<>();
        for(Map<String, Object> map : list){
            Map<String, Object> query1 = systemPriceMapper.query(companyId, type, Integer.valueOf(map.get("id").toString()));
            if(null == query1){//排除没有设置价格的车型
                continue;
            }
            //开始根据不同的方式计算金额
            double amount = 0;
            if(type == 1){//专车
 
                double d = distance / 1000;//实际公里
                double t = duration / 60;//实际时间
                JSONObject jsonObject = JSON.parseObject(query1.get("content").toString());//等待费
                Double waitMoney = jsonObject.getDouble("num2");
                JSONObject contentPutOne = JSON.parseObject(query1.get("contentPutOne").toString());//一人拼成
                JSONObject contentNotOne = JSON.parseObject(query1.get("contentNotOne").toString());//一人未拼成
                JSONObject contentPutTwo = JSON.parseObject(query1.get("contentPutTwo").toString());//2人拼成
                JSONObject contentNotTwo = JSON.parseObject(query1.get("contentNotTwo").toString());//2人未拼成
                JSONObject contentPutThree = JSON.parseObject(query1.get("contentPutThree").toString());//3人拼成
                JSONObject contentNotThree = JSON.parseObject(query1.get("contentNotThree").toString());//3人未拼成
                JSONObject contentExclusive = JSON.parseObject(query1.get("contentExclusive").toString());//独享
                JSONObject contentPrice = JSON.parseObject(query1.get("contentPrice").toString());//一口价
                //一口价
                ServerCarModelWarpper serverCarModelWarpper = ServerCarModelWarpper.getServerCarModelWarpper(map);
                serverCarModelWarpper.setFareTypeNote1(query1.get("fareTypeNote1").toString());
                serverCarModelWarpper.setFareTypeNote2(query1.get("fareTypeNote2").toString());
                serverCarModelWarpper.setFareTypeNote3(query1.get("fareTypeNote3").toString());
                serverCarModelWarpper.setContentPutOne(contentPutOne);
                serverCarModelWarpper.setContentNotOne(contentNotOne);
 
                serverCarModelWarpper.setContentPutTwo(contentPutTwo);
                serverCarModelWarpper.setContentNotTwo(contentNotTwo);
 
                serverCarModelWarpper.setContentPutThree(contentPutThree);
                serverCarModelWarpper.setContentNotThree(contentNotThree);
 
 
                serverCarModelWarpper.setContentExclusive(contentExclusive);
                serverCarModelWarpper.setContentPrice(contentPrice);
 
 
 
 
                serverCarModelWarpper.setAmount(new BigDecimal(this.calculationPrice(contentExclusive,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setBuyNowAmount(new BigDecimal(this.calculationPrice(contentPrice,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setPutTogetherOneAmount(new BigDecimal(this.calculationPrice(contentPutOne,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setNotAssembledOneAmount(new BigDecimal(this.calculationPrice(contentNotOne,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setPutTogetherTwoAmount(new BigDecimal(this.calculationPrice(contentPutTwo,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setNotAssembledTwoAmount(new BigDecimal(this.calculationPrice(contentNotTwo,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setPutTogetherThreeAmount(new BigDecimal(this.calculationPrice(contentPutThree,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setNotAssembledThreeAmount(new BigDecimal(this.calculationPrice(contentNotThree,distance,duration,wait,waitMoney)).setScale(BigDecimal.ROUND_HALF_EVEN, 2).doubleValue());
                serverCarModelWarpper.setMileage(d);
                serverCarModelWarpper.setDuration(t);
                serverCarModelWarpper.setIsOpen(Integer.parseInt(query1.get("isOpen").toString()));
                serverCarModelWarpper.setIsOpenExclusive(Integer.parseInt(query1.get("isOpenExclusive").toString()));
                serverCarModelWarpper.setIsOpenPrice(Integer.parseInt(query1.get("isOpenPrice").toString()));
                data.add(serverCarModelWarpper);
 
 
 
 
 
 
 
 
            }
        }
        return data;
    }
    public Double  calculationPrice(JSONObject rule, double distance, long duration, long wait,Double waitMoney) throws Exception {
        double amount = 0;
        //等待费
        Date date = new Date();
        double d = distance / 1000;//实际公里
        double t = duration / 60;//实际时间
        double d1 = (d - rule.getDouble("num2")) < 0 ? 0 : d - rule.getDouble("num2");//超出起步里程的公里
        double t1 = (t - rule.getDouble("num3")) < 0 ? 0 : t - rule.getDouble("num3");//超过起步分钟数的时间
        double yt1 = 0;//远途1段
        double yt2 = 0;//远途2段
        double yt3 = 0;//远途3段
 
 
        //夜间服务处理逻辑
        Calendar s = Calendar.getInstance();
        s.setTime(date);
        s.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num14").split(" - ")[0].split(":")[0]));
        s.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num14").split(" - ")[0].split(":")[1]));
 
        Calendar e = Calendar.getInstance();
        e.setTime(date);
        e.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num14").split(" - ")[1].split(":")[0]));
        e.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num14").split(" - ")[1].split(":")[1]));
 
        if(date.getTime() > s.getTimeInMillis() && date.getTime() < e.getTimeInMillis()){
            if(d > rule.getDouble("num6")){
                yt1 = rule.getDouble("num18") * (rule.getDouble("num7") - rule.getDouble("num6"));
            }
            if(d >  rule.getDouble("num9")){
                yt2 = rule.getDouble("num19") * (rule.getDouble("num10") - rule.getDouble("num9"));
            }
            if(d > rule.getDouble("num12")){
                yt3 = rule.getDouble("num20") * (d - rule.getDouble("num12"));
            }
            amount = rule.getDouble("num15") + (d1 * rule.getDouble("num16")) + (t1 * rule.getDouble("num17")) + (wait * waitMoney) + yt1 + yt2 + yt3;
        }else{
            Calendar s1 = Calendar.getInstance();
            s1.setTime(date);
            s1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num21").split(" - ")[0].split(":")[0]));
            s1.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num21").split(" - ")[0].split(":")[1]));
 
            Calendar e1 = Calendar.getInstance();
            e1.setTime(date);
            e1.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num21").split(" - ")[1].split(":")[0]));
            e1.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num21").split(" - ")[1].split(":")[1]));
 
            Calendar s2 = Calendar.getInstance();
            s2.setTime(date);
            s2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num22").split(" - ")[0].split(":")[0]));
            s2.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num22").split(" - ")[0].split(":")[1]));
 
            Calendar e2 = Calendar.getInstance();
            e2.setTime(date);
            e2.set(Calendar.HOUR_OF_DAY, Integer.valueOf(rule.getString("num22").split(" - ")[1].split(":")[0]));
            e2.set(Calendar.MINUTE, Integer.valueOf(rule.getString("num22").split(" - ")[1].split(":")[1]));
            //高峰时段处理逻辑
            if((date.getTime() > s1.getTimeInMillis() && date.getTime() < e1.getTimeInMillis()) || (date.getTime() > s2.getTimeInMillis() && date.getTime() < e2.getTimeInMillis())){
                if(d > rule.getDouble("num6")){
                    yt1 = rule.getDouble("num26") * (rule.getDouble("num7") - rule.getDouble("num6"));
                }
                if(d >  rule.getDouble("num9")){
                    yt2 = rule.getDouble("num27") * (rule.getDouble("num10") - rule.getDouble("num9"));
                }
                if(d > rule.getDouble("num12")){
                    yt3 = rule.getDouble("num28") * (d - rule.getDouble("num12"));
                }
                amount = rule.getDouble("num23") + (d1 * rule.getDouble("num24")) + (t1 * rule.getDouble("num25")) + (wait * waitMoney) + yt1 + yt2 + yt3;
            }else{
                //其他时间段的计算
                if(d > rule.getDouble("num6")){
                    yt1 = rule.getDouble("num8") * (rule.getDouble("num7") - rule.getDouble("num6"));
                }
                if(d >  rule.getDouble("num9")){
                    yt2 = rule.getDouble("num11") * (rule.getDouble("num10") - rule.getDouble("num9"));
                }
                if(d > rule.getDouble("num12")){
                    yt3 = rule.getDouble("num13") * (d - rule.getDouble("num12"));
                }
                amount = rule.getDouble("num1") + (d1 * rule.getDouble("num4")) + (t1 * rule.getDouble("num5")) + (wait * waitMoney) + yt1 + yt2 + yt3;
            }
        }
        return amount;
    }
}