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;
|
}
|
}
|