| | |
| | | package com.stylefeng.guns.modular.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | 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.service.IDriverWorkService; |
| | | import com.stylefeng.guns.modular.system.util.PushUtil; |
| | | import com.stylefeng.guns.modular.system.util.RedisUtil; |
| | | import com.stylefeng.guns.modular.system.util.TextToSpeechUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | redisUtil.setStrValue("ONLINE_" + ty + "_" + driverId, String.valueOf(now / 1000));//存入秒 |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public void addDriverOnline1(Integer driverId) throws Exception { |
| | | DriverWork driverWork = driverWorkService.selectOne(new EntityWrapper<DriverWork>().eq("driverId", driverId).eq("state", 1)); |
| | | if(null == driverWork){//非上班状态不记录 |
| | | return; |
| | | } |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | List<DriverActivityHistory> driverActivityHistories = driverActivityHistoryMapper.selectList(new EntityWrapper<DriverActivityHistory>().eq("driverId", driverId) |
| | | .eq("type", 3).eq("carryOut", 1).last(" and day = '" + sdf.format(new Date()) + "'")); |
| | | //生成每个活动从开始上班到当前时间的时长记录 |
| | | for (DriverActivityHistory driverActivityHistory : driverActivityHistories) { |
| | | String value = redisUtil.getValue("driverActivity_" + driverId + "_" + driverActivityHistory.getId()); |
| | | if(ToolUtil.isNotEmpty(value)){ |
| | | JSONObject jsonObject = JSON.parseObject(value); |
| | | Long startTime = jsonObject.getLong("startTime"); |
| | | long time = System.currentTimeMillis() - startTime; |
| | | redisUtil.setStrValue("driverActivity_" + driverId + "_" + driverActivityHistory.getId(), "{\"startTime\":" + startTime + ", \"time\":" + time + "}"); |
| | | }else{ |
| | | long time = System.currentTimeMillis() - driverWork.getStartTime().getTime(); |
| | | redisUtil.setStrValue("driverActivity_" + driverId + "_" + driverActivityHistory.getId(), "{\"startTime\":" + driverWork.getStartTime().getTime() + ", \"time\":" + time + "}"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取给定日期范围的在线时长 |
| | | * @param driverId |
| | |
| | | mm = snatchOrderTime.getTime(); |
| | | } |
| | | OrderLogistics orderLogistics = orderLogisticsService.selectOne(new EntityWrapper<OrderLogistics>().eq("driverId", driverId).eq("isDelete", 1).ne("state", 10)); |
| | | if(null != orderLogistics && snatchOrderTime.before(orderLogistics.getSnatchOrderTime())){ |
| | | if(null != orderLogistics && (null == snatchOrderTime || snatchOrderTime.before(orderLogistics.getSnatchOrderTime()))){ |
| | | mm = orderLogistics.getSnatchOrderTime().getTime(); |
| | | } |
| | | |
| | | //超时不接单,直接将之前的在线时长清零 |
| | | //推送司机下班提醒 |
| | | if(mm >= m){ |
| | | if((System.currentTimeMillis() - mm) >= m){ |
| | | this.deleteById(driverOnline.getId()); |
| | | driverWork.setState(2); |
| | | driverWork.setEndTime(new Date()); |
| | |
| | | driver.setState(1); |
| | | driverService.updateById(driver); |
| | | Integer language = driver.getLanguage(); |
| | | pushUtil.pushOffline(driverOnline.getDriverId(), 2, language == 1 ? "您已连续" + driverActivityOnline.getOfflineTime() + "小时未接单,系统将强制更改您的状态为:下班" : |
| | | |
| | | String msg = language == 1 ? "您已连续" + driverActivityOnline.getOfflineTime() + "小时未接单,系统将强制更改您的状态为:下班" : |
| | | language == 2 ? "You have not been accepting orders for " + driverActivityOnline.getOfflineTime() + " hour(s) System will change your state to Off-work" : |
| | | "Vous n’acceptez pas de commandes depuis " + driverActivityOnline.getOfflineTime() + " heure(s) Le système changera votre état en Hors travail"); |
| | | "Vous n’acceptez pas de commandes depuis " + driverActivityOnline.getOfflineTime() + " heure(s) Le système changera votre état en Hors travail"; |
| | | String audioUrl = null; |
| | | try { |
| | | audioUrl = TextToSpeechUtil.create(language == 1 ? "cmn-CN" : language == 2 ? "en-US" : "fr-FR", msg, "OffLine" + driver.getId() + ".mp3"); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | Map<String, String> map = new HashMap<>(); |
| | | map.put("msg", msg); |
| | | map.put("audioUrl", audioUrl); |
| | | pushUtil.pushOffline(driverOnline.getDriverId(), 2, map); |
| | | |
| | | //定时任务删除语音文件 |
| | | new Timer().schedule(new TimerTask() { |
| | | @Override |
| | | public void run() { |
| | | Process process = null; |
| | | try { |
| | | process = Runtime.getRuntime().exec("rm -rf /usr/local/nginx/html/files/audio/OffLine" + driver.getId() + ".mp3"); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (process != null) { |
| | | process.destroy(); |
| | | } |
| | | } |
| | | }, 30000); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void deductionDuration1() { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | List<DriverActivityHistory> driverActivityHistories = driverActivityHistoryMapper.selectList(new EntityWrapper<DriverActivityHistory>() |
| | | .eq("type", 3).eq("carryOut", 1).last(" and day = '" + sdf.format(new Date()) + "'")); |
| | | for (DriverActivityHistory driverActivityHistory : driverActivityHistories) { |
| | | DriverWork driverWork = driverWorkService.selectOne(new EntityWrapper<DriverWork>().eq("driverId", driverActivityHistory.getDriverId()).eq("state", 1)); |
| | | if(null == driverWork){ |
| | | continue; |
| | | } |
| | | DriverActivityOnline driverActivityOnline = driverActivityOnlineMapper.selectById(driverActivityHistory.getActivityId()); |
| | | //在线连续xx小时不接单,司机下线,时长不计入总累计时长 |
| | | int m = Double.valueOf(driverActivityOnline.getOfflineTime() * 60).intValue(); |
| | | String value = redisUtil.getValue("driverActivity_" + driverActivityHistory.getDriverId() + "_" + driverActivityHistory.getId()); |
| | | if(ToolUtil.isEmpty(value)){ |
| | | continue; |
| | | } |
| | | JSONObject jsonObject = JSON.parseObject(value); |
| | | //开始记录时长的时间开始时间戳 |
| | | Long startTime = jsonObject.getLong("startTime"); |
| | | //在线时长累计 |
| | | Long time = jsonObject.getLong("time") / 60000; |
| | | int count1 = orderPrivateCarService.selectCount(new EntityWrapper<OrderPrivateCar>().eq("driverId", driverActivityHistory.getDriverId()) |
| | | .eq("isDelete", 1).ne("state", 10).last(" and UNIX_TIMESTAMP(snatchOrderTime) > " + (startTime) / 1000)); |
| | | int count2 = orderLogisticsService.selectCount(new EntityWrapper<OrderLogistics>().eq("driverId", driverActivityHistory.getDriverId()) |
| | | .eq("isDelete", 1).ne("state", 10).last(" and UNIX_TIMESTAMP(snatchOrderTime) > " + (startTime) / 1000)); |
| | | |
| | | Driver driver = driverService.selectById(driverWork.getDriverId()); |
| | | //阶段时间累计时长超过设定时长且没有接单,不计入累计总时长(司机服务中不下班) |
| | | if(m <= time && count1 + count2 == 0 && driver.getState() != 3){ |
| | | //司机下班,清空当前阶段的累计时长 |
| | | driverWork.setState(2); |
| | | driverWork.setEndTime(new Date()); |
| | | driverWorkService.updateById(driverWork); |
| | | driver.setState(1); |
| | | driverService.updateById(driver); |
| | | redisUtil.remove("driverActivity_" + driverActivityHistory.getDriverId() + "_" + driverActivityHistory.getId()); |
| | | Integer language = driver.getLanguage(); |
| | | |
| | | String msg = language == 1 ? "您已连续" + driverActivityOnline.getOfflineTime() + "小时未接单,系统将强制更改您的状态为:下班" : |
| | | language == 2 ? "You have not been accepting orders for " + driverActivityOnline.getOfflineTime() + " hour(s) System will change your state to Off-work" : |
| | | "Vous n’acceptez pas de commandes depuis " + driverActivityOnline.getOfflineTime() + " heure(s) Le système changera votre état en Hors travail"; |
| | | String audioUrl = null; |
| | | try { |
| | | audioUrl = TextToSpeechUtil.create(language == 1 ? "cmn-CN" : language == 2 ? "en-US" : "fr-FR", msg, "OffLine" + driver.getId() + ".mp3"); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | Map<String, String> map = new HashMap<>(); |
| | | map.put("msg", msg); |
| | | map.put("audioUrl", audioUrl); |
| | | pushUtil.pushOffline(driverWork.getDriverId(), 2, map); |
| | | |
| | | //定时任务删除语音文件 |
| | | new Timer().schedule(new TimerTask() { |
| | | @Override |
| | | public void run() { |
| | | Process process = null; |
| | | try { |
| | | process = Runtime.getRuntime().exec("rm -rf /usr/local/nginx/html/files/audio/OffLine" + driver.getId() + ".mp3"); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (process != null) { |
| | | process.destroy(); |
| | | } |
| | | } |
| | | }, 30000); |
| | | } |
| | | //阶段时间累计有新的订单,计入累计总时长,重置新的开始统计时间戳 |
| | | if(m <= time && count1 + count2 > 0){ |
| | | DriverOnline driverOnline = this.selectOne(new EntityWrapper<DriverOnline>().eq("DATE_FORMAT(date, '%Y-%m-%d')", sdf.format(new Date())) |
| | | .eq("driverId", driverActivityHistory.getDriverId())); |
| | | if(null == driverOnline){ |
| | | driverOnline = new DriverOnline(); |
| | | driverOnline.setDriverId(driverActivityHistory.getDriverId()); |
| | | driverOnline.setDate(new Date()); |
| | | driverOnline.setDuration(time * 60); |
| | | driverOnline.setInsertTime(new Date()); |
| | | this.insert(driverOnline); |
| | | }else{ |
| | | driverOnline.setDuration(driverOnline.getDuration() + (time * 60)); |
| | | this.updateById(driverOnline); |
| | | } |
| | | redisUtil.setStrValue("driverActivity_" + driverActivityHistory.getDriverId() + "_" + driverActivityHistory.getId(), "{\"startTime\":" + System.currentTimeMillis() + ", \"time\":" + 0 + "}"); |
| | | } |
| | | } |
| | | } |
| | | } |