TbBusinessService.java 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. package com.pj.project.tb_business;
  2. import java.math.BigDecimal;
  3. import java.time.LocalDateTime;
  4. import java.time.ZoneId;
  5. import java.time.format.DateTimeFormatter;
  6. import java.time.temporal.ChronoUnit;
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9. import cn.hutool.core.date.DateUtil;
  10. import cn.hutool.core.util.NumberUtil;
  11. import cn.hutool.core.util.RandomUtil;
  12. import cn.hutool.core.util.StrUtil;
  13. import cn.hutool.json.JSONUtil;
  14. import cn.hutool.log.StaticLog;
  15. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  16. import com.baomidou.mybatisplus.extension.service.IService;
  17. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  18. import com.pj.api.wx.bo.MsgDataBO;
  19. import com.pj.api.wx.service.WxService;
  20. import com.pj.constants.UserTypeEnum;
  21. import com.pj.current.config.CarConfig;
  22. import com.pj.current.config.MyConfig;
  23. import com.pj.current.config.PartConfig;
  24. import com.pj.current.config.WxConfig;
  25. import com.pj.current.satoken.StpUserUtil;
  26. import com.pj.project.relation_business_car.RelationBusinessCar;
  27. import com.pj.project.relation_business_car.RelationBusinessCarService;
  28. import com.pj.project.relation_type_item.RelationTypeItemService;
  29. import com.pj.project.tb_business_car.TbBusinessCar;
  30. import com.pj.project.tb_business_car.TbBusinessCarService;
  31. import com.pj.project.tb_business_item.TbBusinessItem;
  32. import com.pj.project.tb_business_item.TbBusinessItemService;
  33. import com.pj.project.tb_business_people.TbBusinessPeople;
  34. import com.pj.project.tb_business_people.TbBusinessPeopleService;
  35. import com.pj.project.tb_business_sort.TbBusinessSort;
  36. import com.pj.project.tb_business_sort.TbBusinessSortService;
  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.TbNoticesService;
  54. import com.pj.project.tb_sort_group.TbSortGroup;
  55. import com.pj.project.tb_sort_group.TbSortGroupService;
  56. import com.pj.project.tb_unit.TbUnit;
  57. import com.pj.project.tb_unit.TbUnitService;
  58. import com.pj.project4sp.admin.SpAdmin;
  59. import com.pj.project4sp.admin.SpAdminService;
  60. import com.pj.project4sp.global.BusinessException;
  61. import com.pj.utils.so.SoMap;
  62. import org.springframework.beans.factory.annotation.Autowired;
  63. import org.springframework.context.annotation.Lazy;
  64. import org.springframework.stereotype.Service;
  65. import com.pj.utils.sg.*;
  66. import org.springframework.transaction.annotation.Transactional;
  67. import javax.annotation.Resource;
  68. /**
  69. * Service: tb_business -- 入境登记
  70. *
  71. * @author qzy
  72. */
  73. @Service
  74. @Transactional(rollbackFor = Exception.class)
  75. public class TbBusinessService extends ServiceImpl<TbBusinessMapper, TbBusiness> implements IService<TbBusiness> {
  76. /**
  77. * 底层 Mapper 对象
  78. */
  79. @Autowired
  80. TbBusinessMapper tbBusinessMapper;
  81. @Resource
  82. private TbCarService tbCarService;
  83. @Resource
  84. private TbDriverService tbDriverService;
  85. @Resource
  86. private TbCostomerService tbCostomerService;
  87. @Resource
  88. private TbItemService tbItemService;
  89. @Resource
  90. private TbItemTypeService tbItemTypeService;
  91. @Resource
  92. private TbBusinessItemService tbBusinessItemService;
  93. @Resource
  94. TbUnitService tbUnitService;
  95. @Resource
  96. TbBusinessPeopleService tbBusinessPeopleService;
  97. @Resource
  98. private PartConfig partConfig;
  99. @Resource
  100. private CarConfig carConfig;
  101. @Resource
  102. private TbNoticesService tbNoticesService;
  103. @Resource
  104. private TbCarNoColorService tbCarNoColorService;
  105. @Resource
  106. TbBusinessCarService tbBusinessCarService;
  107. @Resource
  108. @Lazy
  109. TbDeclareService tbDeclareService;
  110. @Resource
  111. WxConfig wxConfig;
  112. @Resource
  113. @Lazy
  114. WxService wxService;
  115. @Resource
  116. MyConfig myConfig;
  117. @Resource
  118. private SpAdminService spAdminService;
  119. @Resource
  120. private TbBusinessSortService tbBusinessSortService;
  121. @Resource
  122. private TbSortGroupService tbSortGroupService;
  123. @Resource
  124. private TbGoodsService tbGoodsService;
  125. @Resource
  126. private RelationBusinessCarService relationBusinessCarService;
  127. private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");
  128. /**
  129. * 增
  130. */
  131. public AjaxJson addOrUpdate(TbBusiness t) {
  132. t.setBusinessType(TbBusiness.BusinessType.HOLD_CAR.getCode());
  133. if (StrUtil.isEmpty(t.getDeclareNo())) {
  134. throw new BusinessException("请选择申报单");
  135. }
  136. if (StrUtil.isNotEmpty(t.getId()) && StrUtil.isNotEmpty(t.getDeclareNo())) {
  137. TbBusiness db = this.getById(t.getId());
  138. if (!StrUtil.equals(db.getDeclareNo(), t.getDeclareNo())) {
  139. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(db.getDeclareNo());
  140. if (tbDeclare != null) {
  141. tbDeclare.setBusinessId("");
  142. tbDeclareService.updateById(tbDeclare);
  143. }
  144. }
  145. }
  146. Double carSize = t.getCardSize();
  147. if (carSize != null && carSize > carConfig.getMaxLength()) {
  148. return AjaxJson.getError("车辆规格不能大于" + carConfig.getMaxLength());
  149. }
  150. Double netWeight = t.getNetWeight();
  151. if (netWeight != null && netWeight > carConfig.getMaxWeight()) {
  152. return AjaxJson.getError("车辆载重不能大于" + carConfig.getMaxWeight());
  153. }
  154. t.setCreateBy(StpUserUtil.getAdmin().getName());
  155. createCarUnit(t);
  156. String customerId = t.getCustomerId();
  157. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  158. t.setCustomerName(tbCostomer.getName()).setCustomerType(tbCostomer.getPayType());
  159. if (StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  160. t.setConfirmInputBy(StpUserUtil.getAdmin().getName())
  161. .setConfirmInput(1)
  162. .setConfirmInputTime(new Date());
  163. }
  164. if (StrUtil.isEmpty(t.getId())) {
  165. t.setCreateTime(new Date());
  166. t.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  167. storeMsg(t.getCustomerId(), "新增一条业务数据:【" + t.getNo() + "】" + DateUtil.now() + "。");
  168. } else {
  169. storeMsg(t.getCustomerId(), "业务订单:【" + t.getNo() + "】数据发生更改," + DateUtil.now() + "。");
  170. }
  171. saveOrUpdate(t);
  172. createBusinessPeople(t);
  173. tbBusinessItemService.removeByBusinessId(t.getId());
  174. BigDecimal price = new BigDecimal(0);
  175. List<TbBusinessItem> items = JSONUtil.toList(t.getItemJson(), TbBusinessItem.class);
  176. int index = 1;
  177. for (TbBusinessItem item : items) {
  178. Double num = NumberUtil.parseDouble(item.getNum());
  179. TbItem tbItem = tbItemService.getById(item.getItemId());
  180. String typeId = tbItem.getTypeId();
  181. price = price.add(NumberUtil.mul(num, tbItem.getPrice()));
  182. TbItemType tbItemType = tbItemTypeService.getById(typeId);
  183. item.setBusinessId(t.getId()).setItemCode(tbItem.getItemCode()).setBusinessType(tbItem.getBusinessType())
  184. .setPayType(tbItem.getPayType()).setPayTypeName(tbItem.getPayTypeName())
  185. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  186. .setTaxRate(NumberUtil.div(tbItem.getTaxRate().doubleValue(), 100D, 2))
  187. .setItemTypeId(typeId).setItemTypeName(tbItemType.getName()).setBusinessType(tbItemType.getBusinessType())
  188. .setUnit(tbItem.getUnit()).setTotal(NumberUtil.mul(num, tbItem.getPrice())).setCreateTime(new Date())
  189. .setNo(t.getNo() + "0" + index);
  190. tbBusinessItemService.save(item);
  191. index++;
  192. }
  193. List<TbBusinessCar> carList = JSONUtil.toList(t.getCarJson(), TbBusinessCar.class);
  194. String chinaCarNo = carList.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  195. t.setChinaCarNo(StrUtil.isNotEmpty(chinaCarNo) ? chinaCarNo.toUpperCase() : "");
  196. carList.forEach(tbBusinessCar -> {
  197. tbBusinessCar.setIsLock(0);
  198. String carNo = tbBusinessCar.getCarNo().toUpperCase();
  199. TbBusinessCar db = tbBusinessCarService.findByBusinessIdAndCarNo(t.getId(), carNo);
  200. if (db != null) {
  201. tbBusinessCar.setId(db.getId()).setIsLock(db.getIsLock());
  202. }
  203. TbBusinessCar car = tbBusinessCarService.findInAndNoBusinessCar(carNo);
  204. if (car != null) {
  205. tbBusinessCar.setId(car.getId()).setRealInTime(car.getRealInTime()).setIsLock(car.getIsLock());
  206. }
  207. TbCar tbCar = tbCarService.findByCardNo(carNo);
  208. tbBusinessCar.setBusinessId(t.getId());
  209. tbBusinessCar.setCarNo(carNo).setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时");
  210. tbBusinessCar.setBusinessId(t.getId()).setCustomerId(t.getCustomerId());
  211. if (StrUtil.isEmpty(tbBusinessCar.getNo())) {
  212. tbBusinessCar.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  213. }
  214. });
  215. String yueCarNo = t.getCardNo().toUpperCase();
  216. TbBusinessCar yueCar = tbBusinessCarService.findInAndNoBusinessCar(yueCarNo);
  217. if (yueCar == null) {
  218. yueCar = tbBusinessCarService.findByBusinessIdAndCarNo(t.getId(), yueCarNo);
  219. if (yueCar == null) {
  220. yueCar = new TbBusinessCar();
  221. yueCar.setIsLock(0);
  222. }
  223. }
  224. yueCar.setBusinessId(t.getId()).setCarSize(t.getCardSize()).setCarNo(yueCarNo).setCustomerId(t.getCustomerId());
  225. if (StrUtil.isEmpty(yueCar.getNo())) {
  226. yueCar.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  227. }
  228. carList.add(yueCar);
  229. tbBusinessCarService.saveOrUpdateBatch(carList);
  230. t.setItemPrice(price)
  231. .setTotalMoney(price);
  232. this.saveOrUpdate(t);
  233. if (StrUtil.isNotEmpty(t.getDeclareNo())) {
  234. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(t.getDeclareNo());
  235. tbDeclare.setBusinessId(t.getId());
  236. tbDeclareService.updateById(tbDeclare);
  237. }
  238. return AjaxJson.getSuccess();
  239. }
  240. public void storeMsg(String businessCustomerId, String content) {
  241. String currentCustomerId = StpUserUtil.getCustomerId();
  242. if (StrUtil.equals(currentCustomerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  243. BusinessMessageManager.set(businessCustomerId, content);
  244. } else {
  245. BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), content);
  246. }
  247. }
  248. private void createBusinessPeople(TbBusiness t) {
  249. tbBusinessPeopleService.removeByBusinessId(t.getId());
  250. List<TbBusinessPeople> peopleList = JSONUtil.toList(t.getPeopleJson(), TbBusinessPeople.class);
  251. peopleList.forEach(tbBusinessPeople -> tbBusinessPeople.setBusinessId(t.getId()).setCreateTime(new Date()));
  252. tbBusinessPeopleService.saveBatch(peopleList);
  253. }
  254. private void createCarUnit(TbBusiness t) {
  255. TbUnit tbUnit = tbUnitService.findByUnit(t.getCardSize());
  256. if (tbUnit == null) {
  257. tbUnit = new TbUnit();
  258. tbUnit.setUnit(t.getCardSize() + "");
  259. tbUnitService.save(tbUnit);
  260. }
  261. }
  262. private TbDriver createDriver(TbBusiness business) {
  263. TbDriver tbDriver = tbDriverService.findByIdCardNo(business.getCardNo());
  264. if (tbDriver == null) {
  265. tbDriver = new TbDriver();
  266. tbDriver.setCreateTime(new Date()).setIdCard(business.getCardNo())
  267. .setName(business.getDriverName()).setPhone(business.getDriverPhone());
  268. tbDriverService.save(tbDriver);
  269. }
  270. return tbDriver;
  271. }
  272. private TbCar createCar(TbBusiness business) {
  273. String cardNo = business.getCardNo();
  274. TbCar tbCar = tbCarService.findByCardNo(cardNo);
  275. if (tbCar == null) {
  276. tbCar = new TbCar();
  277. }
  278. tbCar.setCardNo(cardNo)
  279. .setCardSize(business.getCardSize())
  280. .setCountryName(business.getCountryName())
  281. .setLastGoodsName(business.getGoodsName())
  282. .setLastNetWeight(business.getNetWeight());
  283. tbCarService.saveOrUpdate(tbCar);
  284. return tbCar;
  285. }
  286. /**
  287. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  288. */
  289. public List<TbBusiness> getList(SoMap so) {
  290. return tbBusinessMapper.getList(so);
  291. }
  292. public void pay(String id, String payTicket) {
  293. TbBusiness tbBusiness = this.getById(id);
  294. SpAdmin admin = StpUserUtil.getAdmin();
  295. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  296. .setPayBy(StpUserUtil.getAdmin().getName())
  297. .setPayTicket(payTicket)
  298. .setPayTime(new Date());
  299. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  300. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  301. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  302. }
  303. this.updateById(tbBusiness);
  304. storeMsg(tbBusiness.getCustomerId(), "业务订单【" + tbBusiness.getNo() + "】已支付" + DateUtil.now() + "。");
  305. //发送消息
  306. String text = "您的费用已支付,业务单号[" + tbBusiness.getNo() + "]";
  307. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  308. }
  309. public void confirm(List<String> ids) {
  310. ids.forEach(id -> {
  311. TbBusiness business = this.getById(id);
  312. business.setConfirmInput(1).setAdminConfirmInput(1)
  313. .setConfirmInputTime(new Date())
  314. .setConfirmInputBy(StpUserUtil.getAdmin().getName());
  315. this.updateById(business);
  316. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(business.getId());
  317. String carNoStr = cars.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  318. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认" + DateUtil.now() + "。");
  319. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  320. String messageType = business.getGoodsId();
  321. items.forEach(item -> {
  322. String businessType = item.getBusinessType();
  323. //合作伙伴创建+是否下单后付款
  324. if (!StrUtil.equals(businessType, TbCostomer.CustomerEnum.BUSINESS_TYPE.getType())) {
  325. String customerId = business.getCreateByCustomerId();
  326. List<String> customerType=new ArrayList<>();
  327. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  328. if (tbCostomer!=null){
  329. customerType = StrUtil.splitTrim(tbCostomer.getType(),",");
  330. }
  331. if (customerType.contains(businessType)) {
  332. StaticLog.error("订单自动确认======================="+id);
  333. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), "系统自动确认",
  334. DateUtil.now(),
  335. business.getGoodsName() + "(" + item.getItemTypeName() + item.getItemName() + "-" + carNoStr + ")");
  336. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(customerId);
  337. Date now = new Date();
  338. item.setPickCustomerId(customerId).setPick(1)
  339. .setPickCustomerName(tbCostomer.getName()).setPickTime(now)
  340. .setConfirm(1).setConfirmTime(now);
  341. tbBusinessItemService.updateById(item);
  342. spAdminList.stream().map(SpAdmin::getOpenid).forEach(openId -> {
  343. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openId;
  344. wxService.sendTemplateMsg(wxConfig.getBusinessConfirmTemplate(), openId, msgDataBO, detailUrl);
  345. });
  346. } else {
  347. StaticLog.error("正常流程======================="+id);
  348. String remark = "车牌:";
  349. String carNo = business.getCardNo();
  350. String chinaCarNo = business.getChinaCarNo();
  351. if (StrUtil.isNotEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  352. remark += carNo + "、" + chinaCarNo;
  353. }
  354. if (StrUtil.isEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  355. remark += chinaCarNo;
  356. }
  357. if (StrUtil.isNotEmpty(carNo) && StrUtil.isEmpty(chinaCarNo)) {
  358. remark += carNo;
  359. }
  360. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), item.getItemTypeName(),
  361. item.getItemName(), business.getGoodsName(), DateUtil.now(), remark);
  362. List<String> openidList = tbCostomerService.findByMessageTypeOpenid(businessType, messageType);
  363. openidList.forEach(openid -> {
  364. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openid;
  365. wxService.sendTemplateMsg(wxConfig.getBusinessNoticeTemplate(), openid, msgDataBO, detailUrl);
  366. });
  367. }
  368. }
  369. });
  370. });
  371. }
  372. public void adminConfirmPay(List<String> ids, String ticket) {
  373. ids.forEach(id -> {
  374. TbBusiness business = this.getById(id);
  375. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  376. .setPayStatus(3).setPayTicket(ticket);
  377. this.updateById(business);
  378. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认支付" + DateUtil.now() + "。");
  379. //发送消息
  380. String text = "您的费用已支付,业务单号[" + business.getNo() + "]";
  381. tbNoticesService.sendNotice(business.getId(), business.getNo(), business.getCustomerId(), text);
  382. });
  383. }
  384. @Deprecated
  385. public void adminConfirmIn(String id, String inChannel) {
  386. TbBusiness tbBusiness = this.getById(id);
  387. tbBusiness.setRealInTime(new Date()).setInChannel(inChannel);
  388. this.updateById(tbBusiness);
  389. //更新境外车入场记录
  390. //更新中国车入场记录
  391. //发送消息
  392. String text = "您的车辆已入场,业务单号[" + tbBusiness.getNo() + "]";
  393. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  394. }
  395. public BigDecimal calculationPartMoney(Date iTime, Date oTime) {
  396. BigDecimal zero = new BigDecimal("0");
  397. if (iTime == null || oTime == null) {
  398. return zero;
  399. }
  400. LocalDateTime inDayTime = iTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  401. LocalDateTime outDayTime = oTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  402. long minutes = ChronoUnit.MINUTES.between(inDayTime, outDayTime);
  403. if (minutes < 0) {
  404. return zero;
  405. }
  406. long days = ChronoUnit.DAYS.between(inDayTime.toLocalDate(), outDayTime.toLocalDate());
  407. BigDecimal p = partConfig.getBasePrice();//乘积因子
  408. BigDecimal extraPrice = partConfig.getExtraPrice();
  409. int unit = (int) Math.ceil(NumberUtil.div(minutes, 24 * 60));
  410. if (minutes < partConfig.getFreeMinutes()) {
  411. unit = 0;
  412. }
  413. return p.multiply(new BigDecimal(unit)).add(new BigDecimal(days).multiply(extraPrice));
  414. }
  415. public Map<String, Object> getBusinessMoney(String businessCarId, String state) {
  416. Map<String, Object> result = new HashMap<>();
  417. List<TbBusiness> businessList = this.findOtherBusinessByCarId(businessCarId);
  418. Set<TbBusinessCar> cars = new HashSet<>();
  419. //越南车是否需要支付
  420. int vietnamCarPay = businessList.stream().anyMatch(tbBusiness -> {
  421. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  422. return tbGoods.getVietnamCarPay() == 1;
  423. }) ? 1 : 0;
  424. //中国车是否需要支付
  425. int chinaCarPay = businessList.stream().anyMatch(tbBusiness -> {
  426. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  427. return tbGoods.getChinaCarPay() == 1;
  428. }) ? 1 : 0;
  429. //无业务的车辆,中国车和越南车都需要支付
  430. if (businessList.isEmpty()) {
  431. vietnamCarPay = 1;
  432. chinaCarPay = 1;
  433. cars.add(tbBusinessCarService.getById(businessCarId));
  434. }
  435. businessList = businessList.stream().filter(tbBusiness -> {
  436. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  437. Integer payStep = tbGoods.getPayStep();
  438. //确认订单后方可缴费
  439. return TbGoods.PayStep.AFTER_CONFIRM.getCode().equals(payStep) && tbBusiness.getAdminConfirmInput() == 1
  440. //或者下单后可缴费
  441. || TbGoods.PayStep.BEFORE_CONFIRM.getCode().equals(payStep) && tbBusiness.getAdminConfirmInput() >= 0;
  442. }).collect(Collectors.toList());
  443. businessList.forEach(tbBusiness -> {
  444. List<TbBusinessCar> businessCars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  445. cars.addAll(businessCars);
  446. });
  447. List<String> businessIdList = businessList.stream().map(TbBusiness::getId).collect(Collectors.toList());
  448. String businessIds = StrUtil.join(",", businessIdList);
  449. // result.put("type", businessType);
  450. result.put("showPay", true);
  451. result.put("businessId", businessIds);
  452. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessIdList(businessIdList);
  453. Date now = new Date();
  454. List<Map<String, Object>> carsList = new ArrayList<>();
  455. for (TbBusinessCar tbBusinessCar : cars) {
  456. Date inTime = tbBusinessCar.getRealInTime();
  457. BigDecimal partMoney = new BigDecimal("0");
  458. String carType=tbBusinessCar.getCarType();
  459. if (StrUtil.isEmpty(carType)){
  460. carType=TbItem.ItemTypeEnum.EMPTY_TYPE.getType();
  461. }
  462. if (inTime != null && tbBusinessCar.getRealOutTime() == null) {
  463. if (tbBusinessCar.getPay() == 1 && tbBusinessCar.getPayTime() != null) {
  464. inTime = tbBusinessCar.getPayTime();
  465. }
  466. partMoney = this.calculationPartMoney(inTime, now);
  467. }
  468. if (StrUtil.isNotEmpty(tbBusinessCar.getColor())) {//蓝色车辆免费
  469. List<TbCarNoColor> freeList = tbCarNoColorService.getFreeColor();
  470. List<String> colorList = freeList.stream().map(f -> f.getCarNoColor().substring(0, 1)).collect(Collectors.toList());
  471. String color = tbBusinessCar.getColor().substring(0, 1);
  472. if (colorList.contains(color)) {
  473. partMoney = new BigDecimal("0");
  474. }
  475. }
  476. //越南车牌,是否免费
  477. if (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0) {
  478. partMoney = new BigDecimal("0");
  479. }
  480. //中国车,是否免费
  481. if (TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType)&& chinaCarPay == 0) {
  482. partMoney = new BigDecimal("0");
  483. }
  484. Map<String, Object> carMap = new HashMap<>();
  485. carMap.put("price", partMoney);
  486. carMap.put("id", tbBusinessCar.getId());
  487. carMap.put("carNo", tbBusinessCar.getCarNo());
  488. carMap.put("pay", tbBusinessCar.getPay());
  489. if (TbBusinessCar.PayTypeEnum.FEE_TYPE.getType().equals(tbBusinessCar.getPayType())) {
  490. carMap.put("price", 0);
  491. }
  492. carsList.add(carMap);
  493. }
  494. result.put("carList", carsList.stream().sorted(Comparator.comparingDouble(obj -> Double.valueOf(obj.get("price").toString()))).collect(Collectors.toList()));
  495. BigDecimal itemsPrice = new BigDecimal("0");
  496. List<Map<String, Object>> itemList = new ArrayList<>();
  497. for (TbBusinessItem item : items) {
  498. BigDecimal itemPrice = item.getItemPrice().multiply(new BigDecimal(item.getNum()));
  499. if (item.getPayStatus() == 1) {
  500. continue;
  501. }
  502. itemsPrice = itemsPrice.add(itemPrice);
  503. Map<String, Object> itemMap = new HashMap<>();
  504. itemMap.put("id", item.getId());
  505. itemMap.put("name", item.getItemName() + "(" + item.getItemTypeName() + ")");
  506. itemMap.put("price", itemPrice);
  507. itemMap.put("pay", item.getPayStatus());
  508. itemList.add(itemMap);
  509. TbBusinessSort tbBusinessSort = tbBusinessSortService.findByItemTypeId(item.getItemTypeId());
  510. if (tbBusinessSort != null) {
  511. TbSortGroup sortGroup = tbSortGroupService.getById(tbBusinessSort.getGroupId());
  512. if (sortGroup.getCompletePay().equals(1) && sortGroup.getStatus().equals(1)) {
  513. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sortGroup.getId());
  514. if (sorts.size() > items.size()) {
  515. result.put("showPay", false);
  516. }
  517. }
  518. }
  519. }
  520. result.put("itemsPrice", itemsPrice);
  521. result.put("itemList", itemList);
  522. result.put("goodsName", businessList.stream().map(TbBusiness::getGoodsName).distinct().collect(Collectors.joining("、")));
  523. return result;
  524. }
  525. public List<TbBusiness> findOtherBusinessByCarId(String businessCarId) {
  526. return tbBusinessMapper.findOtherBusinessByCarId(businessCarId);
  527. }
  528. public void uploadReport(TbBusiness tbBusiness) {
  529. this.updateById(tbBusiness);
  530. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  531. tbBusinessCarService.updateBatchById(cars);
  532. }
  533. public List<CarDisincle> getCarDisincleList(SoMap soMap) {
  534. return tbBusinessMapper.getCarDisincleList(soMap);
  535. }
  536. public TbBusiness getOtherBusinessById(String id) {
  537. TbBusiness tbBusiness = this.getById(id);
  538. List<TbBusinessItem> tbBusinessItems = tbBusinessItemService.findByBusinessId(id);
  539. tbBusiness.setItems(tbBusinessItems);
  540. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(id);
  541. tbBusiness.setCars(cars);
  542. return tbBusiness;
  543. }
  544. public void deleteOtherBusiness(String id) {
  545. TbBusiness db = super.getById(id);
  546. String declareNo = db.getDeclareNo();
  547. if (StrUtil.isNotEmpty(declareNo)) {
  548. tbDeclareService.rebackDeclareNo(declareNo);
  549. }
  550. this.removeById(id);
  551. //删除响应业务项
  552. tbBusinessItemService.removeByBusinessId(id);
  553. //检查是否删除车辆
  554. List<TbBusinessCar> tbBusinessCarList = tbBusinessCarService.findOtherBusinessCar(id);
  555. tbBusinessCarList.forEach(tbBusinessCar -> {
  556. //未入场
  557. if (tbBusinessCar.getRealInTime() == null) {
  558. List<RelationBusinessCar> relationBusinessCars = relationBusinessCarService.findByBusinessCarId(tbBusinessCar.getId());
  559. //无关联业务==>删除
  560. if (relationBusinessCars.size() == 1) {
  561. tbBusinessCarService.removeById(tbBusinessCar.getId());
  562. }
  563. }
  564. });
  565. //删除关系
  566. relationBusinessCarService.removeByBusinessId(id);
  567. }
  568. public int checkCarBusinessType(String typeId, String carNo, String operateTime, int before) {
  569. return tbBusinessMapper.checkCarBusinessType(typeId, carNo, operateTime, before);
  570. }
  571. public int checkCarBusinessType(String typeId, String carNo, String timeStart, String timeEnd, String businessId) {
  572. return tbBusinessMapper.checkCarBusinessTypeByTime(typeId, carNo, timeStart, timeEnd, businessId);
  573. }
  574. public void manualConfirmPay(String id, List<TbBusinessCar> cars, String remark) {
  575. TbBusiness db = this.getById(id);
  576. if (db == null) {
  577. throw new BusinessException("业务不存在");
  578. }
  579. Date now = new Date();
  580. db.setPayStatus(3).setPayMoney(db.getItemPrice()).setPayTime(now);
  581. this.updateById(db);
  582. cars.forEach(tbBusinessCar -> {
  583. Date realInTime = tbBusinessCar.getRealInTime();
  584. Date realOutTime = tbBusinessCar.getRealOutTime();
  585. if (realInTime != null && realOutTime != null) {
  586. BigDecimal price = calculationPartMoney(realInTime, realOutTime);
  587. tbBusinessCar.setMoney(price);
  588. }
  589. tbBusinessCar.setPay(1).setPayTime(now);
  590. tbBusinessCarService.updateById(tbBusinessCar);
  591. });
  592. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  593. items.forEach(tbBusinessItem -> tbBusinessItem
  594. .setPayStatus(1).setPayTime(now)
  595. .setRemark(StrUtil.isEmpty(remark) ? "手动确认" : remark)
  596. );
  597. tbBusinessItemService.updateBatchById(items);
  598. }
  599. public void unBindCar(String id, String businessCarId) {
  600. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  601. ew.eq("id", id).eq("business_car_id", businessCarId);
  602. TbBusiness db = getOne(ew);
  603. if (db == null) {
  604. throw new BusinessException("业务单不存在");
  605. }
  606. tbBusinessMapper.unBindCar(id);
  607. }
  608. public void addOtherBusiness(OtherBusinessBO otherBusinessBO) {
  609. List<TbItem> tbItems = otherBusinessBO.getItems();
  610. if (tbItems.isEmpty()) {
  611. throw new BusinessException("请选择明细");
  612. }
  613. List<TbBusinessCar> cars = otherBusinessBO.getCars();
  614. if (cars.isEmpty()) {
  615. throw new BusinessException("作业车辆不能为空");
  616. }
  617. Date now = new Date();
  618. TbGoods tbGoods = tbGoodsService.getById(otherBusinessBO.getGoodsId());
  619. TbBusiness tbBusiness = new TbBusiness();
  620. String customerId = otherBusinessBO.getCustomerId();
  621. tbBusiness.setCreateBy(StpUserUtil.getAdmin().getName());
  622. tbBusiness.setPayStep(tbGoods.getPayStep());
  623. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  624. if (tbCostomer != null) {
  625. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  626. }
  627. BigDecimal price = new BigDecimal("0");
  628. List<TbBusinessItem> itemList = new ArrayList<>();
  629. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  630. int index = 1;
  631. for (TbItem tbItem : tbItems) {
  632. TbItem db = tbItemService.getById(tbItem.getId());
  633. TbBusinessItem item = new TbBusinessItem();
  634. int num = tbItem.getNum();
  635. String typeId = tbItem.getTypeId();
  636. TbItemType tbItemType = tbItemTypeService.getById(typeId);
  637. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  638. item.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  639. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2));
  640. item.setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId())
  641. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  642. .setItemTypeId(typeId).setItemTypeName(tbItemType.getName())
  643. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  644. price = price.add(itemTotalPrice);
  645. itemList.add(item);
  646. index++;
  647. }
  648. String declareNo = otherBusinessBO.getDeclareNo();
  649. tbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getNetWeight())
  650. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  651. .setNo(no).setGoodsName(tbGoods.getName()).setGoodsId(otherBusinessBO.getGoodsId())
  652. .setPayStep(tbGoods.getPayStep()).setDeclareNo(declareNo).setCardNo(otherBusinessBO.getCardNo());
  653. tbBusiness.setCreateTime(now).setCreateByCustomerId(StpUserUtil.getCustomerId())
  654. .setItemPrice(price).setTotalMoney(price).setOwner(otherBusinessBO.getOwner());
  655. tbBusiness.setChinaCarNo(otherBusinessBO.getChinaCarNo());
  656. this.save(tbBusiness);
  657. Integer chinaCarPay = tbGoods.getChinaCarPay();
  658. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  659. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  660. for (TbBusinessCar car : cars) {
  661. String carNo = car.getCarNo().trim().toUpperCase();
  662. String carType=car.getCarType();
  663. if (StrUtil.isEmpty(carType)){
  664. throw new BusinessException(carNo+"类型不能为空");
  665. }
  666. List<TbBusinessCar> notOutRecords = tbBusinessCarService.findNotOutCar(carNo);
  667. if (notOutRecords.size() > 1) {
  668. throw new BusinessException(carNo + "有多个未出场记录,请先处理");
  669. }
  670. //最新一条记录
  671. TbBusinessCar db = tbBusinessCarService.findTheLastRecord(carNo);
  672. //不存在,或者已离场记录--->新建记录;
  673. if (db == null || db.getRealInTime() != null && db.getRealOutTime() != null) {
  674. db = new TbBusinessCar();
  675. db.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  676. }
  677. for (TbBusinessItem item : itemList) {
  678. checkOtherBusiness(item.getItemTypeId(), otherBusinessBO.getOperateTime(), tbBusiness.getId(), carNo);
  679. }
  680. db.setPayType(TbBusinessCar.PayTypeEnum.NO_PAY_TYPE.getType());
  681. if ((TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType)&& chinaCarPay == 0) || (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType)&& vietnamCarPay == 0)) {
  682. db.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  683. }
  684. TbCar tbCar = tbCarService.findByCardNo(carNo);
  685. if (tbCar != null) {
  686. db.setCarCompany(tbCar.getCustomerName());
  687. if (!TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  688. db.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  689. }
  690. }
  691. db.setCarNo(carNo).setIsLock(0);
  692. db.setCarSize(car.getCarSize())
  693. .setTimeUpdate(now).setCarType(car.getCarType())
  694. .setNetWeight(car.getNetWeight())
  695. .setCustomerId(customerId)
  696. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  697. tbBusinessCarService.saveOrUpdate(db);
  698. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  699. relationBusinessCar.setBusinessId(tbBusiness.getId()).setBusinessCarId(db.getId());
  700. relationBusinessCarService.save(relationBusinessCar);
  701. }
  702. if (StrUtil.isNotEmpty(declareNo)) {
  703. TbDeclare declare = tbDeclareService.findByDeclareNo(declareNo);
  704. declare.setBusinessId(tbBusiness.getId());
  705. tbDeclareService.updateById(declare);
  706. }
  707. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  708. tbBusinessItemService.saveBatch(itemList);
  709. }
  710. public List<TbBusiness> getOtherBusiness(SoMap startPage) {
  711. return tbBusinessMapper.getOtherBusiness(startPage);
  712. }
  713. public void editOtherBusiness(OtherBusinessBO otherBusinessBO) {
  714. String id = otherBusinessBO.getId();
  715. TbBusiness dbBusiness = this.getById(id);
  716. if (dbBusiness == null) {
  717. throw new BusinessException("记录不存在");
  718. }
  719. List<TbItem> tbItems = otherBusinessBO.getItems();
  720. if (tbItems.isEmpty()) {
  721. throw new BusinessException("请选择收费明细");
  722. }
  723. List<TbBusinessCar> cars = otherBusinessBO.getCars();
  724. if (cars.isEmpty()) {
  725. throw new BusinessException("作业车辆不能为空");
  726. }
  727. tbBusinessItemService.removeByBusinessId(id);
  728. Date now = new Date();
  729. List<RelationBusinessCar> relationBusinessCars = relationBusinessCarService.findByBusinessId(id);
  730. List<String> businessCarIds = cars.stream().filter(tbBusinessCar -> StrUtil.isNotEmpty(tbBusinessCar.getId()))
  731. .map(TbBusinessCar::getId).collect(Collectors.toList());
  732. List<String> removeIds = relationBusinessCars.stream()
  733. .filter(relationBusinessCar -> !businessCarIds.contains(relationBusinessCar.getBusinessCarId()))
  734. .map(RelationBusinessCar::getId)
  735. .collect(Collectors.toList());
  736. if (!removeIds.isEmpty()) {
  737. relationBusinessCarService.removeByIds(removeIds);
  738. }
  739. TbGoods tbGoods = tbGoodsService.getById(otherBusinessBO.getGoodsId());
  740. Integer chinaCarPay = tbGoods.getChinaCarPay();
  741. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  742. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  743. BigDecimal price = new BigDecimal("0");
  744. List<TbBusinessItem> itemList = new ArrayList<>();
  745. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  746. int index = 1;
  747. for (TbItem tbItem : tbItems) {
  748. TbItem db = tbItemService.getById(tbItem.getId());
  749. TbBusinessItem item = new TbBusinessItem();
  750. int num = tbItem.getNum();
  751. TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId());
  752. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  753. item.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  754. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2));
  755. item.setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId()).setBusinessId(dbBusiness.getId())
  756. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  757. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName())
  758. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  759. price = price.add(itemTotalPrice);
  760. itemList.add(item);
  761. index++;
  762. }
  763. dbBusiness.setItemPrice(price).setTotalMoney(price);
  764. tbBusinessItemService.saveBatch(itemList);
  765. for (TbBusinessCar tbBusinessCar : cars) {
  766. String businessCarId = tbBusinessCar.getId();
  767. String carNo = tbBusinessCar.getCarNo();
  768. for (TbBusinessItem item : itemList) {
  769. checkOtherBusiness(item.getItemTypeId(), otherBusinessBO.getOperateTime(), dbBusiness.getId(), carNo);
  770. }
  771. String carType=tbBusinessCar.getCarType();
  772. if (StrUtil.isNotEmpty(businessCarId)) {
  773. //原来已存在的
  774. TbBusinessCar dbBusinessCar = tbBusinessCarService.getById(businessCarId);
  775. //如果修改了车牌号
  776. String dbCarNo = dbBusinessCar.getCarNo();
  777. if (!dbCarNo.equals(carNo)) {
  778. //把关联删除掉,然后添加新的关联
  779. relationBusinessCarService.removeByBusinessIdAndCarId(dbBusiness.getId(), businessCarId);
  780. TbBusinessCar otherCar = tbBusinessCarService.findTheLastRecord(carNo);
  781. if (otherCar == null ||//不存在或者已离场===>新建
  782. (otherCar.getRealInTime() != null && otherCar.getRealOutTime() != null)) {
  783. otherCar = new TbBusinessCar();
  784. otherCar.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4))
  785. .setCarNo(carNo).setCarType(tbBusinessCar.getCarType()).setCarSize(tbBusinessCar.getCarSize())
  786. .setNetWeight(tbBusinessCar.getNetWeight()).setCustomerId(dbBusiness.getCustomerId())
  787. .setTimeUpdate(now).setIsLock(0).setCarType(tbBusinessCar.getCarType());
  788. if ((TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) || (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  789. otherCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  790. }
  791. tbBusinessCarService.save(otherCar);
  792. } else {
  793. List<TbBusiness> tbBusinessList = this.findOtherBusinessByCarId(businessCarId);
  794. if (tbBusinessList.size() == 1 && dbBusinessCar.getRealInTime() == null) {
  795. tbBusinessCarService.removeById(businessCarId);
  796. }
  797. }
  798. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  799. relationBusinessCar.setBusinessId(dbBusiness.getId()).setBusinessCarId(otherCar.getId());
  800. relationBusinessCarService.save(relationBusinessCar);
  801. } else {
  802. dbBusinessCar.setNetWeight(tbBusinessCar.getNetWeight()).setNetWeight(tbBusinessCar.getNetWeight())
  803. .setCarSize(tbBusinessCar.getCarSize()).setCarType(tbBusinessCar.getCarType());
  804. tbBusinessCarService.updateById(dbBusinessCar);
  805. }
  806. } else {
  807. TbBusinessCar checkCar = tbBusinessCarService.findTheLastRecord(carNo);
  808. if (checkCar == null ||
  809. (checkCar.getRealInTime() != null && checkCar.getRealOutTime() != null)) {
  810. checkCar = new TbBusinessCar();
  811. checkCar.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  812. }
  813. checkCar.setCarNo(carNo).setCarType(tbBusinessCar.getCarType()).setCarSize(tbBusinessCar.getCarSize())
  814. .setNetWeight(tbBusinessCar.getNetWeight()).setCustomerId(dbBusiness.getCustomerId())
  815. .setTimeUpdate(now).setIsLock(0).setCarType(tbBusinessCar.getCarType());
  816. if ((TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) || (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  817. checkCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  818. }
  819. tbBusinessCarService.saveOrUpdate(checkCar);
  820. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  821. relationBusinessCar.setBusinessId(dbBusiness.getId()).setBusinessCarId(checkCar.getId());
  822. relationBusinessCarService.save(relationBusinessCar);
  823. }
  824. }
  825. if (!StrUtil.equals(dbBusiness.getGoodsId(), otherBusinessBO.getGoodsId())) {
  826. dbBusiness.setGoodsId(otherBusinessBO.getGoodsId())
  827. .setGoodsName(tbGoods.getName()).setPayStep(tbGoods.getPayStep());
  828. }
  829. String declareNo = otherBusinessBO.getDeclareNo();
  830. if (!StrUtil.equals(declareNo, dbBusiness.getDeclareNo())) {
  831. tbDeclareService.rebackDeclareNo(dbBusiness.getDeclareNo());
  832. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(declareNo);
  833. tbDeclare.setBusinessId(dbBusiness.getId());
  834. tbDeclareService.updateById(tbDeclare);
  835. }
  836. dbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getNetWeight())
  837. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  838. .setNo(no).setGoodsId(otherBusinessBO.getGoodsId()).setOwner(otherBusinessBO.getOwner())
  839. .setDeclareNo(declareNo).setCardNo(otherBusinessBO.getCardNo());
  840. dbBusiness.setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  841. .setItemPrice(price).setTotalMoney(price);
  842. dbBusiness.setChinaCarNo(otherBusinessBO.getChinaCarNo());
  843. this.updateById(dbBusiness);
  844. }
  845. public void bindOtherBusinessCar(String businessId, List<String> businessCarIdList) {
  846. TbBusiness tbBusiness = this.getById(businessId);
  847. relationBusinessCarService.removeByBusinessId(businessId);
  848. List<TbBusinessCar> carList = tbBusinessCarService.listByIds(businessCarIdList);
  849. String chinaCarNo = carList.stream().filter(tbBusinessCar -> {
  850. String carNo = tbBusinessCar.getCarNo();
  851. String carStr = StrUtil.sub(carNo, 0, 1);
  852. return CAR_LIST.contains(carStr);
  853. }).map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  854. String yueCarNo = carList.stream().filter(tbBusinessCar -> {
  855. String carNo = tbBusinessCar.getCarNo();
  856. String carStr = StrUtil.sub(carNo, 0, 1);
  857. return !CAR_LIST.contains(carStr);
  858. }).map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  859. tbBusiness.setChinaCarNo(chinaCarNo);
  860. tbBusiness.setCardNo(yueCarNo);
  861. this.updateById(tbBusiness);
  862. businessCarIdList.forEach(businessCarId -> {
  863. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  864. relationBusinessCar.setBusinessCarId(businessCarId).setBusinessId(businessId);
  865. relationBusinessCarService.save(relationBusinessCar);
  866. });
  867. }
  868. private void checkOtherBusiness(String itemTypeId, String operateTimeStr, String businessId, String carNo) {
  869. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  870. TbBusinessSort sort = tbBusinessSortService.findByItemTypeId(itemTypeId);
  871. if (sort == null) {
  872. return;
  873. }
  874. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sort.getGroupId());
  875. LocalDateTime operateTime = LocalDateTime.parse(operateTimeStr, formatter);
  876. int i = 0;
  877. for (TbBusinessSort tbBusinessSort : sorts) {
  878. if (i == 0 && StrUtil.equals(tbBusinessSort.getTypeId(), itemTypeId) && sorts.size() > 1) {
  879. TbBusinessSort afterItem = sorts.get(i + 1);
  880. String typeId = afterItem.getTypeId();
  881. Integer interval = afterItem.getIntervalTime();
  882. LocalDateTime endTime = operateTime.plusMinutes(interval);
  883. if (afterItem.getIntervalTime() > 0) {
  884. int count = this.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 1);
  885. if (count > 0) {
  886. throw new BusinessException("车辆:[" + carNo + "]" + tbBusinessSort.getTypeName() + "后" + afterItem.getIntervalTime() + "分钟才可以执行" + afterItem.getTypeName());
  887. }
  888. }
  889. }
  890. if (i > 0 && StrUtil.equals(tbBusinessSort.getTypeId(), itemTypeId)) {
  891. TbBusinessSort beforeItem = sorts.get(i - 1);
  892. String typeId = beforeItem.getTypeId();
  893. Integer interval = tbBusinessSort.getIntervalTime();
  894. LocalDateTime endTime = operateTime.minusMinutes(interval);
  895. if (tbBusinessSort.getIntervalTime() > 0) {
  896. int count = this.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 0);
  897. if (count > 0) {
  898. throw new BusinessException("车辆:[" + carNo + "]" + beforeItem.getTypeName() + "后" + tbBusinessSort.getIntervalTime() + "分钟才可以执行" + tbBusinessSort.getTypeName());
  899. }
  900. }
  901. }
  902. Integer singleIntervalTime = tbBusinessSort.getSingleIntervalTime();
  903. String timeStart = operateTime.minusHours(singleIntervalTime).format(formatter);
  904. String timeEnd = operateTime.plusHours(singleIntervalTime).format(formatter);
  905. int count = this.checkCarBusinessType(itemTypeId, carNo, timeStart, timeEnd, businessId);
  906. if (count > 0) {
  907. throw new BusinessException("车辆:[" + carNo + "]" + singleIntervalTime + "小时内已有相同作业");
  908. }
  909. i++;
  910. }
  911. }
  912. public TbBusiness findByDeclareNo(String declareNo) {
  913. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  914. ew.eq("declare_no", declareNo).orderByDesc("id");
  915. List<TbBusiness> list = this.list(ew);
  916. return list.isEmpty() ? null : list.get(0);
  917. }
  918. }