package com.aoyang.tms.service.impl; import com.alibaba.fastjson.JSON; import com.aoyang.common.result.Result; import com.aoyang.tms.common.WorkOrderEnum; import com.aoyang.tms.controller.vo.LocationVO; import com.aoyang.tms.controller.vo.WorkerInfo; import com.aoyang.tms.entity.*; import com.aoyang.tms.feign.StrategyFeignClient; import com.aoyang.tms.feign.UserFeignClient; import com.aoyang.tms.feign.result.Org; import com.aoyang.tms.feign.result.TmsMongoDoc; import com.aoyang.tms.service.*; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.IOException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @Description: 公共类,方便服务间调用 * @Author guoyong * @Date 2022/4/21 13:58 * @Version 1.0 */ @Service @Slf4j public class CommonServiceImpl implements CommonService { @Resource private StrategyFeignClient strategyFeignClient; @Resource private UserFeignClient userFeignClient; @Resource private MongoTemplate mongoTemplate; @Resource private TruckService truckService; @Resource private TruckDriverService truckDriverService; @Resource private TruckSimCardService truckSimCardService; @Resource private TruckEscortService truckEscortService; @Resource private WorkOrderService workOrderService; @Resource private WorkCycleService workCycleService; final static String BAIDUMAP_AK = "Zvd6FzmertUwjhZih5Zfq0D8uTUhvqsH"; final static String BAIDUMAP_URL = "http://api.map.baidu.com/reverse_geocoding/v3/?output=json&coordtype=BD09&pois=1"; @Override public boolean checkTruckZhong(Long truckId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("truck_id", truckId); queryWrapper.eq("status", 1); WorkCycle cycle = workCycleService.getOne(queryWrapper); return cycle == null ? false : cycle.getJingWeight() > 0; } @Override public Org findOrg(Long orgId) { Org orgParam = new Org(); orgParam.setOrgId(orgId); Result result = userFeignClient.findOrg(orgParam); if (verifyResult(result)) { return result.getData(); } return null; } @Override public WorkerInfo findTruckInfo(Long truckId) { WorkerInfo workerInfo = new WorkerInfo(); workerInfo.setTruckId(truckId); log.info("findTruckInfo findTruck 参数:{}", JSON.toJSONString(truckId)); Truck truckParam = new Truck(); truckParam.setTruckId(truckId); Truck truck = truckService.getById(truckParam); log.info("findTruckInfo findTruck 结果:{}", JSON.toJSONString(truck)); if (truck != null) { workerInfo.setTruckStatus(truck.getTmsStatus()); workerInfo.setCarNumber(truck.getCarNumber()); workerInfo.setTrailerNumber(truck.getTrailerNumber()); workerInfo.setDeptId(truck.getDeptId()); workerInfo.setDeptName(truck.getDeptName()); workerInfo.setIsZhongche("空车"); //查询最后一笔已经执行或执行中的订单 WorkOrder workOrder = workOrderService.findLastWorkOrder(truckId); if (workOrder != null) { workerInfo.setOrderId(workOrder.getId()); WorkCycle workCycle = workCycleService.getById(workOrder.getCycleId()); if (workCycle != null && workCycle.getEndTime() == null) { workerInfo.setCycleId(workCycle.getId()); //装车订单判断 if (workOrder.getType() == 1) { if (workOrder.getOrderStatus().equals(WorkOrderEnum.ZHUANGCHE_RESULT_SUBMIT.orderStatus()) || workOrder.getOrderStatus().equals(WorkOrderEnum.COMPLETED.orderStatus()) || workOrder.getOrderStatus().equals(WorkOrderEnum.CLOSED.orderStatus())) { workerInfo.setIsZhongche("重车"); } } //卸车订单判断 if (workOrder.getType() == 2) { if (workOrder.getOrderStatus().equals(WorkOrderEnum.COMPLETED.orderStatus())) { if (Double.valueOf(workOrder.getJingWeight()) > 0) { workerInfo.setIsZhongche("重车"); } } } } } } log.info("findTruckInfo findTruckDrivers 参数:{}", JSON.toJSONString(truckId)); List truckDrivers = truckDriverService.findTruckDrivers(truckId); log.info("findTruckInfo findTruckDrivers 结果:{}", JSON.toJSONString(truckDrivers)); if (truckDrivers != null && truckDrivers.size() > 0) { workerInfo.setDriverIdOne(truckDrivers.get(0).getDriverId()); workerInfo.setDriverNameOne(truckDrivers.get(0).getDriverName()); User user = findUser(truckDrivers.get(0).getDriverId()); if (user != null) { workerInfo.setDriverPhoneOne(user.getMobile()); } if (truckDrivers.size() > 1) { workerInfo.setDriverIdTwo(truckDrivers.get(1).getDriverId()); workerInfo.setDriverNameTwo(truckDrivers.get(1).getDriverName()); user = findUser(truckDrivers.get(1).getDriverId()); if (user != null) { workerInfo.setDriverPhoneTwo(user.getMobile()); } } } TruckEscort truckEscortParam = new TruckEscort(); truckEscortParam.setTruckId(truckId); log.info("findTruckInfo findTruckEscort 参数:{}", JSON.toJSONString(truckEscortParam)); TruckEscort truckEscort = truckEscortService.findTruckEscort(truckEscortParam); log.info("findTruckInfo findTruckEscort:{}", JSON.toJSONString(truckEscort)); if (truckEscort != null) { workerInfo.setEscortId(truckEscort.getEscortId()); workerInfo.setEscortName(truckEscort.getEscortName()); User user = findUser(truckEscort.getEscortId()); if (user != null) { workerInfo.setEscortPhone(user.getMobile()); } } LocationVO locationVO = getLocationByDate(truckId, LocalDateTime.now()); if (locationVO != null) { workerInfo.setLatitude(locationVO.getLatitude()); workerInfo.setLongitude(locationVO.getLongitude()); workerInfo.setLocalTime(locationVO.getDate()); } return workerInfo; } @Override public Integer findOrgTruckNumber(Long orgId, Long deptId) { Truck truck = new Truck(); truck.setOrgId(orgId); if (deptId != null) { truck.setDeptId(deptId); } Result result = strategyFeignClient.findOrgTruckNumber(truck); if (verifyResult(result)) { return result.getData(); } return null; } @Override public Boolean updateTruckTmsStatus(Long truckId, Integer tmsStatus) { //先通过主键拿出来,然后更新其状态 Truck truckParam = new Truck(); truckParam.setTruckId(truckId); Truck truck4Update = truckService.getById(truckParam); truck4Update.setTruckId(truckParam.getTruckId()); truck4Update.setTmsStatus(truckParam.getTmsStatus()); Boolean flag = truckService.updateById(truck4Update); return flag; } @Override public User findUser(Long userId) { User user = new User(); user.setUserId(userId); Result result = userFeignClient.find(user); if (verifyResult(result)) { return result.getData(); } return null; } @Override public User addUser(User user) { Result result = userFeignClient.add(user.getCreater() + "", user); if (verifyResult(result)) { return result.getData(); } return null; } @Override public Boolean updateUser(User user) { Result result = userFeignClient.update(user.getCreater() + "", user); if (verifyResult(result)) { return result.getData(); } return null; } @Override public Boolean deleteUser(Long userId) { Result result = userFeignClient.delete(userId); if (verifyResult(result)) { return result.getData(); } return null; } @Override public boolean verifyResult(Result result) { return null != result && result.getCode() == 0; } @Override public LocationVO getLocation(Long truckId) { LocationVO location = null; QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("truck_id", truckId); TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper); if (truckSimCard == null) { log.error("绑定信息不存在 truckId:{}", truckId); return location; } Query query = new Query(Criteria.where("clientId").is(truckSimCard.getSimCard())); query.with(new Sort(Sort.Direction.DESC, "dateTime")); TmsMongoDoc tmsMongoDoc = mongoTemplate.findOne(query, TmsMongoDoc.class); if (tmsMongoDoc != null) { location = new LocationVO(); location.setLatitude(tmsMongoDoc.getLatitude() + ""); location.setLongitude(tmsMongoDoc.getLongitude() + ""); location.setCarNumber(truckSimCard.getCarNumber()); location.setTruckId(truckSimCard.getTruckId()); location.setDate(tmsMongoDoc.getCreateTime()); } return location; } @Override public LocationVO getLocationByDate(Long truckId, LocalDateTime date) { LocationVO location = null; QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("truck_id", truckId); TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper); if (truckSimCard == null) { log.error("绑定信息不存在 truckId:{}", truckId); return location; } Criteria criteria = Criteria.where("clientId").is(truckSimCard.getSimCard()); criteria.andOperator(Criteria.where("createTime").gt(date)); Query query = new Query(criteria); query.with(new Sort(Sort.Direction.ASC, "dateTime")); TmsMongoDoc tmsMongoDoc = mongoTemplate.findOne(query, TmsMongoDoc.class); if (tmsMongoDoc != null) { location = new LocationVO(); location.setLatitude(tmsMongoDoc.getLatitude() + ""); location.setLongitude(tmsMongoDoc.getLongitude() + ""); location.setCarNumber(truckSimCard.getCarNumber()); location.setTruckId(truckSimCard.getTruckId()); location.setDate(tmsMongoDoc.getCreateTime()); } return location; } @Override public List getLocationtrip(Long truckId, LocalDateTime startDate, LocalDateTime endDate) { if (startDate == null || endDate == null) { log.error("getLocationtrip 查询时间错误"); return Collections.EMPTY_LIST; } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("truck_id", truckId); TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper); if (truckSimCard == null) { return Collections.EMPTY_LIST; } Criteria criteria = Criteria.where("clientId").is(truckSimCard.getSimCard()); criteria.andOperator(Criteria.where("createTime").gt(startDate), Criteria.where("createTime").lt(endDate)); Query query = new Query(criteria); List tmsMongoDocs = mongoTemplate.find(query, TmsMongoDoc.class); List result = new ArrayList<>(); for (TmsMongoDoc tmsMongoDoc : tmsMongoDocs) { result.add(tmsMongoDoc.getLatitude() + "," + tmsMongoDoc.getLongitude()); } return result; } @Override public String longitudeToAddress(String lng, String lat) { //拼接请求路径 String url = BAIDUMAP_URL + "&ak=" + BAIDUMAP_AK + "&location=" + lat + "," + lng; log.info("请求url:" + url); String res = doGet(url); String Addresslocation= JSON.parseObject(res).getJSONObject("result").getString("formatted_address"); System.out.println(Addresslocation); return Addresslocation; } public static String doGet(String url) { //创建一个Http客户端 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //创建一个get请求 HttpGet httpGet = new HttpGet(url); //响应模型 CloseableHttpResponse response = null; try { //由客户端发送get请求 response = httpClient.execute(httpGet); //从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { return EntityUtils.toString(responseEntity); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (httpClient != null) { httpClient.close(); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } }