ApiCarFreeServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package com.jkcredit.invoice.hub.service.apiCarFree;
  2. import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
  3. import cn.com.taiji.sdk.comm.ETCCommHelper;
  4. import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleQueryRequest;
  5. import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleQueryResponse;
  6. import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleRegisterRequest;
  7. import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleRegisterResponse;
  8. import cn.com.taiji.sdk.model.comm.protocol.tts.waybill.*;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.alibaba.fastjson.serializer.SerializerFeature;
  12. import com.jkcredit.invoice.hub.constant.CommonConstant;
  13. import com.jkcredit.invoice.hub.enums.ApiResponseCodeEnum;
  14. import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResult;
  15. import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResultDto;
  16. import com.jkcredit.invoice.hub.model.dto.apiCarFree.WayBillNumFindInvoiceDto;
  17. import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillEnd.CarFreeCarrierBillEndDto;
  18. import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillStart.CarFreeCarrierBillStartDto;
  19. import com.jkcredit.invoice.hub.model.dto.apiCarFree.VehicleRegisterDto;
  20. import com.jkcredit.invoice.hub.model.dto.searchInvoice.SearchInvoiceDto;
  21. import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.CarFreeCarrierBillStartPo;
  22. import com.jkcredit.invoice.hub.model.po.searchInvoice.SearchInvoicePo;
  23. import com.jkcredit.invoice.hub.service.base.BaseService;
  24. import com.jkcredit.invoice.hub.service.carFreeCarrierBillEnd.CarFreeCarrierBillEndService;
  25. import com.jkcredit.invoice.hub.service.carFreeCarrierBillStart.CarFreeCarrierBillStartService;
  26. import com.jkcredit.invoice.hub.service.searchInvoice.SearchInvoiceService;
  27. import com.jkcredit.invoice.hub.service.searchInvoiceResult.SearchInvoiceResultService;
  28. import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
  29. import com.jkcredit.invoice.hub.spi.rest.data.ApiResponseData;
  30. import com.jkcredit.invoice.hub.util.BeanUtil;
  31. import io.netty.util.internal.StringUtil;
  32. import lombok.extern.slf4j.Slf4j;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.io.IOException;
  37. import java.util.Date;
  38. import java.util.List;
  39. /**
  40. * @description: 无车api接口
  41. * @author: xusonglin
  42. * @create: 2020/1/16 21:52
  43. * @version: V1.0
  44. **/
  45. @Service
  46. @Slf4j
  47. public class ApiCarFreeServiceImpl extends BaseService implements ApiCarFreeService {
  48. @Autowired
  49. CarFreeCarrierBillStartService wayBillStartService;
  50. @Autowired
  51. CarFreeCarrierBillEndService wayBillEndService;
  52. @Autowired
  53. SearchInvoiceResultService searchInvoiceResultService;
  54. @Autowired
  55. SearchInvoiceService searchInvoiceService;
  56. @Autowired
  57. ApiCarFreeChargeService apiCarFreeChargeService;
  58. @Override
  59. public ApiResponseData vehicleRegisterQuery(JSONObject param) {
  60. long costTimeStart = System.currentTimeMillis();
  61. String result;
  62. try {
  63. VehicleRegisterDto dto = JSON.toJavaObject(param, VehicleRegisterDto.class);
  64. validate(dto);
  65. VehicleQueryRequest request = new VehicleQueryRequest();
  66. request.setPlateNum(dto.getPlateNumber());
  67. request.setPlateColor(dto.getPlateColor());
  68. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  69. request.setWaybillSource(CommonConstant.WAYBILL_SOURCE_1);
  70. String fileName = request.getFilename();
  71. // 调用upload 发送数据
  72. VehicleQueryResponse response = ETCCommHelper.upload(fileName, request, VehicleQueryResponse.class);
  73. result = response.toJson();
  74. long costTimeEnd = System.currentTimeMillis();
  75. log.info("[-vehicleRegisterQuery-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  76. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  77. + ",endTime=" + costTimeEnd);
  78. } catch (IOException e) {
  79. log.error("[-vehicleRegisterQuery-] 网络异常 " + e);
  80. throw new ServiceException(CommonConstant.QUERY_FAILED);
  81. } catch (ApiRequestException apie) {
  82. log.error("[-vehicleRegisterQuery-] 错误信息:" + apie.getMessage());
  83. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  84. } catch (ServiceException se) {
  85. log.error("[-vehicleRegisterQuery-] 错误信息:" + se.getMessage());
  86. throw new ServiceException(se.getMessage());
  87. }
  88. return analyzeApiResultItems(result);
  89. }
  90. @Override
  91. public ApiResponseData vehicleRegister(JSONObject param) {
  92. long costTimeStart = System.currentTimeMillis();
  93. String result;
  94. try {
  95. VehicleRegisterDto dto = JSON.toJavaObject(param, VehicleRegisterDto.class);
  96. validate(dto);
  97. VehicleRegisterRequest request = new VehicleRegisterRequest();
  98. request.setPlateNum(dto.getPlateNumber());
  99. request.setPlateColor(dto.getPlateColor());
  100. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  101. String fileName = request.getFilename();
  102. // 调用upload 发送数据
  103. VehicleRegisterResponse response = ETCCommHelper.upload(fileName, request, VehicleRegisterResponse.class);
  104. result = response.toJson();
  105. long costTimeEnd = System.currentTimeMillis();
  106. log.info("[-apiVehicleRegister-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  107. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  108. + ",endTime=" + costTimeEnd);
  109. } catch (IOException e) {
  110. log.error("[-apiVehicleRegister-] 网络异常 " + e);
  111. throw new ServiceException(CommonConstant.QUERY_FAILED);
  112. } catch (ApiRequestException apie) {
  113. log.error("[-apiVehicleRegister-] 错误信息:" + apie.getMessage());
  114. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  115. } catch (ServiceException se) {
  116. log.error("[-apiVehicleRegister-] 错误信息:" + se.getMessage());
  117. throw new ServiceException(se.getMessage());
  118. }
  119. return analyzeApiResultItems(result);
  120. }
  121. @Override
  122. public ApiResponseData wayBillStart(JSONObject param) {
  123. long costTimeStart = System.currentTimeMillis();
  124. String result;
  125. try {
  126. CarFreeCarrierBillStartDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillStartDto.class);
  127. validate(dto);
  128. WaybillStartRequest request = new WaybillStartRequest();
  129. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  130. request.setNum(dto.getNum());
  131. request.setPlateNum(dto.getPlateNumber());
  132. request.setPlateColor(dto.getPlateColor());
  133. request.setStartTime(dto.getStartTime());
  134. request.setSourceAddr(dto.getSourceAddr());
  135. request.setDestAddr(dto.getDestAddr());
  136. request.setPredictEndTime(dto.getPredictEndTime());
  137. request.setFee(dto.getFee());
  138. request.setTitleType(dto.getTitleType());
  139. if (!StringUtil.isNullOrEmpty(dto.getTaxPlayerCode())) {
  140. request.setTaxplayerCode(dto.getTaxPlayerCode());
  141. }
  142. String fileName = request.getFilename();
  143. // 调用upload 发送数据
  144. WaybillStartResponse response = ETCCommHelper.upload(fileName, request, WaybillStartResponse.class);
  145. dto.setIsHistory(CommonConstant.REALTIME_WAY_BILL);
  146. wayBillStartService.saveBillStart(dto);
  147. result = response.toJson();
  148. long costTimeEnd = System.currentTimeMillis();
  149. log.info("[-wayBillStart-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  150. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  151. + ",endTime=" + costTimeEnd);
  152. } catch (IOException e) {
  153. log.error("[-wayBillStart-] 网络异常 " + e);
  154. throw new ServiceException(CommonConstant.QUERY_FAILED);
  155. } catch (ApiRequestException apie) {
  156. log.error("[-wayBillStart-] 错误信息:" + apie.getMessage());
  157. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  158. } catch (ServiceException se) {
  159. log.error("[-wayBillStart-] 错误信息:" + se.getMessage());
  160. throw new ServiceException(se.getMessage());
  161. }
  162. return analyzeApiResultItems(result);
  163. }
  164. @Override
  165. @Transactional(rollbackFor = ServiceException.class)
  166. public ApiResponseData wayBillEnd(JSONObject param) {
  167. long costTimeStart = System.currentTimeMillis();
  168. String result;
  169. try {
  170. CarFreeCarrierBillEndDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillEndDto.class);
  171. validate(dto);
  172. WaybillEndRequest request = new WaybillEndRequest();
  173. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  174. request.setNum(dto.getNum());
  175. request.setRealDestAddr(dto.getRealDestAddr());
  176. request.setEndTime(dto.getEndTime());
  177. String fileName = request.getFilename();
  178. // 调用upload 发送数据
  179. WaybillEndResponse response = ETCCommHelper.upload(fileName, request, WaybillEndResponse.class);
  180. dto.setIsHistory(CommonConstant.REALTIME_WAY_BILL);
  181. wayBillEndService.saveBillEnd(dto);
  182. wayBillStartService.updateBillStartStatus(dto.getNum(), CommonConstant.STATUS_OVER);
  183. result = response.toJson();
  184. long costTimeEnd = System.currentTimeMillis();
  185. log.info("[-waybillEnd-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  186. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  187. + ",endTime=" + costTimeEnd);
  188. } catch (IOException e) {
  189. log.error("[-wayBillEnd-] 网络异常 " + e);
  190. throw new ServiceException(CommonConstant.QUERY_FAILED);
  191. } catch (ApiRequestException apie) {
  192. log.error("[-wayBillEnd-] 错误信息:" + apie.getMessage());
  193. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  194. } catch (ServiceException se) {
  195. log.error("[-wayBillEnd-] 错误信息:" + se.getMessage());
  196. throw new ServiceException(se.getMessage());
  197. }
  198. return analyzeApiResultItems(result);
  199. }
  200. @Override
  201. public ApiResponseData wayBillHistoryStart(JSONObject param) {
  202. long costTimeStart = System.currentTimeMillis();
  203. String result;
  204. try {
  205. CarFreeCarrierBillStartDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillStartDto.class);
  206. validate(dto);
  207. WaybillHistoryStartRequest request = new WaybillHistoryStartRequest();
  208. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  209. request.setNum(dto.getNum());
  210. request.setPlateNum(dto.getPlateNumber());
  211. request.setPlateColor(dto.getPlateColor());
  212. request.setStartTime(dto.getStartTime());
  213. request.setSourceAddr(dto.getSourceAddr());
  214. request.setDestAddr(dto.getDestAddr());
  215. request.setPredictEndTime(dto.getPredictEndTime());
  216. request.setFee(dto.getFee());
  217. request.setTitleType(dto.getTitleType());
  218. if (!StringUtil.isNullOrEmpty(dto.getTaxPlayerCode())) {
  219. request.setTaxplayerCode(dto.getTaxPlayerCode());
  220. }
  221. String fileName = request.getFilename();
  222. // 调用upload 发送数据
  223. WaybillHistoryStartResponse response = ETCCommHelper.upload(fileName, request, WaybillHistoryStartResponse.class);
  224. dto.setIsHistory(CommonConstant.HISTORY_WAY_BILL);
  225. wayBillStartService.saveBillStart(dto);
  226. result = response.toJson();
  227. long costTimeEnd = System.currentTimeMillis();
  228. log.info("[-wayBillHistoryStart-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  229. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  230. + ",endTime=" + costTimeEnd);
  231. } catch (IOException e) {
  232. log.error("[-wayBillHistoryStart-] 网络异常 " + e);
  233. throw new ServiceException(CommonConstant.QUERY_FAILED);
  234. } catch (ApiRequestException apie) {
  235. log.error("[-wayBillHistoryStart-] 错误信息:" + apie.getMessage());
  236. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  237. } catch (ServiceException se) {
  238. log.error("[-wayBillHistoryStart-] 错误信息:" + se.getMessage());
  239. throw new ServiceException(se.getMessage());
  240. }
  241. return analyzeApiResultItems(result);
  242. }
  243. @Override
  244. public ApiResponseData wayBillHistoryEnd(JSONObject param) {
  245. long costTimeStart = System.currentTimeMillis();
  246. String result;
  247. try {
  248. CarFreeCarrierBillEndDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillEndDto.class);
  249. validate(dto);
  250. WaybillHistoryEndRequest request = new WaybillHistoryEndRequest();
  251. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  252. request.setNum(dto.getNum());
  253. request.setRealDestAddr(dto.getRealDestAddr());
  254. request.setEndTime(dto.getEndTime());
  255. String fileName = request.getFilename();
  256. // 调用upload 发送数据
  257. WaybillHistoryEndResponse response = ETCCommHelper.upload(fileName, request, WaybillHistoryEndResponse.class);
  258. dto.setIsHistory(CommonConstant.HISTORY_WAY_BILL);
  259. wayBillEndService.saveBillEnd(dto);
  260. wayBillStartService.updateBillStartStatus(dto.getNum(), CommonConstant.STATUS_OVER);
  261. result = response.toJson();
  262. long costTimeEnd = System.currentTimeMillis();
  263. log.info("[-wayBillHistoryEnd-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  264. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  265. + ",endTime=" + costTimeEnd);
  266. } catch (IOException e) {
  267. log.error("[-wayBillHistoryEnd-] 网络异常 " + e);
  268. throw new ServiceException(CommonConstant.QUERY_FAILED);
  269. } catch (ApiRequestException apie) {
  270. log.error("[-wayBillHistoryEnd-] 错误信息:" + apie.getMessage());
  271. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  272. } catch (ServiceException se) {
  273. log.error("[-wayBillHistoryEnd-] 错误信息:" + se.getMessage());
  274. throw new ServiceException(se.getMessage());
  275. }
  276. return analyzeApiResultItems(result);
  277. }
  278. @Override
  279. public ApiResponseData wayBillNumFindInvoice(JSONObject param) {
  280. long costTimeStart = System.currentTimeMillis();
  281. String result;
  282. try {
  283. WayBillNumFindInvoiceDto dto = JSONObject.toJavaObject(param, WayBillNumFindInvoiceDto.class);
  284. validate(dto);
  285. WaybillNumFindInvoiceRequest request = new WaybillNumFindInvoiceRequest();
  286. request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
  287. request.setWaybillNum(dto.getNum());
  288. String fileName = request.getFilename();
  289. // 调用upload 发送数据
  290. WaybillNumFindInvoiceResponse response = ETCCommHelper.upload(fileName, request, WaybillNumFindInvoiceResponse.class);
  291. result = response.toJson();
  292. long costTimeEnd = System.currentTimeMillis();
  293. log.info("[-waiBillNumFindInvoice-] result is " + result.replaceAll("\r|\n", "") + " , request is "
  294. + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
  295. + ",endTime=" + costTimeEnd);
  296. } catch (IOException e) {
  297. log.error("[-waiBillNumFindInvoice-] 网络异常 " + e);
  298. throw new ServiceException(CommonConstant.QUERY_FAILED);
  299. } catch (ApiRequestException apie) {
  300. log.error("[-waiBillNumFindInvoice-] 错误信息:" + apie.getMessage());
  301. throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
  302. } catch (ServiceException se) {
  303. log.error("[-waiBillNumFindInvoice-] 错误信息:" + se.getMessage());
  304. throw new ServiceException(se.getMessage());
  305. }
  306. return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), result);
  307. }
  308. @Override
  309. public ApiResponseData findInvoice(JSONObject param) {
  310. try {
  311. CarFreeCarrierBillStartPo po = wayBillStartService.getBillStartByNum(param.getString("num"));
  312. if (po != null && po.getStatus().equals(CommonConstant.STATUS_INVOICE_OVER)) {
  313. // 数据库中已经存在运单开始信息,并且运单状态为已结束,直接从数据库中查询运单发票数据,封装返回
  314. wayBillStartService.updateBillStartIsSearch(param.getString("num"));
  315. // 结果集外层数据
  316. SearchInvoiceDto searchInvoiceDto = searchInvoiceService.getSearchInvoiceByNum(param.getString("num"));
  317. // 结果集发票集合
  318. List<InvoiceResult> invoiceResultList = searchInvoiceResultService.getSearchInvoiceResult(param.getString("num"), param.getLong("userId"));
  319. // 返回结果集
  320. InvoiceResultDto invoiceResultDto = new InvoiceResultDto();
  321. BeanUtil.copyProperties(invoiceResultDto, searchInvoiceDto);
  322. invoiceResultDto.setResult(invoiceResultList);
  323. return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), JSON.toJSONString(invoiceResultDto, SerializerFeature.WriteMapNullValue));
  324. } else if (po != null && !po.getStatus().equals(CommonConstant.STATUS_INVOICE_OVER)) {
  325. // 数据库中已经存在运单开始信息,并且运单状态非已结束,需要重新查询接口
  326. return apiCarFreeChargeService.charge(po);
  327. } else {
  328. // 数据库中没有运单开始信息(这部分数据为遗留数据,老平台迁移用户,没有运单开始数据的存储),需要调接口
  329. po = new CarFreeCarrierBillStartPo();
  330. po.setUserId(param.getLong("userId"));
  331. po.setNum(param.getString("num"));
  332. return apiCarFreeChargeService.charge(po);
  333. }
  334. } catch (ServiceException se) {
  335. log.error("[-findInvoice-] 错误信息:" + se.getMessage());
  336. throw new ServiceException(se.getMessage());
  337. }
  338. }
  339. @Override
  340. public ApiResponseData findNoSearchNums(JSONObject param) {
  341. try {
  342. List<String> nums = wayBillStartService.getNoSearchNums(param.getString("userId"));
  343. return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), JSON.toJSONString(nums));
  344. } catch (ServiceException e) {
  345. log.error("[-findNoSearchNums-] 错误信息:" + e.getMessage());
  346. throw new ServiceException(e.getMessage());
  347. }
  348. }
  349. }