cloudroam
2024-12-10 6ee2a947a786edad9eb19a5401de7fcc456b90a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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());
            }
        }
    }
 
}