package com.stylefeng.guns.modular.system.util; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity; import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService; import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar; import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService; import com.stylefeng.guns.modular.taxi.model.OrderTaxi; import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.*; /** * socket推单处理类 */ @Component public class PushUtil { @Value("${qyt.socket_uri}") private String socket_uri; @Autowired private IOrderTaxiService orderTaxiService; @Autowired private GDMapElectricFenceUtil gdMapElectricFenceUtil; @Autowired private IOrderPrivateCarService orderPrivateCarService; @Autowired private IOrderCrossCityService orderCrossCityService; private Map taskMap = new HashMap<>();//存储定时推送的定时器 @Resource private RedisTemplate redisTemplate; /** * 推送订单状态 * * @param type 1=用户,2=司机 * @param uid 对象id * @param orderId 订单id * @param orderType 订单类型(1=专车,2=出租车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=包车) * @param state 订单状态(1=待接单,2=待出发,3=待到达预约地点,4=待乘客上车,5=服务中,6=完成服务,7=待支付,8=待评价,9=已完成,10=已取消,11=改派中,12=取消待支付) */ public void pushOrderState(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state, Integer time) { JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "ORDER_STATUS"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); map.put("time", time); if (orderType == 1 && state == 100) { map.put("carpooling", 1); map.put("state", 2); } else { map.put("state", state); } jsonObject.put("data", map); //调用推送 HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient"); post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); //将请求头部和参数合成一个请求 Map params = new HashMap<>(); params.put("msg", jsonObject.toJSONString()); params.put("id", uid); params.put("type", type); params.put("bussinessType", "dache"); post.form(params); HttpResponse execute = post.execute(); if (200 != execute.getStatus()) { System.err.println("推送异常"+execute.body()); } else { JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class); if (jsonObject1.getIntValue("code") != 200) { System.err.println(jsonObject1.getString("msg")); } } } /** * 推单完成后,没有司机接单的推送提醒 * * @param type * @param uid * @param orderId * @param orderType */ public void pushEndPush(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state) { JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "END_PUSH"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); map.put("state", state); jsonObject.put("data", map); //调用推送 HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient"); post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); //将请求头部和参数合成一个请求 Map params = new HashMap<>(); params.put("msg", jsonObject.toJSONString()); params.put("id", uid); params.put("type", type); params.put("bussinessType", "dache"); post.form(params); HttpResponse execute = post.execute(); if (200 != execute.getStatus()) { System.err.println("推送异常"); } else { JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class); if (jsonObject1.getIntValue("code") != 200) { System.err.println(jsonObject1.getString("msg")); } } } /** * 推送司机位置给乘客端 */ public void pushDriverPosition(Integer orderId, Integer orderType) { Integer userId = null; switch (orderType) { case 1: OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(orderId); userId = orderPrivateCar.getUserId(); break; case 2: OrderTaxi orderTaxi = orderTaxiService.selectById(orderId); userId = orderTaxi.getUserId(); break; case 3: OrderCrossCity orderCrossCity = orderCrossCityService.selectById(orderId); userId = orderCrossCity.getUserId(); break; } JSONObject data = new JSONObject(); data.put("id", userId); data.put("type", 1); String str = (String) redisTemplate.opsForValue().get(orderId + "_" + orderType); if (ToolUtil.isEmpty(str)) { redisTemplate.opsForValue().set(orderId + "_" + orderType, data.toJSONString()); createTask(orderId, orderType); } } /** * 创建定时任务 * * @param orderId */ public void createTask(Integer orderId, Integer orderType) { TimerTask task = new TimerTask() { @Override public void run() { try { PushUtil.this.pushPositon(orderId, orderType); } catch (Exception e) { e.printStackTrace(); } } }; Timer timer = new Timer(); timer.schedule(task, 1000, 10000);//1秒后开始10秒钟一次的重复执行 taskMap.put(orderId + "_" + orderType, timer); } /** * 推送处理方法 * * @param orderId * @param orderType * @throws Exception */ public void pushPositon(Integer orderId, Integer orderType) throws Exception { Integer driverId = null; String startLonLat = null; String endLonLat = null; Integer state = 0; Integer oldState = 0; Long startServiceTime = null; Double servedMileage = null; switch (orderType) { case 1: OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(orderId); if(null == orderPrivateCar){ return; } driverId = orderPrivateCar.getDriverId(); startLonLat = orderPrivateCar.getStartLon() + "," + orderPrivateCar.getStartLat(); state = orderPrivateCar.getState(); oldState = orderPrivateCar.getOldState(); startServiceTime = null != orderPrivateCar.getStartServiceTime() ? orderPrivateCar.getStartServiceTime().getTime() : null; servedMileage = orderPrivateCar.getMileage(); endLonLat = orderPrivateCar.getEndLon() + "," + orderPrivateCar.getEndLat(); break; case 2: OrderTaxi orderTaxi = orderTaxiService.selectById(orderId); if(null == orderTaxi){ return; } driverId = orderTaxi.getDriverId(); startLonLat = orderTaxi.getStartLon() + "," + orderTaxi.getStartLat(); state = orderTaxi.getState(); oldState = orderTaxi.getOldState(); startServiceTime = null != orderTaxi.getStartServiceTime() ? orderTaxi.getStartServiceTime().getTime() : null; servedMileage = orderTaxi.getMileage(); endLonLat = orderTaxi.getEndLon() + "," + orderTaxi.getEndLat(); break; case 3: OrderCrossCity orderCrossCity = orderCrossCityService.selectById(orderId); if(null == orderCrossCity){ return; } driverId = orderCrossCity.getDriverId(); startLonLat = orderCrossCity.getStartLon() + "," + orderCrossCity.getStartLat(); state = orderCrossCity.getState(); oldState = orderCrossCity.getOldState(); startServiceTime = null != orderCrossCity.getStartServiceTime() ? orderCrossCity.getStartServiceTime().getTime() : null; servedMileage = orderCrossCity.getMileage(); endLonLat = orderCrossCity.getEndLon() + "," + orderCrossCity.getEndLat(); break; } if (state == 7 || state == 8 || state == 9 || state == 10 || state == 12) {//删除定时任务 this.removeTask(orderId, orderType); return; } //计算预计距离和剩余时间 String value = (String) redisTemplate.opsForValue().get("dache:DRIVER" + String.valueOf(driverId)); if (null == value || "".equals(value)) { return; } JSONObject msg = new JSONObject(); msg.put("code", 200); msg.put("msg", "SUCCESS"); msg.put("method", "DRIVER_POSITION"); Map map = new HashMap<>(); map.put("orderId", String.valueOf(orderId)); map.put("orderType", String.valueOf(orderType)); map.put("lon", (null != value ? value.split(",")[0] : "")); map.put("lat", (null != value ? value.split(",")[1] : "")); if ((state == 2 || state == 3 || state == 4) || (oldState != null && (oldState == 2 || oldState == 3 || oldState == 4))) {//前往预约地 // TODO: 2023/11/4 无法修改 Map distance = gdMapElectricFenceUtil.getDistance(value, startLonLat, 1); String d = "0"; String t = "0"; if (null == distance) { System.err.println("查询距离出错了"); } else { d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000), new MathContext(2, RoundingMode.HALF_EVEN)).toString(); t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60), new MathContext(2, RoundingMode.HALF_EVEN)).intValue() + ""; } map.put("reservationMileage", d);//当前位置距离预约点的剩余里程 map.put("reservationTime", t);//当前位置距离预约点的剩余分钟 map.put("servedMileage", "0");//距离起点已经服务的里程 map.put("servedTime", "0");//距离起点已经服务的时间 map.put("laveMileage", "0");//距离终点剩余未服务的里程数 map.put("laveTime", "0");//距离终端剩余未服务的预计时间 } if ((state == 5 || state == 6) || (oldState != null && (oldState == 5 || oldState == 6))) {//服务中 map.put("reservationMileage", "0");//当前位置距离预约点的剩余里程 map.put("reservationTime", "0");//当前位置距离预约点的剩余分钟 map.put("servedMileage", String.valueOf(servedMileage / 1000));//距离起点已经服务的里程 Integer servedTime = Long.valueOf((new Date().getTime() - startServiceTime) / 60000).intValue(); map.put("servedTime", servedTime + "");//距离起点已经服务的时间 // TODO: 2023/11/4 无法修改 Map distance = gdMapElectricFenceUtil.getDistance(value, endLonLat, 1); String d = "0"; String t = "0"; if (null == distance) { System.err.println("查询距离出错了"); } else { d = new BigDecimal(distance.get("distance")).divide(new BigDecimal(1000), new MathContext(2, RoundingMode.HALF_EVEN)).toString(); t = new BigDecimal(distance.get("duration")).divide(new BigDecimal(60), new MathContext(2, RoundingMode.HALF_EVEN)).intValue() + ""; } map.put("laveMileage", d);//距离终点剩余未服务的里程数 map.put("laveTime", t);//距离终端剩余未服务的预计时间 } msg.put("data", map); //将请求头部和参数合成一个请求 String value1 = (String) redisTemplate.opsForValue().get(orderId + "_" + orderType); if (ToolUtil.isEmpty(value1)) { this.removeTask(orderId, orderType); return; } JSONObject jsonObject = JSON.parseObject(value1); //调用推送 HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient"); post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); //将请求头部和参数合成一个请求 Map params = new HashMap<>(); params.put("msg", msg.toJSONString()); params.put("id", jsonObject.getIntValue("id")); params.put("type", jsonObject.getIntValue("type")); params.put("bussinessType", "dache"); post.form(params); HttpResponse execute = post.execute(); if (200 != execute.getStatus()) { System.err.println("推送异常"); } else { JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class); if (jsonObject1.getIntValue("code") != 200) { System.err.println(jsonObject1.getString("msg")); } } } /** * 删除定时任务 * * @param orderId */ public void removeTask(Integer orderId, Integer orderType) { Timer timer = taskMap.get(orderId + "_" + orderType); if (null != timer) { timer.cancel(); taskMap.remove(orderId + "_" + orderType); redisTemplate.delete(orderId + "_" + orderType); } } /** * 推送强迫下线 * * @param id * @param type */ public void pushOffline(Integer id, Integer type) { JSONObject msg = new JSONObject(); msg.put("code", 200); msg.put("msg", "SUCCESS"); msg.put("method", "OFFLINE"); msg.put("data", new Object()); //调用推送 HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient"); post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); //将请求头部和参数合成一个请求 Map params = new HashMap<>(); params.put("msg", msg.toJSONString()); params.put("id", id); params.put("type", type); params.put("bussinessType", "dache"); post.form(params); HttpResponse execute = post.execute(); if (200 != execute.getStatus()) { System.err.println("推送异常"); } else { JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class); if (jsonObject1.getIntValue("code") != 200) { System.err.println(jsonObject1.getString("msg")); } } } /** * 摆渡抢单成功后推单 * * @param type * @param uid * @param orderId * @param orderType * @param state */ public void pushFerryOrderState(Integer type, Integer uid, Integer orderId, Integer orderType, Integer state) { JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "FERRY"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); map.put("state", state); jsonObject.put("data", map); //调用推送 HttpRequest post = HttpUtil.createPost(socket_uri + "/netty/sendMsgToClient"); post.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE); //将请求头部和参数合成一个请求 Map params = new HashMap<>(); params.put("msg", jsonObject.toJSONString()); params.put("id", uid); params.put("type", type); params.put("bussinessType", "dache"); post.form(params); HttpResponse execute = post.execute(); if (200 != execute.getStatus()) { System.err.println("推送异常"); } else { JSONObject jsonObject1 = JSON.parseObject(execute.body(), JSONObject.class); if (jsonObject1.getIntValue("code") != 200) { System.err.println(jsonObject1.getString("msg")); } } } }