OpenService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package com.pj.api.open.service;
  2. import cn.hutool.cache.CacheUtil;
  3. import cn.hutool.cache.impl.TimedCache;
  4. import cn.hutool.core.util.RandomUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.json.JSONUtil;
  7. import cn.hutool.log.StaticLog;
  8. import com.pj.api.open.ResultJson;
  9. import com.pj.api.open.bo.CheckCarNumberBO;
  10. import com.pj.api.open.bo.InRecordBO;
  11. import com.pj.api.wx.bo.ManagerBO;
  12. import com.pj.constants.business.CarEnum;
  13. import com.pj.constants.business.GoodsEnum;
  14. import com.pj.constants.business.PayEnum;
  15. import com.pj.current.config.PartConfig;
  16. import com.pj.current.task.TaskService;
  17. import com.pj.project.sync.SyncService;
  18. import com.pj.project.sync.request.item.IOrderItem;
  19. import com.pj.project.sync.response.IOrderPriceRes;
  20. import com.pj.project.tb_account.AutomaticPay;
  21. import com.pj.project.tb_business.TbBusiness;
  22. import com.pj.project.tb_business.TbBusinessService;
  23. import com.pj.project.tb_business_car.TbBusinessCar;
  24. import com.pj.project.tb_business_car.TbBusinessCarService;
  25. import com.pj.project.tb_business_item.TbBusinessItem;
  26. import com.pj.project.tb_business_item.TbBusinessItemService;
  27. import com.pj.project.tb_car.TbCar;
  28. import com.pj.project.tb_car.TbCarService;
  29. import com.pj.project.tb_deduction_bind.TbDeductionBindService;
  30. import com.pj.project.tb_goods.TbGoods;
  31. import com.pj.project.tb_goods.TbGoodsService;
  32. import com.pj.project.tb_mild_car.TbMildCar;
  33. import com.pj.project.tb_mild_car.TbMildCarService;
  34. import com.pj.utils.cache.RedisUtil;
  35. import lombok.extern.slf4j.Slf4j;
  36. import org.springframework.context.annotation.Lazy;
  37. import org.springframework.scheduling.annotation.Async;
  38. import org.springframework.stereotype.Service;
  39. import org.springframework.transaction.annotation.Transactional;
  40. import javax.annotation.Resource;
  41. import java.math.BigDecimal;
  42. import java.time.LocalDateTime;
  43. import java.time.format.DateTimeFormatter;
  44. import java.util.ArrayList;
  45. import java.util.Date;
  46. import java.util.List;
  47. import java.util.stream.Collectors;
  48. @Service
  49. @Transactional
  50. @Slf4j
  51. public class OpenService {
  52. @Resource
  53. @Lazy
  54. private TbBusinessService tbBusinessService;
  55. @Resource
  56. @Lazy
  57. private TbBusinessCarService tbBusinessCarService;
  58. @Resource
  59. private TbCarService tbCarService;
  60. @Resource
  61. TaskService taskService;
  62. @Resource
  63. private TbGoodsService tbGoodsService;
  64. @Resource
  65. @Lazy
  66. AutomaticPay automaticPay;
  67. @Resource
  68. private TbDeductionBindService tbDeductionBindService;
  69. @Resource
  70. private PartConfig partConfig;
  71. @Resource
  72. private TbMildCarService tbMildCarService;
  73. @Resource
  74. private TbBusinessItemService tbBusinessItemService;
  75. @Resource
  76. private SyncService syncService;
  77. public ResultJson checkCarNumber(CheckCarNumberBO checkCarNumberBO) {
  78. String carNo = checkCarNumberBO.getCarNumber();
  79. int inOrOut = checkCarNumberBO.getInOrOut();
  80. String image = checkCarNumberBO.getImageData();
  81. if (inOrOut == 1) {
  82. return handlerIn(carNo, checkCarNumberBO.getChannelName(), image);
  83. }
  84. return handlerOut(carNo, checkCarNumberBO.getChannelName(), image);
  85. }
  86. private ResultJson handlerIn(String carNo, String channel, String image) {
  87. carNo = carNo.toUpperCase();
  88. if (StrUtil.isNotEmpty(image)) {
  89. StaticLog.error("缓存入场图片:{}", carNo);
  90. IMAGE_CACHE.put(carNo, image);
  91. }
  92. List<TbBusinessCar> notOutList = tbBusinessCarService.findNotOutCar(carNo);
  93. if (!notOutList.isEmpty()) {
  94. String msg = "车辆有" + notOutList.size() + "条未离场记录";
  95. log.error("入场返回:{}", msg);
  96. return ResultJson.error(msg);
  97. }
  98. // automaticPay.unbindRun(carNo);
  99. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  100. Date now = new Date();
  101. TbCar tbCar = tbCarService.findByCardNo(carNo);
  102. //1、记录不存在;
  103. // 2、已离场重新入场;
  104. if (tbBusinessCar == null
  105. || (tbBusinessCar.getRealInTime() != null && tbBusinessCar.getRealOutTime() != null)
  106. ) {
  107. //=======创建新的放行记录==========
  108. tbBusinessCar = new TbBusinessCar();
  109. tbBusinessCar.setCarNo(carNo).setPay(0).setCarSize(0D).setIsLock(0).setPayType(CarEnum.PayTypeEnum.NO_PAY_TYPE.getType())
  110. .setPay(0)
  111. .setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时").setCreateTime(now)
  112. .setInChannel(channel).setRealInTime(now).setCarType(CarEnum.CarTypeEnum.EMPTY_TYPE.getType())
  113. .setBasePartMoney(new BigDecimal("0")).setTimeUpdate(now).setConfirmJudge(CarEnum.ConfirmJudgeEnum.NO_NEED_JUDGE.getCode())
  114. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  115. tbBusinessCarService.save(tbBusinessCar);
  116. CoverBase64ToUrl(image, tbBusinessCar.getId(), carNo, 1);
  117. String msg = "无业务车辆";
  118. log.error("入场返回:{}", JSONUtil.toJsonStr(ResultJson.success(msg, msg)));
  119. return ResultJson.success(msg, msg);
  120. }
  121. tbBusinessCar.setRealInTime(now).setInChannel(channel).setTimeUpdate(now)
  122. .setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时");
  123. tbBusinessCarService.saveOrUpdate(tbBusinessCar);
  124. CoverBase64ToUrl(image, tbBusinessCar.getId(), carNo, 1);
  125. return ResultJson.success();
  126. }
  127. private void CoverBase64ToUrl(String base64image, String carId, String carNo, int inOrOut) {
  128. String cacheBase64 = IMAGE_CACHE.get(carNo);
  129. if (StrUtil.isEmpty(base64image)) {
  130. base64image = cacheBase64;
  131. }
  132. if (StrUtil.isEmpty(base64image)) {
  133. log.error("无出场图片:{}", carNo);
  134. return;
  135. }
  136. taskService.addTask(new CoverBase64ToImageTask("=======" + carId + "", 2000, carId, base64image, inOrOut));
  137. IMAGE_CACHE.remove(carNo);
  138. }
  139. final TimedCache<String, String> IMAGE_CACHE = CacheUtil.newTimedCache(30 * 60 * 1000);
  140. private ResultJson handlerOut(String carNo, String channel, String image) {
  141. carNo = carNo.toUpperCase();
  142. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  143. if (tbBusinessCar == null || tbBusinessCar.getRealInTime() == null || tbBusinessCar.getRealOutTime() != null) {
  144. log.error("请求返回【未查询到入场记录】:{}", carNo);
  145. return ResultJson.error("未查询到入场记录");
  146. }
  147. Date now = new Date();
  148. Date realInTime = tbBusinessCar.getRealInTime();
  149. if (now.getTime() / 1000 - realInTime.getTime() / 1000 < partConfig.getMinSeconds()) {
  150. log.error("请求返回【无效离场记录,返回到设备端为:“未查询到入场记录”让其停止轮询】:{}", carNo);
  151. return ResultJson.error("无效离场记录");
  152. }
  153. if (StrUtil.isNotEmpty(image)) {
  154. StaticLog.error("缓存离场图片,{}", carNo);
  155. IMAGE_CACHE.put(carNo, image);
  156. }
  157. //判断是否4.2米以下蓝色车牌===免停车费
  158. if (StrUtil.isNotEmpty(tbBusinessCar.getColor())) {
  159. String freeColor = partConfig.getFreeColor();
  160. //4蓝色车辆
  161. if (tbBusinessCar.getColor().contains(freeColor)) {
  162. log.error("请求返回【蓝牌未备案车辆,放行】:{},{}", carNo, ResultJson.success());
  163. freeOut(tbBusinessCar, now, channel, image);
  164. return ResultJson.success();
  165. }
  166. }
  167. //是否行政车辆=====
  168. if (checkFree(carNo, 2, now)) {
  169. freeOut(tbBusinessCar, now, channel, image);
  170. return ResultJson.success();
  171. }
  172. String businessCarId = tbBusinessCar.getId();
  173. String carType = tbBusinessCar.getCarType();
  174. IOrderPriceRes partMoneyRes;
  175. IOrderPriceRes businessRes = new IOrderPriceRes();
  176. List<IOrderItem> expenses = new ArrayList<>();
  177. List<TbBusiness> businessList = tbBusinessService.findOtherBusinessByCarId(businessCarId);
  178. long judgeCount = businessList.stream().filter(tbBusiness -> tbBusiness.getConfirmJudge() == CarEnum.ConfirmJudgeEnum.JUDGE_PASS.getCode()).count();
  179. int size = businessList.size();
  180. if (size > judgeCount) {
  181. //业务未审核完
  182. log.error("请求返回【业务未审核】:{}", carNo);
  183. return ResultJson.error("业务未审核");
  184. }
  185. BigDecimal hasPayPartMoney = tbBusinessCar.getMoney();
  186. if (businessList.isEmpty()) {
  187. //无业务车辆===>计算停车费
  188. //如果已经支付过,则从支付时间算起,重新计算停车费
  189. Date inTime = tbBusinessCar.getRealInTime();
  190. if (hasPayPartMoney.compareTo(BigDecimal.ZERO) > 0) {
  191. inTime = now;
  192. }
  193. IOrderPriceRes res = tbBusinessService.getPartMoney(inTime, now, carNo, tbBusinessCar.getColor());
  194. double dif = res.getTotalOrderPrice().subtract(tbBusinessCar.getMoney()).doubleValue();
  195. if (dif > 0) {
  196. RedisUtil.setByMINUTES(channel, carNo, 5);
  197. log.error("请求返回:请缴停车费:{},{}元", carNo, dif);
  198. //是否可以预充值缴费 true扣款成功 false否
  199. return ResultJson.error("请缴停车费" + dif + "元");
  200. } else {
  201. //无需交停车费,又没有支付的--->设置成免费车辆
  202. if (tbBusinessCar.getPayTime() == null) {
  203. tbBusinessCar.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType()).setPay(1);
  204. }
  205. freeOut(tbBusinessCar, now, channel, image);
  206. return ResultJson.success();
  207. }
  208. } else {
  209. //有业务的车--->都要交停车费
  210. //该车所有的业务都是到达卡口才进行扣费才会进入自动扣费
  211. Date inTime = tbBusinessCar.getRealInTime();
  212. if (hasPayPartMoney.compareTo(BigDecimal.ZERO) > 0) {
  213. inTime = now;
  214. }
  215. partMoneyRes = tbBusinessService.getPartMoney(inTime, now, carNo, tbBusinessCar.getColor());
  216. double dif = partMoneyRes.getTotalOrderPrice().doubleValue();
  217. if (dif > 0) {
  218. RedisUtil.setByMINUTES(channel, carNo, 5);
  219. log.error("停车费:{},{}元", carNo, dif);
  220. } else {
  221. if (tbBusinessCar.getPayTime() == null) {
  222. tbBusinessCar.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType()).setPay(1);
  223. }
  224. }
  225. }
  226. //============业务费计算是否交完
  227. //businessList = businessList.stream().filter(tbBusiness -> tbBusiness.getPayStatus() != PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode()).collect(Collectors.toList());
  228. List<String> businessIds = businessList.stream().map(TbBusiness::getId).collect(Collectors.toList());
  229. List<TbBusinessItem> businessItems = tbBusinessItemService.findByBusinessId(businessIds);
  230. for (TbBusiness tbBusiness : businessList) {
  231. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  232. //重车离场限制
  233. int weiCarLeave = tbGoods.getVietnamCarLeave();
  234. //空车离场限制
  235. int emptyCarLeave = tbGoods.getChinaCarLeave();
  236. boolean pay = tbBusiness.getPayMoney().doubleValue() >= tbBusiness.getItemPrice().doubleValue();
  237. //需要交业务费
  238. if (CarEnum.CarTypeEnum.WEIGHT_TYPE.getType().equals(carType)) {//==重车
  239. if (GoodsEnum.LeaveEnum.PART_MONEY.getCode() != weiCarLeave
  240. && !pay) {
  241. RedisUtil.setByMINUTES(channel, carNo, 5);
  242. tbBusinessItemService.buildExpenses(businessItems, expenses);
  243. log.error("越南车==重车:缴纳业务费用:{}", carNo);
  244. }
  245. } else {
  246. if (GoodsEnum.LeaveEnum.PART_MONEY.getCode() != emptyCarLeave
  247. && !pay) {
  248. RedisUtil.setByMINUTES(channel, carNo, 5);
  249. tbBusinessItemService.buildExpenses(businessItems, expenses);
  250. }
  251. }
  252. }
  253. if (!expenses.isEmpty()) {
  254. businessRes = syncService.orderPriceCal(expenses);
  255. }
  256. BigDecimal carMoney = partMoneyRes.getTotalOrderPrice();
  257. BigDecimal businessMoney = businessRes.getTotalOrderPrice();
  258. BigDecimal total = businessMoney.add(carMoney);
  259. //入场管理费。。。。
  260. //ManagerBO dto = tbBusinessService.needPayManagerMoney(carNo, businessCarId, businessItems);
  261. // total = dto.getPrice().add(total);
  262. if (total.doubleValue() > 0) {
  263. String desc = "请缴";
  264. if (businessMoney.doubleValue() > 0) {
  265. desc += "业务费" + businessMoney.toString() + "元";
  266. }
  267. // if (dto.getPrice().doubleValue() > 0) {
  268. // desc += " 入场管理费" + dto.getPrice().toString() + "元";
  269. // }
  270. if (carMoney.doubleValue() > 0) {
  271. desc += " 停车费" + carMoney.toString() + "元";
  272. }
  273. RedisUtil.setByMINUTES(channel, carNo, 5);
  274. // boolean flag = automaticPay.payBusinessAndPartMoney(businessList, tbBusinessCar, partMoneyRes);
  275. // if (flag) {
  276. //
  277. // log.error("预存款扣款成功,返回:{},{}", carNo, desc);
  278. // freeOut(tbBusinessCar, now, channel, image);
  279. // return ResultJson.success();
  280. // }
  281. log.error("请求返回:{},{}", carNo, desc);
  282. return ResultJson.error(desc);
  283. }
  284. freeOut(tbBusinessCar, now, channel, image);
  285. return ResultJson.success();
  286. }
  287. private void freeOut(TbBusinessCar tbBusinessCar, Date now, String outChannel, String base64Image) {
  288. tbBusinessCar.setRealOutTime(now).setTimeUpdate(now).setOutChannel(outChannel);
  289. if (tbBusinessCar.getPay() == 0) {
  290. tbBusinessCar.setPay(1);
  291. }
  292. tbBusinessCarService.updateById(tbBusinessCar);
  293. String carNo = tbBusinessCar.getCarNo();
  294. CoverBase64ToUrl(base64Image, tbBusinessCar.getId(), carNo, 2);
  295. RedisUtil.del(outChannel);
  296. log.error("请求返回【放行】:{}", carNo);
  297. }
  298. private boolean checkFree(String carNo, int inOrOut, Date now) {
  299. TbCar tbCar = tbCarService.findByCardNo(carNo);
  300. if (tbCar != null) {
  301. if (inOrOut == 1) {
  302. tbCar.setLastInTime(now);
  303. } else {
  304. tbCar.setLastOutTime(now);
  305. }
  306. tbCarService.updateById(tbCar);
  307. }
  308. return tbCar != null && !TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType());
  309. }
  310. @Async
  311. public void handlerInRecord(InRecordBO inRecordBO) {
  312. String carNo = inRecordBO.getCarNumber().trim().toUpperCase();
  313. // 入场==>取车牌颜色
  314. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  315. if (tbBusinessCar != null) {
  316. String color = inRecordBO.getCarColor();
  317. TbMildCar tbMildCar = tbMildCarService.findByCarNo(carNo);
  318. if (tbMildCar != null) {//车长
  319. tbBusinessCar.setCarSize(tbMildCar.getCarLength());
  320. }
  321. //蓝牌 4.2米以及以下
  322. if (color.contains(partConfig.getFreeColor())) {
  323. if (tbMildCar == null) {
  324. tbBusinessCar.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType()).setPay(1);
  325. }
  326. }
  327. TbCar tbCar = tbCarService.findByCardNo(tbBusinessCar.getCarNo());
  328. if (tbCar != null && !TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  329. tbBusinessCar.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  330. List<TbBusiness> businessList = tbBusinessService.findOtherBusinessByCarId(tbBusinessCar.getId());
  331. if (businessList.isEmpty()) {
  332. //无业务&免费车===>无需审核
  333. tbBusinessCar.setConfirmJudge(CarEnum.ConfirmJudgeEnum.NO_NEED_JUDGE.getCode());
  334. }
  335. }
  336. tbBusinessCar.setColor(color);
  337. tbBusinessCarService.updateById(tbBusinessCar);
  338. }
  339. }
  340. }