zhangherong
2025-05-27 92bc6dca274eb45dc330f63b5a3f90a01458e157
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package org.jeecg.modules.tms.service.impl;
 
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService;
import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
import org.jeecg.modules.flowable.apithird.service.FlowCommonService;
import org.jeecg.modules.flowable.service.IFlowDefinitionService;
import org.jeecg.modules.flowable.service.IFlowTaskService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.tms.entity.BaseTools;
import org.jeecg.modules.tms.entity.ToolsLossBound;
import org.jeecg.modules.tms.entity.ToolsStocktakingBound;
import org.jeecg.modules.tms.entity.ToolsStocktakingBoundDetail;
import org.jeecg.modules.tms.entity.dto.LossBoundFlowDto;
import org.jeecg.modules.tms.entity.dto.StocktakingBoundFlowDto;
import org.jeecg.modules.tms.enums.OutBillStatus;
import org.jeecg.modules.tms.enums.OutBoundStatusEnum;
import org.jeecg.modules.tms.mapper.ToolsStocktakingBoundDetailMapper;
import org.jeecg.modules.tms.mapper.ToolsStocktakingBoundMapper;
import org.jeecg.modules.tms.service.IToolsStocktakingBoundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
 
/**
 * @Description: 盘点单表
 * @Author: jeecg-boot
 * @Date: 2025-05-16
 * @Version: V1.0
 */
 
@Service("IToolsStocktakingBoundService")
public class ToolsStocktakingBoundServiceImpl extends ServiceImpl<ToolsStocktakingBoundMapper, ToolsStocktakingBound> implements IToolsStocktakingBoundService, FlowCallBackServiceI {
 
 
    @Autowired
    private ISysUserService systemUserService;
    @Autowired
    private ToolsStocktakingBoundMapper toolsStocktakingBoundMapper;
    @Autowired
    private ToolsStocktakingBoundDetailMapper toolsStocktakingBoundDetailMapper;
    @Resource
    private FlowCommonService flowCommonService;
    @Resource
    private IFlowDefinitionService flowDefinitionService;
    @Autowired
    private IFlowTaskService flowTaskService;
    @Autowired
    private IFlowMyBusinessService flowMyBusinessService;
    @Autowired
    private TaskService taskService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(ToolsStocktakingBound toolsStocktakingBound, List<ToolsStocktakingBoundDetail> toolsStocktakingBoundDetailList) {
        toolsStocktakingBoundMapper.insert(toolsStocktakingBound);
        if (toolsStocktakingBoundDetailList != null && toolsStocktakingBoundDetailList.size() > 0) {
            for (ToolsStocktakingBoundDetail entity : toolsStocktakingBoundDetailList) {
                //外键设置
                entity.setGoodsShelvesId(toolsStocktakingBound.getId());
                toolsStocktakingBoundDetailMapper.insert(entity);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(ToolsStocktakingBound toolsStocktakingBound, List<ToolsStocktakingBoundDetail> toolsStocktakingBoundDetailList) {
        toolsStocktakingBoundMapper.updateById(toolsStocktakingBound);
 
        //1.先删除子表数据
        toolsStocktakingBoundDetailMapper.deleteByMainId(toolsStocktakingBound.getId());
 
        //2.子表数据重新插入
        if (toolsStocktakingBoundDetailList != null && toolsStocktakingBoundDetailList.size() > 0) {
            for (ToolsStocktakingBoundDetail entity : toolsStocktakingBoundDetailList) {
                //外键设置
                entity.setGoodsShelvesId(toolsStocktakingBound.getId());
                toolsStocktakingBoundDetailMapper.insert(entity);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        toolsStocktakingBoundDetailMapper.deleteByMainId(id);
        toolsStocktakingBoundMapper.deleteById(id);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            toolsStocktakingBoundDetailMapper.deleteByMainId(id.toString());
            toolsStocktakingBoundMapper.deleteById(id);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submintOrder(String id) {
 
        ToolsStocktakingBound toolsStocktakingBound = this.getById(id);
        if (toolsStocktakingBound == null) {
            return false;
        } else {
            toolsStocktakingBound.setReviewer(toolsStocktakingBound.getReviewer());
            //修改状态
//            toolsStocktakingBound.setApprovalStatus(OutBillStatus.SUBMITTED.getValue());
            flowCommonService.initActBusiness("盘点单号:" + toolsStocktakingBound.getOrderCode() + ";盘点名称: " + toolsStocktakingBound.getStocktakingName() + ";进行盘点",
                    toolsStocktakingBound.getId(), "IToolsStocktakingBoundService", "tools_stocktaking_bound", null);
            Map<String, Object> variables = new HashMap<>();
            variables.put("dataId", toolsStocktakingBound.getId());
            if (StrUtil.isEmpty(toolsStocktakingBound.getReviewer())) {
                variables.put("organization", "新增工具盘点单默认启动流程");
                variables.put("comment", "新增工具盘点单默认启动流程");
            } else {
                variables.put("organization", toolsStocktakingBound.getRemark());
                variables.put("comment", toolsStocktakingBound.getRemark());
            }
            variables.put("proofreading", true);
            List<String> usernames = new ArrayList<>();
            usernames.add(toolsStocktakingBound.getReviewer());
            variables.put("NextAssignee", usernames);
            Result result = flowDefinitionService.startProcessInstanceByKey("tools_stocktaking_bound", variables);
            if (result != null) {
                toolsStocktakingBound.setInventoryTime(new Date());
                toolsStocktakingBound.setApprovalStatus(OutBillStatus.SUBMITTED.getValue());
                //保存工单
                toolsStocktakingBoundMapper.updateById(toolsStocktakingBound);
                return result.isSuccess();
            }
            return true;
        }
 
 
    }
 
 
    @Override
    public void approvalProcess(StocktakingBoundFlowDto stocktakingBoundFlowDto) {
        if (StrUtil.isBlank(stocktakingBoundFlowDto.getTaskId()) || StrUtil.isBlank(stocktakingBoundFlowDto.getDataId())) {
            throw new JeecgBootException("非法参数!");
        }
 
        // 获取当前登录用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null || StrUtil.isBlank(user.getId())) {
            throw new JeecgBootException("账号不存在");
        }
 
        //获取报损单信息
        ToolsStocktakingBound toolsStocktakingBound = getById(stocktakingBoundFlowDto.getDataId());
        if (toolsStocktakingBound == null) {
            throw new JeecgBootException("未找到对应的出库申请单!");
        }
 
        //获取流程业务记录
        FlowMyBusiness flowMyBusiness = getFlowMyBusiness(stocktakingBoundFlowDto.getInstanceId());
        if (flowMyBusiness == null) {
            throw new JeecgBootException("流程记录不存在");
        }
 
        // 检查用户是否有权限操作任务
        if (!isUserAuthorized(flowMyBusiness, user)) {
            throw new JeecgBootException("用户无权操作此任务");
        }
 
        // 认领任务
        if (!claimTask(flowMyBusiness.getTaskId(), user)) {
            throw new JeecgBootException("任务不存在、已完成或已被他人认领");
        }
 
        //设置流程变量
        setupProcessVariables(stocktakingBoundFlowDto, toolsStocktakingBound, user);
 
        //完成流程任务
        Result result = flowTaskService.complete(stocktakingBoundFlowDto);
 
        //根据任务完成结果更新申请单状态
        if (result.isSuccess()) {
            toolsStocktakingBound.setApprovalStatus(stocktakingBoundFlowDto.getStatus());
            if (OutBillStatus.APPROVED.getValue().equals(stocktakingBoundFlowDto.getStatus())) {
                toolsStocktakingBound.setApprovalStatus(OutBoundStatusEnum.NOT_OUTBOUND.getValue());
            }
            toolsStocktakingBound.setApprovalDate(new Date());
            toolsStocktakingBound.setApprovalOpinion(stocktakingBoundFlowDto.getApprovalOpinion());
            updateById(toolsStocktakingBound);
        }
    }
 
    private void setupProcessVariables(StocktakingBoundFlowDto stocktakingBoundFlowDto, ToolsStocktakingBound toolsStocktakingBound, LoginUser user) {
        if (OutBillStatus.SUBMITTED.getValue().equals(toolsStocktakingBound.getApprovalStatus()) && user.getUsername().equals(toolsStocktakingBound.getReviewer())) {
            Map<String, Object> values = new HashMap<>();
            values.put("dataId", toolsStocktakingBound.getId());
            values.put("organization", stocktakingBoundFlowDto.getApprovalOpinion());
            values.put("comment", stocktakingBoundFlowDto.getApprovalOpinion());
            values.put("status", stocktakingBoundFlowDto.getStatus());
            values.put("NextAssignee", Collections.singletonList(toolsStocktakingBound.getReviewer()));
            stocktakingBoundFlowDto.setValues(values);
        }
    }
 
    private FlowMyBusiness getFlowMyBusiness(String instanceId) {
        List<FlowMyBusiness> businessList = flowMyBusinessService.list(
                new LambdaQueryWrapper<FlowMyBusiness>().eq(FlowMyBusiness::getProcessInstanceId, instanceId));
        return businessList.isEmpty() ? null : businessList.get(0);
    }
 
    private boolean isUserAuthorized(FlowMyBusiness flowMyBusiness, LoginUser user) {
        List<String> todoUsers = JSON.parseArray(flowMyBusiness.getTodoUsers(), String.class);
        return todoUsers != null && todoUsers.contains(user.getUsername());
    }
 
    private boolean claimTask(String taskId, LoginUser user) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            return false;
        }
        if (task.getAssignee() != null && !task.getAssignee().equals(user.getUsername())) {
            return false;
        }
        taskService.claim(taskId, user.getUsername());
        return true;
    }
    @Override
    public void afterFlowHandle(FlowMyBusiness business) {
        business.getTaskNameId();//接下来审批的节点
        business.getValues();
        business.getActStatus();
    }
 
    @Override
    public Object getBusinessDataById(String dataId) {
        return this.getById(dataId);
    }
 
    @Override
    public Map<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) {
        return null;
    }
 
    @Override
    public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
        Object object = values.get("NextAssignee");
        return (List<String>) object;
    }
}