TbBusinessService.java 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. SpAdmin spAdmin= StpUserUtil.getAdmin();
  155. String createName=StrUtil.isEmpty(spAdmin.getNickname())?spAdmin.getName():spAdmin.getNickname();
  156. t.setCreateBy(createName);
  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());
  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. carList.add(yueCar);
  231. tbBusinessCarService.saveOrUpdateBatch(carList);
  232. t.setItemPrice(price)
  233. .setTotalMoney(price);
  234. this.saveOrUpdate(t);
  235. if (StrUtil.isNotEmpty(t.getDeclareNo())) {
  236. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(t.getDeclareNo());
  237. tbDeclare.setBusinessId(t.getId());
  238. tbDeclareService.updateById(tbDeclare);
  239. }
  240. return AjaxJson.getSuccess();
  241. }
  242. public void storeMsg(String businessCustomerId, String content) {
  243. String currentCustomerId = StpUserUtil.getCustomerId();
  244. if (StrUtil.equals(currentCustomerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  245. BusinessMessageManager.set(businessCustomerId, content);
  246. } else {
  247. BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), content);
  248. }
  249. }
  250. private void createBusinessPeople(TbBusiness t) {
  251. tbBusinessPeopleService.removeByBusinessId(t.getId());
  252. List<TbBusinessPeople> peopleList = JSONUtil.toList(t.getPeopleJson(), TbBusinessPeople.class);
  253. peopleList.forEach(tbBusinessPeople -> tbBusinessPeople.setBusinessId(t.getId()).setCreateTime(new Date()));
  254. tbBusinessPeopleService.saveBatch(peopleList);
  255. }
  256. private void createCarUnit(TbBusiness t) {
  257. TbUnit tbUnit = tbUnitService.findByUnit(t.getCardSize());
  258. if (tbUnit == null) {
  259. tbUnit = new TbUnit();
  260. tbUnit.setUnit(t.getCardSize() + "");
  261. tbUnitService.save(tbUnit);
  262. }
  263. }
  264. private TbDriver createDriver(TbBusiness business) {
  265. TbDriver tbDriver = tbDriverService.findByIdCardNo(business.getCardNo());
  266. if (tbDriver == null) {
  267. tbDriver = new TbDriver();
  268. tbDriver.setCreateTime(new Date()).setIdCard(business.getCardNo())
  269. .setName(business.getDriverName()).setPhone(business.getDriverPhone());
  270. tbDriverService.save(tbDriver);
  271. }
  272. return tbDriver;
  273. }
  274. private TbCar createCar(TbBusiness business) {
  275. String cardNo = business.getCardNo();
  276. TbCar tbCar = tbCarService.findByCardNo(cardNo);
  277. if (tbCar == null) {
  278. tbCar = new TbCar();
  279. }
  280. tbCar.setCardNo(cardNo)
  281. .setCardSize(business.getCardSize())
  282. .setCountryName(business.getCountryName())
  283. .setLastGoodsName(business.getGoodsName())
  284. .setLastNetWeight(business.getNetWeight());
  285. tbCarService.saveOrUpdate(tbCar);
  286. return tbCar;
  287. }
  288. /**
  289. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  290. */
  291. public List<TbBusiness> getList(SoMap so) {
  292. return tbBusinessMapper.getList(so);
  293. }
  294. public void pay(String id, String payTicket) {
  295. TbBusiness tbBusiness = this.getById(id);
  296. SpAdmin admin = StpUserUtil.getAdmin();
  297. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  298. .setPayBy(StpUserUtil.getAdmin().getName())
  299. .setPayTicket(payTicket)
  300. .setPayTime(new Date());
  301. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  302. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  303. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  304. }
  305. this.updateById(tbBusiness);
  306. storeMsg(tbBusiness.getCustomerId(), "业务订单【" + tbBusiness.getNo() + "】已支付" + DateUtil.now() + "。");
  307. //发送消息
  308. String text = "您的费用已支付,业务单号[" + tbBusiness.getNo() + "]";
  309. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  310. }
  311. public void confirm(List<String> ids) {
  312. ids.forEach(id -> {
  313. TbBusiness business = this.getById(id);
  314. business.setConfirmInput(1).setAdminConfirmInput(1)
  315. .setConfirmInputTime(new Date())
  316. .setConfirmInputBy(StpUserUtil.getAdmin().getName());
  317. this.updateById(business);
  318. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(business.getId());
  319. String carNoStr = cars.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  320. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认" + DateUtil.now() + "。");
  321. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  322. String messageType = business.getGoodsId();
  323. items.forEach(item -> {
  324. String businessType = item.getBusinessType();
  325. //合作伙伴创建+是否下单后付款
  326. if (!StrUtil.equals(businessType, TbCostomer.CustomerEnum.BUSINESS_TYPE.getType())) {
  327. String customerId = business.getCreateByCustomerId();
  328. List<String> customerType=new ArrayList<>();
  329. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  330. if (tbCostomer!=null){
  331. customerType = StrUtil.splitTrim(tbCostomer.getType(),",");
  332. }
  333. if (customerType.contains(businessType)) {
  334. StaticLog.error("订单自动确认======================="+id);
  335. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), "系统自动确认",
  336. DateUtil.now(),
  337. business.getGoodsName() + "(" + item.getItemTypeName() + item.getItemName() + "-" + carNoStr + ")");
  338. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(customerId);
  339. Date now = new Date();
  340. item.setPickCustomerId(customerId).setPick(1)
  341. .setPickCustomerName(tbCostomer.getName()).setPickTime(now)
  342. .setConfirm(1).setConfirmTime(now);
  343. tbBusinessItemService.updateById(item);
  344. spAdminList.stream().map(SpAdmin::getOpenid).forEach(openId -> {
  345. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openId;
  346. wxService.sendTemplateMsg(wxConfig.getBusinessConfirmTemplate(), openId, msgDataBO, detailUrl);
  347. });
  348. } else {
  349. StaticLog.error("正常流程======================="+id);
  350. String remark = "车牌:";
  351. String carNo = business.getCardNo();
  352. String chinaCarNo = business.getChinaCarNo();
  353. if (StrUtil.isNotEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  354. remark += carNo + "、" + chinaCarNo;
  355. }
  356. if (StrUtil.isEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  357. remark += chinaCarNo;
  358. }
  359. if (StrUtil.isNotEmpty(carNo) && StrUtil.isEmpty(chinaCarNo)) {
  360. remark += carNo;
  361. }
  362. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), item.getItemTypeName(),
  363. item.getItemName(), business.getGoodsName(), DateUtil.now(), remark);
  364. List<String> openidList = tbCostomerService.findByMessageTypeOpenid(businessType, messageType);
  365. openidList.stream().filter(StrUtil::isNotEmpty).forEach(openid -> {
  366. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openid;
  367. wxService.sendTemplateMsg(wxConfig.getBusinessNoticeTemplate(), openid, msgDataBO, detailUrl);
  368. });
  369. }
  370. }
  371. });
  372. });
  373. }
  374. public void adminConfirmPay(List<String> ids, String ticket) {
  375. ids.forEach(id -> {
  376. TbBusiness business = this.getById(id);
  377. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  378. .setPayStatus(3).setPayTicket(ticket);
  379. this.updateById(business);
  380. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认支付" + DateUtil.now() + "。");
  381. //发送消息
  382. String text = "您的费用已支付,业务单号[" + business.getNo() + "]";
  383. tbNoticesService.sendNotice(business.getId(), business.getNo(), business.getCustomerId(), text);
  384. });
  385. }
  386. @Deprecated
  387. public void adminConfirmIn(String id, String inChannel) {
  388. TbBusiness tbBusiness = this.getById(id);
  389. tbBusiness.setRealInTime(new Date()).setInChannel(inChannel);
  390. this.updateById(tbBusiness);
  391. //更新境外车入场记录
  392. //更新中国车入场记录
  393. //发送消息
  394. String text = "您的车辆已入场,业务单号[" + tbBusiness.getNo() + "]";
  395. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  396. }
  397. public BigDecimal calculationPartMoney(Date iTime, Date oTime) {
  398. BigDecimal zero = new BigDecimal("0");
  399. if (iTime == null || oTime == null) {
  400. return zero;
  401. }
  402. LocalDateTime inDayTime = iTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  403. LocalDateTime outDayTime = oTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  404. long minutes = ChronoUnit.MINUTES.between(inDayTime, outDayTime);
  405. if (minutes < 0) {
  406. return zero;
  407. }
  408. long days = ChronoUnit.DAYS.between(inDayTime.toLocalDate(), outDayTime.toLocalDate());
  409. BigDecimal p = partConfig.getBasePrice();//乘积因子
  410. BigDecimal extraPrice = partConfig.getExtraPrice();
  411. int unit = (int) Math.ceil(NumberUtil.div(minutes, 24 * 60));
  412. if (minutes < partConfig.getFreeMinutes()) {
  413. unit = 0;
  414. }
  415. return p.multiply(new BigDecimal(unit)).add(new BigDecimal(days).multiply(extraPrice));
  416. }
  417. public Map<String, Object> getBusinessMoney(String businessCarId, String state) {
  418. Map<String, Object> result = new HashMap<>();
  419. List<TbBusiness> businessList = this.findOtherBusinessByCarId(businessCarId);
  420. businessList=businessList.stream().filter(tbBusiness -> 3 > tbBusiness.getPayStatus()).collect(Collectors.toList());
  421. Set<TbBusinessCar> cars = new HashSet<>();
  422. //越南车是否需要支付
  423. int vietnamCarPay = businessList.stream().anyMatch(tbBusiness -> {
  424. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  425. return tbGoods.getVietnamCarPay() == 1;
  426. }) ? 1 : 0;
  427. //中国车是否需要支付
  428. int chinaCarPay = businessList.stream().anyMatch(tbBusiness -> {
  429. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  430. return tbGoods.getChinaCarPay() == 1;
  431. }) ? 1 : 0;
  432. //无业务的车辆,中国车和越南车都需要支付
  433. if (businessList.isEmpty()) {
  434. vietnamCarPay = 1;
  435. chinaCarPay = 1;
  436. cars.add(tbBusinessCarService.getById(businessCarId));
  437. }
  438. businessList = businessList.stream().filter(tbBusiness -> {
  439. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  440. Integer payStep = tbGoods.getPayStep();
  441. //确认订单后方可缴费
  442. return TbGoods.PayStep.AFTER_CONFIRM.getCode().equals(payStep) && tbBusiness.getAdminConfirmInput() == 1
  443. //或者下单后可缴费
  444. || TbGoods.PayStep.BEFORE_CONFIRM.getCode().equals(payStep) && tbBusiness.getAdminConfirmInput() >= 0;
  445. }).collect(Collectors.toList());
  446. businessList.forEach(tbBusiness -> {
  447. List<TbBusinessCar> businessCars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  448. cars.addAll(businessCars);
  449. });
  450. List<String> businessIdList = businessList.stream().map(TbBusiness::getId).collect(Collectors.toList());
  451. String businessIds = StrUtil.join(",", businessIdList);
  452. // result.put("type", businessType);
  453. result.put("showPay", true);
  454. result.put("businessId", businessIds);
  455. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessIdList(businessIdList);
  456. Date now = new Date();
  457. List<Map<String, Object>> carsList = new ArrayList<>();
  458. for (TbBusinessCar tbBusinessCar : cars) {
  459. Date inTime = tbBusinessCar.getRealInTime();
  460. BigDecimal partMoney = new BigDecimal("0");
  461. String carType=tbBusinessCar.getCarType();
  462. if (StrUtil.isEmpty(carType)){
  463. carType=TbItem.ItemTypeEnum.EMPTY_TYPE.getType();
  464. }
  465. if (inTime != null && tbBusinessCar.getRealOutTime() == null) {
  466. if (tbBusinessCar.getPay() == 1 && tbBusinessCar.getPayTime() != null) {
  467. inTime = tbBusinessCar.getPayTime();
  468. }
  469. partMoney = this.calculationPartMoney(inTime, now);
  470. }
  471. if (StrUtil.isNotEmpty(tbBusinessCar.getColor())) {//蓝色车辆免费
  472. List<TbCarNoColor> freeList = tbCarNoColorService.getFreeColor();
  473. List<String> colorList = freeList.stream().map(f -> f.getCarNoColor().substring(0, 1)).collect(Collectors.toList());
  474. String color = tbBusinessCar.getColor().substring(0, 1);
  475. if (colorList.contains(color)) {
  476. partMoney = new BigDecimal("0");
  477. }
  478. }
  479. //越南车牌,是否免费
  480. if (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0) {
  481. partMoney = new BigDecimal("0");
  482. }
  483. //中国车,是否免费
  484. if (TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType)&& chinaCarPay == 0) {
  485. partMoney = new BigDecimal("0");
  486. }
  487. Map<String, Object> carMap = new HashMap<>();
  488. carMap.put("price", partMoney);
  489. carMap.put("id", tbBusinessCar.getId());
  490. carMap.put("carNo", tbBusinessCar.getCarNo());
  491. carMap.put("pay", tbBusinessCar.getPay());
  492. carMap.put("no", tbBusinessCar.getNo());
  493. if (TbBusinessCar.PayTypeEnum.FEE_TYPE.getType().equals(tbBusinessCar.getPayType())) {
  494. carMap.put("price", 0);
  495. }
  496. carsList.add(carMap);
  497. }
  498. result.put("carList", carsList.stream().sorted(Comparator.comparingDouble(obj -> Double.valueOf(obj.get("price").toString()))).collect(Collectors.toList()));
  499. BigDecimal itemsPrice = new BigDecimal("0");
  500. List<Map<String, Object>> itemList = new ArrayList<>();
  501. for (TbBusinessItem item : items) {
  502. BigDecimal itemPrice = item.getItemPrice().multiply(new BigDecimal(item.getNum()));
  503. if (item.getPayStatus() == 1) {
  504. continue;
  505. }
  506. itemsPrice = itemsPrice.add(itemPrice);
  507. Map<String, Object> itemMap = new HashMap<>();
  508. itemMap.put("id", item.getId());
  509. itemMap.put("name", item.getItemName() + "(" + item.getItemTypeName() + ")");
  510. itemMap.put("price", itemPrice);
  511. itemMap.put("pay", item.getPayStatus());
  512. itemList.add(itemMap);
  513. TbBusinessSort tbBusinessSort = tbBusinessSortService.findByItemTypeId(item.getItemTypeId());
  514. if (tbBusinessSort != null) {
  515. TbSortGroup sortGroup = tbSortGroupService.getById(tbBusinessSort.getGroupId());
  516. if (sortGroup.getCompletePay().equals(1) && sortGroup.getStatus().equals(1)) {
  517. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sortGroup.getId());
  518. if (sorts.size() > items.size()) {
  519. result.put("showPay", false);
  520. }
  521. }
  522. }
  523. }
  524. result.put("itemsPrice", itemsPrice);
  525. result.put("itemList", itemList);
  526. result.put("businessNo", businessList.stream().map(TbBusiness::getNo).distinct().collect(Collectors.joining("、")));
  527. result.put("goodsName", businessList.stream().map(TbBusiness::getGoodsName).distinct().collect(Collectors.joining("、")));
  528. return result;
  529. }
  530. public List<TbBusiness> findOtherBusinessByCarId(String businessCarId) {
  531. return tbBusinessMapper.findOtherBusinessByCarId(businessCarId);
  532. }
  533. public void uploadReport(TbBusiness tbBusiness) {
  534. this.updateById(tbBusiness);
  535. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  536. tbBusinessCarService.updateBatchById(cars);
  537. }
  538. public List<CarDisincle> getCarDisincleList(SoMap soMap) {
  539. return tbBusinessMapper.getCarDisincleList(soMap);
  540. }
  541. public TbBusiness getOtherBusinessById(String id) {
  542. TbBusiness tbBusiness = this.getById(id);
  543. List<TbBusinessItem> tbBusinessItems = tbBusinessItemService.findByBusinessId(id);
  544. tbBusiness.setItems(tbBusinessItems);
  545. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(id);
  546. tbBusiness.setCars(cars);
  547. return tbBusiness;
  548. }
  549. public void deleteOtherBusiness(String id) {
  550. TbBusiness db = super.getById(id);
  551. String declareNo = db.getDeclareNo();
  552. if (StrUtil.isNotEmpty(declareNo)) {
  553. tbDeclareService.rebackDeclareNo(declareNo);
  554. }
  555. this.removeById(id);
  556. //删除响应业务项
  557. tbBusinessItemService.removeByBusinessId(id);
  558. //检查是否删除车辆
  559. List<TbBusinessCar> tbBusinessCarList = tbBusinessCarService.findOtherBusinessCar(id);
  560. tbBusinessCarList.forEach(tbBusinessCar -> {
  561. //未入场
  562. if (tbBusinessCar.getRealInTime() == null) {
  563. List<RelationBusinessCar> relationBusinessCars = relationBusinessCarService.findByBusinessCarId(tbBusinessCar.getId());
  564. //无关联业务==>删除
  565. if (relationBusinessCars.size() == 1) {
  566. tbBusinessCarService.removeById(tbBusinessCar.getId());
  567. }
  568. }
  569. });
  570. //删除关系
  571. relationBusinessCarService.removeByBusinessId(id);
  572. }
  573. public int checkCarBusinessType(String typeId, String carNo, String operateTime, int before) {
  574. return tbBusinessMapper.checkCarBusinessType(typeId, carNo, operateTime, before);
  575. }
  576. public int checkCarBusinessType(String typeId, String carNo, String timeStart, String timeEnd, String businessId) {
  577. return tbBusinessMapper.checkCarBusinessTypeByTime(typeId, carNo, timeStart, timeEnd, businessId);
  578. }
  579. public void manualConfirmPay(String id, List<TbBusinessCar> cars, String remark) {
  580. TbBusiness db = this.getById(id);
  581. if (db == null) {
  582. throw new BusinessException("业务不存在");
  583. }
  584. Date now = new Date();
  585. db.setPayStatus(3).setPayMoney(db.getItemPrice()).setPayTime(now);
  586. this.updateById(db);
  587. cars.forEach(tbBusinessCar -> {
  588. Date realInTime = tbBusinessCar.getRealInTime();
  589. Date realOutTime = tbBusinessCar.getRealOutTime();
  590. if (realInTime != null && realOutTime != null) {
  591. BigDecimal price = calculationPartMoney(realInTime, realOutTime);
  592. tbBusinessCar.setMoney(price);
  593. }
  594. tbBusinessCar.setPay(1).setPayTime(now);
  595. tbBusinessCarService.updateById(tbBusinessCar);
  596. });
  597. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  598. items.forEach(tbBusinessItem -> tbBusinessItem
  599. .setPayStatus(1).setPayTime(now)
  600. .setRemark(StrUtil.isEmpty(remark) ? "手动确认" : remark)
  601. );
  602. tbBusinessItemService.updateBatchById(items);
  603. }
  604. public void unBindCar(String id, String businessCarId) {
  605. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  606. ew.eq("id", id).eq("business_car_id", businessCarId);
  607. TbBusiness db = getOne(ew);
  608. if (db == null) {
  609. throw new BusinessException("业务单不存在");
  610. }
  611. tbBusinessMapper.unBindCar(id);
  612. }
  613. public void addOtherBusiness(OtherBusinessBO otherBusinessBO) {
  614. List<TbItem> tbItems = otherBusinessBO.getItems();
  615. if (tbItems.isEmpty()) {
  616. throw new BusinessException("请选择明细");
  617. }
  618. List<TbBusinessCar> cars = otherBusinessBO.getCars();
  619. if (cars.isEmpty()) {
  620. throw new BusinessException("作业车辆不能为空");
  621. }
  622. Date now = new Date();
  623. TbGoods tbGoods = tbGoodsService.getById(otherBusinessBO.getGoodsId());
  624. TbBusiness tbBusiness = new TbBusiness();
  625. String customerId = otherBusinessBO.getCustomerId();
  626. tbBusiness.setCreateBy(StpUserUtil.getAdmin().getName());
  627. tbBusiness.setPayStep(tbGoods.getPayStep());
  628. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  629. if (tbCostomer != null) {
  630. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  631. }
  632. BigDecimal price = new BigDecimal("0");
  633. List<TbBusinessItem> itemList = new ArrayList<>();
  634. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  635. int index = 1;
  636. for (TbItem tbItem : tbItems) {
  637. TbItem db = tbItemService.getById(tbItem.getId());
  638. TbBusinessItem item = new TbBusinessItem();
  639. int num = tbItem.getNum();
  640. String typeId = tbItem.getTypeId();
  641. TbItemType tbItemType = tbItemTypeService.getById(typeId);
  642. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  643. item.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  644. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2));
  645. item.setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId())
  646. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  647. .setItemTypeId(typeId).setItemTypeName(tbItemType.getName())
  648. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  649. price = price.add(itemTotalPrice);
  650. itemList.add(item);
  651. index++;
  652. }
  653. String declareNo = otherBusinessBO.getDeclareNo();
  654. tbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getNetWeight())
  655. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  656. .setNo(no).setGoodsName(tbGoods.getName()).setGoodsId(otherBusinessBO.getGoodsId())
  657. .setPayStep(tbGoods.getPayStep()).setDeclareNo(declareNo).setCardNo(otherBusinessBO.getCardNo());
  658. tbBusiness.setCreateTime(now).setCreateByCustomerId(StpUserUtil.getCustomerId())
  659. .setItemPrice(price).setTotalMoney(price).setOwner(otherBusinessBO.getOwner());
  660. tbBusiness.setChinaCarNo(otherBusinessBO.getChinaCarNo());
  661. this.save(tbBusiness);
  662. Integer chinaCarPay = tbGoods.getChinaCarPay();
  663. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  664. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  665. for (TbBusinessCar car : cars) {
  666. String carNo = car.getCarNo().trim().toUpperCase();
  667. String carType=car.getCarType();
  668. if (StrUtil.isEmpty(carType)){
  669. throw new BusinessException(carNo+"类型不能为空");
  670. }
  671. List<TbBusinessCar> notOutRecords = tbBusinessCarService.findNotOutCar(carNo);
  672. if (notOutRecords.size() > 1) {
  673. throw new BusinessException(carNo + "有多个未出场记录,请先处理");
  674. }
  675. //最新一条记录
  676. TbBusinessCar db = tbBusinessCarService.findTheLastRecord(carNo);
  677. //不存在,或者已离场记录--->新建记录;
  678. if (db == null || db.getRealInTime() != null && db.getRealOutTime() != null) {
  679. db = new TbBusinessCar();
  680. db.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  681. }
  682. for (TbBusinessItem item : itemList) {
  683. checkOtherBusiness(item.getItemTypeId(), otherBusinessBO.getOperateTime(), tbBusiness.getId(), carNo);
  684. }
  685. db.setPayType(TbBusinessCar.PayTypeEnum.NO_PAY_TYPE.getType());
  686. if ((TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType)&& chinaCarPay == 0) || (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType)&& vietnamCarPay == 0)) {
  687. db.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  688. }
  689. TbCar tbCar = tbCarService.findByCardNo(carNo);
  690. if (tbCar != null) {
  691. db.setCarCompany(tbCar.getCustomerName());
  692. if (!TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  693. db.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  694. }
  695. }
  696. db.setCarNo(carNo).setIsLock(0);
  697. db.setCarSize(car.getCarSize())
  698. .setTimeUpdate(now).setCarType(car.getCarType())
  699. .setNetWeight(car.getNetWeight())
  700. .setCustomerId(customerId)
  701. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  702. tbBusinessCarService.saveOrUpdate(db);
  703. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  704. relationBusinessCar.setBusinessId(tbBusiness.getId()).setBusinessCarId(db.getId());
  705. relationBusinessCarService.save(relationBusinessCar);
  706. }
  707. if (StrUtil.isNotEmpty(declareNo)) {
  708. TbDeclare declare = tbDeclareService.findByDeclareNo(declareNo);
  709. declare.setBusinessId(tbBusiness.getId());
  710. tbDeclareService.updateById(declare);
  711. }
  712. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  713. tbBusinessItemService.saveBatch(itemList);
  714. }
  715. public List<TbBusiness> getOtherBusiness(SoMap startPage) {
  716. return tbBusinessMapper.getOtherBusiness(startPage);
  717. }
  718. public void editOtherBusiness(OtherBusinessBO otherBusinessBO) {
  719. String id = otherBusinessBO.getId();
  720. TbBusiness dbBusiness = this.getById(id);
  721. if (dbBusiness == null) {
  722. throw new BusinessException("记录不存在");
  723. }
  724. List<TbItem> tbItems = otherBusinessBO.getItems();
  725. if (tbItems.isEmpty()) {
  726. throw new BusinessException("请选择收费明细");
  727. }
  728. List<TbBusinessCar> cars = otherBusinessBO.getCars();
  729. if (cars.isEmpty()) {
  730. throw new BusinessException("作业车辆不能为空");
  731. }
  732. tbBusinessItemService.removeByBusinessId(id);
  733. Date now = new Date();
  734. List<RelationBusinessCar> relationBusinessCars = relationBusinessCarService.findByBusinessId(id);
  735. List<String> businessCarIds = cars.stream().filter(tbBusinessCar -> StrUtil.isNotEmpty(tbBusinessCar.getId()))
  736. .map(TbBusinessCar::getId).collect(Collectors.toList());
  737. List<String> removeIds = relationBusinessCars.stream()
  738. .filter(relationBusinessCar -> !businessCarIds.contains(relationBusinessCar.getBusinessCarId()))
  739. .map(RelationBusinessCar::getId)
  740. .collect(Collectors.toList());
  741. if (!removeIds.isEmpty()) {
  742. relationBusinessCarService.removeByIds(removeIds);
  743. }
  744. TbGoods tbGoods = tbGoodsService.getById(otherBusinessBO.getGoodsId());
  745. Integer chinaCarPay = tbGoods.getChinaCarPay();
  746. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  747. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  748. BigDecimal price = new BigDecimal("0");
  749. List<TbBusinessItem> itemList = new ArrayList<>();
  750. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  751. int index = 1;
  752. for (TbItem tbItem : tbItems) {
  753. TbItem db = tbItemService.getById(tbItem.getId());
  754. TbBusinessItem item = new TbBusinessItem();
  755. int num = tbItem.getNum();
  756. TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId());
  757. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  758. item.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  759. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2));
  760. item.setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId()).setBusinessId(dbBusiness.getId())
  761. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  762. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName())
  763. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  764. price = price.add(itemTotalPrice);
  765. itemList.add(item);
  766. index++;
  767. }
  768. dbBusiness.setItemPrice(price).setTotalMoney(price);
  769. tbBusinessItemService.saveBatch(itemList);
  770. for (TbBusinessCar tbBusinessCar : cars) {
  771. String businessCarId = tbBusinessCar.getId();
  772. String carNo = tbBusinessCar.getCarNo();
  773. for (TbBusinessItem item : itemList) {
  774. checkOtherBusiness(item.getItemTypeId(), otherBusinessBO.getOperateTime(), dbBusiness.getId(), carNo);
  775. }
  776. String carType=tbBusinessCar.getCarType();
  777. if (StrUtil.isNotEmpty(businessCarId)) {
  778. //原来已存在的
  779. TbBusinessCar dbBusinessCar = tbBusinessCarService.getById(businessCarId);
  780. //如果修改了车牌号
  781. String dbCarNo = dbBusinessCar.getCarNo();
  782. if (!dbCarNo.equals(carNo)) {
  783. //把关联删除掉,然后添加新的关联
  784. relationBusinessCarService.removeByBusinessIdAndCarId(dbBusiness.getId(), businessCarId);
  785. TbBusinessCar otherCar = tbBusinessCarService.findTheLastRecord(carNo);
  786. if (otherCar == null ||//不存在或者已离场===>新建
  787. (otherCar.getRealInTime() != null && otherCar.getRealOutTime() != null)) {
  788. otherCar = new TbBusinessCar();
  789. otherCar.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4))
  790. .setCarNo(carNo).setCarType(tbBusinessCar.getCarType()).setCarSize(tbBusinessCar.getCarSize())
  791. .setNetWeight(tbBusinessCar.getNetWeight()).setCustomerId(dbBusiness.getCustomerId())
  792. .setTimeUpdate(now).setIsLock(0).setCarType(tbBusinessCar.getCarType());
  793. if ((TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) || (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  794. otherCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  795. }
  796. tbBusinessCarService.save(otherCar);
  797. } else {
  798. List<TbBusiness> tbBusinessList = this.findOtherBusinessByCarId(businessCarId);
  799. if (tbBusinessList.size() == 1 && dbBusinessCar.getRealInTime() == null) {
  800. tbBusinessCarService.removeById(businessCarId);
  801. }
  802. }
  803. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  804. relationBusinessCar.setBusinessId(dbBusiness.getId()).setBusinessCarId(otherCar.getId());
  805. relationBusinessCarService.save(relationBusinessCar);
  806. } else {
  807. dbBusinessCar.setNetWeight(tbBusinessCar.getNetWeight()).setNetWeight(tbBusinessCar.getNetWeight())
  808. .setCarSize(tbBusinessCar.getCarSize()).setCarType(tbBusinessCar.getCarType());
  809. tbBusinessCarService.updateById(dbBusinessCar);
  810. }
  811. } else {
  812. TbBusinessCar checkCar = tbBusinessCarService.findTheLastRecord(carNo);
  813. if (checkCar == null ||
  814. (checkCar.getRealInTime() != null && checkCar.getRealOutTime() != null)) {
  815. checkCar = new TbBusinessCar();
  816. checkCar.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  817. }
  818. checkCar.setCarNo(carNo).setCarType(tbBusinessCar.getCarType()).setCarSize(tbBusinessCar.getCarSize())
  819. .setNetWeight(tbBusinessCar.getNetWeight()).setCustomerId(dbBusiness.getCustomerId())
  820. .setTimeUpdate(now).setIsLock(0).setCarType(tbBusinessCar.getCarType());
  821. if ((TbItem.ItemTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) || (TbItem.ItemTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  822. checkCar.setPayType(TbBusinessCar.PayTypeEnum.FEE_TYPE.getType());
  823. }
  824. tbBusinessCarService.saveOrUpdate(checkCar);
  825. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  826. relationBusinessCar.setBusinessId(dbBusiness.getId()).setBusinessCarId(checkCar.getId());
  827. relationBusinessCarService.save(relationBusinessCar);
  828. }
  829. }
  830. if (!StrUtil.equals(dbBusiness.getGoodsId(), otherBusinessBO.getGoodsId())) {
  831. dbBusiness.setGoodsId(otherBusinessBO.getGoodsId())
  832. .setGoodsName(tbGoods.getName()).setPayStep(tbGoods.getPayStep());
  833. }
  834. String declareNo = otherBusinessBO.getDeclareNo();
  835. if (!StrUtil.equals(declareNo, dbBusiness.getDeclareNo())) {
  836. tbDeclareService.rebackDeclareNo(dbBusiness.getDeclareNo());
  837. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(declareNo);
  838. tbDeclare.setBusinessId(dbBusiness.getId());
  839. tbDeclareService.updateById(tbDeclare);
  840. }
  841. dbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getNetWeight())
  842. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  843. .setNo(no).setGoodsId(otherBusinessBO.getGoodsId()).setOwner(otherBusinessBO.getOwner())
  844. .setDeclareNo(declareNo).setCardNo(otherBusinessBO.getCardNo());
  845. dbBusiness.setItemPrice(price).setTotalMoney(price);
  846. dbBusiness.setChinaCarNo(otherBusinessBO.getChinaCarNo());
  847. this.updateById(dbBusiness);
  848. }
  849. public void bindOtherBusinessCar(String businessId, List<String> businessCarIdList) {
  850. TbBusiness tbBusiness = this.getById(businessId);
  851. relationBusinessCarService.removeByBusinessId(businessId);
  852. List<TbBusinessCar> carList = tbBusinessCarService.listByIds(businessCarIdList);
  853. String chinaCarNo = carList.stream().filter(tbBusinessCar -> {
  854. String carNo = tbBusinessCar.getCarNo();
  855. String carStr = StrUtil.sub(carNo, 0, 1);
  856. return CAR_LIST.contains(carStr);
  857. }).map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  858. String yueCarNo = carList.stream().filter(tbBusinessCar -> {
  859. String carNo = tbBusinessCar.getCarNo();
  860. String carStr = StrUtil.sub(carNo, 0, 1);
  861. return !CAR_LIST.contains(carStr);
  862. }).map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  863. tbBusiness.setChinaCarNo(chinaCarNo);
  864. tbBusiness.setCardNo(yueCarNo);
  865. this.updateById(tbBusiness);
  866. businessCarIdList.forEach(businessCarId -> {
  867. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  868. relationBusinessCar.setBusinessCarId(businessCarId).setBusinessId(businessId);
  869. relationBusinessCarService.save(relationBusinessCar);
  870. });
  871. }
  872. private void checkOtherBusiness(String itemTypeId, String operateTimeStr, String businessId, String carNo) {
  873. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  874. TbBusinessSort sort = tbBusinessSortService.findByItemTypeId(itemTypeId);
  875. if (sort == null) {
  876. return;
  877. }
  878. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sort.getGroupId());
  879. LocalDateTime operateTime = LocalDateTime.parse(operateTimeStr, formatter);
  880. int i = 0;
  881. for (TbBusinessSort tbBusinessSort : sorts) {
  882. if (i == 0 && StrUtil.equals(tbBusinessSort.getTypeId(), itemTypeId) && sorts.size() > 1) {
  883. TbBusinessSort afterItem = sorts.get(i + 1);
  884. String typeId = afterItem.getTypeId();
  885. Integer interval = afterItem.getIntervalTime();
  886. LocalDateTime endTime = operateTime.plusMinutes(interval);
  887. if (afterItem.getIntervalTime() > 0) {
  888. int count = this.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 1);
  889. if (count > 0) {
  890. throw new BusinessException("车辆:[" + carNo + "]" + tbBusinessSort.getTypeName() + "后" + afterItem.getIntervalTime() + "分钟才可以执行" + afterItem.getTypeName());
  891. }
  892. }
  893. }
  894. if (i > 0 && StrUtil.equals(tbBusinessSort.getTypeId(), itemTypeId)) {
  895. TbBusinessSort beforeItem = sorts.get(i - 1);
  896. String typeId = beforeItem.getTypeId();
  897. Integer interval = tbBusinessSort.getIntervalTime();
  898. LocalDateTime endTime = operateTime.minusMinutes(interval);
  899. if (tbBusinessSort.getIntervalTime() > 0) {
  900. int count = this.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 0);
  901. if (count > 0) {
  902. throw new BusinessException("车辆:[" + carNo + "]" + beforeItem.getTypeName() + "后" + tbBusinessSort.getIntervalTime() + "分钟才可以执行" + tbBusinessSort.getTypeName());
  903. }
  904. }
  905. }
  906. Integer singleIntervalTime = tbBusinessSort.getSingleIntervalTime();
  907. String timeStart = operateTime.minusHours(singleIntervalTime).format(formatter);
  908. String timeEnd = operateTime.plusHours(singleIntervalTime).format(formatter);
  909. int count = this.checkCarBusinessType(itemTypeId, carNo, timeStart, timeEnd, businessId);
  910. if (count > 0) {
  911. throw new BusinessException("车辆:[" + carNo + "]" + singleIntervalTime + "小时内已有相同作业");
  912. }
  913. i++;
  914. }
  915. }
  916. public TbBusiness findByDeclareNo(String declareNo) {
  917. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  918. ew.eq("declare_no", declareNo).orderByDesc("id");
  919. List<TbBusiness> list = this.list(ew);
  920. return list.isEmpty() ? null : list.get(0);
  921. }
  922. public List<TbBusiness> getPay(String day) {
  923. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  924. ew.eq("date_format(pay_time,'%Y-%m-%d')",day).orderByDesc("id");
  925. return this.list(ew);
  926. }
  927. }