XiaoRuby
2023-08-09 ea1f48f286fa96abcd70f7063c1f5bdc94f13cc5
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
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.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 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;
 
    @Resource
    DeviceService deviceService;
 
    @Resource
    MaterialMapper materialMapper;
 
    @Resource
    UserService userService;
 
    @Resource
    SpecificationsService specificationsService;
 
    @Resource
    StandardService standardService;
 
    @Resource
    ProductMapper productMapper;
 
    @Override
    public Map<String, Object> selectRawInspectsList(int pageSize, int countSize, String formTime, String createTime, int insState, int judgeState) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", rawInspectMapper.selectCount(new QueryWrapper<RawInspect>().eq("state", 1)));
        map.put("data", rawInspectMapper.selectRawInspectsByLimit((pageSize - 1) * countSize, pageSize * countSize, formTime, createTime, insState, judgeState));
        return map;
    }
 
    //根据检验单id查询原材料检验单详情
    @Override
    public RawInspectVo selectRawInspectsListById(Integer id) {
        //将检验单基本信息查询出来并封装到RawInspectVo对象中
        RawInspect rawInspect = rawInspectMapper.selectById(id);
        RawInspectVo rawInspectVo = new RawInspectVo();
        BeanUtils.copyProperties(rawInspect, rawInspectVo);
        //查询检验单里面的检验项目,并封装到RawInspectVo对象中
        LambdaQueryWrapper<RawInsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RawInsProduct::getRawInspectId, id);
        List<RawInsProduct> rawInsProducts = rawInsProductMapper.selectList(queryWrapper);
        //这里查到的设备id和检验员id要查询名称
        List<RawInsProductVo> rawInsProductVos = rawInsProducts.stream().map(rawInsProduct -> {
            //将一个对象的值赋值给另一个对象
            RawInsProductVo rawInsProductVo = new RawInsProductVo();
            BeanUtils.copyProperties(rawInsProduct, rawInsProductVo);
            //获取设备名(前提是如果存在)
            if (rawInsProduct.getDeviceId() != null) {
                String deviceName = deviceService.getDeviceNameById(rawInsProduct.getDeviceId());
                rawInsProductVo.setDeviceName(deviceName);
            }
            //获取用户名(前提是如果存在)
            if (rawInsProduct.getUserId() != null) {
                String userName = userService.selectNameById(rawInsProduct.getUserId());
                rawInsProductVo.setUserName(userName);
            }
            return rawInsProductVo;
        }).collect(Collectors.toList());
        rawInspectVo.setRawInsProducts(rawInsProductVos);
        return rawInspectVo;
    }
 
    //更新检验状态(上报)
    @Override
    public boolean updateRawInspectsById(Integer id) {
        //更新检验单里面的检验状态和检验结论
        RawInspectVo rawInspectVo = selectRawInspectsListById(id);
        RawInspect rawInspect = RawInspect.builder()
                .id(id)
                .insState(1)
                .insTime(new Date())
                .judgeState(rawInspectVo.getJudgeState())
                .build();
        rawInspectMapper.updateById(rawInspect);
        //如果检验结论为不合格,则需要新增不合格检验单
        if (rawInspectVo.getJudgeState() == 0) {
            InspectUnaccepted rawUnaccepted = InspectUnaccepted.builder()
                    .reason(rawInspectVo.getName() + "不合格")  //暂且定义为原材料不合格
                    .rawInspectId(id)
                    .type(2)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }
        return true;
    }
 
 
    //新增检验单
    @Override
    public Integer addRawInspects(String userName, RawInspect rawInspect) {
        rawInspect.setUserName(userName);
        //新增检验单
        rawInspectMapper.insert(rawInspect);
        //获取物料id
        Material material = materialMapper.selectOne(Wrappers.<Material>query()
                .eq("name", rawInspect.getName())
                .eq("code", rawInspect.getCode()));
        //获取规格名称和型号名称
        String specification = rawInspect.getSpecifications();
        String[] strings = specification.split("-");
        String stName = strings[0];//规格名称
        String spName = strings[1];//型号名称
        //获取规格id
        Standard standard = standardService.getOne(Wrappers.<Standard>query()
                .eq("name", stName)
                .eq("material_id", material.getId()));
        //获取型号id
        Specifications specifications = specificationsService.selectSpIdByname(standard.getId(), spName);
        //根据型号id查询项目信息
        List<Product> productList = productMapper.selectList(Wrappers.<Product>query().eq("specifications_id", specifications.getId()));
        ArrayList<RawInsProduct> list = new ArrayList<>();
        for (Product product : productList) {
            RawInsProduct rawInsProduct = RawInsProduct.builder()
                    .name(product.getName())
                    .unit(product.getUnit())
                    .required(product.getRequired())
                    .internal(product.getInternal())
                    .rawInspectId(rawInspect.getId())
                    .build();
            list.add(rawInsProduct);
        }
        //检验项目批量添加
        rawInsProductService.saveBatch(list);
        return rawInspect.getId();
    }
 
    //判断数组是否包含0
    private static boolean containsZero(Object[] array) {
        for (Object num : array) {
            if (num.equals(0)) {
                return true;
            }
        }
        return false;
    }
 
    //判断数组是否全部为1
    private static boolean allOnes(Object[] array) {
        for (Object num : array) {
            if (!num.equals(1)) {
                return false;
            }
        }
        return true;
    }
}