TbBusinessService.java 19 KB

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