TbBusinessService.java 95 KB

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