/* * Copyright (c) 2018-2025, ztt All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the pig4cloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: ztt */ package com.chinaztt.mes.quality.service.impl; import cn.hutool.core.collection.CollectionUtil; 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.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.quality.dto.TemplateDTO; import com.chinaztt.mes.quality.entity.Items; import com.chinaztt.mes.quality.entity.Parts; import com.chinaztt.mes.quality.entity.Requirements; import com.chinaztt.mes.quality.entity.Template; import com.chinaztt.mes.quality.mapper.ItemsMapper; import com.chinaztt.mes.quality.mapper.PartsMapper; import com.chinaztt.mes.quality.mapper.RequirementsMapper; import com.chinaztt.mes.quality.mapper.TemplateMapper; import com.chinaztt.mes.quality.service.TemplateService; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; import java.time.LocalDateTime; import java.util.HashSet; import java.util.List; import java.util.Set; /** * 检测模板 * * @author liuth * @date 2020-10-09 13:42:42 */ @Service @AllArgsConstructor public class TemplateServiceImpl extends ServiceImpl implements TemplateService { private ItemsMapper itemsMapper; private RequirementsMapper requirementsMapper; private PartsMapper partsMapper; @Override @Transactional(rollbackFor = Exception.class) public boolean fullSave(TemplateDTO templateDTO) { try { // 1.将检测模板的数据插入到主表中 templateDTO.setCreateDate(LocalDateTime.now()); baseMapper.insert(templateDTO); // 2.1判断关联表检测项的数据是否为空 if (CollectionUtil.isNotEmpty(templateDTO.getItems())) { templateDTO.getItems().forEach(a -> { a.setTemplateId(templateDTO.getId()); itemsMapper.insert(a); }); } } catch (Exception e) { log.error("创建检测模板失败", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return false; } return true; } @Override @Transactional(rollbackFor = Exception.class) public boolean fullUpdate(TemplateDTO templateDTO) { try { Set itemsIds = new HashSet(); Set partsIds = new HashSet(); //更新主表 baseMapper.updateById(templateDTO); //更新检验项关系表 if (CollectionUtil.isNotEmpty(templateDTO.getItems())) { templateDTO.getItems().forEach(a -> { if (a.getId() == null || a.getId() == 0) { a.setTemplateId(templateDTO.getId()); itemsMapper.insert(a); } else { itemsMapper.updateById(a); } itemsIds.add(a.getId()); }); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Items::getTemplateId, templateDTO.getId()); if (CollectionUtil.isNotEmpty(itemsIds)) { wrapper.notIn(Items::getId, itemsIds); } itemsMapper.delete(wrapper); //修改检测模板时删除检测项同时要把检测要求中对应的检测项目删除 LambdaQueryWrapper wrapperRequirements = new LambdaQueryWrapper<>(); wrapperRequirements.eq(Requirements::getTemplateId,templateDTO.getId()); if (CollectionUtil.isNotEmpty(itemsIds)) { wrapperRequirements.notIn(Requirements::getItemsId, itemsIds); } requirementsMapper.delete(wrapperRequirements); } catch (Exception e) { log.error("更新检测模板失败:" + templateDTO.getQtplNo(), e); throw new RuntimeException("更新检测模板失败"); } return true; } /** * 关联删除 * @param id */ @Override @Transactional(rollbackFor = Exception.class) public boolean fullDelete(Long id) { baseMapper.deleteById(id); itemsMapper.delete(Wrappers.lambdaQuery().eq(Items::getTemplateId, id)); //删除检测模板对应的检测要求 requirementsMapper.delete(Wrappers.lambdaQuery().eq(Requirements::getTemplateId, id)); return true; } @Override public TemplateDTO getTemplateById(Long id) { TemplateDTO templateDTO = new TemplateDTO(); if (id != 0L) { //1.获取检测模板 templateDTO = baseMapper.selectDtoById(id); //2.获取关联检验项 List itemsList = itemsMapper.selectList(Wrappers.lambdaQuery().eq(Items::getTemplateId, id)); if (CollectionUtil.isNotEmpty(itemsList)) { templateDTO.setItems(itemsList); } } return templateDTO; } @Override public int deleteItemsById(Long id) { return itemsMapper.deleteById(id); } @Override public Template selectTemplateByPartId(Long id) { return baseMapper.selectTemplateByPartId(id); } @Override public IPage> getTemplatePageByStatus(Page page, QueryWrapper