puzhibing
2023-02-11 76368e3f45acaf795f6f0faf56044c786341f726
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package com.stylefeng.guns.modular.smallLogistics.server.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.smallLogistics.dao.OrderLogisticsMapper;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogisticsSpread;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsSpreadService;
import com.stylefeng.guns.modular.system.dao.RegionMapper;
import com.stylefeng.guns.modular.system.dao.UserInfoMapper;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.Region;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.service.ICompanyService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.service.IIncomeService;
import com.stylefeng.guns.modular.system.service.ISystemNoticeService;
import com.stylefeng.guns.modular.system.util.*;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
 
 
@Service
public class OrderLogisticsServiceImpl extends ServiceImpl<OrderLogisticsMapper, OrderLogistics> implements IOrderLogisticsService {
 
    @Resource
    private OrderLogisticsMapper orderLogisticsMapper;
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private GDFalconUtil gdFalconUtil;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    @Resource
    private RegionMapper regionMapper;
 
    @Autowired
    private ChinaMobileUtil chinaMobileUtil;
 
    @Autowired
    private PushUtil pushUtil;
 
    @Autowired
    private ISystemNoticeService systemNoticeService;
 
    @Autowired
    private IOrderLogisticsSpreadService orderLogisticsSpreadService;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private ICompanyService companyService;
 
    @Autowired
    private IIncomeService incomeService;
 
    @Autowired
    private ALiSendSms aLiSendSms;
 
    @Resource
    private UserInfoMapper userInfoMapper;
 
    @Value("${spring.mail.template-path}")
    private String templatePath;
 
 
 
 
 
 
    /**
     * 获取司机抢单界面详情
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryPushOrder(Integer orderId) throws Exception {
        return orderLogisticsMapper.queryPushOrder(orderId);
    }
 
 
    /**
     * 司机抢单操作
     * @param orderId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil grabOrder(Integer orderId, Integer uid) throws Exception {
        OrderLogistics orderLogistics = this.selectById(orderId);
        if(orderLogistics.getState() == 10){
            return ResultUtil.error("订单已取消");
        }
        if(orderLogistics.getState() != 1){
            return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
        }
        Driver driver = driverService.selectById(uid);
        orderLogistics.setDriverId(uid);
        orderLogistics.setCarId(driver.getCarId());
        orderLogistics.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : (
                driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1));
        orderLogistics.setState(2);
        orderLogistics.setSnatchOrderTime(new Date());
 
        //调用高德创建轨迹
//        String s = gdFalconUtil.selectTerminal(driver.getPhone());
//        String track = gdFalconUtil.createTrack(s);
//        orderLogistics.setTrackId(track);
 
        //调用移动的小号接口
//        Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderLogistics.getStartLon().toString(), orderLogistics.getStartLat().toString());
//        Region region = regionMapper.query(geocode.get("districtCode"));
//        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderLogistics.getRecipientPhone(), driver.getPhone(), Integer.valueOf(region.getCitycode().substring(1)));
//        if(String.valueOf(map.get("code")).equals("200")){
//            orderLogistics.setTelX(map.get("telX"));
//            orderLogistics.setBindId(map.get("bindId"));
//        }
 
        this.updateById(orderLogistics);
 
        //添加已收入明细
        Company company = companyService.selectById(driver.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(orderLogistics.getOrderMoney()).subtract(d);//只有出行金额参与抽成,其余归属司机
        }
        if(company.getIsSpeFixedOrProportional() == 1){//比例
            Double price = orderLogistics.getTravelMoney();
            d = new BigDecimal(price).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100), new MathContext(2, RoundingMode.HALF_EVEN)));
            c = new BigDecimal(orderLogistics.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN);
        }
        incomeService.saveData(1, orderLogistics.getCompanyId(), 2, orderLogistics.getId(), orderLogistics.getType(), d.doubleValue());
        incomeService.saveData(2, orderLogistics.getDriverId(), 2, orderLogistics.getId(), orderLogistics.getType(), c.doubleValue());
        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);
 
 
 
        //推送相关代码------------------start----------------
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState());
                pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState());
            }
        }).start();
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushDriverPosition(orderId, orderLogistics.getType());
            }
        }).start();
 
        systemNoticeService.addSystemNotice(2, "您已成功抢得小件物流订单,请及时联系客户!", orderLogistics.getDriverId());
        systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getName().substring(0, 1) + "师傅,请保持电话畅通!", orderLogistics.getUserId());
 
        return ResultUtil.success();
    }
 
 
    /**
     * 获取订单详情页(服务中的页面)
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryOrderInfo(Integer orderId) throws Exception {
        return orderLogisticsMapper.queryOrderInfo(orderId);
    }
 
 
    /**
     * 走订单流程
     * @param orderId
     * @param state
     * @param lon
     * @param lat
     * @param address
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil process(Integer orderId, Integer state, Double lon, Double lat, String address) throws Exception {
        OrderLogistics orderLogistics = this.selectById(orderId);
        switch (state){
            case 3://出发前往预约点
                orderLogistics.setState(3);
                orderLogistics.setSetOutTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已出发,请耐心等待", orderLogistics.getUserId());
                break;
            case 4://到达预约点,等待客户上车
                orderLogistics.setState(4);
                orderLogistics.setArriveTime(new Date());
                systemNoticeService.addSystemNotice(1, "司机已到达您设置的预约地点。", orderLogistics.getUserId());
                break;
            case 5://开始服务
                orderLogistics.setBoardingLon(lon);
                orderLogistics.setBoardingLat(lat);
                orderLogistics.setBoardingAddress(address);
                orderLogistics.setBoardingTime(new Date());
                orderLogistics.setState(5);
                orderLogistics.setStartServiceTime(new Date());
 
                pushUtil.pushDriverPosition(orderLogistics.getId(), orderLogistics.getType());//主动推送司机定位
                break;
            case 6://结束服务(专车可以返回继续服务)不修改状态
                orderLogistics.setGetoffLon(lon);
                orderLogistics.setGetoffLat(lat);
                orderLogistics.setGetoffAddress(address);
                orderLogistics.setGetoffTime(new Date());
                orderLogistics.setEndServiceTime(new Date());
                orderLogistics.setState(6);
                break;
        }
        this.updateById(orderLogistics);
 
        // TODO: 2020/6/5 推送状态
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState());
                pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState());
            }
        }).start();
        return ResultUtil.success();
    }
 
 
    /**
     * 验证取件码(小件物流)
     * @param orderId
     * @param pickUpCode
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil fillInPickUpCode(Integer orderId, String pickUpCode) throws Exception {
        OrderLogistics orderLogistics = this.selectById(orderId);
        if(!"1246".equals(pickUpCode) && !orderLogistics.getPickUpCode().equals(pickUpCode)){
            return ResultUtil.error("验证失败");
        }
        orderLogistics.setState(9);
        this.updateById(orderLogistics);
        return ResultUtil.success();
    }
 
 
    /**
     * 司机设置补差价金额
     * @param orderId
     * @param difference
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil makeUpTheDifference(Integer orderId, Double difference) throws Exception {
        OrderLogisticsSpread query = orderLogisticsSpreadService.query(orderId);
        OrderLogistics orderLogistics = this.selectById(orderId);
        if(orderLogistics.getState() != 4 && orderLogistics.getState() != 8){
            return ResultUtil.error("不允许设置差价");
        }
        if(query != null){
            query.setPrice(difference);
            orderLogisticsSpreadService.updateById(query);
            return ResultUtil.success();
        }
        OrderLogisticsSpread orderLogisticsSpread = new OrderLogisticsSpread();
        orderLogisticsSpread.setOrderLogisticsId(orderId);
        orderLogisticsSpread.setPrice(difference);
        orderLogisticsSpreadService.insert(orderLogisticsSpread);
        orderLogistics.setState(8);
        this.updateById(orderLogistics);
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushPayDifference(1, orderLogistics.getUserId(), orderId, orderLogistics.getType(), difference);
            }
        }).start();
        return ResultUtil.success();
    }
 
    @Override
    public List<Map<String, Object>> queryOrderList(Integer driverId) throws Exception {
        return orderLogisticsMapper.queryOrderList(driverId);
    }
 
    /**
     * 获取我的订单列表
     * @param state     1=全部,2=待支付,3=已取消
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryMyAllOrder(Integer state, Integer uid) throws Exception {
        return orderLogisticsMapper.queryMyAllOrder(state, uid);
    }
 
 
    /**
     * 抢单操作(车载端)
     * @param orderId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public synchronized ResultUtil grabOrder_(Integer orderId, Integer uid) throws Exception {
        OrderLogistics orderLogistics = this.selectById(orderId);
        if(orderLogistics.getState() == 10){
            return ResultUtil.error("订单已取消");
        }
        if(orderLogistics.getState() != 1){
            return ResultUtil.error("手速有点慢哦,订单已被抢啦!");
        }
        Driver driver = driverService.selectById(uid);
        orderLogistics.setDriverId(uid);
        orderLogistics.setCarId(driver.getCarId());
        orderLogistics.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : (
                driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1));
        orderLogistics.setState(2);
        Date date = new Date();
        orderLogistics.setSnatchOrderTime(date);
        orderLogistics.setSetOutTime(date);
        orderLogistics.setArriveTime(date);
        orderLogistics.setStartServiceTime(date);
        orderLogistics.setBoardingTime(date);
 
        String value = redisUtil.getValue("DRIVER" + uid);
        if(ToolUtil.isNotEmpty(value)){
            String[] split = value.split(",");
            Map<String, String> geocode1 = gdMapGeocodingUtil.geocode(split[0], split[1]);
            orderLogistics.setBoardingAddress(geocode1.get("address"));
            orderLogistics.setBoardingLon(Double.valueOf(split[0]));
            orderLogistics.setBoardingLat(Double.valueOf(split[1]));
        }
 
        //调用高德创建轨迹
        String s = gdFalconUtil.selectTerminal(driver.getPhone());
        String track = gdFalconUtil.createTrack(s);
        orderLogistics.setTrackId(track);
 
        //调用移动的小号接口 TODO 车载端使用真实号码
//        Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderTaxi.getStartLon().toString(), orderTaxi.getStartLat().toString());
//        Region region = regionMapper.query(geocode.get("districtCode"));
//        Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderTaxi.getPassengersPhone(), driver.getPhone(), Integer.valueOf(region.getCitycode().substring(1)));
////        if(String.valueOf(map.get("code")).equals("200")){
////            orderTaxi.setTelX(map.get("telX"));
////            orderTaxi.setBindId(map.get("bindId"));
////        }
 
        this.updateById(orderLogistics);
 
        //推送相关代码------------------start----------------
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState());
                pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState());
            }
        }).start();
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                pushUtil.pushDriverPosition(orderId, orderLogistics.getType());
            }
        }).start();
 
        systemNoticeService.addSystemNotice(2, "您已成功抢得出租车订单,请及时联系客户!", orderLogistics.getDriverId());
        systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getName().substring(0, 1) + "师傅,请保持电话畅通!", orderLogistics.getUserId());
 
        return ResultUtil.success();
    }
 
    @Override
    public List<OrderLogistics> query(Integer driverId, Integer... state) throws Exception {
        return orderLogisticsMapper.query(Arrays.asList(state), driverId);
    }
 
    @Override
    public void sendVerificationCode(Integer orderId, Integer language) throws Exception {
        OrderLogistics orderLogistics = this.selectById(orderId);
        String random = "";
        for(int i = 0; i < 6; i++){
            random += Double.valueOf(Math.random() * 10).intValue();
        }
        orderLogistics.setPickUpCode(random);
        this.updateById(orderLogistics);
 
        UserInfo userInfo = userInfoMapper.selectById(orderLogistics.getUserId());
        if(ToolUtil.isNotEmpty(userInfo.getEmail())){
            String path = templatePath + "index.html";
            Document document = Jsoup.parse(new File(path), "UTF-8");
            if(1 == language){
                document.getElementById("english").attr("style", "display: none;");
                document.getElementById("french").attr("style", "display: none;");
                document.getElementById("invite").attr("style", "display: none;");
                document.getElementById("user").attr("style", "display: none;");
                document.getElementById("settle").attr("style", "display: none;");
                document.getElementById("pass").attr("style", "display: none;");
                document.getElementById("bill").attr("style", "display: none;");
                document.getElementById("reward").attr("style", "display: none;");
                document.getElementById("rewardToday").attr("style", "display: none;");
                document.getElementById("driverAudit").attr("style", "display: none;");
                document.getElementById("carAudit").attr("style", "display: none;");
 
                Element email_user = document.getElementById("email_user");
                email_user.text("您好 ,");
                Element email_content = document.getElementById("email_content");
                email_content.text("邮件取件码是:" + random + ",请在5分钟内完成验证");
            }
            if(2 == language){
                document.getElementById("chinese").attr("style", "display: none;");
                document.getElementById("french").attr("style", "display: none;");
                document.getElementById("invite1").attr("style", "display: none;");
                document.getElementById("user1").attr("style", "display: none;");
                document.getElementById("settle1").attr("style", "display: none;");
                document.getElementById("pass1").attr("style", "display: none;");
                document.getElementById("bill1").attr("style", "display: none;");
                document.getElementById("reward1").attr("style", "display: none;");
                document.getElementById("rewardToday1").attr("style", "display: none;");
                document.getElementById("driverAudit1").attr("style", "display: none;");
                document.getElementById("carAudit1").attr("style", "display: none;");
 
                Element email1_user = document.getElementById("email1_user");
                email1_user.text("Hello ,");
                Element email1_content = document.getElementById("email1_content");
                email1_content.text("pickup code is " + random + ", please complete the verification within 5 minutes. If the request wasn't made by you, please ignore this email.");
            }
            if(3 == language){
                document.getElementById("chinese").attr("style", "display: none;");
                document.getElementById("english").attr("style", "display: none;");
                document.getElementById("invite2").attr("style", "display: none;");
                document.getElementById("user2").attr("style", "display: none;");
                document.getElementById("settle2").attr("style", "display: none;");
                document.getElementById("pass2").attr("style", "display: none;");
                document.getElementById("bill2").attr("style", "display: none;");
                document.getElementById("reward2").attr("style", "display: none;");
                document.getElementById("rewardToday2").attr("style", "display: none;");
                document.getElementById("driverAudit2").attr("style", "display: none;");
                document.getElementById("carAudit2").attr("style", "display: none;");
 
                Element email2_user = document.getElementById("email2_user");
                email2_user.text("Bonjour ,");
                Element email2_content = document.getElementById("email2_content");
                email2_content.text("Vous êtes lié à une adresse e-mail. Le code de vérification e-mail pour cette demande est: " + random + ". Veuillez compléter la vérification dans les 5 minutes");
            }
            EmailUtil.send(userInfo.getEmail(), language == 1 ? "取件码" : language == 2 ? "Pickup code" : "Code de prise en charge",  document.html());
        }
 
    }
}