TbBusinessService.java 20 KB

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