package com.stylefeng.guns.modular.system.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; 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.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.*; /** * socket推单处理类 */ @Component public class PushUtil { @Autowired private RestTemplate internalRestTemplate; @Autowired private RedisUtil redisUtil; @Autowired private IOrderTaxiService orderTaxiService; @Autowired private GDMapElectricFenceUtil gdMapElectricFenceUtil; @Autowired private IOrderPrivateCarService orderPrivateCarService; @Autowired private IOrderCrossCityService orderCrossCityService; private Map pushMap = new HashMap<>();//存储需要定时推送的数据 private Map taskMap = new HashMap<>();//存储定时推送的定时器 /** * 推送订单状态 * @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, String audioUrl, String from){ 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("state", state); map.put("time", time); map.put("audioUrl", audioUrl); map.put("from", from); jsonObject.put("data", map); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, 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){ 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); jsonObject.put("data", map); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class); if(jsonObject1.getIntValue("code") != 200){ System.err.println(jsonObject1.getString("msg")); } } /** * 司机超时提醒 * @param type * @param uid * @param orderId * @param orderType */ public void pushDriverTimeOut(Integer type, Integer uid, Integer orderId, Integer orderType){ JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "DRIVER_TIME_OUT"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); jsonObject.put("data", map); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class); if(jsonObject1.getIntValue("code") != 200){ System.err.println(jsonObject1.getString("msg")); } } /** * 修改目的地推送通知 * @param type * @param uid * @param orderId * @param orderType * @param status 1=申请,2=同意,3=拒绝 */ public void pushModifyAddress(Integer type, Integer uid, Integer orderId, Integer orderType, Integer status, String audioUrl){ JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "MODIFY_ADDRESS"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); map.put("status", status); map.put("audioUrl", audioUrl); jsonObject.put("data", map); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, 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); this.pushMap.put(orderId + "_" + orderType, data); this.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); 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); 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); 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; } //计算预计距离和剩余时间 String value = redisUtil.getValue("DRIVER" + String.valueOf(driverId)); if(null == value || "".equals(value)){ System.err.println("司机没有上传位置信息"); } 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() + ""; } 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 == 7 || state == 8 || state == 9 || state == 10 || state == 12){//删除定时任务 this.removeTask(orderId, orderType); return; } if((state == 2 || state == 3 || state == 4) || (oldState != null && (oldState == 2 || oldState == 3 || oldState == 4))){//前往预约地 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 + "");//距离起点已经服务的时间 distance = gdMapElectricFenceUtil.getDistance(value, endLonLat, 1); 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); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 JSONObject jsonObject = pushMap.get(orderId + "_" + orderType); MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", msg.toJSONString()); params.add("id", jsonObject.getString("id")); params.add("type", jsonObject.getString("type")); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class); if(jsonObject1.getIntValue("code") != 200){ System.err.println(jsonObject1.getString("msg")); } } /** * 推送强迫下线 * @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()); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", msg.toJSONString()); params.add("id", id.toString()); params.add("type", type.toString()); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, 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); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, 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(); } } /** * 线下支付数据推送 * @param type * @param uid * @param orderId * @param orderType */ public void pushOfflinePayment(Integer type, Integer uid, Integer orderId, Integer orderType, Double money, String audioUrl){ JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "OFFLINE_PAYMENT"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); map.put("money", money); map.put("audioUrl", audioUrl); jsonObject.put("data", map); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class); if(jsonObject1.getIntValue("code") != 200){ System.err.println(jsonObject1.getString("msg")); } } /** * 小件物流差价支付的推送 * @param type * @param uid * @param orderId * @param orderType * @param money * @param status 1=申请,2=同意,3=拒绝 */ public void pushPayDifference(Integer type, Integer uid, Integer orderId, Integer orderType, Double money, Integer status, String audioUrl){ JSONObject jsonObject = new JSONObject(); jsonObject.put("code", 200); jsonObject.put("msg", "SUCCESS"); jsonObject.put("method", "DIFFERENCE"); Map map = new HashMap<>(); map.put("orderId", orderId); map.put("orderType", orderType); map.put("money", money); map.put("status", status); map.put("audioUrl", audioUrl); jsonObject.put("data", map); //调用推送 HttpHeaders headers = new HttpHeaders(); // 以表单的方式提交 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //将请求头部和参数合成一个请求 MultiValueMap params = new LinkedMultiValueMap<>(); params.add("msg", jsonObject.toJSONString()); params.add("id", String.valueOf(uid)); params.add("type", String.valueOf(type)); HttpEntity> requestEntity = new HttpEntity<>(params, headers); String s = internalRestTemplate.postForObject("http://zuul-gateway/netty/sendMsgToClient",requestEntity , String.class); JSONObject jsonObject1 = JSON.parseObject(s, JSONObject.class); if(jsonObject1.getIntValue("code") != 200){ System.err.println(jsonObject1.getString("msg")); } } }