CommonServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package com.aoyang.tms.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aoyang.common.result.Result;
  4. import com.aoyang.tms.common.WorkOrderEnum;
  5. import com.aoyang.tms.controller.vo.LocationVO;
  6. import com.aoyang.tms.controller.vo.WorkerInfo;
  7. import com.aoyang.tms.entity.*;
  8. import com.aoyang.tms.feign.StrategyFeignClient;
  9. import com.aoyang.tms.feign.UserFeignClient;
  10. import com.aoyang.tms.feign.result.Org;
  11. import com.aoyang.tms.feign.result.TmsMongoDoc;
  12. import com.aoyang.tms.service.*;
  13. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.http.HttpEntity;
  16. import org.apache.http.client.methods.CloseableHttpResponse;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.impl.client.CloseableHttpClient;
  19. import org.apache.http.impl.client.HttpClientBuilder;
  20. import org.apache.http.util.EntityUtils;
  21. import org.springframework.data.domain.Sort;
  22. import org.springframework.data.mongodb.core.MongoTemplate;
  23. import org.springframework.data.mongodb.core.query.Criteria;
  24. import org.springframework.data.mongodb.core.query.Query;
  25. import org.springframework.stereotype.Service;
  26. import javax.annotation.Resource;
  27. import java.io.IOException;
  28. import java.time.LocalDateTime;
  29. import java.util.ArrayList;
  30. import java.util.Collections;
  31. import java.util.List;
  32. /**
  33. * @Description: 公共类,方便服务间调用
  34. * @Author guoyong
  35. * @Date 2022/4/21 13:58
  36. * @Version 1.0
  37. */
  38. @Service
  39. @Slf4j
  40. public class CommonServiceImpl implements CommonService {
  41. @Resource
  42. private StrategyFeignClient strategyFeignClient;
  43. @Resource
  44. private UserFeignClient userFeignClient;
  45. @Resource
  46. private MongoTemplate mongoTemplate;
  47. @Resource
  48. private TruckService truckService;
  49. @Resource
  50. private TruckDriverService truckDriverService;
  51. @Resource
  52. private TruckSimCardService truckSimCardService;
  53. @Resource
  54. private TruckEscortService truckEscortService;
  55. @Resource
  56. private WorkOrderService workOrderService;
  57. @Resource
  58. private WorkCycleService workCycleService;
  59. final static String BAIDUMAP_AK = "Zvd6FzmertUwjhZih5Zfq0D8uTUhvqsH";
  60. final static String BAIDUMAP_URL = "http://api.map.baidu.com/reverse_geocoding/v3/?output=json&coordtype=BD09&pois=1";
  61. @Override
  62. public boolean checkTruckZhong(Long truckId) {
  63. QueryWrapper<WorkCycle> queryWrapper = new QueryWrapper<>();
  64. queryWrapper.eq("truck_id", truckId);
  65. queryWrapper.eq("status", 1);
  66. WorkCycle cycle = workCycleService.getOne(queryWrapper);
  67. return cycle == null ? false : cycle.getJingWeight() > 0;
  68. }
  69. @Override
  70. public Org findOrg(Long orgId) {
  71. Org orgParam = new Org();
  72. orgParam.setOrgId(orgId);
  73. Result<Org> result = userFeignClient.findOrg(orgParam);
  74. if (verifyResult(result)) {
  75. return result.getData();
  76. }
  77. return null;
  78. }
  79. @Override
  80. public WorkerInfo findTruckInfo(Long truckId) {
  81. WorkerInfo workerInfo = new WorkerInfo();
  82. workerInfo.setTruckId(truckId);
  83. log.info("findTruckInfo findTruck 参数:{}", JSON.toJSONString(truckId));
  84. Truck truckParam = new Truck();
  85. truckParam.setTruckId(truckId);
  86. Truck truck = truckService.getById(truckParam);
  87. log.info("findTruckInfo findTruck 结果:{}", JSON.toJSONString(truck));
  88. if (truck != null) {
  89. workerInfo.setTruckStatus(truck.getTmsStatus());
  90. workerInfo.setCarNumber(truck.getCarNumber());
  91. workerInfo.setTrailerNumber(truck.getTrailerNumber());
  92. workerInfo.setDeptId(truck.getDeptId());
  93. workerInfo.setDeptName(truck.getDeptName());
  94. workerInfo.setIsZhongche("空车");
  95. //查询最后一笔已经执行或执行中的订单
  96. WorkOrder workOrder = workOrderService.findLastWorkOrder(truckId);
  97. if (workOrder != null) {
  98. workerInfo.setOrderId(workOrder.getId());
  99. WorkCycle workCycle = workCycleService.getById(workOrder.getCycleId());
  100. if (workCycle != null && workCycle.getEndTime() == null) {
  101. workerInfo.setCycleId(workCycle.getId());
  102. //装车订单判断
  103. if (workOrder.getType() == 1) {
  104. if (workOrder.getOrderStatus().equals(WorkOrderEnum.ZHUANGCHE_RESULT_SUBMIT.orderStatus()) || workOrder.getOrderStatus().equals(WorkOrderEnum.COMPLETED.orderStatus()) || workOrder.getOrderStatus().equals(WorkOrderEnum.CLOSED.orderStatus())) {
  105. workerInfo.setIsZhongche("重车");
  106. }
  107. }
  108. //卸车订单判断
  109. if (workOrder.getType() == 2) {
  110. if (workOrder.getOrderStatus().equals(WorkOrderEnum.COMPLETED.orderStatus())) {
  111. if (Double.valueOf(workOrder.getJingWeight()) > 0) {
  112. workerInfo.setIsZhongche("重车");
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. log.info("findTruckInfo findTruckDrivers 参数:{}", JSON.toJSONString(truckId));
  120. List<TruckDriver> truckDrivers = truckDriverService.findTruckDrivers(truckId);
  121. log.info("findTruckInfo findTruckDrivers 结果:{}", JSON.toJSONString(truckDrivers));
  122. if (truckDrivers != null && truckDrivers.size() > 0) {
  123. workerInfo.setDriverIdOne(truckDrivers.get(0).getDriverId());
  124. workerInfo.setDriverNameOne(truckDrivers.get(0).getDriverName());
  125. User user = findUser(truckDrivers.get(0).getDriverId());
  126. if (user != null) {
  127. workerInfo.setDriverPhoneOne(user.getMobile());
  128. }
  129. if (truckDrivers.size() > 1) {
  130. workerInfo.setDriverIdTwo(truckDrivers.get(1).getDriverId());
  131. workerInfo.setDriverNameTwo(truckDrivers.get(1).getDriverName());
  132. user = findUser(truckDrivers.get(1).getDriverId());
  133. if (user != null) {
  134. workerInfo.setDriverPhoneTwo(user.getMobile());
  135. }
  136. }
  137. }
  138. TruckEscort truckEscortParam = new TruckEscort();
  139. truckEscortParam.setTruckId(truckId);
  140. log.info("findTruckInfo findTruckEscort 参数:{}", JSON.toJSONString(truckEscortParam));
  141. TruckEscort truckEscort = truckEscortService.findTruckEscort(truckEscortParam);
  142. log.info("findTruckInfo findTruckEscort:{}", JSON.toJSONString(truckEscort));
  143. if (truckEscort != null) {
  144. workerInfo.setEscortId(truckEscort.getEscortId());
  145. workerInfo.setEscortName(truckEscort.getEscortName());
  146. User user = findUser(truckEscort.getEscortId());
  147. if (user != null) {
  148. workerInfo.setEscortPhone(user.getMobile());
  149. }
  150. }
  151. LocationVO locationVO = getLocationByDate(truckId, LocalDateTime.now());
  152. if (locationVO != null) {
  153. workerInfo.setLatitude(locationVO.getLatitude());
  154. workerInfo.setLongitude(locationVO.getLongitude());
  155. workerInfo.setLocalTime(locationVO.getDate());
  156. }
  157. return workerInfo;
  158. }
  159. @Override
  160. public Integer findOrgTruckNumber(Long orgId, Long deptId) {
  161. Truck truck = new Truck();
  162. truck.setOrgId(orgId);
  163. if (deptId != null) {
  164. truck.setDeptId(deptId);
  165. }
  166. Result<Integer> result = strategyFeignClient.findOrgTruckNumber(truck);
  167. if (verifyResult(result)) {
  168. return result.getData();
  169. }
  170. return null;
  171. }
  172. @Override
  173. public Boolean updateTruckTmsStatus(Long truckId, Integer tmsStatus) {
  174. //先通过主键拿出来,然后更新其状态
  175. Truck truckParam = new Truck();
  176. truckParam.setTruckId(truckId);
  177. Truck truck4Update = truckService.getById(truckParam);
  178. truck4Update.setTruckId(truckParam.getTruckId());
  179. truck4Update.setTmsStatus(truckParam.getTmsStatus());
  180. Boolean flag = truckService.updateById(truck4Update);
  181. return flag;
  182. }
  183. @Override
  184. public User findUser(Long userId) {
  185. User user = new User();
  186. user.setUserId(userId);
  187. Result<User> result = userFeignClient.find(user);
  188. if (verifyResult(result)) {
  189. return result.getData();
  190. }
  191. return null;
  192. }
  193. @Override
  194. public User addUser(User user) {
  195. Result<User> result = userFeignClient.add(user.getCreater() + "", user);
  196. if (verifyResult(result)) {
  197. return result.getData();
  198. }
  199. return null;
  200. }
  201. @Override
  202. public Boolean updateUser(User user) {
  203. Result<Boolean> result = userFeignClient.update(user.getCreater() + "", user);
  204. if (verifyResult(result)) {
  205. return result.getData();
  206. }
  207. return null;
  208. }
  209. @Override
  210. public Boolean deleteUser(Long userId) {
  211. Result<Boolean> result = userFeignClient.delete(userId);
  212. if (verifyResult(result)) {
  213. return result.getData();
  214. }
  215. return null;
  216. }
  217. @Override
  218. public boolean verifyResult(Result result) {
  219. return null != result && result.getCode() == 0;
  220. }
  221. @Override
  222. public LocationVO getLocation(Long truckId) {
  223. LocationVO location = null;
  224. QueryWrapper<TruckSimCard> queryWrapper = new QueryWrapper<>();
  225. queryWrapper.eq("truck_id", truckId);
  226. TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper);
  227. if (truckSimCard == null) {
  228. log.error("绑定信息不存在 truckId:{}", truckId);
  229. return location;
  230. }
  231. Query query = new Query(Criteria.where("clientId").is(truckSimCard.getSimCard()));
  232. query.with(new Sort(Sort.Direction.DESC, "dateTime"));
  233. TmsMongoDoc tmsMongoDoc = mongoTemplate.findOne(query, TmsMongoDoc.class);
  234. if (tmsMongoDoc != null) {
  235. location = new LocationVO();
  236. location.setLatitude(tmsMongoDoc.getLatitude() + "");
  237. location.setLongitude(tmsMongoDoc.getLongitude() + "");
  238. location.setCarNumber(truckSimCard.getCarNumber());
  239. location.setTruckId(truckSimCard.getTruckId());
  240. location.setDate(tmsMongoDoc.getCreateTime());
  241. }
  242. return location;
  243. }
  244. @Override
  245. public LocationVO getLocationByDate(Long truckId, LocalDateTime date) {
  246. LocationVO location = null;
  247. QueryWrapper<TruckSimCard> queryWrapper = new QueryWrapper<>();
  248. queryWrapper.eq("truck_id", truckId);
  249. TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper);
  250. if (truckSimCard == null) {
  251. log.error("绑定信息不存在 truckId:{}", truckId);
  252. return location;
  253. }
  254. Criteria criteria = Criteria.where("clientId").is(truckSimCard.getSimCard());
  255. criteria.andOperator(Criteria.where("createTime").gt(date));
  256. Query query = new Query(criteria);
  257. query.with(new Sort(Sort.Direction.ASC, "dateTime"));
  258. TmsMongoDoc tmsMongoDoc = mongoTemplate.findOne(query, TmsMongoDoc.class);
  259. if (tmsMongoDoc != null) {
  260. location = new LocationVO();
  261. location.setLatitude(tmsMongoDoc.getLatitude() + "");
  262. location.setLongitude(tmsMongoDoc.getLongitude() + "");
  263. location.setCarNumber(truckSimCard.getCarNumber());
  264. location.setTruckId(truckSimCard.getTruckId());
  265. location.setDate(tmsMongoDoc.getCreateTime());
  266. }
  267. return location;
  268. }
  269. @Override
  270. public List<String> getLocationtrip(Long truckId, LocalDateTime startDate, LocalDateTime endDate) {
  271. if (startDate == null || endDate == null) {
  272. log.error("getLocationtrip 查询时间错误");
  273. return Collections.EMPTY_LIST;
  274. }
  275. QueryWrapper<TruckSimCard> queryWrapper = new QueryWrapper<>();
  276. queryWrapper.eq("truck_id", truckId);
  277. TruckSimCard truckSimCard = truckSimCardService.getOne(queryWrapper);
  278. if (truckSimCard == null) {
  279. return Collections.EMPTY_LIST;
  280. }
  281. Criteria criteria = Criteria.where("clientId").is(truckSimCard.getSimCard());
  282. criteria.andOperator(Criteria.where("createTime").gt(startDate), Criteria.where("createTime").lt(endDate));
  283. Query query = new Query(criteria);
  284. List<TmsMongoDoc> tmsMongoDocs = mongoTemplate.find(query, TmsMongoDoc.class);
  285. List<String> result = new ArrayList<>();
  286. for (TmsMongoDoc tmsMongoDoc : tmsMongoDocs) {
  287. result.add(tmsMongoDoc.getLatitude() + "," + tmsMongoDoc.getLongitude());
  288. }
  289. return result;
  290. }
  291. @Override
  292. public String longitudeToAddress(String lng, String lat) {
  293. //拼接请求路径
  294. String url = BAIDUMAP_URL + "&ak=" + BAIDUMAP_AK + "&location=" + lat + "," + lng;
  295. log.info("请求url:" + url);
  296. String res = doGet(url);
  297. String Addresslocation= JSON.parseObject(res).getJSONObject("result").getString("formatted_address");
  298. System.out.println(Addresslocation);
  299. return Addresslocation;
  300. }
  301. public static String doGet(String url) {
  302. //创建一个Http客户端
  303. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  304. //创建一个get请求
  305. HttpGet httpGet = new HttpGet(url);
  306. //响应模型
  307. CloseableHttpResponse response = null;
  308. try {
  309. //由客户端发送get请求
  310. response = httpClient.execute(httpGet);
  311. //从响应模型中获取响应实体
  312. HttpEntity responseEntity = response.getEntity();
  313. if (responseEntity != null) {
  314. return EntityUtils.toString(responseEntity);
  315. }
  316. } catch (IOException e) {
  317. e.printStackTrace();
  318. } finally {
  319. try {
  320. if (httpClient != null) {
  321. httpClient.close();
  322. }
  323. if (response != null) {
  324. response.close();
  325. }
  326. } catch (IOException e) {
  327. e.printStackTrace();
  328. }
  329. }
  330. return null;
  331. }
  332. }