zss
2023-09-04 e4aed6ad69c6b4b55d6d043f3706967439e2500d
inspect-server/src/main/java/com/yuanchu/mom/service/impl/RawInspectServiceImpl.java
@@ -2,8 +2,10 @@
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.*;
@@ -14,6 +16,7 @@
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.*;
@@ -25,8 +28,7 @@
 * @createDate 2023-07-31 14:43:15
 */
@Service
public class RawInspectServiceImpl extends ServiceImpl<RawInspectMapper, RawInspect>
        implements RawInspectService {
public class RawInspectServiceImpl extends ServiceImpl<RawInspectMapper, RawInspect> implements RawInspectService {
    @Resource
    RawInspectMapper rawInspectMapper;
@@ -40,128 +42,89 @@
    @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;
    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 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;
    public List<Map<String, Object>> selectRawInspectsListById(Integer id) {
        return rawInspectMapper.selectRawInspectsListById(id);
    }
    //更新检验状态(上报)
    @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();
    @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);
        //不管检验合格与不合格,都要新增到原材料库存
       /* //如果检验结论为不合格,则需要新增不合格检验单
        if (rawInspectVo.getJudgeState() == 0) {
        } else if (count == results.size()) {
            rawInspect.setJudgeState(1);
            rawInspectMapper.updateById(rawInspect);
        } else return "项目未检验完!";
        /*如果检验结论为不合格,则需要新增不合格检验单*/
        if (rawInspect.getJudgeState() == 0) {
            InspectUnaccepted rawUnaccepted = InspectUnaccepted.builder()
                    .reason(rawInspectVo.getName() + "不合格")  //暂且定义为原材料不合格
                    .reason(rawInspect.getName() + "不合格")  //暂且定义为原材料不合格
                    .rawInspectId(id)
                    .type(2)        //类型为原材料
                    .type(0)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }*/
        return true;
        }
        return "上报成功!";
    }
    //新增检验单
    //新增原材料检验单
    @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);
    @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(list);
        return rawInspect.getId();
        rawInsProductService.saveBatch(rawInsProductList);
        return "新增检验单成功!";
    }
}