package com.jpsoft.bus.scheduled; import cn.hutool.core.date.DateUtil; import cn.hutool.db.sql.Order; import com.jpsoft.bus.modules.base.entity.OrderInfo; import com.jpsoft.bus.modules.base.service.OrderInfoService; import com.jpsoft.bus.modules.common.dto.Sort; import com.jpsoft.bus.modules.merchant.entity.AccountInfo; import com.jpsoft.bus.modules.merchant.entity.CapitalInfo; import com.jpsoft.bus.modules.merchant.service.AccountInfoService; import com.jpsoft.bus.modules.merchant.service.CapitalInfoService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; /** * @author 墨鱼_mo * @date 2021-5-08 10:28 */ @Component @Slf4j @Transactional public class SettlementTask { @Autowired private CapitalInfoService capitalInfoService; @Autowired private OrderInfoService orderInfoService; @Autowired private AccountInfoService accountInfoService; //每天凌晨12点01分开始 @Scheduled(cron = "0 01 0 ? * *") public void run() { log.warn("每日资金结算定时任务开始"); Date yesterdayDate = DateUtil.offsetDay(new Date(),-1); // Date startDate = DateUtil.beginOfDay(yesterdayDate); Date endDate = DateUtil.endOfDay(yesterdayDate); List capitalInfoList = capitalInfoService.list(); if (capitalInfoList.size()>0){ for (CapitalInfo capitalInfo : capitalInfoList){ //线上收益 BigDecimal onlineRevenue = BigDecimal.ZERO; //线下收益 BigDecimal downRevenue = BigDecimal.ZERO; AccountInfo accountInfo = accountInfoService.get(capitalInfo.getMerchantId()); Map searchParams = new HashMap<>(); searchParams.put("orderCompanyId",accountInfo.getCompanyId()); searchParams.put("payStatus",20); searchParams.put("settlementFlag",false); searchParams.put("payEndTime",endDate); List sortList = new ArrayList<>(); sortList.add(new Sort("a.pay_time","desc")); List orderInfoList = orderInfoService.findBySearchParams(searchParams,sortList); if (orderInfoList.size()>0){ for (OrderInfo orderInfo : orderInfoList){ if ("wechat".equals(orderInfo.getPayName()) || "alipay".equals(orderInfo.getPayName())){ onlineRevenue = onlineRevenue.add(orderInfo.getPayFee()); } if ("cash".equals(orderInfo.getPayName()) || "ticket".equals(orderInfo.getPayName())){ downRevenue = downRevenue.add(orderInfo.getPayFee()); } orderInfo.setSettlementFlag(true); orderInfoService.update(orderInfo); } } capitalInfo.setWithdrawalAmount(capitalInfo.getWithdrawalAmount().add(onlineRevenue)); capitalInfo.setTotalRevenue(capitalInfo.getTotalRevenue().add(onlineRevenue).add(downRevenue)); capitalInfo.setOnlineTotalRevenue(capitalInfo.getOnlineTotalRevenue().add(onlineRevenue)); capitalInfoService.update(capitalInfo); } } log.warn("每日资金结算定时任务结束"); } }