Fixiaobai
2023-08-17 58f820f804046e7fe82bef5a7b428b442a728e8a
inspection-server/src/main/java/com/yuanchu/limslaboratory/service/impl/InspectionServiceImpl.java
@@ -1,102 +1,219 @@
package com.yuanchu.limslaboratory.service.impl;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuanchu.limslaboratory.mapper.InspectionMaterialListMapper;
import com.yuanchu.limslaboratory.mapper.InspectionProductListMapper;
import com.yuanchu.limslaboratory.mapper.PlanMapper;
import com.yuanchu.limslaboratory.pojo.Inspection;
import com.yuanchu.limslaboratory.mapper.InspectionMapper;
import com.yuanchu.limslaboratory.pojo.InspectionMaterialList;
import com.yuanchu.limslaboratory.pojo.InspectionProductList;
import com.yuanchu.limslaboratory.pojo.Plan;
import com.yuanchu.limslaboratory.pojo.dto.InspectionDto;
import com.yuanchu.limslaboratory.service.InspectionProductListService;
import com.yuanchu.limslaboratory.service.InspectionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.limslaboratory.mapper.*;
import com.yuanchu.limslaboratory.pojo.*;
import com.yuanchu.limslaboratory.pojo.vo.InsProductVo;
import com.yuanchu.limslaboratory.pojo.vo.InspectDetailVo;
import com.yuanchu.limslaboratory.pojo.vo.InspectionVo;
import com.yuanchu.limslaboratory.service.*;
import com.yuanchu.limslaboratory.utils.MyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
 * 服务实现类
 * </p>
 * 申请表(Inspection)表服务实现类
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2023-07-17
 * @author zss
 * @since 2023-08-03 13:03:36
 */
@Service
public class InspectionServiceImpl extends ServiceImpl<InspectionMapper, Inspection> implements InspectionService {
    @Resource
    private InspectionMapper inspectionMapper;
    InspectionMapper inspectionMapper;
    @Resource
    private PlanMapper planMapper;
    InspectionMaterialMapper inspectionMaterialMapper;
    @Resource
    InspectionMaterialListMapper inspectionMaterialListMapper;
    InspectionProductService inspectionProductService;
    @Resource
    InspectionProductListService inspectionProductListService;
    InspectionProductMapper inspectionProductMapper;
    //添加检验申请单
    @Resource
    MaterialMapper materialMapper;
    @Resource
    StandardService standardService;
    @Resource
    SpecificationsService specificationsService;
    @Resource
    ProductMapper productMapper;
    @Resource
    InstrumentService instrumentService;
    @Resource
    UserMapper userMapper;
    @Resource
    ReportMapper reportMapper;
    /**
     * 查询检验申请单列表
     *
     * @param message
     * @return
     */
    @Override
    public Inspection addInspection(String userName, int type) {
        Inspection inspection = new Inspection(type, 0, 1, 1, userName);
        int judge = inspectionMapper.insert(inspection);
        return judge > 0 ? inspection : null;
    public IPage<Map<String, Object>> selectInspectsList(Page<Object> page, String message) {
        return inspectionMapper.selectInspectsList(page, message);
    }
    //查询所有检验单列表
    /**
     * 新增检验申请表
     *
     * @param id
     * @param
     * @return
     */
    @Override
    public List<InspectionDto> selectAllInspection(int pageSize, int countSize, Integer state) {
        return inspectionMapper.selectAllInspection((pageSize - 1) * countSize, pageSize * countSize, state);
    }
    //作废申请检验单
    @Override
    public void delInspectionByInsId(String inspectionId) {
        /*检验单作废*/
        Inspection inspection = inspectionMapper.selectById(inspectionId);
        inspection.setState(0);
        inspectionMapper.updateById(inspection);
        /*检验计划作废*/
        UpdateWrapper<Plan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("inspection_id", inspectionId).set("state", 0);
        planMapper.update(new Plan(), updateWrapper);
        /*检验样品作废(根据报检单id删除样品信息)*/
        UpdateWrapper<InspectionMaterialList> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("inspection_id", inspectionId).set("state", 0);
        inspectionMaterialListMapper.update(new InspectionMaterialList(), wrapper1);
        /*检验样品中的检验项目作废*/
        //查出检验样品id
        LambdaQueryWrapper<InspectionMaterialList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InspectionMaterialList::getInspectionId,inspectionId);
        List<InspectionMaterialList> inspectionMaterialLists = inspectionMaterialListMapper.selectList(queryWrapper);
        for (InspectionMaterialList inspectionMaterialList : inspectionMaterialLists) {
            UpdateWrapper<InspectionProductList> wrapper = new UpdateWrapper<>();
            wrapper.eq("inspection_material_list_id", inspectionMaterialList.getId()).set("state", 0);
            inspectionProductListService.update(new InspectionProductList(), wrapper);
    public Integer addInspect(Integer id, InspectionVo inspectionVo) {
        /*新增检验申请表*/
        Inspection inspection = Inspection.builder()
                .startTime(inspectionVo.getStartTime())
                .endTime(inspectionVo.getEndTime())
                .type(inspectionVo.getType())
                .code(new Snowflake(1, 1).nextIdStr())
                .userId(id)
                .build();
        inspectionMapper.insert(inspection);
        /*新增检验样品表*/
        InspectionMaterial inspectionMaterial = InspectionMaterial.builder()
                .code(inspectionVo.getMcode())
                .name(inspectionVo.getName())
                .num(inspectionVo.getNum())
                .unit(inspectionVo.getUnit())
                .supplier(inspectionVo.getSupplier())
                .specifications(inspectionVo.getSpecifications())
                .formTime(inspectionVo.getFormTime())
                .inspectionId(inspection.getId())
                .build();
        inspectionMaterialMapper.insert(inspectionMaterial);
        /*新增检验项目表*/
        //获取物料id
        Material material = materialMapper.selectOne(Wrappers.<Material>query()
                .eq("name", inspectionVo.getName())
                .eq("code", inspectionVo.getMcode()));
        //获取规格名称和型号名称
        String specification = inspectionVo.getSpecifications();
        String[] split = specification.split("-");
        String stName = split[0];
        String spName = split[1];
        //获取规格id
        Standard standard = standardService.getOne(Wrappers.<Standard>query()
                .eq("name", stName)
                .eq("material_id", material.getId()));
        //获取型号id
        Specifications specifications = specificationsService.getOne(Wrappers.<Specifications>query()
                .eq("name", spName)
                .eq("standard_id", standard.getId()));
        //根据型号id查询项目信息
        List<Product> productList = productMapper.selectList(Wrappers.<Product>query().eq("specifications_id", specifications.getId()));
        ArrayList<InspectionProduct> list = new ArrayList<>();
        for (Product product : productList) {
            InspectionProduct rawInsProduct = InspectionProduct.builder()
                    .name(product.getName())
                    .unit(product.getUnit())
                    .required(product.getRequired())
                    .internal(product.getInternal())
                    .inspectionMaterialId(material.getId())
                    .build();
            list.add(rawInsProduct);
        }
        //检验项目批量添加
        inspectionProductService.saveBatch(list);
        return inspection.getId();
    }
    //提交申请检验单
    //根据检验单id查询原材料检验单详情
    @Override
    public void subInspectionByInsId(String inspectionId) {
        Inspection inspection = inspectionMapper.selectById(inspectionId);
        //状态改为已提交2
        inspection.setState(2);
    public InspectDetailVo selectInspectsListById(Integer id) {
        /*将检验单基本信息查询出来并封装到RawInspectVo对象中*/
        Inspection inspection = inspectionMapper.selectById(id);
        InspectDetailVo inspectDetailVo = new InspectDetailVo();
        //报检人
        User user = userMapper.selectById(inspection.getUserId());
        inspectDetailVo.setUserName(user.getName());
        //报检开始时间和结束时间
        inspectDetailVo.setStartTime(inspection.getStartTime());
        inspectDetailVo.setEndTime(inspection.getEndTime());
        //检验结论
        inspectDetailVo.setInspectionStatus(inspection.getInspectionStatus());
        //查询检验物料
        InspectionMaterial inspectionMaterial = inspectionMaterialMapper.selectOne(Wrappers.<InspectionMaterial>query().eq("inspection_id", id));
        //来料日期,供应商名称,原材料编码,原材料名称,规格型号,单位,数量
        BeanUtils.copyProperties(inspectionMaterial, inspectDetailVo);
        /*查询检验单里面的检验项目,并封装到RawInspectVo对象中*/
        LambdaQueryWrapper<InspectionProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InspectionProduct::getInspectionMaterialId, inspectionMaterial.getId());
        List<InspectionProduct> inspectionProducts = inspectionProductMapper.selectList(queryWrapper);
        //这里查到的设备id和检验员id要查询名称
        List<InsProductVo> insProductVos = inspectionProducts.stream().map(insProduct -> {
            //将一个对象的值赋值给另一个对象
            InsProductVo insProductVo = new InsProductVo();
            BeanUtils.copyProperties(insProduct, insProductVo);
            //获取设备名(前提是如果存在)
            if (insProduct.getInstrumentId() != null) {
                String equipmentName = instrumentService.getById(insProduct.getInstrumentId()).getEquipmentName();
                insProductVo.setInstrumentName(equipmentName);
            }
            //获取用户名(前提是如果存在)
            if (insProduct.getUserId() != null) {
                String userName = userMapper.selectById(insProduct.getUserId()).getName();
                insProductVo.setUserName(userName);
            }
            //项目关联物料id
            insProductVo.setInspectionMaterialId(inspectionMaterial.getId());
            return insProductVo;
        }).collect(Collectors.toList());
        inspectDetailVo.setInsProducts(insProductVos);
        return inspectDetailVo;
    }
    //更新检验单检验结果
    @Override
    public boolean updateInspectsById(Integer id) {
        //更新检验单里面的检验状态和检验结论
        InspectDetailVo inspectDetailVo = selectInspectsListById(id);
        Inspection inspection = Inspection.builder()
                .id(id)
                .inspectionStatus(inspectDetailVo.getInspectionStatus())
                .build();
        inspectionMapper.updateById(inspection);
        //计划表新增
        Plan plan = Plan.builder().inspectionId(inspectionId).state(1).userId(inspection.getInspectUserId()).createTime(new Date()).build();
        planMapper.insert(plan);
        //生成报告单
        Report report = new Report();
        //生成报告单号
        String code = MyUtil.getTimeSixNumberCode("BG","BG");
        //获取检验结论
        String conclusion = "";
        Inspection inspection1 = inspectionMapper.selectById(id);
        if (inspection1.getInspectionStatus().equals(1)) {
            conclusion = "合格";
        }else {
            conclusion = "不合格";
        }
        report.setCode(code);
        report.setStatus(0);
        report.setConclusion(conclusion);
        report.setInspectionId(id);
        reportMapper.insert(report);
        return true;
    }
}