TbBusinessService.java 19 KB

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