TbBusinessService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package com.pj.project.tb_business;
  2. import java.math.BigDecimal;
  3. import java.time.LocalDate;
  4. import java.time.LocalDateTime;
  5. import java.time.LocalTime;
  6. import java.time.ZoneId;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.temporal.ChronoUnit;
  9. import java.util.Date;
  10. import java.util.List;
  11. import cn.hutool.core.date.DateUtil;
  12. import cn.hutool.core.util.NumberUtil;
  13. import cn.hutool.core.util.RandomUtil;
  14. import cn.hutool.core.util.StrUtil;
  15. import cn.hutool.json.JSONUtil;
  16. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  17. import com.baomidou.mybatisplus.extension.service.IService;
  18. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  19. import com.pj.constants.UserTypeEnum;
  20. import com.pj.current.config.PartConfig;
  21. import com.pj.current.satoken.StpUserUtil;
  22. import com.pj.project.tb_business_item.TbBusinessItem;
  23. import com.pj.project.tb_business_item.TbBusinessItemService;
  24. import com.pj.project.tb_business_people.TbBusinessPeople;
  25. import com.pj.project.tb_business_people.TbBusinessPeopleService;
  26. import com.pj.project.tb_car.TbCar;
  27. import com.pj.project.tb_car.TbCarService;
  28. import com.pj.project.tb_costomer.TbCostomer;
  29. import com.pj.project.tb_costomer.TbCostomerService;
  30. import com.pj.project.tb_driver.TbDriver;
  31. import com.pj.project.tb_driver.TbDriverService;
  32. import com.pj.project.tb_item.TbItem;
  33. import com.pj.project.tb_item.TbItemService;
  34. import com.pj.project.tb_item_type.TbItemType;
  35. import com.pj.project.tb_item_type.TbItemTypeService;
  36. import com.pj.project.tb_pass_record.TbPassRecord;
  37. import com.pj.project.tb_pass_record.TbPassRecordService;
  38. import com.pj.project.tb_unit.TbUnit;
  39. import com.pj.project.tb_unit.TbUnitService;
  40. import com.pj.project4sp.admin.SpAdmin;
  41. import com.pj.project4sp.role.SpRoleUtil;
  42. import com.pj.project4sp.role4permission.SpRolePermissionService;
  43. import com.pj.utils.so.SoMap;
  44. import org.aspectj.weaver.loadtime.Aj;
  45. import org.springframework.beans.factory.annotation.Autowired;
  46. import org.springframework.scheduling.annotation.Async;
  47. import org.springframework.stereotype.Service;
  48. import com.pj.utils.sg.*;
  49. import javax.annotation.Resource;
  50. /**
  51. * Service: tb_business -- 入境登记
  52. *
  53. * @author qzy
  54. */
  55. @Service
  56. public class TbBusinessService extends ServiceImpl<TbBusinessMapper, TbBusiness> implements IService<TbBusiness> {
  57. /**
  58. * 底层 Mapper 对象
  59. */
  60. @Autowired
  61. TbBusinessMapper tbBusinessMapper;
  62. @Resource
  63. private TbCarService tbCarService;
  64. @Resource
  65. private TbDriverService tbDriverService;
  66. @Resource
  67. private TbCostomerService tbCostomerService;
  68. @Resource
  69. private TbItemService tbItemService;
  70. @Resource
  71. private TbItemTypeService tbItemTypeService;
  72. @Resource
  73. private TbBusinessItemService tbBusinessItemService;
  74. @Resource
  75. TbUnitService tbUnitService;
  76. @Resource
  77. TbBusinessPeopleService tbBusinessPeopleService;
  78. @Resource
  79. private PartConfig partConfig;
  80. @Resource
  81. private TbPassRecordService tbPassRecordService;
  82. public TbBusiness checkCarNo(String carNo) {
  83. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  84. ew.eq("card_no", carNo)
  85. .eq("admin_confirm_out",0);
  86. return this.getOne(ew);
  87. }
  88. /**
  89. * 增
  90. */
  91. public AjaxJson addOrUpdate(TbBusiness t) {
  92. if (checkCarNo(t.getCardNo()) != null&&StrUtil.isEmpty(t.getId())) {
  93. return AjaxJson.getError("该车辆【" + t.getCardNo() + "】有未完成业务");
  94. }
  95. if (checkCarNo(t.getChinaCarNo()) != null&&StrUtil.isEmpty(t.getId())) {
  96. return AjaxJson.getError("该车辆【" + t.getChinaCarNo() + "】有未完成业务");
  97. }
  98. t.setCreateBy(StpUserUtil.getAdmin().getName());
  99. createCarUnit(t);
  100. String customerId = t.getCustomerId();
  101. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  102. t.setCustomerName(tbCostomer.getName()).setCustomerType(tbCostomer.getPayType());
  103. if (StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  104. t.setConfirmInputBy(StpUserUtil.getAdmin().getName())
  105. .setConfirmInput(1)
  106. .setConfirmInputTime(new Date());
  107. }
  108. if (StrUtil.isEmpty(t.getId())) {
  109. t.setCreateTime(new Date());
  110. t.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  111. BusinessMessageManager.set(t.getCustomerId(),"您有一条新的业务数据:【"+t.getNo()+"】");
  112. }else {
  113. BusinessMessageManager.set(t.getCustomerId(),"您的业务数据:【"+t.getNo()+"】发生更改");
  114. }
  115. List<TbBusinessItem> items = JSONUtil.toList(t.getItemJson(), TbBusinessItem.class);
  116. saveOrUpdate(t);
  117. createBusinessPeople(t);
  118. tbBusinessItemService.removeByBusinessId(t.getId());
  119. BigDecimal price = new BigDecimal(0);
  120. for (TbBusinessItem item : items) {
  121. Double num = NumberUtil.parseDouble(item.getNum());
  122. TbItem tbItem = tbItemService.getById(item.getItemId());
  123. price = price.add(NumberUtil.mul(num, tbItem.getPrice()));
  124. TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId());
  125. item.setBusinessId(t.getId()).setItemCode(tbItem.getItemCode())
  126. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  127. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName())
  128. .setUnit(tbItem.getUnit()).setTotal(NumberUtil.mul(num, tbItem.getPrice()));
  129. }
  130. t.setItemPrice(price).setTotalMoney(price);
  131. this.saveOrUpdate(t);
  132. tbBusinessItemService.saveBatch(items);
  133. //增加或修改一条境外车未入场记录
  134. tbPassRecordService.addOrUpdate(t.getId(), t.getCustomerId(), t.getCustomerName(),
  135. t.getCardNo(), 1, null, null, t.getCountryName());
  136. //增加或修改一条中国车未入场记录
  137. tbPassRecordService.addOrUpdate(t.getId(), t.getCustomerId(), t.getCustomerName(),
  138. t.getChinaCarNo(), 1, null, null, "中国车");
  139. return AjaxJson.getSuccess();
  140. }
  141. private void createBusinessPeople(TbBusiness t) {
  142. tbBusinessPeopleService.removeByBusinessId(t.getId());
  143. List<TbBusinessPeople> peopleList = JSONUtil.toList(t.getPeopleJson(), TbBusinessPeople.class);
  144. peopleList.forEach(tbBusinessPeople -> tbBusinessPeople.setBusinessId(t.getId()).setCreateTime(new Date()));
  145. tbBusinessPeopleService.saveBatch(peopleList);
  146. }
  147. private void createCarUnit(TbBusiness t) {
  148. TbUnit tbUnit = tbUnitService.findByUnit(t.getCardSize());
  149. if (tbUnit == null) {
  150. tbUnit = new TbUnit();
  151. tbUnit.setUnit(t.getCardSize() + "");
  152. tbUnitService.save(tbUnit);
  153. }
  154. }
  155. private TbDriver createDriver(TbBusiness business) {
  156. TbDriver tbDriver = tbDriverService.findByIdCardNo(business.getCardNo());
  157. if (tbDriver == null) {
  158. tbDriver = new TbDriver();
  159. tbDriver.setCreateTime(new Date()).setIdCard(business.getCardNo())
  160. .setName(business.getDriverName()).setPhone(business.getDriverPhone());
  161. tbDriverService.save(tbDriver);
  162. }
  163. return tbDriver;
  164. }
  165. private TbCar createCar(TbBusiness business) {
  166. String cardNo = business.getCardNo();
  167. TbCar tbCar = tbCarService.findByCardNo(cardNo);
  168. if (tbCar == null) {
  169. tbCar = new TbCar();
  170. }
  171. tbCar.setCardNo(cardNo)
  172. .setCardSize(business.getCardSize())
  173. .setCountryName(business.getCountryName())
  174. .setLastGoodsName(business.getGoodsName())
  175. .setLastNetWeight(business.getNetWeight());
  176. tbCarService.saveOrUpdate(tbCar);
  177. return tbCar;
  178. }
  179. /**
  180. * 查集合 - 根据条件(参数为空时代表忽略指定条件)
  181. */
  182. public List<TbBusiness> getList(SoMap so) {
  183. return tbBusinessMapper.getList(so);
  184. }
  185. public void pay(String id, String payTicket) {
  186. TbBusiness tbBusiness = this.getById(id);
  187. SpAdmin admin = StpUserUtil.getAdmin();
  188. tbBusiness.setPayStatus(2).setPayBy(admin.getName())
  189. .setPayBy(StpUserUtil.getAdmin().getName())
  190. .setPayTicket(payTicket)
  191. .setPayTime(new Date());
  192. if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
  193. tbBusiness.setAdminConfirmPay(1).setPayStatus(3)
  194. .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date());
  195. }
  196. this.updateById(tbBusiness);
  197. BusinessMessageManager.set(tbBusiness.getCustomerId(),"您的业务订单【"+tbBusiness.getNo()+"】已支付");
  198. }
  199. public void confirm(List<String> ids) {
  200. ids.forEach(id -> {
  201. TbBusiness business = this.getById(id);
  202. business.setConfirmInput(1).setConfirmInputTime(new Date()).setConfirmInputBy(StpUserUtil.getAdmin().getName());
  203. this.updateById(business);
  204. BusinessMessageManager.set(business.getCustomerId(),"您的业务订单【"+business.getNo()+"】已确认");
  205. });
  206. }
  207. public void adminConfirmPay(List<String> ids, String ticket) {
  208. ids.forEach(id -> {
  209. TbBusiness business = this.getById(id);
  210. business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName())
  211. .setPayStatus(3).setPayTicket(ticket);
  212. this.updateById(business);
  213. BusinessMessageManager.set(business.getCustomerId(),"您的业务订单【"+business.getNo()+"】已确认支付");
  214. });
  215. }
  216. public TbBusiness findCarTheNewRecord(String carNo,int inOrOut) {
  217. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  218. ew.eq("card_no", carNo);
  219. if (inOrOut==1){
  220. ew.eq("admin_confirm_in",0).isNull("in_channel");
  221. }else {
  222. ew.eq("admin_confirm_out",0).isNull("out_channel").isNotNull("real_in_time");
  223. }
  224. return getOne(ew);
  225. }
  226. @Deprecated
  227. public void adminConfirmIn(String id, String inChannel) {
  228. TbBusiness tbBusiness = this.getById(id);
  229. tbBusiness.setRealInTime(new Date()).setInChannel(inChannel);
  230. this.updateById(tbBusiness);
  231. //更新境外车入场记录
  232. tbPassRecordService.addOrUpdate(tbBusiness.getId(), tbBusiness.getCustomerId(), tbBusiness.getCustomerName(),
  233. tbBusiness.getCardNo(), 2, tbBusiness.getRealInTime(), null, tbBusiness.getCountryName());
  234. //更新中国车入场记录
  235. tbPassRecordService.addOrUpdate(tbBusiness.getId(), tbBusiness.getCustomerId(), tbBusiness.getCustomerName(),
  236. tbBusiness.getChinaCarNo(), 2, tbBusiness.getChinaCarInTime(), null, "中国车");
  237. }
  238. public void adminOut(String id, Date outDayTime, String outChannel, String carType) {
  239. TbBusiness tbBusiness = this.getById(id);
  240. tbBusiness.setOutDayTime(outDayTime).setOutChannel(outChannel);
  241. calculationPartMoney(carType, tbBusiness);
  242. this.updateById(tbBusiness);
  243. //更新境外车出场记录
  244. tbPassRecordService.addOrUpdate(tbBusiness.getId(), tbBusiness.getCustomerId(), tbBusiness.getCustomerName(),
  245. tbBusiness.getCardNo(), 3,
  246. tbBusiness.getRealInTime(), tbBusiness.getOutDayTime(), tbBusiness.getCountryName());
  247. //更新中国车出场记录
  248. tbPassRecordService.addOrUpdate(tbBusiness.getId(), tbBusiness.getCustomerId(), tbBusiness.getCustomerName(),
  249. tbBusiness.getChinaCarNo(), 3,
  250. tbBusiness.getChinaCarInTime(), tbBusiness.getChinaCarOutTime(), "中国车");
  251. }
  252. @Async
  253. public void calculationPartMoney(String itemType, TbBusiness tbBusiness) {
  254. Date iTime = tbBusiness.getRealInTime();
  255. Date oTime = tbBusiness.getOutDayTime();
  256. if (itemType.contains("中国")) {
  257. iTime = tbBusiness.getChinaCarInTime();
  258. oTime = tbBusiness.getChinaCarOutTime();
  259. }
  260. LocalDateTime inDayTime = iTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  261. LocalDateTime outDayTime = oTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  262. long minutes = ChronoUnit.MINUTES.between(inDayTime, outDayTime);
  263. if (minutes < 0) {
  264. return;
  265. }
  266. double carSize = tbBusiness.getCardSize();
  267. if (carSize < partConfig.getFreeCarLength() || minutes <= partConfig.getFreeMinutes()) {
  268. return;
  269. }
  270. long days = ChronoUnit.DAYS.between(inDayTime.toLocalDate(), outDayTime.toLocalDate());
  271. long hour = ChronoUnit.HOURS.between(inDayTime, outDayTime);
  272. BigDecimal basePrice = partConfig.getBasePrice();
  273. BigDecimal extraPrice = partConfig.getExtraPrice();
  274. BigDecimal price = basePrice.add(basePrice.multiply(new BigDecimal(hour/24))).add(new BigDecimal(days).multiply(extraPrice));
  275. if (itemType.contains("中国")) {
  276. tbBusiness.setChinaPartMoney(price).setChinaCarPartTime(minutes);
  277. } else {
  278. tbBusiness.setPartMoney(price).setPartTime(minutes);
  279. }
  280. tbBusiness.setTotalMoney(tbBusiness.getItemPrice().add(tbBusiness.getChinaPartMoney()).add(tbBusiness.getPartMoney()));
  281. }
  282. /**
  283. *
  284. * @param carNo
  285. * @param inOrOut 1入 2出
  286. * @return
  287. */
  288. public TbBusiness findByChinaCarNo(String carNo,int inOrOut) {
  289. QueryWrapper<TbBusiness> ew = new QueryWrapper<>();
  290. ew.eq("china_car_no", carNo);
  291. if (inOrOut==1){
  292. ew.eq("admin_confirm_in",0).isNull("china_car_in_channel");
  293. }else {
  294. ew.eq("admin_confirm_out",0)
  295. .isNull("china_car_out_channel")
  296. .isNotNull("china_car_in_time");
  297. }
  298. return this.getOne(ew);
  299. }
  300. }