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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.stylefeng.guns.modular.system.service.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.system.dao.OrderCrossCityMapper;
import com.stylefeng.guns.modular.system.dao.OrderPrivateCarMapper;
import com.stylefeng.guns.modular.system.dao.OrderTaxiMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.*;
import com.stylefeng.guns.modular.system.warpper.BaseWarpper;
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.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service
public class OrderTaxiServiceImpl extends ServiceImpl<OrderTaxiMapper, OrderTaxi> implements IOrderTaxiService {
 
    @Autowired
    private IDispatchService dispatchService;
 
    @Autowired
    private IOrderCancelService orderCancelService;
 
    @Autowired
    private ISystemNoticeService systemNoticeService;
 
    @Autowired
    private PushUtil pushUtil;
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private PushMinistryOfTransportUtil pushMinistryOfTransportUtil;
 
    @Autowired
    private ICompanyCityService companyCityService;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    @Autowired
    private IDriverServiceService driverServiceService;
 
    @Autowired
    private ChinaMobileUtil chinaMobileUtil;
 
    @Autowired
    private IUserInfoService userInfoService;
 
    @Resource
    private OrderPrivateCarMapper orderPrivateCarMapper;
 
    @Resource
    private OrderTaxiMapper orderTaxiMapper;
 
    @Resource
    private OrderCrossCityMapper orderCrossCityMapper;
 
    @Value("${pushMinistryOfTransport}")
    private boolean pushMinistryOfTransport;
 
 
 
    @Override
    public List<Map<String, Object>> queryOrderList(String search, String orderSource, String state, Integer pageNum, Integer size, Integer uid) throws Exception {
        Dispatch dispatch = dispatchService.selectById(uid);
        Integer companyId = null != dispatch.getFranchiseeId() ? dispatch.getFranchiseeId() : dispatch.getCompanyId();
        pageNum = (pageNum - 1) * size;
        List<String> orderSources = null;
        if(ToolUtil.isNotEmpty(orderSource)){
            orderSources = Arrays.asList(orderSource.split(","));
        }
        List<String> states = null;
        if(ToolUtil.isNotEmpty(state)){
            states = Arrays.asList(state.split(","));
        }
        return this.baseMapper.queryOrderList(search, orderSources, states, companyId, pageNum, size);
    }
 
    @Override
    public Map<String, Object> queryOrderInfo(Integer orderId) throws Exception {
        return this.baseMapper.queryOrderInfo(orderId);
    }
 
    @Override
    public ResultUtil cancelOrder(Integer orderId) throws Exception {
        OrderTaxi orderTaxi = this.selectById(orderId);
        if(orderTaxi.getState() == 10 || orderTaxi.getState() == 12){
            return ResultUtil.error("不允许重复取消");
        }
        if(orderTaxi.getState() == 8 || orderTaxi.getState() == 9){
            return ResultUtil.error("订单已完成,不允许取消");
        }
        orderTaxi.setState(10);
        this.updateById(orderTaxi);
 
        //添加取消记录
        OrderCancel orderCancel = new OrderCancel();
        orderCancel.setOrderId(orderId);
        orderCancel.setOrderType(2);
        orderCancel.setReason("调度端取消");
        orderCancel.setRemark("调度端取消");
        orderCancel.setState(2);
        orderCancel.setInsertTime(new Date());
        orderCancel.setUserType(2);
        orderCancelService.insert(orderCancel);
 
        new Thread(new Runnable() {//发送消息提醒
            @Override
            public void run() {
                pushUtil.pushOrderState(1, orderTaxi.getUserId(), orderId, 2, 10, 0);
                if(null != orderTaxi.getDriverId()){
                    //修改司机为空闲
                    Driver driver = driverService.selectById(orderTaxi.getDriverId());
                    driver.setState(2);
                    driverService.updateById(driver);
                    pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderId, 2, 10, 0);
                }
            }
        }).start();
        //添加消息
        systemNoticeService.addSystemNotice(1, "调度已成功取消出行订单,谢谢使用!", orderTaxi.getUserId(), 1);
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(pushMinistryOfTransport){//上传数据
                    pushMinistryOfTransportUtil.orderCancel(orderId);
                }
            }
        }).start();
 
        return ResultUtil.success();
    }
 
    @Override
    public ResultUtil<BaseWarpper> taxiOrder(OrderTaxi orderTaxi, Integer uid) throws Exception {
        //定义用户所属公司
        Company query = companyCityService.query(String.valueOf(orderTaxi.getStartLon()), String.valueOf(orderTaxi.getStartLat()));
        if(null == query){
            return ResultUtil.error("出发点暂未开通");
        }
        Dispatch dispatch = dispatchService.selectById(uid);
        UserInfo userInfo = userInfoService.selectOne(new EntityWrapper<UserInfo>().eq("phone", orderTaxi.getPassengersPhone()).ne("flag", 3));
        if(userInfo == null){
            userInfo = new UserInfo();
            userInfo.setName(orderTaxi.getPassengers());
            userInfo.setPhone(orderTaxi.getPassengersPhone());
            userInfo.setCompanyId(dispatch.getCompanyId());
            userInfo.setState(1);
            userInfo.setFlag(1);
            userInfo.setInsertTime(new Date());
            userInfoService.insert(userInfo);
        }
 
        if(orderTaxi.getTravelTime().getTime() > (System.currentTimeMillis() + 600000)){
            orderTaxi.setOrderType(2);
        }
 
        /**
         * 1.出租车、专车、跨城有待支付的订单不能叫车
         * 2.小件物流有未完成的订单可以下跨城、专车、出租车
         * 3.出租车、专车、跨城有预约单可以下即时单
         */
        List<OrderPrivateCar> orderPrivateCars = orderPrivateCarMapper.queryByState(userInfo.getId(), null, 1, 7, 12);
        if(orderPrivateCars.size() > 0){
            return ResultUtil.error("乘客有未完成的订单");
        }
        List<OrderTaxi> list = orderTaxiMapper.queryByState(userInfo.getId(), null, 1, 7, 12);
        if(list.size() > 0){
            return ResultUtil.error("乘客有未完成的订单");
        }
        List<OrderCrossCity> orderCrossCities1 = orderCrossCityMapper.queryByState(userInfo.getId(), 7, 12);
        if(orderCrossCities1.size() > 0){
            return ResultUtil.error("乘客有未完成的订单");
        }
        if(orderTaxi.getOrderType() == 1){
            orderPrivateCars = orderPrivateCarMapper.queryByState(userInfo.getId(), 1, 1, 1, 2, 3, 4, 5, 6, 7, 11, 12);
            if(orderPrivateCars.size() > 0){
                return ResultUtil.error("乘客有未完成的订单");
            }
            list = orderTaxiMapper.queryByState(userInfo.getId(), 1, 1, 1, 2, 3, 4, 5, 6, 7, 11, 12);
            if(list.size() > 0){
                return ResultUtil.error("乘客有未完成的订单");
            }
        }
 
 
 
 
        orderTaxi.setOrderNum(this.getOrderNum());
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(String.valueOf(orderTaxi.getPlacementLon()), String.valueOf(orderTaxi.getPlacementLat()));
        orderTaxi.setPlacementAddress(geocode.get("address"));
        orderTaxi.setStartAddress(orderTaxi.getStartAddress().replaceAll("& #40;", "\\("));//特殊字符转义
        orderTaxi.setStartAddress(orderTaxi.getStartAddress().replaceAll("& #41;", "\\)"));
        orderTaxi.setEndAddress(orderTaxi.getEndAddress().replaceAll("& #40;", "\\("));
        orderTaxi.setEndAddress(orderTaxi.getEndAddress().replaceAll("& #41;", "\\)"));
        orderTaxi.setUserId(userInfo.getId());
        orderTaxi.setMileage(0D);
        orderTaxi.setOrderMoney(0D);
        orderTaxi.setTravelMoney(0D);
        orderTaxi.setParkMoney(0D);
        orderTaxi.setRoadTollMoney(0D);
        orderTaxi.setRedPacketMoney(0D);
        orderTaxi.setCouponMoney(0D);
        orderTaxi.setInsertTime(new Date());
        orderTaxi.setIsReassign(1);
        orderTaxi.setState(1);//待接单
        if(null != orderTaxi.getDriverId()){
            Driver driver = driverService.selectById(orderTaxi.getDriverId());
            if(null == driver){
                return ResultUtil.error("司机信息有误");
            }
            if(driver.getAuthState() == 1){
                return ResultUtil.error("司机信息还未完成审核,无法完成下单");
            }
            if(driver.getAuthState() == 3){
                return ResultUtil.error("司机账户已被冻结,无法提供服务");
            }
            if(driver.getAuthState() == 4){
                return ResultUtil.error("司机信息未通过审核,无法提供服务");
            }
            if(driver.getState() == 1){
                return ResultUtil.error("司机还未上线,无法提供服务");
            }
            if(driver.getState() == 3){
                return ResultUtil.error("司机正在服务中,无法提供服务");
            }
            List<DriverService> driverServices = driverServiceService.query(orderTaxi.getDriverId(), 2);
            if(driverServices.size() == 0){
                return ResultUtil.error("该司机不能服务此业务");
            }
 
            orderTaxi.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : (
                    driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1));
            orderTaxi.setCarId(driver.getCarId());
            orderTaxi.setState(2);//待出发
            orderTaxi.setSnatchOrderTime(new Date());
 
//            //调用高德创建轨迹
//            String s = gdFalconUtil.selectTerminal(driver.getPhone());
//            String track = gdFalconUtil.createTrack(s);
//            orderTaxi.setTrackId(track);
//
            //调用移动的小号接口
            Map<String, String> map = chinaMobileUtil.midAxbBindSend(orderTaxi.getPassengersPhone(), driver.getPhone(), (System.currentTimeMillis() + 86400000));
            if(String.valueOf(map.get("code")).equals("200")){
                orderTaxi.setTelX(map.get("telX"));
                orderTaxi.setBindId(map.get("bindId"));
            }
 
            driver.setState(3);
            driverService.updateById(driver);
        }
 
        this.insert(orderTaxi);
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(orderTaxi.getState() == 2){
                    //推送司机订单状态
                    pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderTaxi.getId(), 2, orderTaxi.getState(), 0);
 
                    pushUtil.pushDriverPosition(orderTaxi.getId(), 2);
                }
            }
        }).start();
 
        //添加消息
        systemNoticeService.addSystemNotice(1, "您的出租车订单已下单成功,我们正在为您指派司机,请稍后!", orderTaxi.getUserId(), 1);
 
        BaseWarpper baseWarpper = new BaseWarpper();
        baseWarpper.setId(orderTaxi.getId());
        return ResultUtil.success(baseWarpper);
    }
 
 
    public synchronized String getOrderNum() throws Exception{
        int size = this.selectCount(null);
        return "TAXI" + String.valueOf(1000000 + size + 1).substring(1);
    }
}