SettlementTask.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.jpsoft.bus.scheduled;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.db.sql.Order;
  4. import com.jpsoft.bus.modules.base.entity.OrderInfo;
  5. import com.jpsoft.bus.modules.base.service.OrderInfoService;
  6. import com.jpsoft.bus.modules.common.dto.Sort;
  7. import com.jpsoft.bus.modules.merchant.entity.AccountInfo;
  8. import com.jpsoft.bus.modules.merchant.entity.CapitalInfo;
  9. import com.jpsoft.bus.modules.merchant.service.AccountInfoService;
  10. import com.jpsoft.bus.modules.merchant.service.CapitalInfoService;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import java.math.BigDecimal;
  17. import java.util.*;
  18. /**
  19. * @author 墨鱼_mo
  20. * @date 2021-5-08 10:28
  21. */
  22. @Component
  23. @Slf4j
  24. @Transactional
  25. public class SettlementTask {
  26. @Autowired
  27. private CapitalInfoService capitalInfoService;
  28. @Autowired
  29. private OrderInfoService orderInfoService;
  30. @Autowired
  31. private AccountInfoService accountInfoService;
  32. //每天凌晨12点01分开始
  33. @Scheduled(cron = "0 01 0 ? * *")
  34. public void run() {
  35. log.warn("每日资金结算定时任务开始");
  36. Date yesterdayDate = DateUtil.offsetDay(new Date(),-1);
  37. // Date startDate = DateUtil.beginOfDay(yesterdayDate);
  38. Date endDate = DateUtil.endOfDay(yesterdayDate);
  39. List<CapitalInfo> capitalInfoList = capitalInfoService.list();
  40. if (capitalInfoList.size()>0){
  41. for (CapitalInfo capitalInfo : capitalInfoList){
  42. //线上收益
  43. BigDecimal onlineRevenue = BigDecimal.ZERO;
  44. //线下收益
  45. BigDecimal downRevenue = BigDecimal.ZERO;
  46. AccountInfo accountInfo = accountInfoService.get(capitalInfo.getMerchantId());
  47. Map<String,Object> searchParams = new HashMap<>();
  48. searchParams.put("orderCompanyId",accountInfo.getCompanyId());
  49. searchParams.put("payStatus",20);
  50. searchParams.put("settlementFlag",false);
  51. searchParams.put("payEndTime",endDate);
  52. List<Sort> sortList = new ArrayList<>();
  53. sortList.add(new Sort("a.pay_time","desc"));
  54. List<OrderInfo> orderInfoList = orderInfoService.findBySearchParams(searchParams,sortList);
  55. if (orderInfoList.size()>0){
  56. for (OrderInfo orderInfo : orderInfoList){
  57. if ("wechat".equals(orderInfo.getPayName()) || "alipay".equals(orderInfo.getPayName())){
  58. onlineRevenue = onlineRevenue.add(orderInfo.getPayFee());
  59. }
  60. if ("cash".equals(orderInfo.getPayName()) || "ticket".equals(orderInfo.getPayName())){
  61. downRevenue = downRevenue.add(orderInfo.getPayFee());
  62. }
  63. orderInfo.setSettlementFlag(true);
  64. orderInfoService.update(orderInfo);
  65. }
  66. }
  67. capitalInfo.setWithdrawalAmount(capitalInfo.getWithdrawalAmount().add(onlineRevenue));
  68. capitalInfo.setTotalRevenue(capitalInfo.getTotalRevenue().add(onlineRevenue).add(downRevenue));
  69. capitalInfo.setOnlineTotalRevenue(capitalInfo.getOnlineTotalRevenue().add(onlineRevenue));
  70. capitalInfoService.update(capitalInfo);
  71. }
  72. }
  73. log.warn("每日资金结算定时任务结束");
  74. }
  75. }