危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.gkhy.hazmat.framework.listener;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gkhy.hazmat.common.config.IdTableNameHandler;
import com.gkhy.hazmat.common.enums.CodePrexEnum;
import com.gkhy.hazmat.common.enums.OperateStatusEnum;
import com.gkhy.hazmat.common.listener.EntryEvent;
import com.gkhy.hazmat.system.domain.HzHazmat;
import com.gkhy.hazmat.system.domain.HzHazmatFlow;
import com.gkhy.hazmat.system.domain.HzProduct;
import com.gkhy.hazmat.system.domain.HzProductFlow;
import com.gkhy.hazmat.system.service.HzHazmatFlowService;
import com.gkhy.hazmat.system.service.HzHazmatService;
import com.gkhy.hazmat.system.service.HzProductFlowService;
import com.gkhy.hazmat.system.service.HzProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Component
public class CustomListener {
    @Autowired
    private HzHazmatService hazmatService;
    @Autowired
    private HzProductService productService;
    @Autowired
    private HzHazmatFlowService hazmatFlowService;
    @Autowired
    private HzProductFlowService productFlowService;
 
    @Async("threadPoolTaskExecutor")
    @EventListener
    public void addFlow(EntryEvent hazmatEntryEvent){
 
        String codePrex=hazmatEntryEvent.getCodePrex();
        if(codePrex.equals(CodePrexEnum.MATERIAL.getCode())){
            generateHazmatFlow(hazmatEntryEvent);
        }else{
            generateProductFlow(hazmatEntryEvent);
        }
    }
 
    private void generateHazmatFlow(EntryEvent hazmatEntryEvent) {
        Integer pageSize=100;
        Integer pageIndex=1;
        List<HzHazmatFlow> hazmatFlowList=new ArrayList<>();
        //设置分表id
        IdTableNameHandler.setCurrentId(hazmatEntryEvent.getCompanyId());
        while (true){
            LambdaQueryWrapper<HzHazmat> eq = Wrappers.<HzHazmat>lambdaQuery().eq(true, HzHazmat::getEntryId, hazmatEntryEvent.getEntryRecordId());
            eq.last(" limit "+((pageIndex-1)*pageSize)+","+pageSize);
            List<HzHazmat> hazmatList=hazmatService.list(eq);
            if(hazmatList.isEmpty()){
                break;
            }
            hazmatFlowList=hazmatList.stream().map(item -> {
                //生成流向
                HzHazmatFlow hazmatFlow=new HzHazmatFlow();
                hazmatFlow.setHazmatId(item.getId());
                hazmatFlow.setBasicId(item.getBasicId());
                hazmatFlow.setState(OperateStatusEnum.ENTRY.getCode());
                hazmatFlow.setCompanyId(hazmatEntryEvent.getCompanyId());
                hazmatFlow.setNum(item.getRemaining());
                hazmatFlow.setCreateId(hazmatEntryEvent.getUserId());
                return hazmatFlow;
            }).collect(Collectors.toList());
            hazmatFlowService.saveBatch(hazmatFlowList);
            hazmatFlowList.clear();
            pageIndex++;
        }
        IdTableNameHandler.removeCurrentId();
    }
 
    private void generateProductFlow(EntryEvent hazmatEntryEvent) {
        Integer pageSize=100;
        Integer pageIndex=1;
        List<HzProductFlow> productFlowList=new ArrayList<>();
        IdTableNameHandler.setCurrentId(hazmatEntryEvent.getCompanyId());
        while (true){
            LambdaQueryWrapper<HzProduct> eq = Wrappers.<HzProduct>lambdaQuery().eq(true, HzProduct::getEntryId, hazmatEntryEvent.getEntryRecordId());
            eq.last(" limit "+((pageIndex-1)*pageSize)+","+pageSize);
            List<HzProduct> productList=productService.list(eq);
            if(productList.isEmpty()){
                break;
            }
            productFlowList=productList.stream().map(item -> {
                //生成流向
                HzProductFlow productFlow=new HzProductFlow();
                productFlow.setProductId(item.getId());
                productFlow.setBasicId(item.getBasicId());
                productFlow.setState(OperateStatusEnum.ENTRY.getCode());
                productFlow.setCompanyId(hazmatEntryEvent.getCompanyId());
                productFlow.setNum(item.getRemaining());
                productFlow.setCreateId(hazmatEntryEvent.getUserId());
                return productFlow;
            }).collect(Collectors.toList());
            productFlowService.saveBatch(productFlowList);
            productFlowList.clear();
            pageIndex++;
        }
        IdTableNameHandler.removeCurrentId();
    }
}