package com.mzl.flower.schedule; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mzl.flower.constant.Constants; import com.mzl.flower.entity.flower.FlowerCategory; import com.mzl.flower.entity.partner.Partner; import com.mzl.flower.entity.payment.Order; import com.mzl.flower.entity.payment.Transfer; import com.mzl.flower.mapper.flower.FlowerCategoryMapper; import com.mzl.flower.mapper.partner.PartnerMapper; import com.mzl.flower.mapper.payment.OrderMapper; import com.mzl.flower.service.flower.FlowerCategoryService; import com.mzl.flower.service.flower.FlowerService; import com.mzl.flower.service.payment.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @Component @Slf4j public class ScheduleService { @Autowired private FlowerCategoryMapper categoryMapper; @Autowired private PartnerMapper partnerMapper; @Autowired private FlowerService flowerService; @Autowired private OrderMapper orderMapper; @Autowired private UserPaymentV3Service paymentV3Service; @Autowired private OrderService orderService; @Autowired private OrderSettlementService settlementService; @Autowired private FlowerCategoryService categoryService; @Autowired private BillService billService; @Autowired private OrderItemSettlementService orderItemSettlementService; @Scheduled(cron = "1 0/20 * * * ?") public void calculateAvePrice() { log.info("均价计算开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); List cLs = categoryMapper.selectList(new QueryWrapper() .isNotNull("parent_id")); if(cLs != null && cLs.size() > 0){ List ls = partnerMapper.selectList(new QueryWrapper().eq("status", "P")); for(FlowerCategory c : cLs){ flowerService.calculateCategoryDaily(c.getId(), null); if(ls != null && ls.size() > 0){ for(Partner p : ls){ flowerService.calculateCategoryDaily(c.getId(), p.getId()); } } } } log.info("均价计算结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "1 1/30 * * * ?") public void calculateCategoryPriceRange() { log.info("分类加价缓存开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); List cLs = categoryMapper.selectList(new QueryWrapper() .isNotNull("parent_id")); if(cLs != null && cLs.size() > 0){ List ls = partnerMapper.selectList(new QueryWrapper().eq("status", "P")); for(FlowerCategory c : cLs){ categoryService.calculateCategoryPriceRange(c.getId(), null); if(ls != null && ls.size() > 0){ for(Partner p : ls){ categoryService.calculateCategoryPriceRange(c.getId(), p.getId()); } } } } log.info("分类加价缓存结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "0 0/5 * * * ?") public void checkPrepayOrder() {//五分钟未付款自动取消 log.info("未付款确认开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); List ls = orderMapper.selectList(new QueryWrapper() .eq("status", Constants.ORDER_STATUS_BACKEND.PENDING.name()) .eq("deleted", 0)); if(ls != null && ls.size() > 0){ for(Order o : ls){ try { LocalDateTime createdTime = o.getCreateTime().plusMinutes(5); if (createdTime.isBefore(LocalDateTime.now())) { boolean f = paymentV3Service.checkOrderStatus(o.getId()); if(!f){ paymentV3Service.cancelOrder(o.getId()); } } } catch (Exception e) { log.error(e.getMessage(), e); } } } log.info("未付款确认结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "0 10 0 1 * ?") public void calculateSupplierSaleNum(){ log.info("计算上月供应商销售数量开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); orderService.calculateSupplierSaleNum(); log.info("计算上月供应商销售数量结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "1 20 0/1 * * ?") public void autoReceive() { log.info("自动收货开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); orderService.autoReceive(); log.info("自动收货结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "1 0 0 * * ?") public void doSettlement() { log.info("结算开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); //settlementService.doSettlement(); //新结算 List ls = settlementService.getOrders4Settlement(); if(ls != null && ls.size() > 0){ List orderIds = new ArrayList<>(); for(Order o : ls){ orderIds.add(o.getId()); } orderItemSettlementService.saveItemSettlementInfo(orderIds); settlementService.doSettlementNew(ls); } log.info("结算结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "2 0/10 * * * ?") public void checkTransfer() { log.info("确认转账开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); List ls = paymentV3Service.getUnCompletedTransfer(); if(ls != null && ls.size() > 0){ for(Transfer t : ls){ try { paymentV3Service.checkTransferStatus(t); settlementService.updateSettlementStatus(t.getId()); } catch (Exception e) { log.error(e.getMessage(), e); } } } log.info("确认转账结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } @Scheduled(cron = "1 30 0 * * ?") public void generateBill() { log.info("账单开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); LocalDate date = LocalDate.now(); billService.generateBill(date.plusDays(-1)); log.info("账单结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); } }