zhuo
9 天以前 fc543916cfac06f0cf16d018b9751417e3a119f7
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
267
268
269
270
271
272
273
274
275
276
package com.ruoyi.inspect.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.oa.OAProcess;
import com.ruoyi.common.oa.OAProperty;
import com.ruoyi.common.oa.OAResult;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.dto.PushOADto;
import com.ruoyi.inspect.dto.UnqualifiedHandlerDto;
import com.ruoyi.inspect.mapper.InsOrderMapper;
import com.ruoyi.inspect.mapper.InsUnqualifiedHandlerMapper;
import com.ruoyi.inspect.pojo.CommonOa;
import com.ruoyi.inspect.pojo.InsOrder;
import com.ruoyi.inspect.pojo.InsUnqualifiedHandler;
import com.ruoyi.inspect.pojo.InsUnqualifiedHandlerFile;
import com.ruoyi.inspect.service.CommonOaService;
import com.ruoyi.inspect.service.InsUnqualifiedHandlerFileService;
import com.ruoyi.inspect.service.InsUnqualifiedHandlerService;
import com.ruoyi.inspect.vo.UnqualifiedHandlerVO;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author 27233
 * @description 针对表【ins_unqualified_handler(不合格处理表)】的数据库操作Service实现
 * @createDate 2024-07-31 13:38:14
 */
@Service
@Slf4j
public class InsUnqualifiedHandlerServiceImpl extends ServiceImpl<InsUnqualifiedHandlerMapper, InsUnqualifiedHandler>
        implements InsUnqualifiedHandlerService {
 
    @Resource
    private CommonOaService commonOaService;
    @Resource
    private OAProperty oaProperty;
    @Resource
    private InsUnqualifiedHandlerFileService insUnqualifiedHandlerFileService;
    @Resource
    private InsOrderMapper insOrderMapper;
 
 
    @Override
    public IPage<UnqualifiedHandlerVO> pageList(Page page, UnqualifiedHandlerDto unqualifiedHandlerDto) {
        return baseMapper.selectPageList(page, QueryWrappers.queryWrappers(unqualifiedHandlerDto));
    }
 
    @Override
    public Result pushOA(PushOADto pushOADto) {
        //获取不合格处理记录
        UnqualifiedHandlerVO vo = baseMapper.findById(pushOADto.getHandlerId());
 
        if (vo.getRequestId() != null) {
            throw new ErrorException("该不合格处理已提交过OA");
        }
 
        //提交oa相关字段赋值
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Map<String, String> mainFields = new HashMap<>();
        mainFields.put("bh", vo.getNo());//编号
        mainFields.put("gysmc", vo.getSupplierName());//供应商名称
        mainFields.put("wlmc", vo.getMaterialName());//物料名称
        mainFields.put("scpc", vo.getProductionBatch());//生产批次
        mainFields.put("dhsl", vo.getCargoQuantity());//到货数量
        mainFields.put("ggxh", vo.getSpecsModels());//规格型号
        mainFields.put("bjrq", vo.getInspectTime().format(formatter));//报检日期
        mainFields.put("fkr", SecurityUtils.getUsername());//反馈人
        mainFields.put("fkrq", vo.getFeedbackTime().format(formatter));//反馈日期
        mainFields.put("fl", vo.getClassification());//分类
        mainFields.put("bhggs", vo.getOffGradeAscription());//不合格归属
        mainFields.put("bhgqkms", vo.getUnqualifiedDesc());//不合格情况描述
 
        //查询附件
        List<InsUnqualifiedHandlerFile> handlerFiles = insUnqualifiedHandlerFileService.list(Wrappers.<InsUnqualifiedHandlerFile>lambdaQuery()
                .eq(InsUnqualifiedHandlerFile::getUnqualifiedId, vo.getHandlerId()));
        if (CollectionUtils.isNotEmpty(handlerFiles)) {
            StringBuilder fileUrl = new StringBuilder();
            for (int i = 0; i < handlerFiles.size(); i++) {
                String path = handlerFiles.get(i).getType().equals(1) ? "/lims/img/" : "/lims/word/";
                if (i == handlerFiles.size() - 1) {
                    fileUrl.append("<a href='" + oaProperty.getProdIp()).append(path + handlerFiles.get(i).getFileUrl()
                            + "'target='_blank'>" + handlerFiles.get(i).getFileName() + "</a>");
                } else {
                    fileUrl.append("<a href='" + oaProperty.getProdIp()).append(path + handlerFiles.get(i).getFileUrl()
                            + "'target='_blank'>" + handlerFiles.get(i).getFileName() + "</a>").append("<br/>");
                }
            }
            mainFields.put("xlimsfj", fileUrl.toString());
        }
        //流程标题
        String requestName = vo.getHeadline();
        //发起OA
        boolean oa = false;
        try {
            log.info("发起不合格处理OA审核流程");
            String unqualifiedProcessId = oaProperty.getUnqualifiedProcessId();
            OAResult oaResult = OAProcess.start(mainFields, requestName, unqualifiedProcessId,SecurityUtils.getUsername());
            log.info("不合格处理OA审核流程结束,返回结果->{}" + oaResult);
            oa = oaResult.success();
            if (oa) {
                String addWorkflowResult = oaResult.getAddWorkflowResult();
                baseMapper.update(null, new LambdaUpdateWrapper<InsUnqualifiedHandler>()
                        .set(InsUnqualifiedHandler::getRequestId, addWorkflowResult)
                        .set(InsUnqualifiedHandler::getOaState, 2)
                        .eq(InsUnqualifiedHandler::getId, pushOADto.getHandlerId()));
            } else {
                return Result.fail("提交oa失败: " + oaResult.getErrorMsg());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unqualifiedHandlerOACallback(Long oaWorkId, String checkResult, JSONArray objects) {
        // 没有这个就结束
        Long count1 = baseMapper.selectCount(Wrappers.<InsUnqualifiedHandler>lambdaQuery()
                .eq(InsUnqualifiedHandler::getRequestId, Integer.valueOf(String.valueOf(oaWorkId))));
        if (count1 == 0) {
            return;
        }
 
        //oa状态:3通过 4驳回
        Integer oaState = "1".equals(checkResult) ? 3 : 4;
        baseMapper.update(null, new LambdaUpdateWrapper<InsUnqualifiedHandler>()
                .set(InsUnqualifiedHandler::getOaState, oaState)
                .ge(InsUnqualifiedHandler::getRequestId, Integer.valueOf(String.valueOf(oaWorkId))));
        //保存oa审批流程记录
        List<CommonOa> commonOaList = new ArrayList<>();
        objects.forEach(l -> {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(l));
            CommonOa oa = new CommonOa();
            oa.setApprover(jsonObject.getString("operatorName"));
            oa.setWorkflowId(oaWorkId);
            oa.setOperation(jsonObject.getString("operateType"));
            oa.setApprovalDate(jsonObject.getString("operateDate"));
            oa.setApprovalOpinion(jsonObject.getString("remark"));
            oa.setNodeName(jsonObject.getString("nodeName"));
            oa.setApprovalTime(jsonObject.getString("operateTime"));
 
            //查询判断之前是否添加过
            long count = commonOaService.count(Wrappers.<CommonOa>lambdaQuery()
                    .eq(CommonOa::getApprover, oa.getApprover())
                    .eq(CommonOa::getWorkflowId, oa.getWorkflowId())
                    .eq(CommonOa::getOperation, oa.getOperation())
                    .eq(CommonOa::getApprovalDate, oa.getApprovalDate())
                    .eq(CommonOa::getApprovalOpinion, oa.getApprovalOpinion())
                    .eq(CommonOa::getNodeName, oa.getNodeName())
                    .eq(CommonOa::getApprovalTime, oa.getApprovalTime())
            );
            // 没有重复添加
            if (count == 0) {
                commonOaList.add(oa);
            }
        });
        commonOaService.saveBatch(commonOaList);
 
    }
 
    /**
     * 查看oa流程
     * @param id
     * @return
     */
    @Override
    public List<CommonOa> getOaFlow(Integer id) {
        return baseMapper.getOaFlow(id);
    }
 
    /**
     * 新增不合格处理
     * @param unqualifiedHandlerDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addUnqualifiedHandler(UnqualifiedHandlerDto unqualifiedHandlerDto) {
        if (unqualifiedHandlerDto.getInventoryQuantityId() == null) {
            throw new ErrorException("缺少原材料id");
        }
        long count = this.count(Wrappers.<InsUnqualifiedHandler>lambdaQuery()
                .eq(InsUnqualifiedHandler::getInsOrderId, unqualifiedHandlerDto.getInsOrderId()));
        if (count > 0) {
            throw new ErrorException("当前检验单已提交过不合格");
        }
        // 原材料编号切割
        InsOrder order = insOrderMapper.selectOne(Wrappers.<InsOrder>lambdaQuery()
                .eq(InsOrder::getId, unqualifiedHandlerDto.getInsOrderId())
                .select(InsOrder::getEntrustCode));
        unqualifiedHandlerDto.setNo(order.getEntrustCode());
 
        this.save(unqualifiedHandlerDto);
        if (CollectionUtils.isNotEmpty(unqualifiedHandlerDto.getUnqualifiedHandlerFiles())) {
            for (InsUnqualifiedHandlerFile unqualifiedHandlerFile : unqualifiedHandlerDto.getUnqualifiedHandlerFiles()) {
                unqualifiedHandlerFile.setUnqualifiedId(unqualifiedHandlerDto.getId());
            }
            insUnqualifiedHandlerFileService.updateBatchById(unqualifiedHandlerDto.getUnqualifiedHandlerFiles());
        }
 
        return true;
    }
 
    /**
     * 编辑不合格处理
     * @param unqualifiedHandlerDto
     * @return
     */
    @Override
    public boolean updateUnqualifiedHandler(UnqualifiedHandlerDto unqualifiedHandlerDto) {
        // 删除附件, 重新添加
        insUnqualifiedHandlerFileService.remove(Wrappers.<InsUnqualifiedHandlerFile>lambdaQuery()
                .eq(InsUnqualifiedHandlerFile::getUnqualifiedId, unqualifiedHandlerDto.getId()));
        if (CollectionUtils.isNotEmpty(unqualifiedHandlerDto.getUnqualifiedHandlerFiles())) {
            for (InsUnqualifiedHandlerFile unqualifiedHandlerFile : unqualifiedHandlerDto.getUnqualifiedHandlerFiles()) {
                unqualifiedHandlerFile.setUnqualifiedId(unqualifiedHandlerDto.getId());
            }
            insUnqualifiedHandlerFileService.updateBatchById(unqualifiedHandlerDto.getUnqualifiedHandlerFiles());
        }
 
        return this.updateById(unqualifiedHandlerDto);
    }
 
    /**
     * 查看不合格处理界面
     * @param id
     * @return
     */
    @Override
    public UnqualifiedHandlerDto getUnqualifiedHandler(Integer id) {
        InsUnqualifiedHandler insUnqualifiedHandler = this.getById(id);
        UnqualifiedHandlerDto unqualifiedHandlerDto = new UnqualifiedHandlerDto();
        BeanUtil.copyProperties(insUnqualifiedHandler, unqualifiedHandlerDto);
        // 查看附件
        List<InsUnqualifiedHandlerFile> list = insUnqualifiedHandlerFileService.list(Wrappers.<InsUnqualifiedHandlerFile>lambdaQuery()
                .eq(InsUnqualifiedHandlerFile::getUnqualifiedId, unqualifiedHandlerDto.getId()));
        unqualifiedHandlerDto.setUnqualifiedHandlerFiles(list);
        return unqualifiedHandlerDto;
    }
 
    /**
     * 删除不合格
     * @param id
     * @return
     */
    @Override
    public boolean deleteUnqualifiedHandler(Integer id) {
        insUnqualifiedHandlerFileService.remove(Wrappers.<InsUnqualifiedHandlerFile>lambdaQuery()
                .eq(InsUnqualifiedHandlerFile::getUnqualifiedId, id));
        this.removeById(id);
        return true;
    }
}