zss
2023-09-04 d6bc6d435786f1776bb019deeb2fa7ab2736c986
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.RawInsProductVo;
import com.yuanchu.mom.pojo.vo.RawInspectVo;
import com.yuanchu.mom.service.*;
import com.yuanchu.mom.utils.MyUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author Administrator
 * @description 针对表【raw_inspect(原材料申请表)】的数据库操作Service实现
 * @createDate 2023-07-31 14:43:15
 */
@Service
public class RawInspectServiceImpl extends ServiceImpl<RawInspectMapper, RawInspect> implements RawInspectService {
 
    @Resource
    RawInspectMapper rawInspectMapper;
 
    @Resource
    RawInsProductMapper rawInsProductMapper;
 
    @Resource
    RawInsProductService rawInsProductService;
 
    @Resource
    InspectUnacceptedMapper inspectUnacceptedMapper;
 
 
    //分页查询原材料检验单列表
    @Override
    public IPage<Map<String, Object>> selectRawInspectsList(Page<Object> page, String formTime, String code, Integer insState, String name) {
        return rawInspectMapper.selectRawInspectsList(page, formTime, code, insState, name);
    }
 
    //根据检验单id查询原材料检验单详情
    @Override
    public List<Map<String, Object>> selectRawInspectsListById(Integer id) {
        return rawInspectMapper.selectRawInspectsListById(id);
    }
 
    //更新检验状态(上报)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateRawInspectsById(Integer id) {
        /*更新检验单里面的检验结论*/
        //先判断检验结果
        List<Integer> results = rawInsProductMapper.getresult(id);
        int count = 0;
        for (Integer result : results) {
            if (result != null && result == 1) {
                count++;
            }
        }
        RawInspect rawInspect = new RawInspect();
        rawInspect.setId(id);
        rawInspect.setInsState(1);
        //如果检验项目中的结论包含不合格则检验单不合格
        if (results.contains(0)) {
            rawInspect.setJudgeState(0);
            //更新检验单
            rawInspectMapper.updateById(rawInspect);
        } else if (count == results.size()) {
            rawInspect.setJudgeState(1);
            rawInspectMapper.updateById(rawInspect);
        } else return "项目未检验完!";
        /*如果检验结论为不合格,则需要新增不合格检验单*/
        if (rawInspect.getJudgeState() == 0) {
            InspectUnaccepted rawUnaccepted = InspectUnaccepted.builder()
                    .reason(rawInspectMapper.selectById(id).getName() + "不合格")  //暂且定义为原材料不合格
                    .rawInspectId(id)
                    .type(0)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }
        return "上报成功!";
    }
 
 
    //新增原材料检验单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addRawInspects(String userName, RawInspectVo rawInspectVo) {
        //报检人(新增检验单的人)
        rawInspectVo.setUserName(userName);
        //校验标准值,内控值格式
        List<RawInsProductVo> rawInsProductVos = rawInspectVo.getRawInsProducts();
        for (RawInsProductVo rawInsProductVo : rawInsProductVos) {
            char internal = rawInsProductVo.getInternal().charAt(0);
            char required = rawInsProductVo.getRequired().charAt(0);
            if (internal != '>' && internal != '<' && internal != '=') {
                if (required != '>' && required != '<' && required != '=') {
                    return "标准值输入格式有问题!";
                }
                return "内控值输入格式有问题!";
            }
        }
        /*新增原材料检验单*/
        RawInspect rawInspect = new RawInspect();
        BeanUtils.copyProperties(rawInspectVo, rawInspect);
        rawInspectMapper.insert(rawInspect);
        /*新增原材料检验项目单*/
        List<RawInsProduct> rawInsProductList = rawInsProductVos.stream().map(rawInsProVo -> {
            RawInsProduct rawInsProduct = new RawInsProduct();
            BeanUtils.copyProperties(rawInsProVo, rawInsProduct);
            rawInsProduct.setRawInspectId(rawInspect.getId());
            return rawInsProduct;
        }).collect(Collectors.toList());
        //检验项目批量添加
        rawInsProductService.saveBatch(rawInsProductList);
        return "新增检验单成功!";
    }
}