liuxm
2024-06-04 eeee3d8fa04aff41db6ee2418468e7371ecad57d
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.dy.pmsProduct.process;
 
import com.dy.common.webFilter.UserTokenContext;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.daoOth.OthFileMapper;
import com.dy.pmsGlobal.daoPr.PrProductionNodeMapper;
import com.dy.pmsGlobal.daoPr.PrProductionProcessMapper;
import com.dy.pmsGlobal.daoPr.PrWorkingInstructionMapper;
import com.dy.pmsGlobal.dyFile.FileOperate;
import com.dy.pmsGlobal.dyFile.FileRestVo;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.dy.pmsGlobal.pojoOth.OthFile;
import com.dy.pmsGlobal.pojoPr.PrProductionNode;
import com.dy.pmsGlobal.pojoPr.PrProductionProcess;
import com.dy.pmsGlobal.pojoPr.PrWorkingInstruction;
import com.dy.pmsGlobal.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class ProcessSv {
    private PrProductionProcessMapper processDao;
    private PrProductionNodeMapper nodeDao;
    private PrWorkingInstructionMapper workDao;
    private UserUtil userUtil;
    private FileOperate fileOperate;
    private OthFileMapper othFileMapper;
    @Value("${dy.webFile.fmUrl}")
    private String fmUrl ;
    @Autowired
    public void setProcessDao(PrProductionProcessMapper dao){
        processDao = dao;
    }
    @Autowired
    public void setNodeDao(PrProductionNodeMapper dao){
        nodeDao = dao;
    }
    @Autowired
    public void setWorkDao(PrWorkingInstructionMapper dao){
        workDao = dao;
    }
    @Autowired
    public void setUserUtil(UserUtil userUtil){
        this.userUtil = userUtil;
    }
    @Autowired
    public void setFileOperate(FileOperate fileOperate){
        this.fileOperate = fileOperate;
    }
    @Autowired
    public void setOthFileMapper(OthFileMapper othFileMapper){
        this.othFileMapper = othFileMapper;
    }
    @Transactional
    public int save(PrProductionProcess process){
        //流程名称不能重复
        if (processDao.exists(process.name, process.id)) {
            throw new RuntimeException("流程名称不能重复");
        }
        prepareProcess(process);
        int count = processDao.insertSelective(process);
        saveNodesAndInstructions(process);
        return count;
    }
 
    @Transactional
    public int update(PrProductionProcess process){
        //流程名称不能重复
        if (processDao.exists(process.name, process.id)) {
            throw new RuntimeException("流程名称不能重复");
        }
        prepareProcess(process);
        int count = processDao.updateByPrimaryKeySelective(process);
        // 优化:只有当节点有变更时才删除并重新插入
        if (!process.nodes.isEmpty()) {
            List<Long> nodeIdsToDelete = process.getNodes().stream()
                    .map(PrProductionNode::getId) // 映射节点到其ID
                    .collect(Collectors.toList());
            workDao.deleteByNodeId(nodeIdsToDelete);
            nodeDao.deleteByProcessId(process.id);
        }
        saveNodesAndInstructions(process);
        return count;
    }
 
    // 提取共通逻辑到单独方法以减少代码重复
    private void prepareProcess(PrProductionProcess process){
        process.disabled = false;
        process.deleted = false;
        BaUser loginUser = userUtil.getUser(UserTokenContext.get());
        if(loginUser!=null){
            process.creator = loginUser.id;
        }
        process.nodes.forEach(node -> {
            node.processId = process.id;
            node.deleted= false;
        });
    }
 
    // 将节点和工作指示的保存逻辑封装到一个方法中
    private void saveNodesAndInstructions(PrProductionProcess process){
        process.nodes.forEach(node -> {
            node.processId = process.id;
        });
        try{
            nodeDao.insertMany(process.nodes);
        }catch (DuplicateKeyException e){
            throw new RuntimeException("节点顺序重复");
        }
        List<PrWorkingInstruction> workList = process.nodes.stream().map(node -> {
            if(node.instruction !=null){
                node.instruction.nodeId = node.id;
                return node.instruction;
            }
            return null;
        }).filter(work -> work != null).toList();
        workDao.insertMany(workList);
    }
 
 
 
    public int delete(Long id){
        return processDao.deleteLogicById(id);
    }
 
    public PrProductionProcess selectById(Long id){
        PrProductionProcess process = processDao.selectByPrimaryKey(id);
        if(process != null){
            process.nodes.forEach(node -> {
                if(node.instruction != null){
                    addUrl(node.instruction);
                }
            });
        }
        return process;
    }
 
    public QueryResultVo<List<PrProductionProcess>> selectSome(QueryVo queryVo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
 
        //查询符合条件的记录总数
        Long itemTotal = processDao.selectSomeCount(params);
 
        QueryResultVo<List<PrProductionProcess>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
 
        //查询符合条件的记录
        rsVo.obj = processDao.selectSome(params);
        rsVo.obj.stream().forEach(process -> {
            process.nodes.forEach(node -> {
                if(node.instruction != null){
                    addUrl(node.instruction);
                }
            });
        });
        return rsVo ;
    }
 
    public List<Map<String,String>> queryAll(Long proId){
        return processDao.queryAll(proId);
    }
 
    private void addUrl(PrWorkingInstruction ins){
        if (ins == null || ins.fileId == null) {
            return;
        }
        OthFile file = othFileMapper.selectByPrimaryKey(ins.fileId);
        if (file == null) {
            return;
        }
        FileRestVo fileRestVo = fileOperate.parseHashcode(fmUrl, file.hash);
        ins.webUrl = fileRestVo.fileSysRestUrl + fileRestVo.fileWebDownloadPath + ins.fileId;
        ins.orgName = file.orgName;
        ins.extName = file.extName;
    }
}