OpenService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.date.DateUtil;
  5. import cn.hutool.core.img.ImgUtil;
  6. import cn.hutool.core.io.FileUtil;
  7. import cn.hutool.core.util.RandomUtil;
  8. import cn.hutool.core.util.RandomUtil;
  9. import cn.hutool.core.util.StrUtil;
  10. import cn.hutool.log.StaticLog;
  11. import com.pj.api.open.ResultJson;
  12. import com.pj.api.open.bo.CheckCarNumberBO;
  13. import com.pj.api.open.bo.InRecordBO;
  14. import com.pj.constants.UserTypeEnum;
  15. import com.pj.current.config.PartConfig;
  16. import com.pj.current.config.SystemObject;
  17. import com.pj.current.task.TaskService;
  18. import com.pj.project.tb_account.AutomaticPay;
  19. import com.pj.project.tb_business.TbBusiness;
  20. import com.pj.project.tb_business.TbBusinessService;
  21. import com.pj.project.tb_business_car.TbBusinessCar;
  22. import com.pj.project.tb_business_car.TbBusinessCarService;
  23. import com.pj.project.tb_business_item.TbBusinessItem;
  24. import com.pj.project.tb_business_item.TbBusinessItemService;
  25. import com.pj.project.tb_business_people.TbBusinessPeople;
  26. import com.pj.project.tb_business_people.TbBusinessPeopleService;
  27. import com.pj.project.tb_car.TbCar;
  28. import com.pj.project.tb_car.TbCarService;
  29. import com.pj.project.tb_car_no_color.TbCarNoColor;
  30. import com.pj.project.tb_car_no_color.TbCarNoColorService;
  31. import com.pj.project.tb_deduction_bind.TbDeductionBindService;
  32. import com.pj.project.tb_goods.TbGoods;
  33. import com.pj.project.tb_goods.TbGoodsService;
  34. import com.pj.project.tb_item.TbItem;
  35. import com.pj.project.tb_item_type.TbItemType;
  36. import com.pj.project4sp.global.BusinessException;
  37. import com.pj.project4sp.uploadfile.UploadConfig;
  38. import com.pj.utils.cache.RedisUtil;
  39. import com.pj.utils.sg.AjaxJson;
  40. import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
  41. import lombok.extern.slf4j.Slf4j;
  42. import org.springframework.context.annotation.Lazy;
  43. import org.springframework.scheduling.annotation.Async;
  44. import org.springframework.stereotype.Service;
  45. import org.springframework.transaction.annotation.Transactional;
  46. import sun.misc.BASE64Decoder;
  47. import javax.annotation.Resource;
  48. import javax.xml.crypto.Data;
  49. import java.io.File;
  50. import java.io.FileOutputStream;
  51. import java.io.OutputStream;
  52. import java.math.BigDecimal;
  53. import java.time.LocalDateTime;
  54. import java.time.format.DateTimeFormatter;
  55. import java.util.Collections;
  56. import java.util.Date;
  57. import java.util.List;
  58. import java.util.Objects;
  59. import java.util.stream.Collectors;
  60. @Service
  61. @Transactional
  62. @Slf4j
  63. public class OpenService {
  64. @Resource
  65. @Lazy
  66. private TbBusinessService tbBusinessService;
  67. @Resource
  68. @Lazy
  69. private TbBusinessCarService tbBusinessCarService;
  70. @Resource
  71. private TbCarService tbCarService;
  72. @Resource
  73. TaskService taskService;
  74. @Resource
  75. TbCarNoColorService tbCarNoColorService;
  76. @Resource
  77. private TbGoodsService tbGoodsService;
  78. @Resource
  79. @Lazy
  80. AutomaticPay automaticPay;
  81. @Resource
  82. private TbDeductionBindService tbDeductionBindService;
  83. @Resource
  84. private PartConfig partConfig;
  85. public ResultJson checkCarNumber(CheckCarNumberBO checkCarNumberBO) {
  86. String carNo = checkCarNumberBO.getCarNumber();
  87. int inOrOut = checkCarNumberBO.getInOrOut();
  88. String image = checkCarNumberBO.getImageData();
  89. if (inOrOut == 1) {
  90. handlerIn(carNo, checkCarNumberBO.getChannelName(), image);
  91. return ResultJson.success();
  92. }
  93. return handlerOut(carNo, checkCarNumberBO.getChannelName(), image);
  94. }
  95. private void handlerIn(String carNo, String channel, String image) {
  96. carNo = carNo.toUpperCase();
  97. if (StrUtil.isNotEmpty(image)) {
  98. StaticLog.error("缓存入场图片:{}", carNo);
  99. IMAGE_CACHE.put(carNo, image);
  100. }
  101. automaticPay.unbindRun(carNo);
  102. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  103. Date now = new Date();
  104. TbCar tbCar = tbCarService.findByCardNo(carNo);
  105. //1、记录不存在;
  106. // 2、已离场重新入场;
  107. // 3、抬杆离场
  108. if (tbBusinessCar == null
  109. || (tbBusinessCar.getRealInTime() != null && tbBusinessCar.getRealOutTime() != null)
  110. || (tbBusinessCar.getRealInTime() != null && tbBusinessCar.getRealOutTime() == null)) {
  111. //=======创建新的放行记录==========
  112. tbBusinessCar = new TbBusinessCar();
  113. tbBusinessCar.setCarNo(carNo).setPay(0).setCarSize(0D).setIsLock(0)
  114. .setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时")
  115. .setInChannel(channel).setRealInTime(now).setCarType(TbItem.ItemTypeEnum.EMPTY_TYPE.getType())
  116. .setBasePartMoney(new BigDecimal("0")).setTimeUpdate(now)
  117. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  118. tbBusinessCarService.save(tbBusinessCar);
  119. CoverBase64ToUrl(image, tbBusinessCar.getId(), carNo, 1);
  120. return;
  121. }
  122. tbBusinessCar.setRealInTime(now).setInChannel(channel).setTimeUpdate(now)
  123. .setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时");
  124. tbBusinessCarService.saveOrUpdate(tbBusinessCar);
  125. CoverBase64ToUrl(image, tbBusinessCar.getId(), carNo, 1);
  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. //判断是否蓝色车牌===免停车费
  158. if (StrUtil.isNotEmpty(tbBusinessCar.getColor())) {
  159. List<TbCarNoColor> freeList = tbCarNoColorService.getFreeColor();
  160. List<String> colorList = freeList.stream().map(f -> f.getCarNoColor().substring(0, 1)).collect(Collectors.toList());
  161. String color = tbBusinessCar.getColor().substring(0, 1);
  162. if (colorList.contains(color)) {
  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. if (tbBusinessCar.getIsLock() == 1) {
  173. RedisUtil.setByMINUTES(channel, carNo, 2);
  174. log.error("请求返回:车辆已锁定:{}", carNo);
  175. return ResultJson.error("车辆已锁定");
  176. }
  177. String businessCarId = tbBusinessCar.getId();
  178. //String carNoPrefix = StrUtil.sub(carNo, 0, 1);
  179. String carType = tbBusinessCar.getCarType();
  180. BigDecimal carMoney = new BigDecimal(0);
  181. List<TbBusiness> businessList = tbBusinessService.findOtherBusinessByCarId(businessCarId);
  182. if (businessList.isEmpty()) {//无业务车辆===>计算停车费
  183. //如果已经支付过,则从支付时间算起,重新计算停车费
  184. double dif = caulatePrice(tbBusinessCar, now);
  185. if (dif > 0) {
  186. RedisUtil.setByMINUTES(channel, carNo, 1);
  187. log.error("请求返回:请缴停车费:{},{}元", carNo, dif);
  188. automaticPay.run(null, tbBusinessCar.getCarNo(), 1);
  189. return ResultJson.error("请缴停车费" + dif + "元");
  190. } else {
  191. //无需交停车费,又没有支付的--->设置成免费车辆
  192. if (tbBusinessCar.getPayTime() == null) {
  193. tbBusinessCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType()).setPay(1);
  194. }
  195. freeOut(tbBusinessCar, now, channel, image);
  196. return ResultJson.success();
  197. }
  198. } else {
  199. //有业务的车
  200. //1、分越重车和空车
  201. //空车是否收费
  202. //该车所有的业务都是到达卡口才进行扣费才会进入自动扣费
  203. int autoPayCount = 0;
  204. for (TbBusiness business : businessList) {
  205. TbGoods tbGoods = tbGoodsService.getById(business.getGoodsId());
  206. if (TbGoods.DeductionTypeEnum.OUT_KK.getCode().equals(tbGoods.getAutoDeductionType())) {
  207. autoPayCount++;
  208. }
  209. }
  210. if (autoPayCount == businessList.size()) {
  211. log.info("车辆到达卡口进行扣停车费和业务费:{}", carNo);
  212. //自动缴费
  213. automaticPay.run(null, tbBusinessCar.getCarNo(), 3);
  214. tbDeductionBindService.autoUnbindCar(tbBusinessCar.getCarNo());
  215. freeOut(tbBusinessCar, now, channel, image);
  216. return ResultJson.success();
  217. }
  218. if (TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType)) {
  219. boolean chinaCarPay = businessList.stream().anyMatch(tbBusiness -> {
  220. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  221. return tbGoods.getChinaCarPay() == 1;
  222. });
  223. if (chinaCarPay) {
  224. double dif = caulatePrice(tbBusinessCar, now);
  225. if (dif > 0) {
  226. RedisUtil.setByMINUTES(channel, carNo, 1);
  227. log.error("中国车停车费:{},{}元", carNo, dif);
  228. carMoney = new BigDecimal(dif);
  229. // return ResultJson.error("请缴停车费" + dif + "元");
  230. } else {
  231. if (tbBusinessCar.getPayTime() == null) {
  232. tbBusinessCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType()).setPay(1);
  233. }
  234. }
  235. }
  236. } else {//越南车=重车
  237. boolean vietnamCarPay = businessList.stream().anyMatch(tbBusiness -> {
  238. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  239. return tbGoods.getVietnamCarPay() == 1;
  240. });
  241. if (vietnamCarPay) {
  242. double dif = caulatePrice(tbBusinessCar, now);
  243. if (dif > 0) {
  244. RedisUtil.setByMINUTES(channel, carNo, 1);
  245. log.error("越南车停车费:{},{}元", carNo, dif);
  246. carMoney = new BigDecimal(dif);
  247. // return ResultJson.error("请缴停车费" + dif + "元");
  248. } else {
  249. if (tbBusinessCar.getPayTime() == null) {
  250. tbBusinessCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType()).setPay(1);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. //============业务费计算是否交完
  257. BigDecimal businessMoney = new BigDecimal(0);
  258. for (TbBusiness tbBusiness : businessList) {
  259. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  260. int vietnamLeave = tbGoods.getVietnamCarLeave();
  261. int chinaCarLeave = tbGoods.getChinaCarLeave();
  262. boolean pay = tbBusiness.getPayMoney().doubleValue() >= tbBusiness.getItemPrice().doubleValue();
  263. //需要交业务费
  264. if (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType)) {//越南车==重车
  265. if (TbGoods.LeaveEnum.PART_MONEY.getCode() != vietnamLeave
  266. && !pay) {
  267. RedisUtil.setByMINUTES(channel, carNo, 1);
  268. log.error("越南车==重车:缴纳业务费用:{}", carNo);
  269. businessMoney = businessMoney.add(tbBusiness.getItemPrice());
  270. // return ResultJson.error("请缴纳业务费用");
  271. }
  272. } else {
  273. if (TbGoods.LeaveEnum.PART_MONEY.getCode() != chinaCarLeave
  274. && !pay) {
  275. RedisUtil.setByMINUTES(channel, carNo, 1);
  276. businessMoney = businessMoney.add(tbBusiness.getItemPrice());
  277. log.error("请求返回:请缴纳业务费用:{}", carNo);
  278. // return ResultJson.error("请缴纳业务费用");
  279. }
  280. }
  281. }
  282. BigDecimal total = businessMoney.add(carMoney);
  283. if (total.doubleValue() > 0) {
  284. log.error("请求返回:请缴费用:{},{}元", carNo, total.doubleValue());
  285. return ResultJson.error("请缴费用" + total.doubleValue() + "元");
  286. }
  287. //离场直接解绑
  288. tbDeductionBindService.autoUnbindCar(tbBusinessCar.getCarNo());
  289. freeOut(tbBusinessCar, now, channel, image);
  290. return ResultJson.success();
  291. }
  292. private double caulatePrice(TbBusinessCar tbBusinessCar, Date now) {
  293. Date inTime = tbBusinessCar.getRealInTime();
  294. if (tbBusinessCar.getPay() == 1 && tbBusinessCar.getRealOutTime() == null && tbBusinessCar.getPayTime() != null) {
  295. inTime = tbBusinessCar.getPayTime();
  296. }
  297. BigDecimal price = tbBusinessService.calculationPartMoney(inTime, now);
  298. return price.doubleValue();
  299. }
  300. private void freeOut(TbBusinessCar tbBusinessCar, Date now, String outChannel, String base64Image) {
  301. tbBusinessCar.setRealOutTime(now).setTimeUpdate(now).setOutChannel(outChannel);
  302. tbBusinessCarService.updateById(tbBusinessCar);
  303. String carNo = tbBusinessCar.getCarNo();
  304. CoverBase64ToUrl(base64Image, tbBusinessCar.getId(), carNo, 2);
  305. RedisUtil.del(outChannel);
  306. log.error("请求返回【放行】:{}", carNo);
  307. }
  308. private boolean checkFree(String carNo, int inOrOut, Date now) {
  309. TbCar tbCar = tbCarService.findByCardNo(carNo);
  310. if (tbCar != null) {
  311. if (inOrOut == 1) {
  312. tbCar.setLastInTime(now);
  313. } else {
  314. tbCar.setLastOutTime(now);
  315. }
  316. tbCarService.updateById(tbCar);
  317. }
  318. return tbCar != null && !TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType());
  319. }
  320. @Async
  321. public void handlerInRecord(InRecordBO inRecordBO) {
  322. List<TbCarNoColor> carNoColorList = tbCarNoColorService.getFreeColor();
  323. String freeColors = carNoColorList.stream().map(TbCarNoColor::getCarNoColor).collect(Collectors.joining(","));
  324. String carNo = inRecordBO.getCarNumber().trim().toUpperCase();
  325. // 入场==>取车牌颜色
  326. List<TbBusinessCar> cars = tbBusinessCarService.findByCarNo(carNo);
  327. cars.stream().filter(obj -> StrUtil.isEmpty(obj.getColor()))
  328. .forEach(tbBusinessCar -> {
  329. String color = inRecordBO.getCarColor();
  330. String prefixColor = StrUtil.sub(color, 0, 1);
  331. if (StrUtil.contains(freeColors, prefixColor)) {
  332. tbBusinessCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  333. }
  334. TbCar tbCar = tbCarService.findByCardNo(tbBusinessCar.getCarNo());
  335. if (tbCar != null && !TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  336. tbBusinessCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  337. }
  338. tbBusinessCar.setColor(color);
  339. tbBusinessCarService.updateById(tbBusinessCar);
  340. });
  341. }
  342. }