| | |
| | | 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.smallLogistics.model.OrderLogistics; |
| | | import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService; |
| | | import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar; |
| | | import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService; |
| | | import com.stylefeng.guns.modular.system.dao.DriverActivityHistoryMapper; |
| | | import com.stylefeng.guns.modular.system.dao.DriverActivityOnlineMapper; |
| | | import com.stylefeng.guns.modular.system.dao.DriverOnlineMapper; |
| | | import com.stylefeng.guns.modular.system.model.Driver; |
| | | import com.stylefeng.guns.modular.system.model.DriverAssessment; |
| | | import com.stylefeng.guns.modular.system.model.DriverOnline; |
| | | import com.stylefeng.guns.modular.system.model.DriverWork; |
| | | import com.stylefeng.guns.modular.system.model.*; |
| | | import com.stylefeng.guns.modular.system.service.IDriverAssessmentService; |
| | | import com.stylefeng.guns.modular.system.service.IDriverOnlineService; |
| | | import com.stylefeng.guns.modular.system.service.IDriverService; |
| | | 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 org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | public class DriverOnlineServiceImpl extends ServiceImpl<DriverOnlineMapper, DriverOnline> implements IDriverOnlineService { |
| | |
| | | |
| | | @Autowired |
| | | private IDriverService driverService; |
| | | |
| | | @Resource |
| | | private DriverActivityOnlineMapper driverActivityOnlineMapper; |
| | | |
| | | @Resource |
| | | private DriverActivityHistoryMapper driverActivityHistoryMapper; |
| | | |
| | | @Autowired |
| | | private IOrderPrivateCarService orderPrivateCarService; |
| | | |
| | | @Autowired |
| | | private IOrderLogisticsService orderLogisticsService; |
| | | |
| | | @Autowired |
| | | private PushUtil pushUtil; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | |
| | | public void addDriverOnline(Integer driverId) throws Exception { |
| | | DriverWork driverWork = driverWorkService.selectOne(new EntityWrapper<DriverWork>().eq("driverId", driverId).eq("state", 1)); |
| | | if(null == driverWork){//非上班状态不记录 |
| | | redisUtil.remove("ONLINE_" + driverId); |
| | | for (int i = 1; i < 5; i++) { |
| | | redisUtil.remove("ONLINE_" + i + "_" + driverId); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | //查询数据及条件判断 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | DriverOnline query = driverOnlineMapper.query(driverId, sdf.format(new Date()), 0);//正常记录数据 |
| | | DriverOnline query1 = null;//考勤范围内的数据 |
| | | DriverAssessment driverAssessment = driverAssessmentService.selectOne(new EntityWrapper<DriverAssessment>() |
| | | .eq("companyId", driverService.selectById(driverId).getCompanyId())); |
| | | Date s = null; |
| | | Date e = null; |
| | | long now = System.currentTimeMillis(); |
| | | if(null != driverAssessment){//在考勤范围内记录数据 |
| | | SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String[] split = driverAssessment.getAssessment().split(" - "); |
| | | s = sdf1.parse(sdf.format(new Date()) + " " + split[0]); |
| | | e = sdf1.parse(sdf.format(new Date()) + " " + split[1]); |
| | | if(now > s.getTime() && now <= e.getTime()){ |
| | | query1 = driverOnlineMapper.query(driverId, sdf.format(new Date()), 1); |
| | | } |
| | | } |
| | | |
| | | //开始修改数据 |
| | | if(query != null){ |
| | | String value = redisUtil.getValue("ONLINE_" + driverId); |
| | | if(ToolUtil.isEmpty(value)){ |
| | | redisUtil.setStrValue("ONLINE_" + driverId, String.valueOf(now / 1000));//存入秒 |
| | | return; |
| | | } |
| | | Long ss = (now / 1000) - Long.valueOf(value); |
| | | if(ss.longValue() >= 600){//间隔时间大于10分钟则为离线 |
| | | redisUtil.remove("ONLINE_" + driverId); |
| | | return; |
| | | } |
| | | |
| | | query.setDuration(query.getDuration() + ss); |
| | | driverOnlineMapper.updateById(query); |
| | | |
| | | //记录考勤范围内的数据 |
| | | if(null != s && null != e && now > s.getTime() && now <= e.getTime()){ |
| | | if(null != query1){ |
| | | query1.setDuration(query1.getDuration() + ss); |
| | | driverOnlineMapper.updateById(query1); |
| | | }else{ |
| | | query1 = new DriverOnline(); |
| | | query1.setDriverId(driverId); |
| | | query1.setDate(new Date()); |
| | | query1.setAssessment(1); |
| | | query1.setDuration(ss); |
| | | driverOnlineMapper.insert(query1); |
| | | String[] split1 = driverWork.getType().split(","); |
| | | for (String ty : split1) { |
| | | //查询数据及条件判断 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | DriverOnline query = driverOnlineMapper.query(driverId, sdf.format(new Date()), Integer.valueOf(ty), 0);//正常记录数据 |
| | | DriverOnline query1 = null;//考勤范围内的数据 |
| | | DriverAssessment driverAssessment = driverAssessmentService.selectOne(new EntityWrapper<DriverAssessment>() |
| | | .eq("companyId", driverService.selectById(driverId).getCompanyId())); |
| | | Date s = null; |
| | | Date e = null; |
| | | long now = System.currentTimeMillis(); |
| | | if(null != driverAssessment){//在考勤范围内记录数据 |
| | | SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String[] split = driverAssessment.getAssessment().split(" - "); |
| | | s = sdf1.parse(sdf.format(new Date()) + " " + split[0]); |
| | | e = sdf1.parse(sdf.format(new Date()) + " " + split[1]); |
| | | if(now > s.getTime() && now <= e.getTime()){ |
| | | query1 = driverOnlineMapper.query(driverId, sdf.format(new Date()), Integer.valueOf(ty), 1); |
| | | } |
| | | } |
| | | }else{ |
| | | DriverOnline driverOnline = new DriverOnline(); |
| | | driverOnline.setDriverId(driverId); |
| | | driverOnline.setDate(new Date()); |
| | | driverOnline.setAssessment(0); |
| | | driverOnline.setDuration(0L); |
| | | driverOnlineMapper.insert(driverOnline); |
| | | |
| | | //开始修改数据 |
| | | if(query != null){ |
| | | String value = redisUtil.getValue("ONLINE_" + ty + "_" + driverId); |
| | | if(ToolUtil.isEmpty(value)){ |
| | | redisUtil.setStrValue("ONLINE_" + ty + "_" + driverId, String.valueOf(now / 1000));//存入秒 |
| | | return; |
| | | } |
| | | Long ss = (now / 1000) - Long.valueOf(value); |
| | | if(ss.longValue() >= 600){//间隔时间大于10分钟则为离线 |
| | | redisUtil.remove("ONLINE_" + ty + "_" + driverId); |
| | | return; |
| | | } |
| | | |
| | | query.setDuration(query.getDuration() + ss); |
| | | driverOnlineMapper.updateById(query); |
| | | |
| | | //记录考勤范围内的数据 |
| | | if(null != s && null != e && now > s.getTime() && now <= e.getTime()){ |
| | | if(null != query1){ |
| | | query1.setDuration(query1.getDuration() + ss); |
| | | driverOnlineMapper.updateById(query1); |
| | | }else{ |
| | | query1 = new DriverOnline(); |
| | | query1.setDriverId(driverId); |
| | | query1.setType(Integer.valueOf(ty)); |
| | | query1.setDate(new Date()); |
| | | query1.setAssessment(1); |
| | | query1.setDuration(ss); |
| | | driverOnlineMapper.insert(query1); |
| | | } |
| | | } |
| | | }else{ |
| | | DriverOnline driverOnline = new DriverOnline(); |
| | | driverOnline.setDriverId(driverId); |
| | | driverOnline.setType(Integer.valueOf(ty)); |
| | | driverOnline.setDate(new Date()); |
| | | driverOnline.setAssessment(0); |
| | | driverOnline.setDuration(0L); |
| | | driverOnlineMapper.insert(driverOnline); |
| | | } |
| | | redisUtil.setStrValue("ONLINE_" + ty + "_" + driverId, String.valueOf(now / 1000));//存入秒 |
| | | } |
| | | redisUtil.setStrValue("ONLINE_" + driverId, String.valueOf(now / 1000));//存入秒 |
| | | } |
| | | |
| | | |
| | |
| | | public Integer querySumTime(Integer driverId, Integer assessment, Date start, Date end) { |
| | | return driverOnlineMapper.querySumTime(driverId, assessment, start, end); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 定时检测司机是否连续接单 |
| | | */ |
| | | @Override |
| | | public void deductionDuration() { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String format = sdf.format(new Date()); |
| | | List<DriverActivityHistory> driverActivityHistories = driverActivityHistoryMapper.selectList(new EntityWrapper<DriverActivityHistory>().eq("DATE_FORMAT(date, '%Y-%m-%d')", format).eq("type", 3).eq("carryOut", 1)); |
| | | List<Integer> collect = driverActivityHistories.stream().map(DriverActivityHistory::getDriverId).collect(Collectors.toList()); |
| | | if(collect.size() == 0){ |
| | | return; |
| | | } |
| | | 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()); |
| | | DriverOnline driverOnline = this.selectOne(new EntityWrapper<DriverOnline>().eq("DATE_FORMAT(date, '%Y-%m-%d')", format) |
| | | .eq("driverId", driverActivityHistory.getDriverId()).eq("type", driverActivityOnline.getType())); |
| | | if(null == driverOnline){ |
| | | continue; |
| | | } |
| | | Integer driverId = driverOnline.getDriverId(); |
| | | long m = Double.valueOf(driverActivityOnline.getOfflineTime() * 3600000L).longValue(); |
| | | |
| | | //找出最后一次接单的时间 |
| | | long mm = 0L; |
| | | OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectOne(new EntityWrapper<OrderPrivateCar>().eq("driverId", driverId).eq("isDelete", 1).ne("state", 10)); |
| | | Date snatchOrderTime = null; |
| | | if(null != orderPrivateCar){ |
| | | snatchOrderTime = orderPrivateCar.getSnatchOrderTime(); |
| | | 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())){ |
| | | mm = orderLogistics.getSnatchOrderTime().getTime(); |
| | | } |
| | | |
| | | //超时不接单,直接将之前的在线时长清零 |
| | | //推送司机下班提醒 |
| | | if(mm >= m){ |
| | | this.deleteById(driverOnline.getId()); |
| | | driverWork.setState(2); |
| | | driverWork.setEndTime(new Date()); |
| | | driverWorkService.updateById(driverWork); |
| | | Driver driver = driverService.selectById(driverWork.getDriverId()); |
| | | driver.setState(1); |
| | | driverService.updateById(driver); |
| | | // TODO 待翻译 |
| | | pushUtil.pushOffline(driverOnline.getDriverId(), 2, "您已连续" + driverActivityOnline.getOfflineTime() + "小时未接单,系统将强制更改您的状态为:下班"); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | | } |