ApiService.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. package com.pj.api.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.io.unit.DataUnit;
  5. import cn.hutool.core.util.NumberUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import cn.hutool.json.JSONUtil;
  9. import com.pj.api.bo.InOutRecordBO;
  10. import com.pj.api.bo.OtherBusinessBO;
  11. import com.pj.api.wx.bo.MsgDataBO;
  12. import com.pj.api.wx.service.WxService;
  13. import com.pj.constants.UserTypeEnum;
  14. import com.pj.current.config.MyConfig;
  15. import com.pj.current.config.PartConfig;
  16. import com.pj.current.config.WxConfig;
  17. import com.pj.current.satoken.StpUserUtil;
  18. import com.pj.project.relation_type_item.RelationTypeItem;
  19. import com.pj.project.relation_type_item.RelationTypeItemService;
  20. import com.pj.project.tb_account.TbAccount;
  21. import com.pj.project.tb_account.TbAccountService;
  22. import com.pj.project.tb_business.CarDisincle;
  23. import com.pj.project.tb_business.TbBusiness;
  24. import com.pj.project.tb_business.TbBusinessService;
  25. import com.pj.project.tb_business_car.TbBusinessCar;
  26. import com.pj.project.tb_business_car.TbBusinessCarService;
  27. import com.pj.project.tb_business_item.TbBusinessItem;
  28. import com.pj.project.tb_business_item.TbBusinessItemService;
  29. import com.pj.project.tb_business_sort.TbBusinessSort;
  30. import com.pj.project.tb_business_sort.TbBusinessSortService;
  31. import com.pj.project.tb_car.TbCar;
  32. import com.pj.project.tb_car.TbCarService;
  33. import com.pj.project.tb_costomer.TbCostomer;
  34. import com.pj.project.tb_costomer.TbCostomerService;
  35. import com.pj.project.tb_declare.TbDeclare;
  36. import com.pj.project.tb_declare.TbDeclareService;
  37. import com.pj.project.tb_discount.TbDiscount;
  38. import com.pj.project.tb_discount.TbDiscountService;
  39. import com.pj.project.tb_disinfect.TbDisinfect;
  40. import com.pj.project.tb_disinfect.TbDisinfectService;
  41. import com.pj.project.tb_district.TbDistrict;
  42. import com.pj.project.tb_district.TbDistrictService;
  43. import com.pj.project.tb_item.TbItem;
  44. import com.pj.project.tb_item_type.TbItemType;
  45. import com.pj.project.tb_item_type.TbItemTypeService;
  46. import com.pj.project.tb_pass_record.TbPassRecord;
  47. import com.pj.project.tb_pass_record.TbPassRecordService;
  48. import com.pj.project4sp.admin.SpAdmin;
  49. import com.pj.project4sp.admin.SpAdminService;
  50. import com.pj.project4sp.global.BusinessException;
  51. import com.pj.utils.cache.RedisUtil;
  52. import com.pj.utils.sg.AjaxJson;
  53. import com.pj.utils.so.SoMap;
  54. import lombok.extern.slf4j.Slf4j;
  55. import org.aspectj.weaver.ast.Or;
  56. import org.springframework.context.annotation.Lazy;
  57. import org.springframework.stereotype.Service;
  58. import org.springframework.transaction.annotation.Transactional;
  59. import javax.annotation.Resource;
  60. import java.math.BigDecimal;
  61. import java.time.LocalDate;
  62. import java.time.LocalDateTime;
  63. import java.time.format.DateTimeFormatter;
  64. import java.util.*;
  65. import java.util.stream.Collectors;
  66. /**
  67. *
  68. */
  69. @Service
  70. @Transactional(rollbackFor = Exception.class)
  71. @Slf4j
  72. public class ApiService {
  73. @Resource
  74. TbCostomerService tbCostomerService;
  75. @Resource
  76. @Lazy
  77. TbBusinessService tbBusinessService;
  78. @Resource
  79. TbPassRecordService tbPassRecordService;
  80. @Resource
  81. @Lazy
  82. TbBusinessCarService tbBusinessCarService;
  83. @Resource
  84. private TbDeclareService tbDeclareService;
  85. @Resource
  86. @Lazy
  87. private TbDisinfectService tbDisinfectService;
  88. @Resource
  89. private TbAccountService accountService;
  90. @Resource
  91. private TbDiscountService tbDiscountService;
  92. @Resource
  93. private TbBusinessItemService tbBusinessItemService;
  94. @Resource
  95. private SpAdminService spAdminService;
  96. @Resource
  97. private WxService wxService;
  98. @Resource
  99. private WxConfig wxConfig;
  100. @Resource
  101. private MyConfig myConfig;
  102. @Resource
  103. private TbCarService tbCarService;
  104. @Resource
  105. private RelationTypeItemService relationTypeItemService;
  106. @Resource
  107. private TbItemTypeService tbItemTypeService;
  108. @Resource
  109. private TbBusinessSortService tbBusinessSortService;
  110. private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");
  111. public List<InOutRecordBO> getInOutRecord(SoMap so) {
  112. List<TbPassRecord> passRecords = tbPassRecordService.getList(so);
  113. List<InOutRecordBO> recordList = new ArrayList<>();
  114. for (TbPassRecord passRecord : passRecords) {
  115. InOutRecordBO record = new InOutRecordBO();
  116. BeanUtil.copyProperties(passRecord, record);
  117. TbCostomer costomer = tbCostomerService.getById(passRecord.getCustomerId());
  118. record.setCustomerContact(costomer.getPhone()).setDutyPeople(costomer.getDutyPeople());
  119. recordList.add(record);
  120. }
  121. return recordList;
  122. }
  123. public List<TbCostomer> getCustomerList(SoMap so) {
  124. List<TbCostomer> list = tbCostomerService.getList(so);
  125. return list;
  126. }
  127. public void confirmCustomer(Long customerId, String judgeContent) {
  128. TbCostomer costomer = tbCostomerService.getById(customerId);
  129. if (costomer != null && costomer.getJudgeStatus() == 1) {
  130. tbCostomerService.judge(customerId + "", 2, judgeContent);
  131. }
  132. }
  133. public Set<TbBusinessCar> searchPartCar(String carNo) {
  134. if (StrUtil.isEmpty(carNo) || StrUtil.length(carNo) < 3) {
  135. return new HashSet<>();
  136. }
  137. List<TbBusinessCar> list = tbBusinessCarService.searchPartCar(carNo);
  138. list = list.stream().filter(tbBusinessCar -> {
  139. String businessId = tbBusinessCar.getBusinessId();
  140. if (StrUtil.isEmpty(businessId)) {
  141. return false;
  142. }
  143. TbBusiness tbBusiness = tbBusinessService.getById(businessId);
  144. return tbBusiness.getAdminConfirmInput() == 1;
  145. }).collect(Collectors.toList());
  146. List<TbBusinessCar> otherBusinessCarList = tbBusinessCarService.searchOtherBusinessCar(carNo);
  147. otherBusinessCarList = otherBusinessCarList.stream().filter(tbBusinessCar -> {
  148. String businessCarId = tbBusinessCar.getId();
  149. List<TbBusiness> businesses = tbBusinessService.findOtherBusinessByCarId(businessCarId);
  150. return businesses.stream().anyMatch(tbBusiness -> tbBusiness.getPayMoney().doubleValue() < tbBusiness.getTotalMoney().doubleValue()
  151. && (((TbItemType.PayStep.BEFORE_CONFIRM.getCode().equals(tbBusiness.getPayStep()) && tbBusiness.getAdminConfirmInput() >= 0)
  152. || (TbItemType.PayStep.AFTER_CONFIRM.getCode().equals(tbBusiness.getPayStep()) && tbBusiness.getAdminConfirmInput() == 1))
  153. ));
  154. }).collect(Collectors.toList());
  155. List<TbBusinessCar> cars = tbBusinessCarService.findTheNoBusinessCar(carNo);
  156. //过滤掉不用缴费的车
  157. list.addAll(cars);
  158. list.addAll(otherBusinessCarList);
  159. return new HashSet<>(list);
  160. }
  161. public void confirm(List<String> ids) {
  162. tbBusinessService.confirm(ids);
  163. }
  164. public Map<String, Object> getBusinessMoney(String carId, String state) {
  165. return tbBusinessService.getBusinessMoney(carId, state);
  166. }
  167. public Map<String, Object> getPartCarByChannel(String channel) {
  168. Map<String, Object> result = new HashMap<>();
  169. String carNo = RedisUtil.get(channel);
  170. if (StrUtil.isEmpty(carNo)) {
  171. return result;
  172. }
  173. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  174. if (tbBusinessCar == null) {
  175. return result;
  176. }
  177. Date now = new Date();
  178. Date startTime = tbBusinessCar.getRealInTime();
  179. if (tbBusinessCar.getPayTime() != null) {
  180. startTime = tbBusinessCar.getPayTime();
  181. }
  182. BigDecimal price = tbBusinessService.calculationPartMoney(startTime, now);
  183. Map<String, Object> car = new HashMap<>();
  184. car.put("id", tbBusinessCar.getId());
  185. car.put("carNo", tbBusinessCar.getCarNo());
  186. car.put("price", price);
  187. result.put("total", price);
  188. result.put("cars", Collections.singleton(car));
  189. return result;
  190. }
  191. public void addDeclare(TbDeclare tbDeclare) {
  192. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  193. String currentCustomerId = StpUserUtil.getCustomerId();
  194. tbDeclare.setCustomerId(currentCustomerId);
  195. }
  196. TbCostomer tbCostomer = tbCostomerService.getById(tbDeclare.getCustomerId());
  197. tbDeclare.setCustomerName(tbCostomer.getName());
  198. String chinaCarNo = tbDeclare.getChinaCarNo();
  199. if (StrUtil.isNotEmpty(chinaCarNo)) {
  200. chinaCarNo = chinaCarNo.toUpperCase();
  201. tbDeclare.setChinaCarNo(chinaCarNo);
  202. }
  203. String carNo = tbDeclare.getCarNo();
  204. if (StrUtil.isNotEmpty(carNo)) {
  205. carNo = carNo.toUpperCase();
  206. tbDeclare.setCarNo(carNo);
  207. }
  208. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  209. tbDeclare.setCreateTime(new Date()).setDeclareNo(nowStr + RandomUtil.randomNumbers(6));
  210. tbDeclareService.save(tbDeclare);
  211. }
  212. public void addDisinfect(TbDisinfect tbDisinfect) {
  213. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  214. String currentCustomerId = StpUserUtil.getCustomerId();
  215. tbDisinfect.setCustomerId(currentCustomerId);
  216. }
  217. TbCostomer tbCostomer = tbCostomerService.getById(tbDisinfect.getCustomerId());
  218. tbDisinfect.setCustomerName(tbCostomer.getName()).setApplyUnit(tbCostomer.getName());
  219. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  220. tbDisinfect.setCode(nowStr + RandomUtil.randomNumbers(6)).setApplyTime(DateUtil.now());
  221. tbDisinfectService.save(tbDisinfect);
  222. }
  223. public List<TbDeclare> getDeclareList(SoMap so) {
  224. return tbDeclareService.getList(so);
  225. }
  226. public TbAccount getAccountInfo(String customerId) {
  227. return accountService.getByCustomerId(customerId);
  228. }
  229. public List<TbDiscount> getDiscountInfo(SoMap so) {
  230. return tbDiscountService.getList(so);
  231. }
  232. public TbBusinessItem getBusinessItem(String id) {
  233. TbBusinessItem item = tbBusinessItemService.getById(id);
  234. TbBusiness business = tbBusinessService.getById(item.getBusinessId());
  235. item.setCardNo(business.getCardNo()).setChinaCarNo(business.getChinaCarNo()).setGoodsName(business.getGoodsName());
  236. return item;
  237. }
  238. public AjaxJson pickBusinessItem(String openid, long id) {
  239. TbBusinessItem tbBusinessItem = tbBusinessItemService.getById(id);
  240. if (tbBusinessItem == null) {
  241. return AjaxJson.getError("业务项目或已被删除");
  242. }
  243. SpAdmin spAdmin = spAdminService.findByOpenid(openid);
  244. if (spAdmin == null) {
  245. return AjaxJson.getError("用户已被删除或解绑");
  246. }
  247. if (tbBusinessItem.getPick() == 1) {
  248. return AjaxJson.getError("您慢了一步,其他用户已接单");
  249. }
  250. String customerId = spAdmin.getCustomerId();
  251. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  252. tbBusinessItem.setPick(1).setPickTime(new Date()).setPickCustomerId(tbCostomer.getId()).setPickCustomerName(tbCostomer.getName());
  253. tbBusinessItemService.updateById(tbBusinessItem);
  254. //todo 通知录入人员 已接单
  255. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(UserTypeEnum.PLATFORM_ADMIN.getCustomerId());
  256. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  257. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + tbBusinessItem.getNo(), tbCostomer.getName(), DateUtil.now(), tbBusiness.getGoodsName() + "(" + tbBusinessItem.getItemTypeName() + tbBusinessItem.getItemName() + ")");
  258. spAdminList.stream().filter(admin -> StrUtil.isNotEmpty(admin.getOpenid())).forEach(admin -> {
  259. String detailUrl = myConfig.getWebDomain() + "/pages/business-order/business-item?id=" + tbBusiness.getId() + "&itemId=" + tbBusinessItem.getId() + "&openid=" + openid;
  260. log.info("admin confirm business:[{}]", detailUrl);
  261. wxService.sendTemplateMsg(wxConfig.getBusinessPickTemplate(), admin.getOpenid(), msgDataBO, detailUrl);
  262. });
  263. return AjaxJson.getSuccess();
  264. }
  265. public void confirmBusinessItem(long id) {
  266. tbBusinessItemService.confirmBusinessItem(id);
  267. }
  268. public List<TbBusinessItem> getPartnerBusinessItem(SoMap startPage) {
  269. List<TbBusinessItem> list = tbBusinessItemService.getList(startPage);
  270. list.parallelStream().forEach(tbBusinessItem -> {
  271. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  272. if (TbBusiness.BusinessType.CAR_DISINCLE.getCode().equals(tbBusiness.getBusinessType())) {
  273. TbBusinessCar tbBusinessCar = tbBusinessCarService.getById(tbBusiness.getBusinessCarId());
  274. tbBusinessItem.setCardNo(tbBusinessCar.getCarNo());
  275. } else {
  276. List<TbBusinessCar> cars = tbBusinessCarService.findByBusinessId(tbBusiness.getId());
  277. String carStr = cars.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、"));
  278. tbBusinessItem.setCardNo(carStr);
  279. }
  280. });
  281. return list;
  282. }
  283. public void addCarDisinfect(OtherBusinessBO otherBusinessBO) {
  284. List<TbItem> tbItems = otherBusinessBO.getItems();
  285. if (tbItems.isEmpty()) {
  286. throw new BusinessException("请选择明细");
  287. }
  288. String carNo = otherBusinessBO.getCarNo().trim().toUpperCase();
  289. checkOtherBusiness(otherBusinessBO, tbItems, carNo);
  290. TbBusinessCar db = tbBusinessCarService.findNotOutCar(carNo);
  291. String customerId = otherBusinessBO.getCustomerId();
  292. TbItemType tbItemType = tbItemTypeService.getById(otherBusinessBO.getItemTypeId());
  293. TbCar tbCar = tbCarService.findByCardNo(carNo);
  294. if (db == null) {
  295. db = tbBusinessCarService.findNotInCar(carNo);
  296. if (db == null) {
  297. db = new TbBusinessCar();
  298. }
  299. } else {
  300. db.setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode());
  301. tbBusinessCarService.updateById(db);
  302. }
  303. db.setPay(0).setCarNo(carNo).setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  304. .setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  305. .setIsLock(0).setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时");
  306. db.setCarSize(otherBusinessBO.getCarSize()).setTimeUpdate(new Date()).setCustomerId(customerId)
  307. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  308. tbBusinessCarService.saveOrUpdate(db);
  309. TbBusiness tbBusiness = new TbBusiness();
  310. SpAdmin spAdmin= StpUserUtil.getAdmin();
  311. String createName=StrUtil.isEmpty(spAdmin.getNickname())?spAdmin.getName():spAdmin.getNickname();
  312. tbBusiness.setCreateBy(createName);
  313. tbBusiness.setBusinessCarId(db.getId()).setPayStep(tbItemType.getPayStep());
  314. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  315. if (tbCostomer != null) {
  316. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  317. }
  318. BigDecimal price = new BigDecimal("0");
  319. List<TbBusinessItem> itemList = new ArrayList<>();
  320. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  321. int index = 1;
  322. for (TbItem tbItem : tbItems) {
  323. TbBusinessItem item = new TbBusinessItem();
  324. int num = tbItem.getNum();
  325. RelationTypeItem relationTypeItem = relationTypeItemService.findByTypeIdAndItemId(otherBusinessBO.getItemTypeId(), tbItem.getId());
  326. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  327. item.setNo(no + "0" + index).setPayTypeName(tbItem.getPayTypeName()).setPayType(tbItem.getPayType())
  328. .setBusinessType(tbItem.getBusinessType()).setTaxRate(NumberUtil.div(tbItem.getTaxRate().doubleValue(), 100D, 2));
  329. item.setItemCode(tbItem.getItemCode()).setNum(num + "").setItemId(tbItem.getId()).setItemCode(tbItem.getItemCode())
  330. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice()).setRemark(tbItem.getRemark())
  331. .setItemTypeId(otherBusinessBO.getItemTypeId()).setItemTypeName(relationTypeItem.getTypeName())
  332. .setUnit(tbItem.getUnit()).setTotal(itemTotalPrice).setCreateTime(new Date());
  333. price = price.add(itemTotalPrice);
  334. itemList.add(item);
  335. index++;
  336. }
  337. tbBusiness.setCardSize(otherBusinessBO.getCarSize()).setNetWeight(otherBusinessBO.getCarWeight())
  338. .setOperator(otherBusinessBO.getOperator()).setOperateTime(otherBusinessBO.getOperateTime())
  339. .setNo(no).setGoodsName(otherBusinessBO.getCarType()).setItemTypeId(otherBusinessBO.getItemTypeId())
  340. .setCreateTime(new Date()).setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  341. .setItemTypeName(tbItemType.getName())
  342. .setItemPrice(price).setTotalMoney(price);
  343. String prefix = carNo.substring(0, 1);
  344. if (CAR_LIST.contains(prefix)) {
  345. tbBusiness.setChinaCarNo(carNo);
  346. } else {
  347. tbBusiness.setCardNo(carNo);
  348. }
  349. tbBusinessService.save(tbBusiness);
  350. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  351. tbBusinessItemService.saveBatch(itemList);
  352. }
  353. @Deprecated
  354. public void editCarDisinfect(CarDisincle carDisincle, String itemJson, String type) {
  355. String carId = carDisincle.getBusinessCarId();
  356. TbBusinessCar businessCar = tbBusinessCarService.getById(carId);
  357. if (businessCar == null) {
  358. throw new BusinessException("数据不存在");
  359. }
  360. List<TbItem> tbItems = JSONUtil.toList(itemJson, TbItem.class);
  361. }
  362. public void editOtherBusiness(OtherBusinessBO otherBusinessBO) {
  363. List<TbItem> tbItems = otherBusinessBO.getItems();
  364. if (tbItems.isEmpty()) {
  365. throw new BusinessException("请选择明细");
  366. }
  367. TbItemType tbItemType = tbItemTypeService.getById(otherBusinessBO.getItemTypeId());
  368. String businessId = otherBusinessBO.getId();
  369. tbBusinessItemService.removeByBusinessId(businessId);
  370. String carNo = otherBusinessBO.getCarNo();
  371. String customerId = otherBusinessBO.getCustomerId();
  372. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  373. TbBusiness tbBusiness = tbBusinessService.getById(businessId);
  374. checkOtherBusiness(otherBusinessBO, tbItems, carNo);
  375. tbBusiness.setOperateTime(otherBusinessBO.getOperateTime())
  376. .setOperator(otherBusinessBO.getOperator())
  377. .setPayStep(tbItemType.getPayStep()).setItemTypeName(tbItemType.getName())
  378. .setCardSize(otherBusinessBO.getCarSize()).setItemTypeId(tbItemType.getId())
  379. .setCustomerId(customerId).setNetWeight(otherBusinessBO.getCarWeight())
  380. .setGoodsName(otherBusinessBO.getCarType())
  381. .setCustomerName(tbCostomer.getName());
  382. String prefix = carNo.substring(0, 1);
  383. if (CAR_LIST.contains(prefix)) {
  384. tbBusiness.setChinaCarNo(carNo);
  385. } else {
  386. tbBusiness.setCardNo(carNo);
  387. }
  388. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  389. TbBusinessCar tbBusinessCar = tbBusinessCarService.getById(tbBusiness.getBusinessCarId());
  390. TbBusinessCar otherCar = tbBusinessCarService.searchOtherBusinessCarNotOut(carNo);
  391. if (otherCar != null) {
  392. List<TbBusiness> checkList = tbBusinessService.findOtherBusinessByCarId(tbBusiness.getBusinessCarId());
  393. if (checkList.size() == 1) {
  394. tbBusinessCarService.removeById(tbBusiness.getBusinessCarId());
  395. }
  396. tbBusiness.setBusinessCarId(otherCar.getId());
  397. } else {
  398. tbBusinessCar.setCarSize(otherBusinessBO.getCarSize()).setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  399. .setCustomerId(otherBusinessBO.getCustomerId()).setCarNo(carNo)
  400. .setTimeUpdate(new Date());
  401. tbBusinessCarService.updateById(tbBusinessCar);
  402. }
  403. int index = 1;
  404. BigDecimal price = new BigDecimal("0");
  405. List<TbBusinessItem> itemList = new ArrayList<>();
  406. for (TbItem tbItem : tbItems) {
  407. TbBusinessItem item = new TbBusinessItem();
  408. int num = tbItem.getNum();
  409. RelationTypeItem relationTypeItem = relationTypeItemService.findByTypeIdAndItemId(otherBusinessBO.getItemTypeId(), tbItem.getId());
  410. BigDecimal itemTotalPrice = tbItem.getPrice().multiply(new BigDecimal(num));
  411. item.setNo(no + "0" + index).setPayTypeName(tbItem.getPayTypeName())
  412. .setPayType(tbItem.getPayType())
  413. .setBusinessType(tbItem.getBusinessType())
  414. .setTaxRate(NumberUtil.div(tbItem.getTaxRate().doubleValue(), 100D, 2))
  415. .setItemCode(tbItem.getItemCode()).setNum(num + "")
  416. .setItemId(tbItem.getId()).setItemCode(tbItem.getItemCode())
  417. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  418. .setRemark(tbItem.getRemark())
  419. .setItemTypeId(otherBusinessBO.getItemTypeId())
  420. .setItemTypeName(relationTypeItem.getTypeName()).setBusinessId(otherBusinessBO.getId())
  421. .setUnit(tbItem.getUnit()).setTotal(itemTotalPrice).setCreateTime(new Date());
  422. price = price.add(itemTotalPrice);
  423. itemList.add(item);
  424. index++;
  425. }
  426. tbBusiness.setItemPrice(price);
  427. tbBusinessItemService.saveBatch(itemList);
  428. tbBusinessService.updateById(tbBusiness);
  429. }
  430. private void checkOtherBusiness(OtherBusinessBO otherBusinessBO, List<TbItem> tbItems, String carNo) {
  431. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  432. String itemTypeId = otherBusinessBO.getItemTypeId();
  433. TbBusinessSort sort = tbBusinessSortService.findByItemTypeId(itemTypeId);
  434. List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(sort.getGroupId());
  435. LocalDateTime operateTime = LocalDateTime.parse(otherBusinessBO.getOperateTime(), formatter);
  436. int i = 0;
  437. for (TbBusinessSort tbBusinessSort : sorts) {
  438. if (i == 0 && StrUtil.equals(tbBusinessSort.getTypeId(), otherBusinessBO.getItemTypeId()) && sorts.size() > 1) {
  439. TbBusinessSort afterItem = sorts.get(i + 1);
  440. String typeId = afterItem.getTypeId();
  441. Integer interval = afterItem.getIntervalTime();
  442. LocalDateTime endTime = operateTime.plusMinutes(interval);
  443. if (afterItem.getIntervalTime() > 0) {
  444. int count = tbBusinessService.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 1);
  445. if (count > 0) {
  446. throw new BusinessException("车辆:[" + carNo + "]" + tbBusinessSort.getTypeName() + "后" + afterItem.getIntervalTime() + "分钟才可以执行" + afterItem.getTypeName());
  447. }
  448. }
  449. }
  450. if (i > 0 && StrUtil.equals(tbBusinessSort.getTypeId(), otherBusinessBO.getItemTypeId())) {
  451. TbBusinessSort beforeItem = sorts.get(i - 1);
  452. String typeId = beforeItem.getTypeId();
  453. Integer interval = tbBusinessSort.getIntervalTime();
  454. LocalDateTime endTime = operateTime.minusMinutes(interval);
  455. if (tbBusinessSort.getIntervalTime() > 0) {
  456. int count = tbBusinessService.checkCarBusinessType(typeId, carNo, endTime.format(formatter), 0);
  457. if (count > 0) {
  458. throw new BusinessException("车辆:[" + carNo + "]" + beforeItem.getTypeName() + "后" + tbBusinessSort.getIntervalTime() + "分钟才可以执行" + tbBusinessSort.getTypeName());
  459. }
  460. }
  461. }
  462. String typeId = otherBusinessBO.getItemTypeId();
  463. Integer singleIntervalTime = tbBusinessSort.getSingleIntervalTime();
  464. String timeStart = operateTime.minusHours(singleIntervalTime).format(formatter);
  465. String businessId = otherBusinessBO.getId();
  466. String timeEnd = operateTime.plusHours(singleIntervalTime).format(formatter);
  467. int count = tbBusinessService.checkCarBusinessType(typeId, carNo, timeStart, timeEnd, businessId);
  468. if (count > 0) {
  469. throw new BusinessException("车辆:[" + carNo + "]" + singleIntervalTime + "小时内已有相同作业");
  470. }
  471. i++;
  472. }
  473. }
  474. }