TbBusinessService.java 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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.DateUnit;
  10. import cn.hutool.core.date.DateUtil;
  11. import cn.hutool.core.util.NumberUtil;
  12. import cn.hutool.core.util.RandomUtil;
  13. import cn.hutool.core.util.StrUtil;
  14. import cn.hutool.http.HttpUtil;
  15. import cn.hutool.json.JSONObject;
  16. import cn.hutool.json.JSONUtil;
  17. import cn.hutool.log.StaticLog;
  18. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  19. import com.baomidou.mybatisplus.extension.service.IService;
  20. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  21. import com.pj.api.open.bo.ItemPriceBO;
  22. import com.pj.api.pushfee.oa.CallbackBO;
  23. import com.pj.api.wx.bo.MsgDataBO;
  24. import com.pj.api.wx.bo.PriceBO;
  25. import com.pj.api.wx.service.WxService;
  26. import com.pj.constants.UserTypeEnum;
  27. import com.pj.constants.business.*;
  28. import com.pj.current.config.*;
  29. import com.pj.current.satoken.StpUserUtil;
  30. import com.pj.project.oa.bo.OAResultBO;
  31. import com.pj.project.oa.bo.ParamsBO;
  32. import com.pj.project.relation_business_car.RelationBusinessCar;
  33. import com.pj.project.relation_business_car.RelationBusinessCarService;
  34. import com.pj.project.sync.SyncService;
  35. import com.pj.project.sync.request.IOrderPriceReq;
  36. import com.pj.project.sync.request.item.IFactorItem;
  37. import com.pj.project.sync.request.item.IOrderItem;
  38. import com.pj.project.sync.response.IOrderPriceRes;
  39. import com.pj.project.sync.response.item.IExpenseItem;
  40. import com.pj.project.sync.response.item.PriceDetailItem;
  41. import com.pj.project.tb_account.AutomaticPay;
  42. import com.pj.project.tb_account.TbAccount;
  43. import com.pj.project.tb_account.TbAccountService;
  44. import com.pj.project.tb_business_car.TbBusinessCar;
  45. import com.pj.project.tb_business_car.TbBusinessCarService;
  46. import com.pj.project.tb_business_item.TbBusinessItem;
  47. import com.pj.project.tb_business_item.TbBusinessItemService;
  48. import com.pj.project.tb_business_people.TbBusinessPeople;
  49. import com.pj.project.tb_business_people.TbBusinessPeopleService;
  50. import com.pj.project.tb_business_sort.TbBusinessSort;
  51. import com.pj.project.tb_business_sort.TbBusinessSortService;
  52. import com.pj.project.tb_car.TbCar;
  53. import com.pj.project.tb_car.TbCarService;
  54. import com.pj.project.tb_costomer.TbCostomer;
  55. import com.pj.project.tb_costomer.TbCostomerService;
  56. import com.pj.project.tb_declare.TbDeclare;
  57. import com.pj.project.tb_declare.TbDeclareService;
  58. import com.pj.project.tb_deduction_bind.TbDeductionBind;
  59. import com.pj.project.tb_deduction_bind.TbDeductionBindService;
  60. import com.pj.project.tb_driver.TbDriver;
  61. import com.pj.project.tb_driver.TbDriverService;
  62. import com.pj.project.tb_fee_details.TbFeeDetails;
  63. import com.pj.project.tb_fee_details.TbFeeDetailsService;
  64. import com.pj.project.tb_fee_statistics.TbFeeStatisticsService;
  65. import com.pj.project.tb_goods.TbGoods;
  66. import com.pj.project.tb_goods.TbGoodsService;
  67. import com.pj.project.tb_item.TbItem;
  68. import com.pj.project.tb_item.TbItemService;
  69. import com.pj.project.tb_item_fac.TbItemFac;
  70. import com.pj.project.tb_item_fac.TbItemFacService;
  71. import com.pj.project.tb_item_type.TbItemType;
  72. import com.pj.project.tb_item_type.TbItemTypeService;
  73. import com.pj.project.tb_mild_car.TbMildCar;
  74. import com.pj.project.tb_mild_car.TbMildCarService;
  75. import com.pj.project.tb_notices.TbNoticesService;
  76. import com.pj.project.tb_sort_group.TbSortGroup;
  77. import com.pj.project.tb_sort_group.TbSortGroupService;
  78. import com.pj.project.tb_unit.TbUnit;
  79. import com.pj.project.tb_unit.TbUnitService;
  80. import com.pj.project4sp.admin.SpAdmin;
  81. import com.pj.project4sp.admin.SpAdminService;
  82. import com.pj.project4sp.global.BusinessException;
  83. import com.pj.utils.AesUtil;
  84. import com.pj.utils.cache.RedisUtil;
  85. import com.pj.utils.sg.AjaxJson;
  86. import com.pj.utils.so.SoMap;
  87. import lombok.extern.slf4j.Slf4j;
  88. import org.springframework.beans.factory.annotation.Autowired;
  89. import org.springframework.context.annotation.Lazy;
  90. import org.springframework.stereotype.Service;
  91. import com.pj.utils.sg.*;
  92. import org.springframework.transaction.annotation.Transactional;
  93. import javax.annotation.Resource;
  94. /**
  95. * Service: tb_business -- 入境登记
  96. *
  97. * @author qzy
  98. */
  99. @Service
  100. @Transactional(rollbackFor = Exception.class)
  101. @Slf4j
  102. public class TbBusinessService extends ServiceImpl<TbBusinessMapper, TbBusiness> implements IService<TbBusiness> {
  103. /**
  104. * 底层 Mapper 对象
  105. */
  106. @Autowired
  107. TbBusinessMapper tbBusinessMapper;
  108. @Resource
  109. private TbCarService tbCarService;
  110. @Resource
  111. private TbDriverService tbDriverService;
  112. @Resource
  113. private TbCostomerService tbCostomerService;
  114. @Resource
  115. private TbItemService tbItemService;
  116. @Resource
  117. private TbItemTypeService tbItemTypeService;
  118. @Resource
  119. private TbBusinessItemService tbBusinessItemService;
  120. @Resource
  121. private PartConfig partConfig;
  122. @Resource
  123. private TbNoticesService tbNoticesService;
  124. @Resource
  125. private TbAccountService tbAccountService;
  126. @Resource
  127. TbBusinessCarService tbBusinessCarService;
  128. @Resource
  129. @Lazy
  130. TbDeclareService tbDeclareService;
  131. @Resource
  132. WxConfig wxConfig;
  133. @Resource
  134. @Lazy
  135. WxService wxService;
  136. @Resource
  137. MyConfig myConfig;
  138. @Resource
  139. private SpAdminService spAdminService;
  140. @Resource
  141. private TbBusinessSortService tbBusinessSortService;
  142. @Resource
  143. private TbSortGroupService tbSortGroupService;
  144. @Resource
  145. private TbGoodsService tbGoodsService;
  146. @Resource
  147. private RelationBusinessCarService relationBusinessCarService;
  148. @Autowired
  149. @Lazy
  150. private AutomaticPay automaticPay;
  151. @Resource
  152. private OAConfig oaConfig;
  153. @Resource
  154. private PushfeeConfig pushfeeConfig;
  155. @Resource
  156. private TbFeeDetailsService tbFeeDetailsService;
  157. @Resource
  158. private TbDeductionBindService tbDeductionBindService;
  159. @Resource
  160. @Lazy
  161. private TbFeeStatisticsService tbFeeStatisticsService;
  162. @Resource
  163. private SyncService syncService;
  164. @Resource
  165. private TbItemFacService tbItemFacService;
  166. @Resource
  167. private TbMildCarService tbMildCarService;
  168. private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");
  169. public void storeMsg(String businessCustomerId, String content) {
  170. String currentCustomerId = StpUserUtil.getCustomerId();
  171. if (StrUtil.equals(currentCustomerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  172. BusinessMessageManager.set(businessCustomerId, content);
  173. } else {
  174. BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), content);
  175. }
  176. }
  177. /**
  178. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  179. */
  180. public List<TbBusiness> getList(SoMap so) {
  181. return tbBusinessMapper.getList(so);
  182. }
  183. public void pay(String id, String payTicket) {
  184. TbBusiness tbBusiness = this.getById(id);
  185. SpAdmin admin = StpUserUtil.getAdmin();
  186. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  187. .setPayBy(StpUserUtil.getAdmin().getName())
  188. .setPayTicket(payTicket)
  189. .setPayTime(new Date());
  190. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  191. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  192. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  193. }
  194. this.updateById(tbBusiness);
  195. storeMsg(tbBusiness.getCustomerId(), "业务订单【" + tbBusiness.getNo() + "】已支付" + DateUtil.now() + "。");
  196. //发送消息
  197. String text = "您的费用已支付,业务单号[" + tbBusiness.getNo() + "]";
  198. tbNoticesService.sendNotice(tbBusiness.getId(), tbBusiness.getNo(), tbBusiness.getCustomerId(), text);
  199. }
  200. public void confirm(List<String> ids) {
  201. ids.forEach(id -> {
  202. TbBusiness business = this.getById(id);
  203. SpAdmin currAdmin = StpUserUtil.getAdmin();
  204. String adminName = StrUtil.isNotEmpty(currAdmin.getNickname()) ? currAdmin.getNickname() : currAdmin.getName();
  205. business.setConfirmInput(1).setAdminConfirmInput(1)
  206. .setConfirmInputTime(new Date())
  207. .setConfirmInputBy(StpUserUtil.getAdmin().getName())
  208. .setConfirmBy(adminName).setConfirmByAdminId(currAdmin.getId() + "");
  209. this.updateById(business);
  210. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(business.getId());
  211. String carNoStr = cars.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  212. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认" + DateUtil.now() + "。");
  213. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  214. String messageType = business.getGoodsId();
  215. items.forEach(item -> {
  216. String businessType = item.getBusinessType();
  217. //合作伙伴创建+是否下单后付款
  218. if (!StrUtil.equals(businessType, TbCostomer.CustomerEnum.BUSINESS_TYPE.getType())) {
  219. String customerId = business.getCreateByCustomerId();
  220. List<String> customerType = new ArrayList<>();
  221. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  222. if (tbCostomer != null) {
  223. customerType = StrUtil.splitTrim(tbCostomer.getType(), ",");
  224. }
  225. if (customerType.contains(businessType)) {
  226. StaticLog.error("订单自动确认=======================" + id);
  227. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), "系统自动确认",
  228. DateUtil.now(),
  229. business.getGoodsName() + "(" + item.getItemTypeName() + item.getItemName() + "-" + carNoStr + ")");
  230. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(customerId);
  231. Date now = new Date();
  232. item.setPickCustomerId(customerId).setPick(1)
  233. .setPickCustomerName(tbCostomer.getName()).setPickTime(now)
  234. .setConfirm(1).setConfirmTime(now);
  235. List<TbFeeDetails> feeDetailsList = tbFeeDetailsService.getByBusinessItemId(item.getId() + "");//重复交费会存在多个businessItemId
  236. if (feeDetailsList != null) {
  237. for (TbFeeDetails feeDetails : feeDetailsList) {
  238. feeDetails.setPickCustomerName(item.getPickCustomerName());
  239. tbFeeDetailsService.updateById(feeDetails);
  240. }
  241. }
  242. tbBusinessItemService.updateById(item);
  243. List<String> customerMessageType = StrUtil.splitTrim(tbCostomer.getMessageType(), ",");
  244. if (customerMessageType.contains(messageType)) {
  245. spAdminList.stream().map(SpAdmin::getOpenid).forEach(openId -> {
  246. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openId;
  247. wxService.sendTemplateMsg(wxConfig.getBusinessConfirmTemplate(), openId, msgDataBO, detailUrl);
  248. });
  249. }
  250. } else {
  251. StaticLog.error("正常流程=======================" + id);
  252. String remark = "车牌:";
  253. String carNo = business.getCardNo();
  254. String chinaCarNo = business.getChinaCarNo();
  255. if (StrUtil.isNotEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  256. remark += carNo + "、" + chinaCarNo;
  257. }
  258. if (StrUtil.isEmpty(carNo) && StrUtil.isNotEmpty(chinaCarNo)) {
  259. remark += chinaCarNo;
  260. }
  261. if (StrUtil.isNotEmpty(carNo) && StrUtil.isEmpty(chinaCarNo)) {
  262. remark += carNo;
  263. }
  264. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + item.getNo(), item.getItemTypeName(),
  265. item.getItemName(), business.getGoodsName(), DateUtil.now(), remark);
  266. List<String> openidList = tbCostomerService.findByMessageTypeOpenid(businessType, messageType);
  267. openidList.stream().filter(StrUtil::isNotEmpty).forEach(openid -> {
  268. String detailUrl = myConfig.getWebDomain() + "/pages/business-item/item-detail?itemId=" + item.getId() + "&openid=" + openid;
  269. wxService.sendTemplateMsg(wxConfig.getBusinessNoticeTemplate(), openid, msgDataBO, detailUrl);
  270. });
  271. }
  272. }
  273. });
  274. TbGoods tbGoods = tbGoodsService.getById(business.getGoodsId());
  275. //只有改业务是在确认后扣费才需要执行自动扣费
  276. if (GoodsEnum.DeductionTypeEnum.BUSINESS_OK.getCode().equals(tbGoods.getAutoDeductionType())) {
  277. //automaticPay.run(business.getId(), null, 2);
  278. }
  279. });
  280. }
  281. public void adminConfirmPay(List<String> ids, String ticket) {
  282. ids.forEach(id -> {
  283. TbBusiness business = this.getById(id);
  284. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  285. .setPayStatus(3).setPayTicket(ticket);
  286. this.updateById(business);
  287. storeMsg(business.getCustomerId(), "业务订单【" + business.getNo() + "】已确认支付" + DateUtil.now() + "。");
  288. //发送消息
  289. String text = "您的费用已支付,业务单号[" + business.getNo() + "]";
  290. tbNoticesService.sendNotice(business.getId(), business.getNo(), business.getCustomerId(), text);
  291. });
  292. }
  293. /**
  294. * 计费系统计算停车费
  295. *
  296. * @param iTime 入场时间
  297. * @param oTime 离场时间
  298. * @return
  299. */
  300. public IOrderPriceRes getPartMoney(Date iTime, Date oTime, String carNo, String carColor) {
  301. long minutes = DateUtil.between(iTime, oTime, DateUnit.MINUTE);
  302. double hours = NumberUtil.div(minutes, 60, 1);
  303. List<IOrderItem> expenses = new ArrayList<>();
  304. TbMildCar tbMildCar = tbMildCarService.findByCarNo(carNo);
  305. IOrderPriceRes res = new IOrderPriceRes();
  306. double carSize = 8D;
  307. res.setCarDesc(PartFeeEnum.MIDDLER_CAR.getDesc());
  308. res.setHourDesc(PartFeeEnum.IN_24_HOURS.getDesc());
  309. if (tbMildCar == null) {
  310. // 9.6以上
  311. carSize = 13D;
  312. res.setCarDesc(PartFeeEnum.BIG_CAR.getDesc());
  313. }
  314. if (hours > 24) {
  315. res.setOver24Hour(1);
  316. res.setHourDesc(PartFeeEnum.OUT_24_HOURS.getDesc());
  317. }
  318. List<TbItemFac> list = tbItemFacService.list();
  319. if (list.isEmpty()) {
  320. throw new AjaxError("未配置停车费规则");
  321. }
  322. TbItemFac itemFac = list.get(0);
  323. IOrderItem orderItem = new IOrderItem();
  324. String uniqueExpenseId=itemFac.getUniqueExpenseId();
  325. orderItem.setUniqExpenseId(uniqueExpenseId).setExpenseNum(1);
  326. List<IFactorItem> factors = new ArrayList<>();
  327. for (TbItemFac fac : list) {
  328. String operator = fac.getOperator();
  329. if (StrUtil.equals(operator, "carSize")) {
  330. IFactorItem iFactorItem = new IFactorItem();
  331. iFactorItem.setFacId(fac.getFacId() + "");
  332. iFactorItem.setFacParams(carSize + "");
  333. factors.add(iFactorItem);
  334. } else if (StrUtil.equals(operator, "hour")) {
  335. IFactorItem iFactorItem = new IFactorItem();
  336. iFactorItem.setFacId(fac.getFacId() + "");
  337. iFactorItem.setFacParams(hours + "");
  338. factors.add(iFactorItem);
  339. } else if (StrUtil.equals(operator, "carColor")) {
  340. IFactorItem iFactorItem = new IFactorItem();
  341. iFactorItem.setFacId(fac.getFacId() + "");
  342. iFactorItem.setFacParams(carColor);
  343. factors.add(iFactorItem);
  344. }
  345. }
  346. orderItem.setFactors(factors);
  347. expenses.add(orderItem);
  348. IOrderPriceRes orderPriceRes = syncService.orderPriceCal(expenses);
  349. res.setCalculateId(orderPriceRes.getCalculateId())
  350. .setUniqueExpenseId(uniqueExpenseId)
  351. .setFireResult(orderPriceRes.getFireResult())
  352. .setTotalOrderPrice(orderPriceRes.getTotalOrderPrice());
  353. log.info("part money resp:{}",JSONUtil.toJsonStr(res));
  354. return res;
  355. }
  356. /**
  357. * 停车费因子构建
  358. * qzyReal:
  359. * 时长---hour;车长--carSize
  360. * qzyReal:
  361. * 颜色--carColor
  362. *
  363. * @param iTime
  364. * @param oTime
  365. * @param carSize
  366. * @return
  367. */
  368. public List<IOrderItem> getPartExpenses(Date iTime, Date oTime, double carSize) {
  369. long minutes = DateUtil.between(iTime, oTime, DateUnit.MINUTE);
  370. double hours = NumberUtil.div(minutes, 60, 1);
  371. List<IOrderItem> expenses = new ArrayList<>();
  372. TbItem tbItem = tbItemService.getPartItem();
  373. if (tbItem == null) {
  374. throw new AjaxError("未配置停车费规则");
  375. }
  376. IOrderItem orderItem = new IOrderItem();
  377. orderItem.setUniqExpenseId(tbItem.getItemCode()).setExpenseNum(1);
  378. List<TbItemFac> facList = tbItemFacService.getByItemId(tbItem.getId());
  379. List<IFactorItem> factors = new ArrayList<>();
  380. facList.forEach(tbItemFac -> {
  381. String operator = tbItemFac.getOperator();
  382. IFactorItem iFactorItem = new IFactorItem();
  383. iFactorItem.setFacId(tbItemFac.getId());
  384. if (StrUtil.equals(operator, "carSize")) {
  385. iFactorItem.setFacParams(carSize + "");
  386. } else if (StrUtil.equals(operator, "hour")) {
  387. iFactorItem.setFacParams(hours + "");
  388. }
  389. factors.add(iFactorItem);
  390. });
  391. orderItem.setFactors(factors);
  392. return expenses;
  393. }
  394. public BigDecimal calculationPartMoney(Date iTime, Date oTime, double carSize) {
  395. BigDecimal zero = new BigDecimal("0");
  396. if (iTime == null || oTime == null) {
  397. return zero;
  398. }
  399. LocalDateTime inDayTime = iTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  400. LocalDateTime outDayTime = oTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  401. long minutes = ChronoUnit.MINUTES.between(inDayTime, outDayTime);
  402. if (minutes < 0 || minutes < partConfig.getFreeMinutes()) {
  403. return zero;
  404. }
  405. BigDecimal extraPrice = partConfig.getExtraPrice();
  406. BigDecimal basePrice = partConfig.getBasePrice();
  407. BigDecimal uniPrice = basePrice.add(extraPrice);//超过24小时之后每24个小时收费金额
  408. //4.2~9.6
  409. if (carSize <= partConfig.getMildCarLength() && carSize > partConfig.getFreeCarLength()) {
  410. basePrice = partConfig.getMildCarBasePrice();
  411. uniPrice = basePrice.add(extraPrice);
  412. }
  413. //间隔小时
  414. long hours = ChronoUnit.HOURS.between(inDayTime, outDayTime);
  415. final int hoursOf24 = 24;
  416. //24小时内只用交20或者30元
  417. if (hours < hoursOf24) {
  418. return basePrice;
  419. }
  420. //24小时个数
  421. long count = hours / hoursOf24;
  422. //超过24小时总停车费
  423. BigDecimal price = new BigDecimal(count).multiply(uniPrice);
  424. return price.add(basePrice);
  425. }
  426. /**
  427. * 微信端获取缴费信息
  428. *
  429. * @param businessCarId
  430. * @param channel
  431. * @return
  432. */
  433. public Map<String, Object> getBusinessMoney(String businessCarId, String channel) {
  434. Map<String, Object> result = new HashMap<>();
  435. //对应的业务
  436. List<TbBusiness> businessList = this.findOtherBusinessByCarId(businessCarId);
  437. TbBusinessCar tbBusinessCar = tbBusinessCarService.getById(businessCarId);
  438. //越南车是否需要支付
  439. int vietnamCarPay = businessList.stream().anyMatch(tbBusiness -> {
  440. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  441. return tbGoods.getVietnamCarPay() == 1;
  442. }) ? 1 : 0;
  443. //中国车是否需要支付
  444. int chinaCarPay = businessList.stream().anyMatch(tbBusiness -> {
  445. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  446. return tbGoods.getChinaCarPay() == 1;
  447. }) ? 1 : 0;
  448. //无业务的车辆,中国车和越南车都需要支付
  449. if (businessList.isEmpty()) {
  450. vietnamCarPay = 1;
  451. chinaCarPay = 1;
  452. }
  453. businessList = businessList.stream().filter(tbBusiness -> {
  454. TbGoods tbGoods = tbGoodsService.getById(tbBusiness.getGoodsId());
  455. Integer payStep = tbGoods.getPayStep();
  456. //确认订单后方可缴费
  457. return GoodsEnum.PayStep.AFTER_CONFIRM.getCode().equals(payStep) && tbBusiness.getAdminConfirmInput() == 1
  458. //或者下单后可缴费
  459. || GoodsEnum.PayStep.BEFORE_CONFIRM.getCode().equals(payStep) && tbBusiness.getAdminConfirmInput() >= 0;
  460. }).collect(Collectors.toList());
  461. List<String> businessIdList = businessList.stream().map(TbBusiness::getId).collect(Collectors.toList());
  462. String businessIds = StrUtil.join(",", businessIdList);
  463. result.put("showPay", true);
  464. result.put("businessId", businessIds);
  465. result.put("noJudgeCount", 0);
  466. if (CarEnum.BusinessTypeEnum.BUSINESS_CAR.getType().equals(tbBusinessCar.getBusinessType())) {
  467. if (tbBusinessCar.getConfirmJudge() != CarEnum.ConfirmJudgeEnum.JUDGE_PASS.getCode()) {
  468. result.put("noJudgeCount", 1);
  469. }
  470. }
  471. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessIdList(businessIdList);
  472. Date inTime = tbBusinessCar.getRealInTime();
  473. int pay = tbBusinessCar.getPay();
  474. Map<String, Object> carMap = new HashMap<>();
  475. carMap.put("price", 0);
  476. List<String> carCalculateIds = new ArrayList<>();
  477. //停车费
  478. if (pay == 0 && inTime != null) {
  479. Date outTime = tbBusinessCar.getRealOutTime();
  480. String cacheCarNo = RedisUtil.get(channel);
  481. if (outTime == null && StrUtil.isNotEmpty(cacheCarNo)) {
  482. outTime = new Date();
  483. }
  484. if (outTime != null) {
  485. String carType = tbBusinessCar.getCarType();
  486. if (StrUtil.isEmpty(carType)) {
  487. carType = CarEnum.CarTypeEnum.EMPTY_TYPE.getType();
  488. }
  489. //默认需要缴费--->
  490. BigDecimal partMoney = new BigDecimal("1");
  491. if (StrUtil.isNotEmpty(tbBusinessCar.getColor()) && tbBusinessCar.getCarSize() != null) {
  492. String freeColor = partConfig.getFreeColor();
  493. //4.2米以下蓝色车辆
  494. if (tbBusinessCar.getColor().contains(freeColor) && tbBusinessCar.getCarSize() <= partConfig.getFreeCarLength()) {
  495. partMoney = new BigDecimal("0");
  496. }
  497. }
  498. //越南车牌,是否免费
  499. if (CarEnum.CarTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0) {
  500. partMoney = new BigDecimal("0");
  501. }
  502. //中国车,是否免费
  503. if (CarEnum.CarTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) {
  504. partMoney = new BigDecimal("0");
  505. }
  506. if (CarEnum.PayTypeEnum.FEE_TYPE.getType().equals(tbBusinessCar.getPayType())) {
  507. partMoney = new BigDecimal("0");
  508. }
  509. if (partMoney.doubleValue() == 1) {
  510. IOrderPriceRes partPriceRes = getPartMoney(inTime, outTime, tbBusinessCar.getCarNo(), tbBusinessCar.getColor());
  511. BigDecimal partPrice = partPriceRes.getTotalOrderPrice();
  512. carCalculateIds.add(partPriceRes.getCalculateId());
  513. partMoney = partPrice;
  514. if (partPrice.doubleValue() > 0) {
  515. carMap.put("uniqExpenseId", partPriceRes.getFireResult().get(0).getUniqExpenseId());
  516. carMap.put("carDesc", partPriceRes.getCarDesc());
  517. carMap.put("calculateId;", partPriceRes.getCalculateId());
  518. carMap.put("hourDesc", partPriceRes.getHourDesc());
  519. carMap.put("over24Hour", partPriceRes.getOver24Hour());
  520. partPriceRes.getFireResult().get(0)
  521. .getPriceDetail().forEach(obj->carMap.put(obj.getKey(), obj.getValue()));
  522. }
  523. //todo 计算停车费
  524. }
  525. // partMoney = calculationPartMoney(inTime, outTime, tbBusinessCar.getCarSize());
  526. carMap.put("price", partMoney);
  527. }
  528. }
  529. carMap.put("id", tbBusinessCar.getId());
  530. carMap.put("carNo", tbBusinessCar.getCarNo());
  531. carMap.put("pay", pay);
  532. carMap.put("no", tbBusinessCar.getNo());
  533. result.put("carList", Collections.singleton(carMap));
  534. BigDecimal itemsPrice = new BigDecimal("0");
  535. List<Map<String, Object>> itemList = new ArrayList<>();
  536. if (!items.isEmpty()) {
  537. //未支付的
  538. List<IOrderItem> expenses = new ArrayList<>();
  539. items.stream().filter(item -> item.getPayStatus() == 0)
  540. .forEach(item -> {
  541. String num = item.getNum();
  542. String itemCode = item.getItemCode();
  543. IOrderItem iOrderItem = new IOrderItem();
  544. iOrderItem.setExpenseNum(Integer.parseInt(num)).setUniqExpenseId(itemCode);
  545. expenses.add(iOrderItem);
  546. });
  547. IOrderPriceRes orderPriceRes = syncService.orderPriceCal(expenses);
  548. BigDecimal totalPrice = orderPriceRes.getTotalOrderPrice();
  549. carCalculateIds.add(orderPriceRes.getCalculateId());
  550. for (TbBusinessItem item : items) {
  551. BigDecimal itemPrice = item.getItemPrice().multiply(new BigDecimal(item.getNum()));
  552. itemsPrice = itemsPrice.add(itemPrice);
  553. Map<String, Object> itemMap = new HashMap<>();
  554. itemMap.put("id", item.getId());
  555. itemMap.put("name", item.getItemName() + "(" + item.getItemTypeName() + ")");
  556. itemMap.put("price", itemPrice);
  557. itemMap.put("pay", item.getPayStatus());
  558. orderPriceRes.getFireResult().stream().filter(priceItem -> StrUtil.equals(item.getItemCode(), priceItem.getUniqExpenseId()))
  559. .findAny().ifPresent(priceItem -> {
  560. itemMap.put("price", priceItem.getFirePrice().multiply(new BigDecimal(item.getNum())));
  561. });
  562. itemList.add(itemMap);
  563. TbBusinessSort tbBusinessSort = tbBusinessSortService.findByItemTypeId(item.getItemTypeId());
  564. if (tbBusinessSort != null) {
  565. TbSortGroup sortGroup = tbSortGroupService.getById(tbBusinessSort.getGroupId());
  566. if (sortGroup.getCompletePay().equals(1) && sortGroup.getStatus().equals(1)) {
  567. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sortGroup.getId());
  568. for (TbBusinessSort sort : sorts) {
  569. long count = items.stream().filter(tbBusinessItem -> StrUtil.equals(sort.getTypeId(), tbBusinessItem.getItemTypeId()))
  570. .count();
  571. if (count == 0) {
  572. result.put("showPay", false);
  573. }
  574. }
  575. }
  576. }
  577. }
  578. result.put("itemsPrice", totalPrice);
  579. }
  580. result.put("itemList", itemList);
  581. result.put("calculateId", String.join(",", carCalculateIds));
  582. result.put("businessNo", businessList.stream().map(TbBusiness::getNo).distinct().collect(Collectors.joining("、")));
  583. result.put("goodsName", businessList.stream().map(TbBusiness::getGoodsName).distinct().collect(Collectors.joining("、")));
  584. return result;
  585. }
  586. public List<TbBusiness> findOtherBusinessByCarId(String businessCarId) {
  587. return tbBusinessMapper.findOtherBusinessByCarId(businessCarId);
  588. }
  589. public void uploadReport(TbBusiness tbBusiness) {
  590. this.updateById(tbBusiness);
  591. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  592. tbBusinessCarService.updateBatchById(cars);
  593. }
  594. public List<CarDisincle> getCarDisincleList(SoMap soMap) {
  595. return tbBusinessMapper.getCarDisincleList(soMap);
  596. }
  597. public TbBusiness getOtherBusinessById(String id) {
  598. TbBusiness tbBusiness = this.getById(id);
  599. List<TbBusinessItem> tbBusinessItems = tbBusinessItemService.findByBusinessId(id);
  600. tbBusiness.setItems(tbBusinessItems);
  601. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(id);
  602. tbBusiness.setCars(cars);
  603. return tbBusiness;
  604. }
  605. /**
  606. * 获取业务信息,但只取装卸类型的收费项
  607. */
  608. public TbBusiness getZxBusinessById(String id) {
  609. TbBusiness tbBusiness = this.getById(id);
  610. List<TbBusinessItem> tbBusinessItems = tbBusinessItemService.findZxByBusinessId(id);
  611. tbBusiness.setItems(tbBusinessItems);
  612. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(id);
  613. tbBusiness.setCars(cars);
  614. return tbBusiness;
  615. }
  616. public void deleteOtherBusiness(String id) {
  617. TbBusiness db = super.getById(id);
  618. String declareNo = db.getDeclareNo();
  619. if (StrUtil.isNotEmpty(declareNo)) {
  620. tbDeclareService.rebackDeclareNo(declareNo);
  621. }
  622. this.removeById(id);
  623. //删除响应业务项
  624. tbBusinessItemService.removeByBusinessId(id);
  625. //检查是否删除车辆
  626. List<TbBusinessCar> tbBusinessCarList = tbBusinessCarService.findOtherBusinessCar(id);
  627. tbBusinessCarList.forEach(tbBusinessCar -> {
  628. //未入场
  629. if (tbBusinessCar.getRealInTime() == null) {
  630. List<RelationBusinessCar> relationBusinessCars = relationBusinessCarService.findByBusinessCarId(tbBusinessCar.getId());
  631. //无关联业务==>删除
  632. if (relationBusinessCars.size() == 1) {
  633. tbBusinessCarService.removeById(tbBusinessCar.getId());
  634. }
  635. }
  636. });
  637. //删除关系
  638. relationBusinessCarService.removeByBusinessId(id);
  639. }
  640. public int checkCarBusinessType(String typeId, String carNo, String operateTime, int before) {
  641. return tbBusinessMapper.checkCarBusinessType(typeId, carNo, operateTime, before);
  642. }
  643. public int checkCarBusinessType(String typeId, String carNo, String timeStart, String timeEnd, String businessId) {
  644. return tbBusinessMapper.checkCarBusinessTypeByTime(typeId, carNo, timeStart, timeEnd, businessId);
  645. }
  646. /**
  647. * 线下收费
  648. *
  649. * @param id
  650. * @param cars
  651. * @param remark
  652. * @param payTime
  653. * @param payChannelType
  654. * @param payType
  655. * @param transactionId
  656. */
  657. public void manualConfirmPay(String id, List<TbBusinessCar> cars, String remark, Date payTime, int payChannelType, int payType, String transactionId) {
  658. TbBusiness db = this.getById(id);
  659. if (db == null) {
  660. throw new BusinessException("业务不存在");
  661. }
  662. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  663. TbDeductionBind bind = null;
  664. BigDecimal partMoney = new BigDecimal(0);
  665. String plate = null;
  666. List<PriceBO> priceBOList = new ArrayList<>();
  667. TbGoods tbGoods = tbGoodsService.getById(db.getGoodsId());
  668. //该确认确未确认
  669. if (tbGoods.getPayStep().equals(GoodsEnum.PayStep.AFTER_CONFIRM.getCode()) && db.getAdminConfirmInput() == 0) {
  670. throw new AjaxError("该业务需要确认后才能收费");
  671. }
  672. for (TbBusinessCar tbBusinessCar : cars) {
  673. String carNo = tbBusinessCar.getCarNo().toUpperCase();
  674. String prefix = StrUtil.sub(carNo, 0, 1);
  675. if (CAR_LIST.contains(prefix) && tbGoods.getChinaCarPay() == 1
  676. || !CAR_LIST.contains(prefix) && tbGoods.getVietnamCarPay() == 1) {
  677. Date realInTime = tbBusinessCar.getRealInTime();
  678. Date realOutTime = tbBusinessCar.getRealOutTime();
  679. if (realInTime != null && realOutTime != null) {
  680. BigDecimal price = calculationPartMoney(realInTime, realOutTime, tbBusinessCar.getCarSize());
  681. if (price.doubleValue() > 0) {
  682. PriceBO priceBO = new PriceBO();
  683. priceBO.setId(tbBusinessCar.getId()).setP(price);
  684. priceBOList.add(priceBO);
  685. }
  686. partMoney = partMoney.add(price);
  687. tbBusinessCar.setMoney(price);
  688. }
  689. }
  690. tbBusinessCar.setPay(1).setPayTime(payTime)
  691. .setPayType(CarEnum.PayTypeEnum.HAS_PAY_TYPE.getType());
  692. if (payType == PayEnum.PayType.PER_PAY.getCode()) {
  693. TbDeductionBind deductionBind = tbDeductionBindService.getBindCarByPlate(carNo);
  694. if (deductionBind != null) {
  695. plate = carNo;
  696. bind = deductionBind;
  697. }
  698. }
  699. }
  700. BigDecimal itemPrice = db.getItemPrice();
  701. BigDecimal money = itemPrice.add(partMoney);
  702. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + RandomUtil.randomNumbers(4);
  703. if (payType == PayEnum.PayType.PER_PAY.getCode()) {
  704. if (bind == null) {
  705. throw new AjaxError("车辆未绑定预支付款,无法完成收费");
  706. }
  707. TbAccount tbAccount = tbAccountService.getByCustomerId(bind.getCustomerId());
  708. String key = AesUtil.reverse(tbAccount.getAccSalt());
  709. BigDecimal balance = new BigDecimal(tbAccount.getTotalMoney());
  710. if (balance.compareTo(money) < 0) {
  711. throw new AjaxError("车辆绑定账户余额不足,无法完成收费");
  712. }
  713. balance = balance.subtract(money);
  714. tbAccount.setTotalMoney(AesUtil.encryptECB(balance.toString(), key));
  715. tbAccountService.updateById(tbAccount);
  716. //生成扣款记录
  717. if (partMoney.doubleValue() > 0 && !priceBOList.isEmpty()) {
  718. //车辆的扣费记录
  719. List<TbFeeDetails> parkFeeDetailsList = tbFeeDetailsService.chargeParkFee(
  720. priceBOList, null, null, payTime, PayEnum.PayType.PER_PAY);
  721. automaticPay.createTbDeductionRecord(parkFeeDetailsList, tbAccount, plate, bind.getCustomerName(), no);
  722. }
  723. //生成收费明细
  724. List<TbFeeDetails> tbFeeDetailsList = tbFeeDetailsService.chargeBusinessFee(
  725. items, null, null, payTime, PayEnum.PayType.PER_PAY);
  726. //生成扣费记录
  727. automaticPay.createTbDeductionRecord(tbFeeDetailsList, tbAccount, plate, bind.getCustomerName(), no);
  728. automaticPay.unbindRun(plate);
  729. } else if (payType == PayEnum.PayType.OFF_LINE_PAY.getCode()) {
  730. //车辆的扣费记录
  731. if (partMoney.doubleValue() > 0) {
  732. tbFeeDetailsService.chargeParkFee(
  733. priceBOList, null, null, payTime, PayEnum.PayType.OFF_LINE_PAY);
  734. }
  735. //生成收费明细
  736. tbFeeDetailsService.chargeBusinessFee(
  737. items, null, null, payTime, PayEnum.PayType.OFF_LINE_PAY);
  738. }
  739. db.setPayStatus(PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode())
  740. .setPayMoney(db.getItemPrice())
  741. .setPayType(payType).setPayChannelType(payChannelType)
  742. .setTransactionId(transactionId)
  743. .setPayTime(payTime);
  744. items.forEach(tbBusinessItem -> tbBusinessItem
  745. .setPayStatus(1).setPayTime(payTime)
  746. .setRemark(StrUtil.isEmpty(remark) ? "手动确认" : remark)
  747. );
  748. this.updateById(db);
  749. tbBusinessCarService.updateBatchById(cars);
  750. tbBusinessItemService.updateBatchById(items);
  751. tbFeeStatisticsService.addOrUpdateStatistic(payTime);//更新当前日期的日统计
  752. }
  753. public void unBindCar(String id, String businessCarId) {
  754. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  755. ew.eq("id", id).eq("business_car_id", businessCarId);
  756. TbBusiness db = getOne(ew);
  757. if (db == null) {
  758. throw new BusinessException("业务单不存在");
  759. }
  760. tbBusinessMapper.unBindCar(id);
  761. }
  762. public TbBusiness addOtherBusiness(OtherBusinessBO otherBusinessBO) {
  763. List<TbItem> tbItems = otherBusinessBO.getItems();
  764. if (tbItems.isEmpty()) {
  765. throw new BusinessException("请选择明细");
  766. }
  767. List<TbBusinessCar> cars = otherBusinessBO.getCars();
  768. if (cars.isEmpty()) {
  769. throw new BusinessException("作业车辆不能为空");
  770. }
  771. Date now = new Date();
  772. TbGoods tbGoods = tbGoodsService.getById(otherBusinessBO.getGoodsId());
  773. TbBusiness tbBusiness = new TbBusiness();
  774. String customerId = otherBusinessBO.getCustomerId();
  775. tbBusiness.setAutoDeductionType(tbGoods.getAutoDeductionType());
  776. tbBusiness.setCreateBy(StpUserUtil.getAdmin().getName());
  777. tbBusiness.setPayStep(tbGoods.getPayStep());
  778. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  779. if (tbCostomer != null) {
  780. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  781. }
  782. BigDecimal price = new BigDecimal("0");
  783. List<TbBusinessItem> itemList = new ArrayList<>();
  784. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  785. int index = 1;
  786. for (TbItem tbItem : tbItems) {
  787. TbItem db = tbItemService.getById(tbItem.getId());
  788. TbBusinessItem item = new TbBusinessItem();
  789. int num = tbItem.getNum();
  790. String typeId = tbItem.getTypeId();
  791. TbItemType tbItemType = tbItemTypeService.getById(typeId);
  792. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  793. item.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  794. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2));
  795. item.setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId())
  796. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  797. .setItemTypeId(typeId).setItemTypeName(tbItemType.getName())
  798. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  799. price = price.add(itemTotalPrice);
  800. itemList.add(item);
  801. index++;
  802. }
  803. String declareNo = otherBusinessBO.getDeclareNo();
  804. tbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getNetWeight())
  805. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  806. .setNo(no).setGoodsName(tbGoods.getName()).setGoodsId(otherBusinessBO.getGoodsId())
  807. .setBusinessGoodsName(otherBusinessBO.getBusinessGoodsName()).setBusinessGoodsNum(otherBusinessBO.getBusinessGoodsNum())
  808. .setPayStep(tbGoods.getPayStep()).setDeclareNo(declareNo).setCardNo(otherBusinessBO.getCardNo());
  809. tbBusiness.setCreateTime(now).setCreateByCustomerId(StpUserUtil.getCustomerId())
  810. .setItemPrice(price).setTotalMoney(price).setOwner(otherBusinessBO.getOwner());
  811. tbBusiness.setChinaCarNo(otherBusinessBO.getChinaCarNo());
  812. this.save(tbBusiness);
  813. Integer chinaCarPay = tbGoods.getChinaCarPay();
  814. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  815. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  816. for (TbBusinessCar car : cars) {
  817. String carNo = car.getCarNo().trim().toUpperCase();
  818. String carType = car.getCarType();
  819. if (car.getCarSize() == null || car.getCarSize() <= 0) {
  820. throw new AjaxError(carNo + "规格不正确");
  821. }
  822. if (StrUtil.isEmpty(carType)) {
  823. throw new BusinessException(carNo + "类型不能为空");
  824. }
  825. List<TbBusinessCar> notOutRecords = tbBusinessCarService.findNotOutCar(carNo);
  826. if (notOutRecords.size() > 1) {
  827. throw new BusinessException(carNo + "有多个未出场记录,请先处理");
  828. }
  829. //最新一条记录
  830. TbBusinessCar db = tbBusinessCarService.findTheLastRecord(carNo);
  831. //存在并且未离场+已审核
  832. if (db != null && db.getConfirmJudge() == CarEnum.ConfirmJudgeEnum.JUDGE_PASS.getCode() && db.getRealOutTime() == null) {
  833. throw new BusinessException("车辆【" + carNo + "】已审核,请驳回再录入业务");
  834. }
  835. if (!tbGoods.getName().contains("整车")) {
  836. //记录不存在或者已离场
  837. if (db == null || db.getRealOutTime() != null) {
  838. throw new BusinessException("车辆【" + carNo + "】未入场,无法录入业务");
  839. }
  840. }
  841. //不存在,或者已离场记录--->新建记录;
  842. if (db == null || db.getRealInTime() != null && db.getRealOutTime() != null) {
  843. db = new TbBusinessCar();
  844. db.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  845. }
  846. for (TbBusinessItem item : itemList) {
  847. if (StrUtil.isNotEmpty(otherBusinessBO.getOperateTime())) {
  848. checkOtherBusiness(item.getItemTypeId(), otherBusinessBO.getOperateTime(), tbBusiness.getId(), carNo);
  849. }
  850. }
  851. db.setPayType(CarEnum.PayTypeEnum.NO_PAY_TYPE.getType());
  852. if ((CarEnum.CarTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0)
  853. || (CarEnum.CarTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  854. db.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  855. }
  856. TbCar tbCar = tbCarService.findByCardNo(carNo);
  857. if (tbCar != null) {
  858. db.setCarCompany(tbCar.getCustomerName());
  859. if (!TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  860. db.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  861. }
  862. }
  863. db.setCarNo(carNo).setIsLock(0).setBusinessType(CarEnum.BusinessTypeEnum.BUSINESS_CAR.getType());
  864. db.setCarSize(car.getCarSize()).setConfirmJudge(CarEnum.ConfirmJudgeEnum.NO_JUDGE.getCode())//业务车都是未审核状态
  865. .setTimeUpdate(now).setCarType(car.getCarType())
  866. .setNetWeight(car.getNetWeight()).setColor(car.getColor())
  867. .setCustomerId(customerId)
  868. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  869. tbBusinessCarService.saveOrUpdate(db);
  870. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  871. relationBusinessCar.setBusinessId(tbBusiness.getId()).setBusinessCarId(db.getId());
  872. relationBusinessCarService.save(relationBusinessCar);
  873. }
  874. if (StrUtil.isNotEmpty(declareNo)) {
  875. TbDeclare declare = tbDeclareService.findByDeclareNo(declareNo);
  876. declare.setBusinessId(tbBusiness.getId());
  877. tbDeclareService.updateById(declare);
  878. }
  879. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  880. tbBusinessItemService.saveBatch(itemList);
  881. return tbBusiness;
  882. }
  883. public List<TbBusiness> getOtherBusiness(SoMap startPage) {
  884. return tbBusinessMapper.getOtherBusiness(startPage);
  885. }
  886. public void editOtherBusiness(OtherBusinessBO otherBusinessBO) {
  887. String id = otherBusinessBO.getId();
  888. TbBusiness dbBusiness = this.getById(id);
  889. if (dbBusiness == null) {
  890. throw new BusinessException("记录不存在");
  891. }
  892. List<TbItem> tbItems = otherBusinessBO.getItems();
  893. if (tbItems.isEmpty()) {
  894. throw new BusinessException("请选择收费明细");
  895. }
  896. List<TbBusinessCar> cars = otherBusinessBO.getCars();
  897. if (cars.isEmpty()) {
  898. throw new BusinessException("作业车辆不能为空");
  899. }
  900. Date now = new Date();
  901. List<RelationBusinessCar> relationBusinessCars = relationBusinessCarService.findByBusinessId(id);
  902. List<String> businessCarIds = cars.stream().filter(tbBusinessCar -> StrUtil.isNotEmpty(tbBusinessCar.getId()))
  903. .map(TbBusinessCar::getId).collect(Collectors.toList());
  904. List<String> removeIds = relationBusinessCars.stream()
  905. .filter(relationBusinessCar -> !businessCarIds.contains(relationBusinessCar.getBusinessCarId()))
  906. .map(RelationBusinessCar::getId)
  907. .collect(Collectors.toList());
  908. if (!removeIds.isEmpty()) {
  909. relationBusinessCarService.removeByIds(removeIds);
  910. }
  911. TbGoods tbGoods = tbGoodsService.getById(otherBusinessBO.getGoodsId());
  912. Integer chinaCarPay = tbGoods.getChinaCarPay();
  913. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  914. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  915. List<TbBusinessItem> itemList = new ArrayList<>();
  916. if (PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode() != dbBusiness.getPayStatus()) {
  917. tbBusinessItemService.removeByBusinessId(id);
  918. BigDecimal price = new BigDecimal("0");
  919. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  920. int index = 1;
  921. for (TbItem tbItem : tbItems) {
  922. TbItem db = tbItemService.getById(tbItem.getId());
  923. TbBusinessItem item = new TbBusinessItem();
  924. int num = tbItem.getNum();
  925. TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId());
  926. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  927. item.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  928. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2));
  929. item.setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId()).setBusinessId(dbBusiness.getId())
  930. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  931. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName())
  932. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  933. price = price.add(itemTotalPrice);
  934. itemList.add(item);
  935. index++;
  936. }
  937. dbBusiness.setItemPrice(price).setTotalMoney(price);
  938. tbBusinessItemService.saveBatch(itemList);
  939. }
  940. for (TbBusinessCar tbBusinessCar : cars) {
  941. String businessCarId = tbBusinessCar.getId();
  942. String carNo = tbBusinessCar.getCarNo();
  943. for (TbBusinessItem item : itemList) {
  944. checkOtherBusiness(item.getItemTypeId(), otherBusinessBO.getOperateTime(), dbBusiness.getId(), carNo);
  945. }
  946. String carType = tbBusinessCar.getCarType();
  947. if (StrUtil.isNotEmpty(businessCarId)) {
  948. //原来已存在的
  949. TbBusinessCar dbBusinessCar = tbBusinessCarService.getById(businessCarId);
  950. if (dbBusinessCar.getConfirmJudge() == CarEnum.ConfirmJudgeEnum.JUDGE_PASS.getCode()) {
  951. throw new BusinessException("车辆【" + carNo + "】已审核,请驳回再录入业务");
  952. }
  953. //如果修改了车牌号
  954. String dbCarNo = dbBusinessCar.getCarNo();
  955. if (!dbCarNo.equals(carNo)) {
  956. //把关联删除掉,然后添加新的关联
  957. relationBusinessCarService.removeByBusinessIdAndCarId(dbBusiness.getId(), businessCarId);
  958. TbBusinessCar otherCar = tbBusinessCarService.findTheLastRecord(carNo);
  959. if (otherCar == null ||//不存在或者已离场===>新建
  960. (otherCar.getRealInTime() != null && otherCar.getRealOutTime() != null)) {
  961. otherCar = new TbBusinessCar();
  962. otherCar.setCreateTime(now).setPay(0).setNo(carBuseinssNo + RandomUtil.randomNumbers(4))
  963. .setCarNo(carNo).setCarType(tbBusinessCar.getCarType()).setCarSize(tbBusinessCar.getCarSize())
  964. .setNetWeight(tbBusinessCar.getNetWeight()).setCustomerId(dbBusiness.getCustomerId())
  965. .setTimeUpdate(now).setIsLock(0).setCarType(tbBusinessCar.getCarType());
  966. if ((CarEnum.CarTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) || (CarEnum.CarTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  967. otherCar.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  968. }
  969. tbBusinessCarService.save(otherCar);
  970. } else {
  971. List<TbBusiness> tbBusinessList = this.findOtherBusinessByCarId(businessCarId);
  972. if (tbBusinessList.size() == 1 && dbBusinessCar.getRealInTime() == null) {
  973. tbBusinessCarService.removeById(businessCarId);
  974. }
  975. }
  976. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  977. relationBusinessCar.setBusinessId(dbBusiness.getId()).setBusinessCarId(otherCar.getId());
  978. relationBusinessCarService.save(relationBusinessCar);
  979. } else {
  980. dbBusinessCar.setNetWeight(tbBusinessCar.getNetWeight()).setNetWeight(tbBusinessCar.getNetWeight())
  981. .setCarSize(tbBusinessCar.getCarSize()).setCarType(tbBusinessCar.getCarType());
  982. tbBusinessCarService.updateById(dbBusinessCar);
  983. }
  984. } else {
  985. TbBusinessCar checkCar = tbBusinessCarService.findTheLastRecord(carNo);
  986. //存在+审核+未离场的
  987. if (checkCar != null
  988. && checkCar.getConfirmJudge() == CarEnum.ConfirmJudgeEnum.JUDGE_PASS.getCode()
  989. && checkCar.getRealOutTime() == null) {
  990. throw new BusinessException("车辆【" + carNo + "】已审核,请驳回再录入业务");
  991. }
  992. //不存在或者已经离场的
  993. if (checkCar == null ||
  994. (checkCar.getRealInTime() != null && checkCar.getRealOutTime() != null)) {
  995. checkCar = new TbBusinessCar();
  996. checkCar.setCreateTime(now).setPay(0).setBusinessType(1)
  997. .setConfirmJudge(CarEnum.ConfirmJudgeEnum.NO_JUDGE.getCode())
  998. .setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  999. }
  1000. checkCar.setCarNo(carNo).setCarType(tbBusinessCar.getCarType()).setCarSize(tbBusinessCar.getCarSize())
  1001. .setNetWeight(tbBusinessCar.getNetWeight()).setCustomerId(dbBusiness.getCustomerId())
  1002. .setTimeUpdate(now).setIsLock(0).setCarType(tbBusinessCar.getCarType());
  1003. if ((CarEnum.CarTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0) || (CarEnum.CarTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  1004. checkCar.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  1005. }
  1006. tbBusinessCarService.saveOrUpdate(checkCar);
  1007. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  1008. relationBusinessCar.setBusinessId(dbBusiness.getId()).setBusinessCarId(checkCar.getId());
  1009. relationBusinessCarService.save(relationBusinessCar);
  1010. }
  1011. }
  1012. if (!StrUtil.equals(dbBusiness.getGoodsId(), otherBusinessBO.getGoodsId())) {
  1013. dbBusiness.setGoodsId(otherBusinessBO.getGoodsId())
  1014. .setGoodsName(tbGoods.getName()).setPayStep(tbGoods.getPayStep());
  1015. }
  1016. String declareNo = otherBusinessBO.getDeclareNo();
  1017. if (!StrUtil.equals(declareNo, dbBusiness.getDeclareNo())) {
  1018. tbDeclareService.rebackDeclareNo(dbBusiness.getDeclareNo());
  1019. TbDeclare tbDeclare = tbDeclareService.findByDeclareNo(declareNo);
  1020. tbDeclare.setBusinessId(dbBusiness.getId());
  1021. tbDeclareService.updateById(tbDeclare);
  1022. }
  1023. dbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getNetWeight())
  1024. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  1025. .setGoodsId(otherBusinessBO.getGoodsId()).setOwner(otherBusinessBO.getOwner()).setBusinessGoodsName(otherBusinessBO.getBusinessGoodsName())
  1026. .setBusinessGoodsNum(otherBusinessBO.getBusinessGoodsNum())
  1027. .setDeclareNo(declareNo).setCardNo(otherBusinessBO.getCardNo());
  1028. String chinaCar = cars.stream().filter(tbBusinessCar -> CAR_LIST.contains(StrUtil.sub(tbBusinessCar.getCarNo(), 0, 1)))
  1029. .map(TbBusinessCar::getCarNo).collect(Collectors.joining("、")).toUpperCase();
  1030. dbBusiness.setChinaCarNo(chinaCar);
  1031. this.updateById(dbBusiness);
  1032. if (dbBusiness.getConfirmInput() == 1) {
  1033. this.confirm(Collections.singletonList(dbBusiness.getId()));
  1034. }
  1035. }
  1036. public void bindOtherBusinessCar(String businessId, List<String> businessCarIdList) {
  1037. TbBusiness tbBusiness = this.getById(businessId);
  1038. List<TbBusinessCar> list = tbBusinessCarService.findOtherBusinessCar(businessId);
  1039. long count = list.stream().filter(tbBusinessCar -> tbBusinessCar.getConfirmJudge() == 1).count();
  1040. if (count > 0) {
  1041. throw new AjaxError("业务已审核,请先驳回审核");
  1042. }
  1043. list.stream().filter(tbBusinessCar -> !businessCarIdList.contains(tbBusinessCar.getId()) && tbBusinessCar.getRealInTime() == null)
  1044. .forEach(tbBusinessCar -> tbBusinessCarService.removeById(tbBusinessCar.getId()));
  1045. relationBusinessCarService.removeByBusinessId(businessId);
  1046. List<TbBusinessCar> carList = tbBusinessCarService.listByIds(businessCarIdList);
  1047. String chinaCarNo = carList.stream().filter(tbBusinessCar -> {
  1048. String carNo = tbBusinessCar.getCarNo();
  1049. String carStr = StrUtil.sub(carNo, 0, 1);
  1050. return CAR_LIST.contains(carStr);
  1051. }).map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  1052. String yueCarNo = carList.stream().filter(tbBusinessCar -> {
  1053. String carNo = tbBusinessCar.getCarNo();
  1054. String carStr = StrUtil.sub(carNo, 0, 1);
  1055. return !CAR_LIST.contains(carStr);
  1056. }).map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  1057. tbBusiness.setChinaCarNo(chinaCarNo);
  1058. tbBusiness.setCardNo(yueCarNo);
  1059. this.updateById(tbBusiness);
  1060. businessCarIdList.forEach(businessCarId -> {
  1061. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  1062. relationBusinessCar.setBusinessCarId(businessCarId).setBusinessId(businessId);
  1063. relationBusinessCarService.save(relationBusinessCar);
  1064. });
  1065. }
  1066. private void checkOtherBusiness(String itemTypeId, String operateTimeStr, String businessId, String carNo) {
  1067. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  1068. TbBusinessSort sort = tbBusinessSortService.findByItemTypeId(itemTypeId);
  1069. if (sort == null) {
  1070. return;
  1071. }
  1072. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sort.getGroupId());
  1073. LocalDateTime operateTime = LocalDateTime.parse(operateTimeStr, formatter);
  1074. int i = 0;
  1075. for (TbBusinessSort tbBusinessSort : sorts) {
  1076. if (i == 0 && StrUtil.equals(tbBusinessSort.getTypeId(), itemTypeId) && sorts.size() > 1) {
  1077. TbBusinessSort afterItem = sorts.get(i + 1);
  1078. String typeId = afterItem.getTypeId();
  1079. Integer interval = afterItem.getIntervalTime();
  1080. LocalDateTime endTime = operateTime.plusMinutes(interval);
  1081. if (afterItem.getIntervalTime() > 0) {
  1082. int count = this.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 1);
  1083. if (count > 0) {
  1084. throw new BusinessException("车辆:[" + carNo + "]" + tbBusinessSort.getTypeName() + "后" + afterItem.getIntervalTime() + "分钟才可以执行" + afterItem.getTypeName());
  1085. }
  1086. }
  1087. }
  1088. if (i > 0 && StrUtil.equals(tbBusinessSort.getTypeId(), itemTypeId)) {
  1089. TbBusinessSort beforeItem = sorts.get(i - 1);
  1090. String typeId = beforeItem.getTypeId();
  1091. Integer interval = tbBusinessSort.getIntervalTime();
  1092. LocalDateTime endTime = operateTime.minusMinutes(interval);
  1093. if (tbBusinessSort.getIntervalTime() > 0) {
  1094. int count = this.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 0);
  1095. if (count > 0) {
  1096. throw new BusinessException("车辆:[" + carNo + "]" + beforeItem.getTypeName() + "后" + tbBusinessSort.getIntervalTime() + "分钟才可以执行" + tbBusinessSort.getTypeName());
  1097. }
  1098. }
  1099. }
  1100. Integer singleIntervalTime = tbBusinessSort.getSingleIntervalTime();
  1101. String timeStart = operateTime.minusHours(singleIntervalTime).format(formatter);
  1102. String timeEnd = operateTime.plusHours(singleIntervalTime).format(formatter);
  1103. int count = this.checkCarBusinessType(itemTypeId, carNo, timeStart, timeEnd, businessId);
  1104. if (count > 0) {
  1105. throw new BusinessException("车辆:[" + carNo + "]" + singleIntervalTime + "小时内已有相同作业");
  1106. }
  1107. i++;
  1108. }
  1109. }
  1110. public TbBusiness findByDeclareNo(String declareNo) {
  1111. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  1112. ew.eq("declare_no", declareNo).orderByDesc("id");
  1113. List<TbBusiness> list = this.list(ew);
  1114. return list.isEmpty() ? null : list.get(0);
  1115. }
  1116. /**
  1117. * 选择异常单发送
  1118. *
  1119. * @param errorBusinessBO
  1120. * @param isAdmin 是否管理员
  1121. */
  1122. public void selectErrorBusiness(ErrorBusinessBO errorBusinessBO, boolean isAdmin) {
  1123. log.info("is admin :{}", isAdmin);
  1124. TbBusiness tbBusiness = getById(errorBusinessBO.getId());
  1125. if (tbBusiness == null) {
  1126. throw new AjaxError("业务已被删除");
  1127. }
  1128. String businessId = tbBusiness.getId();
  1129. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(businessId);
  1130. tbBusiness.setPayStatus(errorBusinessBO.getPayStatus())
  1131. .setSupplement(1)
  1132. .setSupplementBy(errorBusinessBO.getSupplementBy())
  1133. .setSupplementTime(new Date())
  1134. .setSupplementReason(errorBusinessBO.getSupplementReason());
  1135. if (PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode() == errorBusinessBO.getPayStatus()) {
  1136. tbBusiness.setPayMoney(tbBusiness.getItemPrice())
  1137. .setPayTime(errorBusinessBO.getPayTime());
  1138. items.forEach(item -> item.setPayTime(errorBusinessBO.getPayTime()).setPayStatus(1));
  1139. tbBusinessItemService.updateBatchById(items);
  1140. }
  1141. List<TbBusinessCar> tbBusinessCars = errorBusinessBO.getCars();
  1142. tbBusinessCars.forEach(this::handlerCarPay);
  1143. tbBusinessCarService.updateBatchById(tbBusinessCars);
  1144. if (isAdmin) {
  1145. //发起审批
  1146. sendOA(tbBusiness, items, tbBusinessCars, errorBusinessBO.getSupplementBy());
  1147. }
  1148. this.updateById(tbBusiness);
  1149. }
  1150. /**
  1151. * 处理车辆支付
  1152. *
  1153. * @param tbBusinessCar
  1154. */
  1155. private void handlerCarPay(TbBusinessCar tbBusinessCar) {
  1156. String carNo = tbBusinessCar.getCarNo();
  1157. String payType = tbBusinessCar.getPayType();
  1158. Date payTime = tbBusinessCar.getPayTime();
  1159. Date inTime = tbBusinessCar.getRealInTime();
  1160. if (inTime == null) {
  1161. throw new AjaxError(carNo + "入场时间不能为空");
  1162. }
  1163. Date outTime = tbBusinessCar.getRealOutTime();
  1164. if (outTime == null) {
  1165. throw new AjaxError(carNo + "离场时间不能为空");
  1166. }
  1167. if (payType.equals(CarEnum.PayTypeEnum.HAS_PAY_TYPE.getType())) {
  1168. if (payTime == null) {
  1169. throw new AjaxError(carNo + "支付时间不能为空");
  1170. }
  1171. outTime = payTime;
  1172. }
  1173. //计算停车费
  1174. BigDecimal parkMoney = this.calculationPartMoney(inTime, outTime, tbBusinessCar.getCarSize());
  1175. tbBusinessCar.setMoney(parkMoney);
  1176. }
  1177. /**
  1178. * 发起流程审批
  1179. *
  1180. * @param tbBusiness
  1181. * @param items
  1182. * @param tbBusinessCars
  1183. * @param sendBy
  1184. */
  1185. private void sendOA(TbBusiness tbBusiness, List<TbBusinessItem> items, List<TbBusinessCar> tbBusinessCars, String sendBy) {
  1186. if (!oaConfig.isEnable()) {
  1187. return;
  1188. }
  1189. ParamsBO paramsBO = buildParams(tbBusiness, items, tbBusinessCars);
  1190. String json = JSONUtil.toJsonStr(paramsBO);
  1191. log.info("构建流程表单数据:{}", json);
  1192. String resp = HttpUtil.createPost(oaConfig.getUrl())
  1193. .timeout(2000).setReadTimeout(3000).body(json)
  1194. .execute().body();
  1195. log.info("请求返回:{}", resp);
  1196. OAResultBO oaResultBO = JSONUtil.toBean(resp, OAResultBO.class);
  1197. if (oaResultBO.getSuccess() && oaResultBO.getCode() == 200) {
  1198. String fdId = oaResultBO.getData().getFdId();
  1199. if (StrUtil.isEmpty(fdId)) {
  1200. throw new BusinessException("流程发起失败");
  1201. }
  1202. tbBusiness.setSendOa(1).setOaResult(OAResultEnum.JUDGE_ING.getCode())
  1203. .setSendTime(new Date()).setSendBy(sendBy)
  1204. .setOaFdId(fdId);
  1205. this.updateById(tbBusiness);
  1206. } else {
  1207. throw new BusinessException(oaResultBO.getMessage());
  1208. }
  1209. }
  1210. /**
  1211. * 构建表单数据
  1212. *
  1213. * @param tbBusiness
  1214. * @param items
  1215. * @param tbBusinessCars
  1216. * @return
  1217. */
  1218. private ParamsBO buildParams(TbBusiness tbBusiness, List<TbBusinessItem> items, List<TbBusinessCar> tbBusinessCars) {
  1219. String operator = tbBusiness.getSupplementBy();
  1220. String reason = tbBusiness.getSupplementReason();
  1221. ParamsBO paramsBO = new ParamsBO();
  1222. paramsBO.setRequestId(System.currentTimeMillis() + "")
  1223. .setBusinessSystemCode(oaConfig.getSystemCode());
  1224. ParamsBO.DataBO dataBO = new ParamsBO.DataBO();
  1225. String project = tbBusiness.getGoodsName();
  1226. JSONObject docCreator = new JSONObject();
  1227. docCreator.set("PersonNo", oaConfig.getPersonNo());
  1228. dataBO.setOperationalProject(project)
  1229. .setOperationalPerson(operator).setOperationalReason(reason)
  1230. .setDocSubject(project + "补单流程").setFdTemplateId(oaConfig.getTemplateId()).setDocCreator(JSONUtil.toJsonStr(docCreator));
  1231. //基础信息
  1232. JSONObject formValues = buildFormValues(tbBusiness);
  1233. //车辆信息
  1234. buildCarParams(tbBusinessCars, formValues);
  1235. //业务项信息
  1236. buildItemPrams(items, formValues);
  1237. dataBO.setFormValues(formValues.toJSONString(0));
  1238. paramsBO.setData(dataBO);
  1239. return paramsBO;
  1240. }
  1241. /**
  1242. * 构建业务项
  1243. *
  1244. * @param items
  1245. * @return
  1246. */
  1247. private void buildItemPrams(List<TbBusinessItem> items, JSONObject formValues) {
  1248. List<String> itemTypeNameList = new ArrayList<>();
  1249. List<String> itemNameList = new ArrayList<>();
  1250. List<String> itemPriceList = new ArrayList<>();
  1251. List<String> unitList = new ArrayList<>();
  1252. List<String> numList = new ArrayList<>();
  1253. List<String> payStatusList = new ArrayList<>();
  1254. List<String> payTimeList = new ArrayList<>();
  1255. List<String> remarkList = new ArrayList<>();
  1256. double total = items.stream().collect(Collectors.summarizingDouble(item -> item.getItemPrice().doubleValue())).getSum();
  1257. items.forEach(item -> {
  1258. itemTypeNameList.add(item.getItemTypeName());
  1259. itemNameList.add(item.getItemName());
  1260. itemPriceList.add(item.getItemPrice().toString());
  1261. unitList.add(item.getUnit());
  1262. numList.add(item.getNum());
  1263. payStatusList.add(item.getPayStatus() == 1 ? "已支付" : "未支付");
  1264. payTimeList.add(item.getPayTime() == null ? "" : DateUtil.format(item.getPayTime(), "yyyy-MM-dd HH:mm:ss"));
  1265. remarkList.add(StrUtil.isEmpty(item.getRemark()) ? "-" : item.getRemark());
  1266. });
  1267. formValues
  1268. .set("specificBusinessItems.chargingItem", itemTypeNameList)
  1269. .set("specificBusinessItems.chargeDetail", itemNameList)
  1270. .set("specificBusinessItems.unitPrice", itemPriceList)
  1271. .set("specificBusinessItems.chargingStandard", unitList)
  1272. .set("specificBusinessItems.quantity", numList)
  1273. .set("specificBusinessItems.payoutStatus", payStatusList)
  1274. .set("specificBusinessItems.payTime", payTimeList)
  1275. .set("specificBusinessItems.total", Collections.singleton(total))
  1276. .set("specificBusinessItems.note", remarkList);
  1277. }
  1278. /**
  1279. * 构建基础信息
  1280. *
  1281. * @param tbBusiness
  1282. * @return
  1283. */
  1284. private JSONObject buildFormValues(TbBusiness tbBusiness) {
  1285. ParamsBO.DataBO.FormValuesBO formValuesBO = new ParamsBO.DataBO.FormValuesBO();
  1286. String project = tbBusiness.getGoodsName();
  1287. formValuesBO.setOperationalProject(project).setWorkingTime(tbBusiness.getOperateTime()).setCustomerName(tbBusiness.getCustomerName())
  1288. .setOperatingOutlay(tbBusiness.getItemPrice().toString()).setAmount(tbBusiness.getItemPrice())
  1289. .setPaymentTime(tbBusiness.getPayTime() == null ? "" : DateUtil.format(tbBusiness.getPayTime(), "yyyy-MM-dd HH:mm:ss")).setCargoOwner(tbBusiness.getOwner()).setOperationNo(tbBusiness.getNo())
  1290. .setRecordingTime(DateUtil.format(tbBusiness.getCreateTime(), "yyyy-MM-dd HH:mm:ss")).setRecorder(tbBusiness.getCreateBy())
  1291. .setIsPay(PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode() == tbBusiness.getPayStatus() ? "已支付" : "未支付");
  1292. return JSONUtil.parseObj(formValuesBO);
  1293. }
  1294. /**
  1295. * 构建车辆表单
  1296. *
  1297. * @param tbBusinessCars
  1298. * @return
  1299. */
  1300. private void buildCarParams(List<TbBusinessCar> tbBusinessCars, JSONObject jsonObject) {
  1301. List<String> carNoList = new ArrayList<>();
  1302. List<String> typeList = new ArrayList<>();
  1303. List<String> loadList = new ArrayList<>();
  1304. List<String> unitsList = new ArrayList<>();
  1305. List<String> payStatusList = new ArrayList<>();
  1306. List<String> carStatusList = new ArrayList<>();
  1307. List<String> feeList = new ArrayList<>();
  1308. List<String> inList = new ArrayList<>();
  1309. List<String> outList = new ArrayList<>();
  1310. tbBusinessCars.forEach(tbBusinessCar -> {
  1311. carNoList.add(tbBusinessCar.getCarNo());
  1312. typeList.add(tbBusinessCar.getCarType());
  1313. loadList.add(tbBusinessCar.getNetWeight());
  1314. Double carSize = tbBusinessCar.getCarSize();
  1315. unitsList.add(carSize == null ? "-" : carSize.toString());
  1316. payStatusList.add(tbBusinessCar.getPayType());
  1317. carStatusList.add(tbBusinessCar.getIsLock() == 1 ? "正常" : "锁定");
  1318. feeList.add(tbBusinessCar.getMoney() == null ? "-" : tbBusinessCar.getMoney().toString());
  1319. inList.add(tbBusinessCar.getRealInTime() == null ? "-" : DateUtil.format(tbBusinessCar.getRealInTime(), "yyyy-MM-dd HH:mm:ss"));
  1320. outList.add(tbBusinessCar.getRealOutTime() == null ? "-" : DateUtil.format(tbBusinessCar.getRealOutTime(), "yyyy-MM-dd HH:mm:ss"));
  1321. });
  1322. jsonObject
  1323. .set("vehicleInfo.licenseNo", carNoList)
  1324. .set("vehicleInfo.type", typeList)
  1325. .set("vehicleInfo.load", loadList)
  1326. .set("vehicleInfo.specifications", unitsList)
  1327. .set("vehicleInfo.paymentStatus", payStatusList)
  1328. .set("vehicleInfo.vehicleState", carStatusList)
  1329. .set("vehicleInfo.parkingFee", feeList)
  1330. .set("vehicleInfo.entryTime", inList)
  1331. .set("vehicleInfo.departureTime", outList);
  1332. }
  1333. //添加异常单
  1334. public void addErrorBusiness(ErrorBusinessBO errorBusinessBO, boolean isAdmin) {
  1335. List<TbItem> tbItems = errorBusinessBO.getItems();
  1336. if (tbItems.isEmpty()) {
  1337. throw new BusinessException("请选择明细");
  1338. }
  1339. List<TbBusinessCar> cars = errorBusinessBO.getCars();
  1340. if (cars.isEmpty()) {
  1341. throw new BusinessException("作业车辆不能为空");
  1342. }
  1343. Date now = new Date();
  1344. if (errorBusinessBO.getCreateTime() != null) {
  1345. now = errorBusinessBO.getCreateTime();
  1346. }
  1347. TbGoods tbGoods = tbGoodsService.getById(errorBusinessBO.getGoodsId());
  1348. TbBusiness tbBusiness = new TbBusiness();
  1349. String customerId = errorBusinessBO.getCustomerId();
  1350. tbBusiness.setCreateBy(StpUserUtil.getCreateBy())
  1351. .setPayStep(tbGoods.getPayStep());
  1352. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  1353. if (tbCostomer != null) {
  1354. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  1355. }
  1356. boolean businessIsPay = PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode() == errorBusinessBO.getPayStatus();
  1357. Date businessPayTime = errorBusinessBO.getPayTime();
  1358. BigDecimal price = new BigDecimal("0");
  1359. List<TbBusinessItem> itemList = new ArrayList<>();
  1360. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  1361. if (StrUtil.isNotEmpty(errorBusinessBO.getNo())) {
  1362. no = errorBusinessBO.getNo();
  1363. }
  1364. int index = 1;
  1365. //作业项目
  1366. for (TbItem tbItem : tbItems) {
  1367. TbItem db = tbItemService.getById(tbItem.getId());
  1368. TbBusinessItem tbBusinessItem = new TbBusinessItem();
  1369. int num = tbItem.getNum();
  1370. String typeId = tbItem.getTypeId();
  1371. TbItemType tbItemType = tbItemTypeService.getById(typeId);
  1372. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  1373. tbBusinessItem.setNo(no + "0" + index).setPayTypeName(db.getPayTypeName()).setPayType(db.getPayType())
  1374. .setBusinessType(db.getBusinessType()).setTaxRate(NumberUtil.div(db.getTaxRate().doubleValue(), 100D, 2))
  1375. .setItemCode(db.getItemCode()).setNum(num + "").setItemId(db.getId())
  1376. .setItemName(db.getItemName()).setItemPrice(db.getPrice()).setRemark(tbItem.getRemark())
  1377. .setItemTypeId(typeId).setItemTypeName(tbItemType.getName())
  1378. .setUnit(db.getUnit()).setTotal(itemTotalPrice).setCreateTime(now);
  1379. price = price.add(itemTotalPrice);
  1380. if (businessIsPay) {
  1381. tbBusinessItem.setPayTime(businessPayTime)
  1382. .setPayStatus(1);
  1383. }
  1384. itemList.add(tbBusinessItem);
  1385. index++;
  1386. }
  1387. String declareNo = errorBusinessBO.getDeclareNo();
  1388. tbBusiness.setCardSize(errorBusinessBO.getCarSize()).setNetWeight(errorBusinessBO.getNetWeight()).setTransactionId(errorBusinessBO.getTransactionId())
  1389. .setOperator(errorBusinessBO.getOperator()).setOperateTime(errorBusinessBO.getOperateTime())
  1390. .setNo(no).setGoodsName(tbGoods.getName()).setGoodsId(errorBusinessBO.getGoodsId())
  1391. .setSupplementReason(errorBusinessBO.getSupplementReason()).setSupplementTime(now)
  1392. .setSupplementBy(errorBusinessBO.getSupplementBy()).setSupplement(1).setPayStatus(errorBusinessBO.getPayStatus())
  1393. .setPayStep(tbGoods.getPayStep()).setDeclareNo(declareNo).setCardNo(errorBusinessBO.getCardNo());
  1394. tbBusiness.setCreateTime(now).setCreateByCustomerId(StpUserUtil.getCustomerId())
  1395. .setItemPrice(price).setTotalMoney(price).setOwner(errorBusinessBO.getOwner());
  1396. tbBusiness.setChinaCarNo(errorBusinessBO.getChinaCarNo());
  1397. if (businessIsPay) {//如果已支付
  1398. tbBusiness.setPayMoney(tbBusiness.getItemPrice())
  1399. .setPayTime(businessPayTime);
  1400. }
  1401. this.save(tbBusiness);
  1402. Integer chinaCarPay = tbGoods.getChinaCarPay();
  1403. Integer vietnamCarPay = tbGoods.getVietnamCarPay();
  1404. String carBuseinssNo = DateUtil.format(now, "yyyyMMddHHmm");
  1405. //业务车
  1406. for (TbBusinessCar car : cars) {
  1407. String carNo = car.getCarNo().trim().toUpperCase();
  1408. String carType = car.getCarType();
  1409. if (StrUtil.isEmpty(carType)) {
  1410. throw new BusinessException(carNo + "类型不能为空");
  1411. }
  1412. car.setCreateTime(now).setNo(carBuseinssNo + RandomUtil.randomNumbers(4));
  1413. if ((CarEnum.CarTypeEnum.EMPTY_TYPE.getType().equals(carType) && chinaCarPay == 0)
  1414. || (CarEnum.CarTypeEnum.WEIGHT_TYPE.getType().equals(carType) && vietnamCarPay == 0)) {
  1415. car.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  1416. }
  1417. TbCar tbCar = tbCarService.findByCardNo(carNo);
  1418. if (tbCar != null) {
  1419. car.setCarCompany(tbCar.getCustomerName());
  1420. if (!TbCar.CarTypeEnum.BUSINESS_CAR.getType().equals(tbCar.getCarType())) {
  1421. car.setPayType(CarEnum.PayTypeEnum.FEE_TYPE.getType());
  1422. }
  1423. }
  1424. car.setCarNo(carNo).setIsLock(0);
  1425. car.setCarSize(car.getCarSize())
  1426. .setTimeUpdate(now).setCarType(car.getCarType())
  1427. .setNetWeight(car.getNetWeight())
  1428. .setCustomerId(customerId)
  1429. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  1430. //新增放行记录
  1431. tbBusinessCarService.save(car);
  1432. RelationBusinessCar relationBusinessCar = new RelationBusinessCar();
  1433. relationBusinessCar.setBusinessId(tbBusiness.getId()).setBusinessCarId(car.getId());
  1434. relationBusinessCarService.save(relationBusinessCar);
  1435. }
  1436. if (StrUtil.isNotEmpty(declareNo)) {
  1437. TbDeclare declare = tbDeclareService.findByDeclareNo(declareNo);
  1438. declare.setBusinessId(tbBusiness.getId());
  1439. tbDeclareService.updateById(declare);
  1440. }
  1441. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  1442. tbBusinessItemService.saveBatch(itemList);
  1443. if (isAdmin) {
  1444. sendOA(tbBusiness, itemList, cars, errorBusinessBO.getSupplementBy());
  1445. }
  1446. }
  1447. /**
  1448. * 单独发起审批
  1449. *
  1450. * @param id
  1451. */
  1452. public void applyOA(String id, String sendBy) {
  1453. log.info("发起流程:{},{}", id, sendBy);
  1454. TbBusiness tbBusiness = this.getById(id);
  1455. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
  1456. List<TbBusinessCar> tbBusinessCars = tbBusinessCarService.findOtherBusinessCar(id);
  1457. sendOA(tbBusiness, items, tbBusinessCars, sendBy);
  1458. this.updateById(tbBusiness);
  1459. }
  1460. /**
  1461. * 编辑异常单
  1462. *
  1463. * @param errorBusinessBO
  1464. */
  1465. public void editErrorBusiness(ErrorBusinessBO errorBusinessBO) {
  1466. String id = errorBusinessBO.getId();
  1467. TbBusiness dbBusiness = this.getById(id);
  1468. if (dbBusiness == null) {
  1469. throw new BusinessException("记录不存在");
  1470. }
  1471. if (!(dbBusiness.getSendOa() == 0 || OAResultEnum.NO_PASS.getCode().equals(dbBusiness.getOaResult()))) {
  1472. throw new BusinessException("业务单正在审核,无法修改");
  1473. }
  1474. List<TbItem> tbItems = errorBusinessBO.getItems();
  1475. if (tbItems.isEmpty()) {
  1476. throw new BusinessException("请选择收费明细");
  1477. }
  1478. List<TbBusinessCar> cars = errorBusinessBO.getCars();
  1479. if (cars.isEmpty()) {
  1480. throw new BusinessException("作业车辆不能为空");
  1481. }
  1482. this.removeById(id);
  1483. tbBusinessItemService.removeByBusinessId(id);
  1484. tbBusinessCarService.removeByBusinessId(id);
  1485. relationBusinessCarService.removeByBusinessId(id);
  1486. errorBusinessBO.setNo(dbBusiness.getNo()).setCreateTime(dbBusiness.getCreateTime());
  1487. this.addErrorBusiness(errorBusinessBO, false);
  1488. }
  1489. /**
  1490. * oa审批回调
  1491. *
  1492. * @param callbackBO
  1493. */
  1494. public void approveCallback(CallbackBO callbackBO) {
  1495. String fdId = callbackBO.getFdId();
  1496. TbBusiness tbBusiness = this.findByFdId(fdId);
  1497. if (tbBusiness == null) {
  1498. throw new AjaxError("流程不存在");
  1499. }
  1500. String result = callbackBO.getAuditResult();
  1501. tbBusiness.setOaResult(callbackBO.getAuditResult()).setOaContent(callbackBO.getRefuseReason())
  1502. .setOaJudgeTime(new Date());
  1503. this.updateById(tbBusiness);
  1504. String businessId = tbBusiness.getId();
  1505. if (StrUtil.contains(result, "通过") && pushfeeConfig.isEnable()) {
  1506. //todo 审批通过-==>已支付的生成明细===>推送订单到计费系统
  1507. List<TbBusinessCar> cars = tbBusinessCarService.findOtherBusinessCar(businessId);
  1508. String transactionId = tbBusiness.getTransactionId();
  1509. Date payTime = tbBusiness.getPayTime();
  1510. String outTradeNo = "of" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + RandomUtil.randomNumbers(4);
  1511. //已经支付了的
  1512. List<PriceBO> priceBOList = cars.stream().filter(tbBusinessCar -> tbBusinessCar.getPayTime() != null).
  1513. map(tbBusinessCar -> {
  1514. PriceBO priceBO = new PriceBO();
  1515. priceBO.setId(tbBusinessCar.getId()).setP(tbBusinessCar.getMoney());
  1516. return priceBO;
  1517. }).
  1518. collect(Collectors.toList());
  1519. if (!priceBOList.isEmpty()) {
  1520. tbFeeDetailsService.chargeParkFee(priceBOList, transactionId, outTradeNo, payTime, PayEnum.PayType.OFF_LINE_PAY);//添加cars的收费明细
  1521. }
  1522. if (tbBusiness.getPayStatus() == PayEnum.PayStatusEnum.HAS_PAY_CONFIRM.getCode()) {
  1523. List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(businessId);
  1524. tbFeeDetailsService.chargeBusinessFee(items, transactionId, outTradeNo, payTime, PayEnum.PayType.OFF_LINE_PAY);//添加items的收费明细
  1525. }
  1526. }
  1527. }
  1528. /**
  1529. * 根据流程ID查询
  1530. *
  1531. * @param fdId
  1532. * @return
  1533. */
  1534. private TbBusiness findByFdId(String fdId) {
  1535. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  1536. ew.lambda().eq(TbBusiness::getOaFdId, fdId);
  1537. return getOne(ew);
  1538. }
  1539. public TbBusiness findByNo(String no) {
  1540. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  1541. ew.eq("no", no);
  1542. return this.getOne(ew);
  1543. }
  1544. }