TbBusinessService.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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.api.bo.OtherBusinessBO;
  21. import com.pj.api.bo.OtherBusinessEditBO;
  22. import com.pj.api.open.ResultJson;
  23. import com.pj.api.wx.bo.MsgDataBO;
  24. import com.pj.api.wx.service.WxService;
  25. import com.pj.constants.UserTypeEnum;
  26. import com.pj.current.config.CarConfig;
  27. import com.pj.current.config.MyConfig;
  28. import com.pj.current.config.PartConfig;
  29. import com.pj.current.config.WxConfig;
  30. import com.pj.current.satoken.StpUserUtil;
  31. import com.pj.project.tb_business_car.TbBusinessCar;
  32. import com.pj.project.tb_business_car.TbBusinessCarService;
  33. import com.pj.project.tb_business_item.TbBusinessItem;
  34. import com.pj.project.tb_business_item.TbBusinessItemService;
  35. import com.pj.project.tb_business_people.TbBusinessPeople;
  36. import com.pj.project.tb_business_people.TbBusinessPeopleService;
  37. import com.pj.project.tb_car.TbCar;
  38. import com.pj.project.tb_car.TbCarService;
  39. import com.pj.project.tb_car_no_color.TbCarNoColor;
  40. import com.pj.project.tb_car_no_color.TbCarNoColorService;
  41. import com.pj.project.tb_costomer.TbCostomer;
  42. import com.pj.project.tb_costomer.TbCostomerService;
  43. import com.pj.project.tb_declare.TbDeclare;
  44. import com.pj.project.tb_declare.TbDeclareService;
  45. import com.pj.project.tb_driver.TbDriver;
  46. import com.pj.project.tb_driver.TbDriverService;
  47. import com.pj.project.tb_goods.TbGoods;
  48. import com.pj.project.tb_goods.TbGoodsService;
  49. import com.pj.project.tb_item.TbItem;
  50. import com.pj.project.tb_item.TbItemService;
  51. import com.pj.project.tb_item_type.TbItemType;
  52. import com.pj.project.tb_item_type.TbItemTypeService;
  53. import com.pj.project.tb_notices.TbNoticesMapper;
  54. import com.pj.project.tb_notices.TbNoticesService;
  55. import com.pj.project.tb_pass_record.TbPassRecord;
  56. import com.pj.project.tb_pass_record.TbPassRecordService;
  57. import com.pj.project.tb_unit.TbUnit;
  58. import com.pj.project.tb_unit.TbUnitService;
  59. import com.pj.project4sp.admin.SpAdmin;
  60. import com.pj.project4sp.global.BusinessException;
  61. import com.pj.project4sp.role.SpRoleUtil;
  62. import com.pj.project4sp.role4permission.SpRolePermissionService;
  63. import com.pj.utils.so.SoMap;
  64. import org.aspectj.weaver.loadtime.Aj;
  65. import org.omg.CORBA.OBJ_ADAPTER;
  66. import org.springframework.beans.factory.annotation.Autowired;
  67. import org.springframework.beans.factory.annotation.Value;
  68. import org.springframework.context.annotation.Lazy;
  69. import org.springframework.scheduling.annotation.Async;
  70. import org.springframework.stereotype.Service;
  71. import com.pj.utils.sg.*;
  72. import org.springframework.transaction.annotation.Transactional;
  73. import javax.annotation.Resource;
  74. /**
  75. * Service: tb_business -- 入境登记
  76. *
  77. * @author qzy
  78. */
  79. @Service
  80. @Transactional
  81. public class TbBusinessService extends ServiceImpl<TbBusinessMapper, TbBusiness> implements IService<TbBusiness> {
  82. /**
  83. * 底层 Mapper 对象
  84. */
  85. @Autowired
  86. TbBusinessMapper tbBusinessMapper;
  87. @Resource
  88. private TbCarService tbCarService;
  89. @Resource
  90. private TbDriverService tbDriverService;
  91. @Resource
  92. private TbCostomerService tbCostomerService;
  93. @Resource
  94. private TbItemService tbItemService;
  95. @Resource
  96. private TbItemTypeService tbItemTypeService;
  97. @Resource
  98. private TbBusinessItemService tbBusinessItemService;
  99. @Resource
  100. TbUnitService tbUnitService;
  101. @Resource
  102. TbBusinessPeopleService tbBusinessPeopleService;
  103. @Resource
  104. private PartConfig partConfig;
  105. @Resource
  106. private CarConfig carConfig;
  107. @Resource
  108. private TbNoticesService tbNoticesService;
  109. @Resource
  110. private TbCarNoColorService tbCarNoColorService;
  111. @Resource
  112. TbBusinessCarService tbBusinessCarService;
  113. @Resource
  114. @Lazy
  115. TbDeclareService tbDeclareService;
  116. @Resource
  117. WxConfig wxConfig;
  118. @Resource
  119. @Lazy
  120. WxService wxService;
  121. @Resource
  122. MyConfig myConfig;
  123. private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");
  124. public TbBusiness checkCarNo(String carNo) {
  125. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  126. ew.eq("card_no", carNo)
  127. .eq("admin_confirm_out", 0);
  128. return this.getOne(ew);
  129. }
  130. /**
  131. * 增
  132. */
  133. public AjaxJson addOrUpdate(TbBusiness t) {
  134. t.setBusinessType(TbBusiness.BusinessType.HOLD_CAR.getCode());
  135. if (StrUtil.isEmpty(t.getDeclareNo())) {
  136. throw new BusinessException("请选择申报单");
  137. }
  138. if (StrUtil.isNotEmpty(t.getId()) && StrUtil.isNotEmpty(t.getDeclareNo())) {
  139. TbBusiness db = this.getById(t.getId());
  140. if (!StrUtil.equals(db.getDeclareNo(), t.getDeclareNo())) {
  141. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(db.getDeclareNo());
  142. if (tbDeclare != null) {
  143. tbDeclare.setBusinessId("");
  144. tbDeclareService.updateById(tbDeclare);
  145. }
  146. }
  147. }
  148. Double carSize = t.getCardSize();
  149. if (carSize != null && carSize > carConfig.getMaxLength()) {
  150. return AjaxJson.getError("车辆规格不能大于" + carConfig.getMaxLength());
  151. }
  152. Double netWeight = t.getNetWeight();
  153. if (netWeight != null && netWeight > carConfig.getMaxWeight()) {
  154. return AjaxJson.getError("车辆载重不能大于" + carConfig.getMaxWeight());
  155. }
  156. t.setCreateBy(StpUserUtil.getAdmin().getName());
  157. createCarUnit(t);
  158. String customerId = t.getCustomerId();
  159. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  160. t.setCustomerName(tbCostomer.getName()).setCustomerType(tbCostomer.getPayType());
  161. if (StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  162. t.setConfirmInputBy(StpUserUtil.getAdmin().getName())
  163. .setConfirmInput(1)
  164. .setConfirmInputTime(new Date());
  165. }
  166. if (StrUtil.isEmpty(t.getId())) {
  167. t.setCreateTime(new Date());
  168. t.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  169. storeMsg(t.getCustomerId(), "新增一条业务数据:【" + t.getNo() + "】" + DateUtil.now() + "。");
  170. } else {
  171. storeMsg(t.getCustomerId(), "业务订单:【" + t.getNo() + "】数据发生更改," + DateUtil.now() + "。");
  172. }
  173. saveOrUpdate(t);
  174. createBusinessPeople(t);
  175. tbBusinessItemService.removeByBusinessId(t.getId());
  176. BigDecimal price = new BigDecimal(0);
  177. List<TbBusinessItem> items = JSONUtil.toList(t.getItemJson(), TbBusinessItem.class);
  178. int index = 1;
  179. for (TbBusinessItem item : items) {
  180. Double num = NumberUtil.parseDouble(item.getNum());
  181. TbItem tbItem = tbItemService.getById(item.getItemId());
  182. String typeId = tbItem.getTypeId();
  183. price = price.add(NumberUtil.mul(num, tbItem.getPrice()));
  184. TbItemType tbItemType = tbItemTypeService.getById(typeId);
  185. item.setBusinessId(t.getId()).setItemCode(tbItem.getItemCode()).setBusinessType(tbItem.getBusinessType())
  186. .setPayType(tbItem.getPayType()).setPayTypeName(tbItem.getPayTypeName())
  187. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  188. .setTaxRate(NumberUtil.div(tbItem.getTaxRate().doubleValue(), 100D, 2))
  189. .setItemTypeId(typeId).setItemTypeName(tbItemType.getName()).setBusinessType(tbItemType.getBusinessType())
  190. .setUnit(tbItem.getUnit()).setTotal(NumberUtil.mul(num, tbItem.getPrice())).setCreateTime(new Date())
  191. .setNo(t.getNo() + "0" + index);
  192. tbBusinessItemService.save(item);
  193. index++;
  194. }
  195. List<TbBusinessCar> carList = JSONUtil.toList(t.getCarJson(), TbBusinessCar.class);
  196. String chinaCarNo = carList.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  197. t.setChinaCarNo(StrUtil.isNotEmpty(chinaCarNo) ? chinaCarNo.toUpperCase() : "");
  198. carList.forEach(tbBusinessCar -> {
  199. tbBusinessCar.setIsLock(0);
  200. String carNo = tbBusinessCar.getCarNo().toUpperCase();
  201. TbBusinessCar db = tbBusinessCarService.findByBusinessIdAndCarNo(t.getId(), carNo);
  202. if (db != null) {
  203. tbBusinessCar.setId(db.getId()).setIsLock(db.getIsLock());
  204. }
  205. TbBusinessCar car = tbBusinessCarService.findInAndNoBusinessCar(carNo);
  206. if (car != null) {
  207. tbBusinessCar.setId(car.getId()).setRealInTime(car.getRealInTime()).setIsLock(car.getIsLock());
  208. }
  209. TbCar tbCar = tbCarService.findByCardNo(carNo);
  210. tbBusinessCar.setBusinessId(t.getId()).setBusinessType(TbBusiness.BusinessType.HOLD_CAR.getCode());
  211. tbBusinessCar.setCarNo(carNo).setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时");
  212. tbBusinessCar.setBusinessId(t.getId()).setCustomerId(t.getCustomerId());
  213. if (StrUtil.isEmpty(tbBusinessCar.getNo())) {
  214. tbBusinessCar.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  215. }
  216. });
  217. String yueCarNo = t.getCardNo().toUpperCase();
  218. TbBusinessCar yueCar = tbBusinessCarService.findInAndNoBusinessCar(yueCarNo);
  219. if (yueCar == null) {
  220. yueCar = tbBusinessCarService.findByBusinessIdAndCarNo(t.getId(), yueCarNo);
  221. if (yueCar == null) {
  222. yueCar = new TbBusinessCar();
  223. yueCar.setIsLock(0);
  224. }
  225. }
  226. yueCar.setBusinessId(t.getId()).setCarSize(t.getCardSize()).setCarNo(yueCarNo).setCustomerId(t.getCustomerId());
  227. if (StrUtil.isEmpty(yueCar.getNo())) {
  228. yueCar.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  229. }
  230. yueCar.setBusinessType(TbBusiness.BusinessType.HOLD_CAR.getCode());
  231. carList.add(yueCar);
  232. tbBusinessCarService.saveOrUpdateBatch(carList);
  233. // BigDecimal initCarPartMoney = partConfig.getBasePrice();
  234. t.setItemPrice(price)
  235. .setTotalMoney(price);
  236. this.saveOrUpdate(t);
  237. if (StrUtil.isNotEmpty(t.getDeclareNo())) {
  238. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(t.getDeclareNo());
  239. tbDeclare.setBusinessId(t.getId());
  240. tbDeclareService.updateById(tbDeclare);
  241. }
  242. return AjaxJson.getSuccess();
  243. }
  244. public void storeMsg(String businessCustomerId, String content) {
  245. String currentCustomerId = StpUserUtil.getCustomerId();
  246. if (StrUtil.equals(currentCustomerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  247. BusinessMessageManager.set(businessCustomerId, content);
  248. } else {
  249. BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), content);
  250. }
  251. }
  252. private void createBusinessPeople(TbBusiness t) {
  253. tbBusinessPeopleService.removeByBusinessId(t.getId());
  254. List<TbBusinessPeople> peopleList = JSONUtil.toList(t.getPeopleJson(), TbBusinessPeople.class);
  255. peopleList.forEach(tbBusinessPeople -> tbBusinessPeople.setBusinessId(t.getId()).setCreateTime(new Date()));
  256. tbBusinessPeopleService.saveBatch(peopleList);
  257. }
  258. private void createCarUnit(TbBusiness t) {
  259. TbUnit tbUnit = tbUnitService.findByUnit(t.getCardSize());
  260. if (tbUnit == null) {
  261. tbUnit = new TbUnit();
  262. tbUnit.setUnit(t.getCardSize() + "");
  263. tbUnitService.save(tbUnit);
  264. }
  265. }
  266. private TbDriver createDriver(TbBusiness business) {
  267. TbDriver tbDriver = tbDriverService.findByIdCardNo(business.getCardNo());
  268. if (tbDriver == null) {
  269. tbDriver = new TbDriver();
  270. tbDriver.setCreateTime(new Date()).setIdCard(business.getCardNo())
  271. .setName(business.getDriverName()).setPhone(business.getDriverPhone());
  272. tbDriverService.save(tbDriver);
  273. }
  274. return tbDriver;
  275. }
  276. private TbCar createCar(TbBusiness business) {
  277. String cardNo = business.getCardNo();
  278. TbCar tbCar = tbCarService.findByCardNo(cardNo);
  279. if (tbCar == null) {
  280. tbCar = new TbCar();
  281. }
  282. tbCar.setCardNo(cardNo)
  283. .setCardSize(business.getCardSize())
  284. .setCountryName(business.getCountryName())
  285. .setLastGoodsName(business.getGoodsName())
  286. .setLastNetWeight(business.getNetWeight());
  287. tbCarService.saveOrUpdate(tbCar);
  288. return tbCar;
  289. }
  290. /**
  291. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  292. */
  293. public List<TbBusiness> getList(SoMap so) {
  294. return tbBusinessMapper.getList(so);
  295. }
  296. public void pay(String id, String payTicket) {
  297. TbBusiness tbBusiness = this.getById(id);
  298. SpAdmin admin = StpUserUtil.getAdmin();
  299. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  300. .setPayBy(StpUserUtil.getAdmin().getName())
  301. .setPayTicket(payTicket)
  302. .setPayTime(new Date());
  303. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  304. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  305. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  306. }
  307. this.updateById(tbBusiness);
  308. storeMsg(tbBusiness.getCustomerId(), "业务订单【" + tbBusiness.getNo() + "】已支付" + DateUtil.now() + "。");
  309. //发送消息
  310. String text = "您的费用已支付,业务单号[" + tbBusiness.getNo() + "]";
  311. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  312. }
  313. public void confirm(List<String> ids) {
  314. ids.forEach(id -> {
  315. TbBusiness business = this.getById(id);
  316. business.setConfirmInput(1).setAdminConfirmInput(1)
  317. .setConfirmInputTime(new Date())
  318. .setConfirmInputBy(StpUserUtil.getAdmin().getName());
  319. this.updateById(business);
  320. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认" + DateUtil.now() + "。");
  321. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  322. items.forEach(item -> {
  323. String businessType = item.getBusinessType();
  324. if (!StrUtil.equals(businessType, TbCostomer.CustomerEnum.BUSINESS_TYPE.getType())) {
  325. String remark = "车牌:";
  326. String carNo = business.getCardNo();
  327. String chinaCarNo = business.getChinaCarNo();
  328. if (StrUtil.isNotEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  329. remark += carNo + "、" + chinaCarNo;
  330. }
  331. if (StrUtil.isEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  332. remark += chinaCarNo;
  333. }
  334. if (StrUtil.isNotEmpty(carNo) && StrUtil.isEmpty(chinaCarNo)) {
  335. remark += carNo;
  336. }
  337. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), item.getItemTypeName(), item.getItemName(), business.getGoodsName(), DateUtil.now(), remark);
  338. StrUtil.splitTrim(businessType, ",").forEach(type -> {
  339. List<String> openidList = tbCostomerService.findByBusinessTypeOpenid(type);
  340. openidList.forEach(openid -> {
  341. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openid;
  342. wxService.sendTemplateMsg(wxConfig.getBusinessNoticeTemplate(), openid, msgDataBO, detailUrl);
  343. });
  344. });
  345. }
  346. });
  347. });
  348. }
  349. public void adminConfirmPay(List<String> ids, String ticket) {
  350. ids.forEach(id -> {
  351. TbBusiness business = this.getById(id);
  352. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  353. .setPayStatus(3).setPayTicket(ticket);
  354. this.updateById(business);
  355. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认支付" + DateUtil.now() + "。");
  356. //发送消息
  357. String text = "您的费用已支付,业务单号[" + business.getNo() + "]";
  358. tbNoticesService.sendNotice(business.getId(), business.getNo(), business.getCustomerId(), text);
  359. });
  360. }
  361. public TbBusiness findCarTheNewRecord(String carNo, int inOrOut) {
  362. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  363. ew.eq("card_no", carNo);
  364. if (inOrOut == 1) {
  365. ew.eq("admin_confirm_in", 0).isNull("in_channel");
  366. } else {
  367. ew.eq("admin_confirm_out", 0).isNull("out_channel").isNotNull("real_in_time");
  368. }
  369. return getOne(ew);
  370. }
  371. @Deprecated
  372. public void adminConfirmIn(String id, String inChannel) {
  373. TbBusiness tbBusiness = this.getById(id);
  374. tbBusiness.setRealInTime(new Date()).setInChannel(inChannel);
  375. this.updateById(tbBusiness);
  376. //更新境外车入场记录
  377. //更新中国车入场记录
  378. //发送消息
  379. String text = "您的车辆已入场,业务单号[" + tbBusiness.getNo() + "]";
  380. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  381. }
  382. public void adminOut(String id, Date outDayTime, String outChannel, String carType) {
  383. TbBusiness tbBusiness = this.getById(id);
  384. tbBusiness.setOutDayTime(outDayTime).setOutChannel(outChannel);
  385. calculationPartMoney(carType, tbBusiness);
  386. this.updateById(tbBusiness);
  387. //更新境外车出场记录
  388. //更新中国车出场记录
  389. //发送消息
  390. String text = "您的车辆已离场,业务单号[" + tbBusiness.getNo() + "]";
  391. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  392. }
  393. @Async
  394. @Deprecated
  395. public void calculationPartMoney(String itemType, TbBusiness tbBusiness) {
  396. Date iTime = tbBusiness.getRealInTime();
  397. Date oTime = tbBusiness.getOutDayTime();
  398. if (itemType.contains("中国")) {
  399. iTime = tbBusiness.getChinaCarInTime();
  400. oTime = tbBusiness.getChinaCarOutTime();
  401. }
  402. BigDecimal price = calculationPartMoney(iTime, oTime);
  403. if (itemType.contains("中国")) {
  404. int len = tbBusiness.getCardNo().split(" ").length;
  405. tbBusiness.setChinaPartMoney(price.multiply(new BigDecimal(len)));
  406. } else {
  407. tbBusiness.setPartMoney(price);
  408. }
  409. tbBusiness.setTotalMoney(tbBusiness.getItemPrice().add(tbBusiness.getChinaPartMoney()).add(tbBusiness.getPartMoney()));
  410. }
  411. public BigDecimal calculationPartMoney(Date iTime, Date oTime) {
  412. LocalDateTime inDayTime = iTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  413. LocalDateTime outDayTime = oTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  414. long minutes = ChronoUnit.MINUTES.between(inDayTime, outDayTime);
  415. BigDecimal zero = new BigDecimal("0");
  416. if (minutes < 0) {
  417. return zero;
  418. }
  419. long days = ChronoUnit.DAYS.between(inDayTime.toLocalDate(), outDayTime.toLocalDate());
  420. BigDecimal p = partConfig.getBasePrice();//乘积因子
  421. BigDecimal extraPrice = partConfig.getExtraPrice();
  422. int unit = (int) Math.ceil(NumberUtil.div(minutes, 24 * 60));
  423. if (minutes < partConfig.getFreeMinutes()) {
  424. unit = 0;
  425. }
  426. return p.multiply(new BigDecimal(unit)).add(new BigDecimal(days).multiply(extraPrice));
  427. }
  428. /**
  429. * @param carNo
  430. * @param inOrOut 1入 2出
  431. * @return
  432. */
  433. public TbBusiness findByChinaCarNo(String carNo, int inOrOut) {
  434. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  435. ew.like("china_car_no", carNo);
  436. if (inOrOut == 1) {
  437. ew.eq("admin_confirm_in", 0).isNull("china_car_in_channel");
  438. } else {
  439. ew.eq("admin_confirm_out", 0)
  440. .isNull("china_car_out_channel")
  441. .isNotNull("china_car_in_time");
  442. }
  443. return this.getOne(ew);
  444. }
  445. public TbBusiness finByNo(String no) {
  446. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  447. ew.eq("no", no);
  448. return getOne(ew);
  449. }
  450. public Map<String, Object> getBusinessMoney(String carId, String state) {
  451. Map<String, Object> result = new HashMap<>();
  452. TbBusinessCar car = tbBusinessCarService.getById(carId);
  453. String businessId = car.getBusinessId();
  454. result.put("type", car.getBusinessType());
  455. List<TbBusinessCar> cars = new ArrayList<>();
  456. List<TbBusinessItem> items = new ArrayList<>();
  457. cars.add(car);
  458. //其他业务
  459. if (TbBusiness.BusinessType.CAR_DISINCLE.getCode().equals(car.getBusinessType())) {
  460. List<TbBusiness> businesses = this.findOtherBusinessByCarId(carId);
  461. businesses=businesses.stream()
  462. .filter(tbBusiness -> tbBusiness.getAdminConfirmInput()==1&&tbBusiness.getPayMoney().doubleValue()<tbBusiness.getTotalMoney().doubleValue())
  463. .collect(Collectors.toList());
  464. List<String> businessIdList = businesses.stream().map(TbBusiness::getId).collect(Collectors.toList());
  465. if (!businessIdList.isEmpty()){
  466. items = tbBusinessItemService.findByBusinessIdList(businessIdList);
  467. businessId = String.join(",", businessIdList);
  468. }
  469. } else if (TbBusiness.BusinessType.HOLD_CAR.getCode().equals(car.getBusinessType())) {
  470. cars = tbBusinessCarService.findByBusinessId(businessId);
  471. TbBusiness tbBusiness = this.getById(businessId);
  472. if (tbBusiness.getPayStatus() != TbBusiness.PayStatus.HAS_PAY_CONFIRM.getCode()) {
  473. items = tbBusinessItemService.findByBusinessId(businessId);
  474. }
  475. }
  476. result.put("businessId", businessId);
  477. Date now = new Date();
  478. List<Map<String, Object>> carsList = new ArrayList<>();
  479. for (TbBusinessCar tbBusinessCar : cars) {
  480. Date inTime = tbBusinessCar.getRealInTime();
  481. BigDecimal partMoney = new BigDecimal("0");
  482. String carNo = tbBusinessCar.getCarNo().substring(0, 1);
  483. if (inTime != null) {
  484. if (tbBusinessCar.getPay() == 1 && tbBusinessCar.getPayTime() != null) {
  485. inTime = tbBusinessCar.getPayTime();
  486. }
  487. partMoney = this.calculationPartMoney(inTime, now);
  488. }
  489. if (StrUtil.isNotEmpty(tbBusinessCar.getColor())) {
  490. List<TbCarNoColor> freeList = tbCarNoColorService.getFreeColor();
  491. List<String> colorList = freeList.stream().map(f -> f.getCarNoColor().substring(0, 1)).collect(Collectors.toList());
  492. String color = tbBusinessCar.getColor().substring(0, 1);
  493. if (colorList.contains(color)) {
  494. partMoney = new BigDecimal("0");
  495. }
  496. }
  497. //越南车
  498. if (!CAR_LIST.contains(carNo)) {
  499. TbBusiness tbBusiness = this.getById(businessId);
  500. //整车业务免费
  501. if (tbBusiness != null && Objects.equals(tbBusiness.getBusinessType(), TbBusiness.BusinessType.HOLD_CAR.getCode())) {
  502. partMoney = new BigDecimal("0");
  503. }
  504. }
  505. Map<String, Object> carMap = new HashMap<>();
  506. carMap.put("price", partMoney);
  507. carMap.put("id", tbBusinessCar.getId());
  508. carMap.put("carNo", tbBusinessCar.getCarNo());
  509. carMap.put("pay", tbBusinessCar.getPay());
  510. TbCar tbCar = tbCarService.findByCardNo(tbBusinessCar.getCarNo());
  511. if (tbCar != null && !TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  512. carMap.put("price", 0);
  513. }
  514. carsList.add(carMap);
  515. }
  516. result.put("carList", carsList);
  517. BigDecimal itemsPrice = new BigDecimal("0");
  518. List<Map<String, Object>> itemList = new ArrayList<>();
  519. for (TbBusinessItem item : items) {
  520. BigDecimal itemPrice = item.getItemPrice().multiply(new BigDecimal(item.getNum()));
  521. itemsPrice = itemsPrice.add(itemPrice);
  522. Map<String, Object> itemMap = new HashMap<>();
  523. itemMap.put("id", item.getId());
  524. itemMap.put("name", item.getItemName() + "(" + item.getItemTypeName() + ")");
  525. itemMap.put("price", itemPrice);
  526. itemMap.put("pay", item.getPayStatus());
  527. itemList.add(itemMap);
  528. }
  529. result.put("itemsPrice", itemsPrice);
  530. result.put("itemList", itemList);
  531. return result;
  532. }
  533. public List<TbBusiness> findOtherBusinessByCarId(String businessCarId) {
  534. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  535. ew.eq("business_car_id", businessCarId);
  536. return list(ew);
  537. }
  538. public void uploadReport(TbBusiness tbBusiness) {
  539. this.updateById(tbBusiness);
  540. List<TbBusinessCar> cars = tbBusinessCarService.findByBusinessId(tbBusiness.getId());
  541. tbBusinessCarService.updateBatchById(cars);
  542. }
  543. public TbBusiness checkCar(String carNo) {
  544. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  545. ew.eq("car_no", carNo);
  546. ew.eq("admin_confirm_input", 0);
  547. return this.getOne(ew);
  548. }
  549. public List<CarDisincle> getCarDisincleList(SoMap soMap) {
  550. return tbBusinessMapper.getCarDisincleList(soMap);
  551. }
  552. public OtherBusinessEditBO getOtherBusinessById(String id) {
  553. TbBusiness tbBusiness = this.getById(id);
  554. List<TbBusinessItem> tbBusinessItems = tbBusinessItemService.findByBusinessId(id);
  555. TbBusinessCar car = tbBusinessCarService.getById(tbBusiness.getBusinessCarId());
  556. OtherBusinessEditBO otherBusinessBO = new OtherBusinessEditBO();
  557. otherBusinessBO.setId(tbBusiness.getId()).setOperator(tbBusiness.getOperator()).setOperateTime(tbBusiness.getOperateTime());
  558. otherBusinessBO.setCustomerId(tbBusiness.getCustomerId());
  559. otherBusinessBO.setCarNo(car.getCarNo()).setCarWeight(tbBusiness.getNetWeight())
  560. .setCarSize(tbBusiness.getCardSize()).setCarType(tbBusiness.getGoodsName())
  561. .setItemTypeId(tbBusiness.getItemTypeId());
  562. otherBusinessBO.setItems(tbBusinessItems);
  563. return otherBusinessBO;
  564. }
  565. public void deleteOtherBusiness(String id) {
  566. TbBusiness db = super.getById(id);
  567. this.removeById(id);
  568. tbBusinessItemService.removeByBusinessId(id);
  569. tbBusinessCarService.removeById(db.getBusinessCarId());
  570. }
  571. }