| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- 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<WorkCycle> 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<Org> 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<TruckDriver> 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<Integer> 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<User> result = userFeignClient.find(user);
- if (verifyResult(result)) {
- return result.getData();
- }
- return null;
- }
- @Override
- public User addUser(User user) {
- Result<User> result = userFeignClient.add(user.getCreater() + "", user);
- if (verifyResult(result)) {
- return result.getData();
- }
- return null;
- }
- @Override
- public Boolean updateUser(User user) {
- Result<Boolean> result = userFeignClient.update(user.getCreater() + "", user);
- if (verifyResult(result)) {
- return result.getData();
- }
- return null;
- }
- @Override
- public Boolean deleteUser(Long userId) {
- Result<Boolean> 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<TruckSimCard> 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<TruckSimCard> 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<String> getLocationtrip(Long truckId, LocalDateTime startDate, LocalDateTime endDate) {
- if (startDate == null || endDate == null) {
- log.error("getLocationtrip 查询时间错误");
- return Collections.EMPTY_LIST;
- }
- QueryWrapper<TruckSimCard> 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<TmsMongoDoc> tmsMongoDocs = mongoTemplate.find(query, TmsMongoDoc.class);
- List<String> 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;
- }
- }
|