yanghb
2023-04-21 59f70d9b19408d36f40ee0f418bf98232c40fb33
DispatchTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/OrderTaxiServiceImpl.java
@@ -1,292 +1,22 @@
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 org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
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);
    }
}