Cui Zhi Feng
2024-09-25 118623071a97b66f21c29e2bc02b8f15855ede5d
均价计算 优化
已修改1个文件
已添加1个文件
101 ■■■■ 文件已修改
src/main/java/com/mzl/flower/schedule/ScheduleService.java 25 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mzl/flower/thread/FlowerCategoryPriceThread.java 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mzl/flower/schedule/ScheduleService.java
@@ -14,9 +14,9 @@
import com.mzl.flower.service.coupon.CouponRecordService;
import com.mzl.flower.service.coupon.CouponTemplateService2;
import com.mzl.flower.service.flower.FlowerCategoryService;
import com.mzl.flower.service.flower.FlowerService;
import com.mzl.flower.service.menber.impl.GrowthValueDealService;
import com.mzl.flower.service.payment.*;
import com.mzl.flower.thread.FlowerCategoryPriceThread;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -38,9 +38,6 @@
    @Autowired
    private PartnerMapper partnerMapper;
    @Autowired
    private FlowerService flowerService;
    @Autowired
    private OrderMapper orderMapper;
@@ -75,23 +72,13 @@
    @Autowired
    private CouponTemplateService2 couponTemplateService2;
    @Scheduled(cron = "1 0/30 * * * ?")
    @Autowired
    private FlowerCategoryPriceThread thread;
    @Scheduled(cron = "1 1 0/2 * * ?")
    public void calculateAvePrice() {
        log.info("均价计算开始:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        List<FlowerCategory> cLs = categoryMapper.selectList(new QueryWrapper<FlowerCategory>()
                .isNotNull("parent_id"));
        if(cLs != null && cLs.size() > 0){
            List<Partner> ls = partnerMapper.selectList(new QueryWrapper<Partner>().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());
                    }
                }
            }
        }
        thread.run();
        log.info("均价计算结束:" + DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
    }
src/main/java/com/mzl/flower/thread/FlowerCategoryPriceThread.java
对比新文件
@@ -0,0 +1,76 @@
package com.mzl.flower.thread;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.entity.flower.FlowerCategory;
import com.mzl.flower.entity.partner.Partner;
import com.mzl.flower.mapper.flower.FlowerCategoryMapper;
import com.mzl.flower.mapper.partner.PartnerMapper;
import com.mzl.flower.service.flower.FlowerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Component
@Slf4j
public class FlowerCategoryPriceThread {
    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    @Autowired
    private FlowerService flowerService;
    @Autowired
    private FlowerCategoryMapper categoryMapper;
    @Autowired
    private PartnerMapper partnerMapper;
    public void run() {
        List<FlowerCategory> cLs = categoryMapper.selectList(new QueryWrapper<FlowerCategory>()
                .isNotNull("parent_id"));
        if(cLs != null && cLs.size() > 0){
            List<Partner> ls = partnerMapper.selectList(new QueryWrapper<Partner>().eq("status", "P"));
            for(FlowerCategory c : cLs){
                addTask(flowerService, c.getId(), null);
                if(ls != null && ls.size() > 0){
                    for(Partner p : ls){
                        addTask(flowerService, c.getId(), p.getId());
                    }
                }
            }
        }
    }
    private void addTask(FlowerService service, Long categoryId, Long partnerId) {
        SelfTask t = new SelfTask(service, categoryId, partnerId);
        fixedThreadPool.execute(t);
    }
    private class SelfTask implements Runnable {
        private FlowerService service;
        private Long categoryId;
        private Long partnerId;
        public SelfTask(FlowerService service, Long categoryId, Long partnerId) {
            this.service = service;
            this.categoryId = categoryId;
            this.partnerId = partnerId;
        }
        public void run() {
            try {
                service.calculateCategoryDaily(categoryId, partnerId);
            } catch (Exception e) {
                log.error("==> 计算均价 categoryId: " + categoryId + "; partnerId: " + partnerId + "; 错误信息:" + e.getMessage());
            }
        }
    }
}