zss
2023-09-25 44a9b4729e058e75dfba2892803038ee91963d77
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
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.yuanchu.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.pojo.vo.ProcessInspectVo;
import com.yuanchu.mom.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 过程检验(ProcessInspect)表服务实现类
 *
 * @author zss
 * @since 2023-09-06 13:36:03
 */
@Service
public class ProcessInspectServiceImpl extends ServiceImpl<ProcessInspectMapper, ProcessInspect> implements ProcessInspectService {
 
    @Resource
    ProcessInspectMapper processInspectMapper;
 
    @Resource
    ManualProductService manualProductService;
 
    @Resource
    InspectionItemMapper inspectionItemMapper;
 
    @Resource
    InspectionItemService inspectionItemService;
 
    @Resource
    InspectUnacceptedMapper inspectUnacceptedMapper;
 
    //新增过程检验单-->根据订单号选择产品信息和工艺
    @Override
    public List<Map<String, Object>> chooseMater(String orderNumber) {
        return processInspectMapper.chooseMater(orderNumber);
    }
 
    //新增过程检验单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addProcess(String userId, ProcessInspectVo processInspectVo) {
        /*新增过程检验单*/
        ProcessInspect processInspect = new ProcessInspect();
        processInspect.setUserId(Integer.parseInt(userId));
        BeanUtils.copyProperties(processInspectVo, processInspect);
        processInspectMapper.insert(processInspect);
        /*批量新增过程检验项目表*/
        List<ManualProduct> manualProductList = manualProductService.selByMtid(processInspectVo.getMtId());
        List<InspectionItem> inspectionItemList = manualProductList.stream().map(manualProduct -> {
            InspectionItem inspectionItem = new InspectionItem();
            BeanUtils.copyProperties(manualProduct, inspectionItem);
            inspectionItem.setCreateTime(new Date());
            inspectionItem.setUpdateTime(new Date());
            inspectionItem.setInspectId(processInspect.getId());
            inspectionItem.setType(1);
            return inspectionItem;
        }).collect(Collectors.toList());
        inspectionItemService.saveBatch(inspectionItemList);
        return processInspect.getId();
    }
 
    //上报(更新检验状态)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateProcessInspectsById(Integer id, Integer number) {
        /*更新检验单里面的检验结论*/
        //先判断检验结果
        List<Integer> results = inspectionItemMapper.getResult(id, 1);
        int count = 0;
        for (Integer result : results) {
            if (result != null && result == 1) {
                count++;
            }
        }
        ProcessInspect processInspect = processInspectMapper.selectById(id);
        //如果检验项目中的结论包含不合格则检验单不合格
        if (results.contains(0)) {
            processInspect.setResult(0);//不合格
            //更新检验单
            processInspectMapper.updateById(processInspect);
        } else if (count == results.size()) {
            processInspect.setResult(1);//合格
            processInspectMapper.updateById(processInspect);
        } else return "项目未检验完!";
        /*如果检验结论为不合格,则需要新增不合格检验单*/
        if (processInspect.getResult() == 0) {
            //查询该检验单哪些检验项目不合格
            List<InspectionItem> inspectionItemList = inspectionItemMapper.selectList(Wrappers.<InspectionItem>query()
                    .eq("inspect_id", id)
                    .eq("result", 0)
                    .eq("type", 1));
            String msg = "";
            for (InspectionItem inspectionItem : inspectionItemList) {
                msg = msg + inspectionItem.getName() + "-";
            }
            InspectUnaccepted processUnaccepted = InspectUnaccepted.builder()
                    .reason(processInspect.getTechname() + "-" + msg + "不合格")  //定义为工艺+项目不合格
                    .number(number)
                    .rawInspectId(id)
                    .type(2)        //类型为过程检验
                    .build();
            inspectUnacceptedMapper.insert(processUnaccepted);
        }
        return "上报成功!";
    }
 
    //根据检验单id查询过程检验单详情
    @Override
    public List<Map<String, Object>> selectProcessInspectsListById(Integer id) {
        return processInspectMapper.selectProcessInspectsListById(id);
    }
 
    //分页查询过程检验单列表
    @Override
    public IPage<Map<String, Object>> selectProcessInspectsList(Page<Object> page, String techfather, Integer result, String name) {
        return processInspectMapper.selectProcessInspectsList(page, techfather, result, name);
    }
 
}