TbBusinessService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. package com.pj.project.tb_business;
  2. import java.math.BigDecimal;
  3. import java.time.LocalDate;
  4. import java.time.LocalDateTime;
  5. import java.time.LocalTime;
  6. import java.time.ZoneId;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.temporal.ChronoUnit;
  9. import java.util.*;
  10. import java.util.stream.Collectors;
  11. import cn.hutool.core.bean.BeanUtil;
  12. import cn.hutool.core.date.DateUtil;
  13. import cn.hutool.core.util.NumberUtil;
  14. import cn.hutool.core.util.RandomUtil;
  15. import cn.hutool.core.util.StrUtil;
  16. import cn.hutool.json.JSONUtil;
  17. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  18. import com.baomidou.mybatisplus.extension.service.IService;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import com.pj.constants.UserTypeEnum;
  21. import com.pj.current.config.CarConfig;
  22. import com.pj.current.config.PartConfig;
  23. import com.pj.current.satoken.StpUserUtil;
  24. import com.pj.project.tb_business_car.TbBusinessCar;
  25. import com.pj.project.tb_business_car.TbBusinessCarService;
  26. import com.pj.project.tb_business_item.TbBusinessItem;
  27. import com.pj.project.tb_business_item.TbBusinessItemService;
  28. import com.pj.project.tb_business_people.TbBusinessPeople;
  29. import com.pj.project.tb_business_people.TbBusinessPeopleService;
  30. import com.pj.project.tb_car.TbCar;
  31. import com.pj.project.tb_car.TbCarService;
  32. import com.pj.project.tb_costomer.TbCostomer;
  33. import com.pj.project.tb_costomer.TbCostomerService;
  34. import com.pj.project.tb_declare.TbDeclare;
  35. import com.pj.project.tb_declare.TbDeclareService;
  36. import com.pj.project.tb_driver.TbDriver;
  37. import com.pj.project.tb_driver.TbDriverService;
  38. import com.pj.project.tb_item.TbItem;
  39. import com.pj.project.tb_item.TbItemService;
  40. import com.pj.project.tb_item_type.TbItemType;
  41. import com.pj.project.tb_item_type.TbItemTypeService;
  42. import com.pj.project.tb_notices.TbNoticesMapper;
  43. import com.pj.project.tb_notices.TbNoticesService;
  44. import com.pj.project.tb_pass_record.TbPassRecord;
  45. import com.pj.project.tb_pass_record.TbPassRecordService;
  46. import com.pj.project.tb_unit.TbUnit;
  47. import com.pj.project.tb_unit.TbUnitService;
  48. import com.pj.project4sp.admin.SpAdmin;
  49. import com.pj.project4sp.role.SpRoleUtil;
  50. import com.pj.project4sp.role4permission.SpRolePermissionService;
  51. import com.pj.utils.so.SoMap;
  52. import org.aspectj.weaver.loadtime.Aj;
  53. import org.omg.CORBA.OBJ_ADAPTER;
  54. import org.springframework.beans.factory.annotation.Autowired;
  55. import org.springframework.beans.factory.annotation.Value;
  56. import org.springframework.context.annotation.Lazy;
  57. import org.springframework.scheduling.annotation.Async;
  58. import org.springframework.stereotype.Service;
  59. import com.pj.utils.sg.*;
  60. import org.springframework.transaction.annotation.Transactional;
  61. import javax.annotation.Resource;
  62. /**
  63. * Service: tb_business -- 入境登记
  64. *
  65. * @author qzy
  66. */
  67. @Service
  68. @Transactional
  69. public class TbBusinessService extends ServiceImpl<TbBusinessMapper, TbBusiness> implements IService<TbBusiness> {
  70. /**
  71. * 底层 Mapper 对象
  72. */
  73. @Autowired
  74. TbBusinessMapper tbBusinessMapper;
  75. @Resource
  76. private TbCarService tbCarService;
  77. @Resource
  78. private TbDriverService tbDriverService;
  79. @Resource
  80. private TbCostomerService tbCostomerService;
  81. @Resource
  82. private TbItemService tbItemService;
  83. @Resource
  84. private TbItemTypeService tbItemTypeService;
  85. @Resource
  86. private TbBusinessItemService tbBusinessItemService;
  87. @Resource
  88. TbUnitService tbUnitService;
  89. @Resource
  90. TbBusinessPeopleService tbBusinessPeopleService;
  91. @Resource
  92. private PartConfig partConfig;
  93. @Resource
  94. private CarConfig carConfig;
  95. @Resource
  96. private TbNoticesService tbNoticesService;
  97. @Resource
  98. TbBusinessCarService tbBusinessCarService;
  99. @Resource
  100. @Lazy
  101. TbDeclareService tbDeclareService;
  102. public TbBusiness checkCarNo(String carNo) {
  103. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  104. ew.eq("card_no", carNo)
  105. .eq("admin_confirm_out", 0);
  106. return this.getOne(ew);
  107. }
  108. /**
  109. * 增
  110. */
  111. public AjaxJson addOrUpdate(TbBusiness t) {
  112. if (StrUtil.isNotEmpty(t.getId()) && StrUtil.isNotEmpty(t.getDeclareNo())) {
  113. TbBusiness db = this.getById(t.getId());
  114. if (!StrUtil.equals(db.getDeclareNo(), t.getDeclareNo())) {
  115. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(db.getDeclareNo());
  116. if (tbDeclare != null) {
  117. tbDeclare.setBusinessId("");
  118. tbDeclareService.updateById(tbDeclare);
  119. }
  120. }
  121. }
  122. Double carSize = t.getCardSize();
  123. if (carSize > carConfig.getMaxLength()) {
  124. return AjaxJson.getError("车辆规格不能大于" + carConfig.getMaxLength());
  125. }
  126. double netWeight = t.getNetWeight();
  127. if (netWeight > carConfig.getMaxWeight()) {
  128. return AjaxJson.getError("车辆载重不能大于" + carConfig.getMaxWeight());
  129. }
  130. t.setCreateBy(StpUserUtil.getAdmin().getName());
  131. createCarUnit(t);
  132. String customerId = t.getCustomerId();
  133. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  134. t.setCustomerName(tbCostomer.getName()).setCustomerType(tbCostomer.getPayType());
  135. if (StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  136. t.setConfirmInputBy(StpUserUtil.getAdmin().getName())
  137. .setConfirmInput(1)
  138. .setConfirmInputTime(new Date());
  139. }
  140. if (StrUtil.isEmpty(t.getId())) {
  141. t.setCreateTime(new Date());
  142. t.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  143. storeMsg(t.getCustomerId(), "新增一条业务数据:【" + t.getNo() + "】" + DateUtil.now() + "。");
  144. } else {
  145. storeMsg(t.getCustomerId(), "业务订单:【" + t.getNo() + "】数据发生更改," + DateUtil.now() + "。");
  146. }
  147. List<TbBusinessItem> items = JSONUtil.toList(t.getItemJson(), TbBusinessItem.class);
  148. saveOrUpdate(t);
  149. createBusinessPeople(t);
  150. tbBusinessItemService.removeByBusinessId(t.getId());
  151. BigDecimal price = new BigDecimal(0);
  152. for (TbBusinessItem item : items) {
  153. Double num = NumberUtil.parseDouble(item.getNum());
  154. TbItem tbItem = tbItemService.getById(item.getItemId());
  155. price = price.add(NumberUtil.mul(num, tbItem.getPrice()));
  156. TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId());
  157. item.setBusinessId(t.getId()).setItemCode(tbItem.getItemCode())
  158. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  159. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName())
  160. .setUnit(tbItem.getUnit()).setTotal(NumberUtil.mul(num, tbItem.getPrice()));
  161. }
  162. List<TbBusinessCar> carList = JSONUtil.toList(t.getCarJson(), TbBusinessCar.class);
  163. String chinaCarNo = carList.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  164. t.setChinaCarNo(StrUtil.isNotEmpty(chinaCarNo) ? chinaCarNo.toUpperCase() : "");
  165. carList.forEach(tbBusinessCar -> {
  166. String carNo = tbBusinessCar.getCarNo().toUpperCase();
  167. TbBusinessCar db = tbBusinessCarService.findByBusinessIdAndCarNo(t.getId(), carNo);
  168. if (db != null) {
  169. tbBusinessCar.setId(db.getId());
  170. }
  171. TbBusinessCar car = tbBusinessCarService.findInAndNoBusinessCar(carNo);
  172. if (car != null) {
  173. tbBusinessCar.setId(car.getId()).setRealInTime(car.getRealInTime());
  174. }
  175. tbBusinessCar.setBusinessId(t.getId());
  176. tbBusinessCar.setCarNo(carNo);
  177. tbBusinessCar.setBusinessId(t.getId()).setCustomerId(t.getCustomerId());
  178. });
  179. String yueCarNo = t.getCardNo().toUpperCase();
  180. TbBusinessCar yueCar = tbBusinessCarService.findInAndNoBusinessCar(yueCarNo);
  181. if (yueCar == null) {
  182. yueCar = tbBusinessCarService.findByBusinessIdAndCarNo(t.getId(), yueCarNo);
  183. if (yueCar == null) {
  184. yueCar = new TbBusinessCar();
  185. }
  186. }
  187. yueCar.setBusinessId(t.getId()).setCarSize(t.getCardSize()).setCarNo(yueCarNo).setCustomerId(t.getCustomerId());
  188. carList.add(yueCar);
  189. tbBusinessCarService.saveOrUpdateBatch(carList);
  190. BigDecimal initCarPartMoney = partConfig.getBasePrice();
  191. t.setItemPrice(price)
  192. .setTotalMoney(price.add(initCarPartMoney.multiply(new BigDecimal(carList.size()))))
  193. .setChinaPartMoney(initCarPartMoney.multiply(new BigDecimal(carList.size() - 1)))
  194. .setPartMoney(initCarPartMoney);
  195. this.saveOrUpdate(t);
  196. tbBusinessItemService.saveBatch(items);
  197. if (StrUtil.isNotEmpty(t.getDeclareNo())) {
  198. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(t.getDeclareNo());
  199. tbDeclare.setBusinessId(t.getId());
  200. tbDeclareService.updateById(tbDeclare);
  201. }
  202. return AjaxJson.getSuccess();
  203. }
  204. public void storeMsg(String businessCustomerId, String content) {
  205. String currentCustomerId = StpUserUtil.getCustomerId();
  206. if (StrUtil.equals(currentCustomerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  207. BusinessMessageManager.set(businessCustomerId, content);
  208. } else {
  209. BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), content);
  210. }
  211. }
  212. private void createBusinessPeople(TbBusiness t) {
  213. tbBusinessPeopleService.removeByBusinessId(t.getId());
  214. List<TbBusinessPeople> peopleList = JSONUtil.toList(t.getPeopleJson(), TbBusinessPeople.class);
  215. peopleList.forEach(tbBusinessPeople -> tbBusinessPeople.setBusinessId(t.getId()).setCreateTime(new Date()));
  216. tbBusinessPeopleService.saveBatch(peopleList);
  217. }
  218. private void createCarUnit(TbBusiness t) {
  219. TbUnit tbUnit = tbUnitService.findByUnit(t.getCardSize());
  220. if (tbUnit == null) {
  221. tbUnit = new TbUnit();
  222. tbUnit.setUnit(t.getCardSize() + "");
  223. tbUnitService.save(tbUnit);
  224. }
  225. }
  226. private TbDriver createDriver(TbBusiness business) {
  227. TbDriver tbDriver = tbDriverService.findByIdCardNo(business.getCardNo());
  228. if (tbDriver == null) {
  229. tbDriver = new TbDriver();
  230. tbDriver.setCreateTime(new Date()).setIdCard(business.getCardNo())
  231. .setName(business.getDriverName()).setPhone(business.getDriverPhone());
  232. tbDriverService.save(tbDriver);
  233. }
  234. return tbDriver;
  235. }
  236. private TbCar createCar(TbBusiness business) {
  237. String cardNo = business.getCardNo();
  238. TbCar tbCar = tbCarService.findByCardNo(cardNo);
  239. if (tbCar == null) {
  240. tbCar = new TbCar();
  241. }
  242. tbCar.setCardNo(cardNo)
  243. .setCardSize(business.getCardSize())
  244. .setCountryName(business.getCountryName())
  245. .setLastGoodsName(business.getGoodsName())
  246. .setLastNetWeight(business.getNetWeight());
  247. tbCarService.saveOrUpdate(tbCar);
  248. return tbCar;
  249. }
  250. /**
  251. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  252. */
  253. public List<TbBusiness> getList(SoMap so) {
  254. return tbBusinessMapper.getList(so);
  255. }
  256. public void pay(String id, String payTicket) {
  257. TbBusiness tbBusiness = this.getById(id);
  258. SpAdmin admin = StpUserUtil.getAdmin();
  259. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  260. .setPayBy(StpUserUtil.getAdmin().getName())
  261. .setPayTicket(payTicket)
  262. .setPayTime(new Date());
  263. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  264. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  265. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  266. }
  267. this.updateById(tbBusiness);
  268. storeMsg(tbBusiness.getCustomerId(), "业务订单【" + tbBusiness.getNo() + "】已支付" + DateUtil.now() + "。");
  269. //发送消息
  270. String text = "您的费用已支付,业务单号[" + tbBusiness.getNo() + "]";
  271. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  272. }
  273. public void confirm(List<String> ids) {
  274. ids.forEach(id -> {
  275. TbBusiness business = this.getById(id);
  276. business.setConfirmInput(1).setConfirmInputTime(new Date()).setConfirmInputBy(StpUserUtil.getAdmin().getName());
  277. this.updateById(business);
  278. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认" + DateUtil.now() + "。");
  279. });
  280. }
  281. public void adminConfirmPay(List<String> ids, String ticket) {
  282. ids.forEach(id -> {
  283. TbBusiness business = this.getById(id);
  284. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  285. .setPayStatus(3).setPayTicket(ticket);
  286. this.updateById(business);
  287. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认支付" + DateUtil.now() + "。");
  288. //发送消息
  289. String text = "您的费用已支付,业务单号[" + business.getNo() + "]";
  290. tbNoticesService.sendNotice(business.getId(), business.getNo(), business.getCustomerId(), text);
  291. });
  292. }
  293. public TbBusiness findCarTheNewRecord(String carNo, int inOrOut) {
  294. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  295. ew.eq("card_no", carNo);
  296. if (inOrOut == 1) {
  297. ew.eq("admin_confirm_in", 0).isNull("in_channel");
  298. } else {
  299. ew.eq("admin_confirm_out", 0).isNull("out_channel").isNotNull("real_in_time");
  300. }
  301. return getOne(ew);
  302. }
  303. @Deprecated
  304. public void adminConfirmIn(String id, String inChannel) {
  305. TbBusiness tbBusiness = this.getById(id);
  306. tbBusiness.setRealInTime(new Date()).setInChannel(inChannel);
  307. this.updateById(tbBusiness);
  308. //更新境外车入场记录
  309. //更新中国车入场记录
  310. //发送消息
  311. String text = "您的车辆已入场,业务单号[" + tbBusiness.getNo() + "]";
  312. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  313. }
  314. public void adminOut(String id, Date outDayTime, String outChannel, String carType) {
  315. TbBusiness tbBusiness = this.getById(id);
  316. tbBusiness.setOutDayTime(outDayTime).setOutChannel(outChannel);
  317. calculationPartMoney(carType, tbBusiness);
  318. this.updateById(tbBusiness);
  319. //更新境外车出场记录
  320. //更新中国车出场记录
  321. //发送消息
  322. String text = "您的车辆已离场,业务单号[" + tbBusiness.getNo() + "]";
  323. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  324. }
  325. @Async
  326. public void calculationPartMoney(String itemType, TbBusiness tbBusiness) {
  327. Date iTime = tbBusiness.getRealInTime();
  328. Date oTime = tbBusiness.getOutDayTime();
  329. if (itemType.contains("中国")) {
  330. iTime = tbBusiness.getChinaCarInTime();
  331. oTime = tbBusiness.getChinaCarOutTime();
  332. }
  333. BigDecimal price = calculationPartMoney(iTime, oTime, tbBusiness.getCardSize());
  334. if (itemType.contains("中国")) {
  335. int len = tbBusiness.getCardNo().split(" ").length;
  336. tbBusiness.setChinaPartMoney(price.multiply(new BigDecimal(len)));
  337. } else {
  338. tbBusiness.setPartMoney(price);
  339. }
  340. tbBusiness.setTotalMoney(tbBusiness.getItemPrice().add(tbBusiness.getChinaPartMoney()).add(tbBusiness.getPartMoney()));
  341. }
  342. public BigDecimal calculationPartMoney(Date iTime, Date oTime, double carSize) {
  343. LocalDateTime inDayTime = iTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  344. LocalDateTime outDayTime = oTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  345. long minutes = ChronoUnit.MINUTES.between(inDayTime, outDayTime);
  346. BigDecimal zero = new BigDecimal("0");
  347. if (minutes < 0) {
  348. return zero;
  349. }
  350. if (carSize < partConfig.getFreeCarLength() || minutes <= partConfig.getFreeMinutes()) {
  351. return zero;
  352. }
  353. long days = ChronoUnit.DAYS.between(inDayTime.toLocalDate(), outDayTime.toLocalDate());
  354. long hour = ChronoUnit.HOURS.between(inDayTime, outDayTime);
  355. BigDecimal basePrice = partConfig.getBasePrice();
  356. BigDecimal extraPrice = partConfig.getExtraPrice();
  357. return basePrice.add(basePrice.multiply(new BigDecimal(hour / 24))).add(new BigDecimal(days).multiply(extraPrice));
  358. }
  359. /**
  360. * @param carNo
  361. * @param inOrOut 1入 2出
  362. * @return
  363. */
  364. public TbBusiness findByChinaCarNo(String carNo, int inOrOut) {
  365. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  366. ew.like("china_car_no", carNo);
  367. if (inOrOut == 1) {
  368. ew.eq("admin_confirm_in", 0).isNull("china_car_in_channel");
  369. } else {
  370. ew.eq("admin_confirm_out", 0)
  371. .isNull("china_car_out_channel")
  372. .isNotNull("china_car_in_time");
  373. }
  374. return this.getOne(ew);
  375. }
  376. public TbBusiness finByNo(String no) {
  377. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  378. ew.eq("no", no);
  379. return getOne(ew);
  380. }
  381. public Map<String, Object> getBusinessMoney(String businessId, String state) {
  382. Map<String, Object> result = new HashMap<>();
  383. List<TbBusinessCar> cars = tbBusinessCarService.findByBusinessId(businessId);
  384. Date now = new Date();
  385. List<Map<String, Object>> carsList = new ArrayList<>();
  386. final BigDecimal[] total = {new BigDecimal("0")};
  387. cars.forEach(tbBusinessCar -> {
  388. double partMoney = tbBusinessCar.getBasePartMoney().doubleValue();
  389. if (!StrUtil.equals("STATE", state)) {
  390. Date inTime = tbBusinessCar.getRealInTime();
  391. if (inTime != null) {
  392. BigDecimal price = this.calculationPartMoney(inTime, now, tbBusinessCar.getCarSize());
  393. BigDecimal money = tbBusinessCar.getMoney();
  394. BigDecimal basePrice = tbBusinessCar.getBasePartMoney();
  395. partMoney = money == null ? price.subtract(basePrice).doubleValue() : price.subtract(money).doubleValue();
  396. } else {
  397. partMoney = 0D;
  398. }
  399. }
  400. if (partMoney > 0) {
  401. total[0] = total[0].add(new BigDecimal(partMoney));
  402. Map<String, Object> carMap = new HashMap<>();
  403. carMap.put("price", partMoney);
  404. carMap.put("id", tbBusinessCar.getId());
  405. carMap.put("carNo", tbBusinessCar.getCarNo());
  406. carsList.add(carMap);
  407. }
  408. });
  409. result.put("carList", carsList);
  410. TbBusiness tbBusiness = this.getById(businessId);
  411. if (tbBusiness != null && tbBusiness.getPayStatus() == TbBusiness.PayStatus.NO_PAY.getCode()) {
  412. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(businessId);
  413. result.put("itemsPrice", tbBusiness.getItemPrice());
  414. result.put("businessId", businessId);
  415. List<Map<String, Object>> itemList = new ArrayList<>();
  416. items.forEach(item -> {
  417. Map<String, Object> itemMap = new HashMap<>();
  418. itemMap.put("id", item.getId());
  419. itemMap.put("name", item.getItemName());
  420. itemMap.put("price", item.getItemPrice());
  421. total[0] = total[0].add(item.getItemPrice());
  422. itemList.add(itemMap);
  423. });
  424. result.put("itemList", itemList);
  425. }
  426. result.put("total", total[0]);
  427. return result;
  428. }
  429. }