TbBusinessService.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.pj.project.tb_business;
  2. import java.math.BigDecimal;
  3. import java.time.LocalDateTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.Date;
  6. import java.util.List;
  7. import cn.hutool.core.util.NumberUtil;
  8. import cn.hutool.core.util.RandomUtil;
  9. import cn.hutool.core.util.StrUtil;
  10. import cn.hutool.json.JSONUtil;
  11. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  12. import com.baomidou.mybatisplus.extension.service.IService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.pj.constants.UserTypeEnum;
  15. import com.pj.current.satoken.StpUserUtil;
  16. import com.pj.project.tb_business_item.TbBusinessItem;
  17. import com.pj.project.tb_business_item.TbBusinessItemService;
  18. import com.pj.project.tb_car.TbCar;
  19. import com.pj.project.tb_car.TbCarService;
  20. import com.pj.project.tb_costomer.TbCostomer;
  21. import com.pj.project.tb_costomer.TbCostomerService;
  22. import com.pj.project.tb_driver.TbDriver;
  23. import com.pj.project.tb_driver.TbDriverService;
  24. import com.pj.project.tb_item.TbItem;
  25. import com.pj.project.tb_item.TbItemService;
  26. import com.pj.project.tb_item_type.TbItemType;
  27. import com.pj.project.tb_item_type.TbItemTypeService;
  28. import com.pj.project4sp.admin.SpAdmin;
  29. import com.pj.project4sp.role.SpRoleUtil;
  30. import com.pj.project4sp.role4permission.SpRolePermissionService;
  31. import com.pj.utils.so.SoMap;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import com.pj.utils.sg.*;
  35. import javax.annotation.Resource;
  36. /**
  37. * Service: tb_business -- 入境登记
  38. *
  39. * @author qzy
  40. */
  41. @Service
  42. public class TbBusinessService extends ServiceImpl<TbBusinessMapper, TbBusiness> implements IService<TbBusiness> {
  43. /**
  44. * 底层 Mapper 对象
  45. */
  46. @Autowired
  47. TbBusinessMapper tbBusinessMapper;
  48. @Resource
  49. private TbCarService tbCarService;
  50. @Resource
  51. private TbDriverService tbDriverService;
  52. @Resource
  53. private TbCostomerService tbCostomerService;
  54. @Resource
  55. private TbItemService tbItemService;
  56. @Resource
  57. private TbItemTypeService tbItemTypeService;
  58. @Resource
  59. private TbBusinessItemService tbBusinessItemService;
  60. @Autowired
  61. SpRolePermissionService spRolePermissionService;
  62. /**
  63. * 增
  64. */
  65. public void addOrUpdate(TbBusiness t) {
  66. t.setCreateBy(StpUserUtil.getAdmin().getName());
  67. createCar(t.getCardNo(), t.getCardSize(), t.getGoodsName());
  68. TbDriver tbDriver = createDriver(t.getDriverIdCard(), t.getDriverName(), t.getDriverPhone());
  69. t.setDriverId(tbDriver.getId());
  70. String customerId = t.getCustomerId();
  71. if (!StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  72. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  73. t.setCustomerName(tbCostomer.getName());
  74. } else {
  75. t.setConfirmInputBy(StpUserUtil.getAdmin().getName()).setConfirmInput(1).setConfirmInputTime(new Date());
  76. }
  77. if (StrUtil.isEmpty(t.getNo())) {
  78. t.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  79. }
  80. List<TbBusinessItem> items = JSONUtil.toList(t.getItemJson(), TbBusinessItem.class);
  81. if (StrUtil.isEmpty(t.getId())) {
  82. t.setCreateTime(new Date());
  83. }
  84. saveOrUpdate(t);
  85. tbBusinessItemService.removeByBusinessId(t.getId());
  86. BigDecimal price = new BigDecimal(0);
  87. for (TbBusinessItem item : items) {
  88. Integer num = item.getNum();
  89. TbItem tbItem = tbItemService.getById(item.getItemId());
  90. price = price.add(NumberUtil.mul(num, tbItem.getPrice()));
  91. TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId());
  92. item.setBusinessId(t.getId()).setItemCode(tbItem.getItemCode())
  93. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  94. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName())
  95. .setUnit(tbItem.getUnit()).setTotal(NumberUtil.mul(num, tbItem.getPrice()));
  96. }
  97. t.setItemPrice(price);
  98. this.saveOrUpdate(t);
  99. tbBusinessItemService.saveBatch(items);
  100. }
  101. private TbDriver createDriver(String idCardNo, String name, String phone) {
  102. TbDriver tbDriver = tbDriverService.findByIdCardNo(idCardNo);
  103. if (tbDriver == null) {
  104. tbDriver = new TbDriver();
  105. tbDriver.setCreateTime(new Date()).setIdCard(idCardNo).setName(name).setPhone(phone);
  106. tbDriverService.save(tbDriver);
  107. }
  108. return tbDriver;
  109. }
  110. private TbCar createCar(String cardNo, String carSize, String goodsName) {
  111. TbCar tbCar = tbCarService.findByCardNo(cardNo);
  112. if (tbCar == null) {
  113. tbCar = new TbCar();
  114. }
  115. tbCar.setCardNo(cardNo).setCardSize(carSize).setLastGoodsName(goodsName);
  116. tbCarService.saveOrUpdate(tbCar);
  117. return tbCar;
  118. }
  119. /**
  120. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  121. */
  122. List<TbBusiness> getList(SoMap so) {
  123. return tbBusinessMapper.getList(so);
  124. }
  125. public void pay(String id, String payTicket) {
  126. TbBusiness tbBusiness = this.getById(id);
  127. SpAdmin admin = StpUserUtil.getAdmin();
  128. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  129. .setPayBy(StpUserUtil.getAdmin().getName())
  130. .setPayTicket(payTicket)
  131. .setPayTime(new Date());
  132. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  133. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  134. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  135. }
  136. this.updateById(tbBusiness);
  137. }
  138. public void confirm(List<String> ids) {
  139. ids.forEach(id -> {
  140. TbBusiness business = this.getById(id);
  141. business.setConfirmInput(1).setConfirmInputTime(new Date()).setConfirmInputBy(StpUserUtil.getAdmin().getName());
  142. this.updateById(business);
  143. });
  144. }
  145. public void adminConfirmPay(List<String> ids) {
  146. ids.forEach(id -> {
  147. TbBusiness business = this.getById(id);
  148. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  149. .setPayStatus(3);
  150. this.updateById(business);
  151. });
  152. }
  153. public TbBusiness findCarTheNewRecord(String carNo) {
  154. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  155. ew.eq("card_no", carNo);
  156. ew.orderByDesc("id");
  157. List<TbBusiness> list = this.list(ew);
  158. return list.isEmpty() ? null : list.get(0);
  159. }
  160. }